1
0
mirror of https://github.com/esphome/esphome.git synced 2025-07-29 18:29:35 +01:00
Files
.devcontainer
.github
.vscode
docker
esphome
components
a4988
ac_dimmer
adalight
adc
addressable_light
ade7953
ads1115
aht10
airthings_ble
airthings_wave_mini
airthings_wave_plus
am2320
am43
animation
anova
apds9960
api
as3935
as3935_i2c
as3935_spi
async_tcp
atc_mithermometer
atm90e32
b_parasite
ballu
bang_bang
bh1750
binary
binary_sensor
binary_sensor_map
ble_client
ble_presence
ble_rssi
ble_scanner
bme280
bme680
bme680_bsec
bmp085
bmp280
canbus
cap1188
captive_portal
ccs811
climate
climate_ir
climate_ir_lg
color
color_temperature
coolix
cover
cs5460a
cse7761
cse7766
ct_clamp
current_based
custom
custom_component
cwww
daikin
dallas
daly_bms
dashboard_import
debug
deep_sleep
demo
dfplayer
dht
dht12
display
ds1307
dsmr
duty_cycle
e131
endstop
esp32
esp32_ble
esp32_ble_beacon
esp32_ble_server
esp32_ble_tracker
esp32_camera
esp32_camera_web_server
esp32_dac
esp32_hall
esp32_improv
esp32_touch
esp8266
esp8266_pwm
ethernet
exposure_notifications
external_components
ezo
fan
fastled_base
fastled_clockless
fastled_spi
fingerprint_grow
font
fujitsu_general
globals
gpio
gps
graph
havells_solar
hbridge
hdc1080
heatpumpir
hitachi_ac344
hitachi_ac424
hlw8012
hm3301
hmc5883l
homeassistant
hrxl_maxsonar_wr
http_request
htu21d
hx711
i2c
ili9341
image
improv
ina219
ina226
ina3221
inkbird_ibsth1_mini
inkplate6
integration
interval
json
lcd_base
lcd_gpio
lcd_pcf8574
ledc
light
logger
ltr390
max31855
max31856
max31865
max6675
max7219
max7219digit
mcp23008
mcp23016
mcp23017
mcp23s08
mcp23s17
mcp23x08_base
mcp23x17_base
mcp23xxx_base
mcp2515
mcp3008
mcp4725
mcp9808
md5
mdns
mhz19
midea
midea_ac
mitsubishi
modbus
modbus_controller
monochromatic
mpr121
mpu6050
mqtt
mqtt_subscribe
ms5611
my9231
neopixelbus
network
nextion
nfc
ntc
number
ota
output
packages
partition
pca9685
pcd8544
pcf8574
pid
sensor
__init__.py
climate.py
pid_autotuner.cpp
pid_autotuner.h
pid_climate.cpp
pid_climate.h
pid_controller.h
pid_simulator.h
pipsolar
pm1006
pmsa003i
pmsx003
pn532
pn532_i2c
pn532_spi
power_supply
preferences
prometheus
pulse_counter
pulse_meter
pulse_width
pvvx_mithermometer
pzem004t
pzemac
pzemdc
qmc5883l
rc522
rc522_i2c
rc522_spi
rdm6300
remote_base
remote_receiver
remote_transmitter
resistance
restart
rf_bridge
rgb
rgbct
rgbw
rgbww
rotary_encoder
rtttl
ruuvi_ble
ruuvitag
safe_mode
scd30
scd4x
script
sdm_meter
sdp3x
sds011
selec_meter
select
senseair
sensor
servo
sgp30
sgp40
sht3xd
sht4x
shtcx
shutdown
sim800l
slow_pwm
sm16716
sm2135
sm300d2
sn74hc595
sntp
socket
speed
spi
sps30
ssd1306_base
ssd1306_i2c
ssd1306_spi
ssd1322_base
ssd1322_spi
ssd1325_base
ssd1325_spi
ssd1327_base
ssd1327_i2c
ssd1327_spi
ssd1331_base
ssd1331_spi
ssd1351_base
ssd1351_spi
st7735
st7789v
st7920
status
status_led
stepper
sts3x
substitutions
sun
switch
sx1509
t6615
tca9548a
tcl112
tcs34725
teleinfo
template
text_sensor
thermostat
time
time_based
tlc59208f
tlc5947
tm1637
tm1651
tmp102
tmp117
tof10120
toshiba
total_daily_energy
tsl2561
tsl2591
ttp229_bsf
ttp229_lsf
tuya
tx20
uart
uln2003
ultrasonic
uptime
version
vl53l0x
voltage_sampler
waveshare_epaper
web_server
web_server_base
whirlpool
wifi
wifi_info
wifi_signal
wled
xiaomi_ble
xiaomi_cgd1
xiaomi_cgdk2
xiaomi_cgg1
xiaomi_cgpr1
xiaomi_gcls002
xiaomi_hhccjcy01
xiaomi_hhccpot002
xiaomi_jqjcy01ym
xiaomi_lywsd02
xiaomi_lywsd03mmc
xiaomi_lywsdcgq
xiaomi_mhoc401
xiaomi_miscale
xiaomi_miscale2
xiaomi_mjyd02yla
xiaomi_mue4094rt
xiaomi_wx08zm
xpt2046
yashima
zyaura
__init__.py
core
dashboard
__init__.py
__main__.py
automation.py
codegen.py
config.py
config_helpers.py
config_validation.py
const.py
coroutine.py
cpp_generator.py
cpp_helpers.py
cpp_types.py
espota2.py
final_validate.py
git.py
helpers.py
jsonschema.py
loader.py
log.py
mqtt.py
pins.py
platformio_api.py
storage_json.py
types.py
util.py
voluptuous_schema.py
vscode.py
wizard.py
writer.py
yaml_util.py
zeroconf.py
script
tests
.clang-format
.clang-tidy
.coveragerc
.dockerignore
.editorconfig
.gitattributes
.gitignore
.gitpod.yml
.pre-commit-config.yaml
CODEOWNERS
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE
MANIFEST.in
README.md
platformio.ini
pylintrc
pyproject.toml
pytest.ini
requirements.txt
requirements_optional.txt
requirements_test.txt
sdkconfig.defaults
setup.cfg
setup.py
esphome/esphome/components/pid/pid_climate.cpp
Otto Winter ac0d921413 ESP-IDF support and generic target platforms ()
* Socket refactor and SSL

* esp-idf temp

* Fixes

* Echo component and noise

* Add noise API transport support

* Updates

* ESP-IDF

* Complete

* Fixes

* Fixes

* Versions update

* New i2c APIs

* Complete i2c refactor

* SPI migration

* Revert ESP Preferences migration, too complex for now

* OTA support

* Remove echo again

* Remove ssl again

* GPIOFlags updates

* Rename esphal and ICACHE_RAM_ATTR

* Make ESP32 arduino compilable again

* Fix GPIO flags

* Complete pin registry refactor and fixes

* Fixes to make test1 compile

* Remove sdkconfig file

* Ignore sdkconfig file

* Fixes in reviewing

* Make test2 compile

* Make test4 compile

* Make test5 compile

* Run clang-format

* Fix lint errors

* Use esp-idf APIs instead of btStart

* Another round of fixes

* Start implementing ESP8266

* Make test3 compile

* Guard esp8266 code

* Lint

* Reformat

* Fixes

* Fixes v2

* more fixes

* ESP-IDF tidy target

* Convert ARDUINO_ARCH_ESPxx

* Update WiFiSignalSensor

* Update time ifdefs

* OTA needs millis from hal

* RestartSwitch needs delay from hal

* ESP-IDF Uart

* Fix OTA blank password

* Allow setting sdkconfig

* Fix idf partitions and allow setting sdkconfig from yaml

* Re-add read/write compat APIs and fix esp8266 uart

* Fix esp8266 store log strings in flash

* Fix ESP32 arduino preferences not initialized

* Update ifdefs

* Change how sdkconfig change is detected

* Add checks to ci-custom and fix them

* Run clang-format

* Add esp-idf clang-tidy target and fix errors

* Fixes from clang-tidy idf round 2

* Fixes from compiling tests with esp-idf

* Run clang-format

* Switch test5.yaml to esp-idf

* Implement ESP8266 Preferences

* Lint

* Re-do PIO package version selection a bit

* Fix arduinoespressif32 package version

* Fix unit tests

* Lint

* Lint fixes

* Fix readv/writev not defined

* Fix graphing component

* Re-add all old options from core/config.py

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
2021-09-20 11:47:51 +02:00

152 lines
5.4 KiB
C++

#include "pid_climate.h"
#include "esphome/core/log.h"
namespace esphome {
namespace pid {
static const char *const TAG = "pid.climate";
void PIDClimate::setup() {
this->sensor_->add_on_state_callback([this](float state) {
// only publish if state/current temperature has changed in two digits of precision
this->do_publish_ = roundf(state * 100) != roundf(this->current_temperature * 100);
this->current_temperature = state;
this->update_pid_();
});
this->current_temperature = this->sensor_->state;
// restore set points
auto restore = this->restore_state_();
if (restore.has_value()) {
restore->to_call(this).perform();
} else {
// restore from defaults, change_away handles those for us
if (supports_heat_() && supports_cool_())
this->mode = climate::CLIMATE_MODE_HEAT_COOL;
else if (supports_cool_())
this->mode = climate::CLIMATE_MODE_COOL;
else if (supports_heat_())
this->mode = climate::CLIMATE_MODE_HEAT;
this->target_temperature = this->default_target_temperature_;
}
}
void PIDClimate::control(const climate::ClimateCall &call) {
if (call.get_mode().has_value())
this->mode = *call.get_mode();
if (call.get_target_temperature().has_value())
this->target_temperature = *call.get_target_temperature();
// If switching to off mode, set output immediately
if (this->mode == climate::CLIMATE_MODE_OFF)
this->write_output_(0.0f);
this->publish_state();
}
climate::ClimateTraits PIDClimate::traits() {
auto traits = climate::ClimateTraits();
traits.set_supports_current_temperature(true);
traits.set_supports_two_point_target_temperature(false);
traits.set_supported_modes({climate::CLIMATE_MODE_OFF});
if (supports_cool_())
traits.add_supported_mode(climate::CLIMATE_MODE_COOL);
if (supports_heat_())
traits.add_supported_mode(climate::CLIMATE_MODE_HEAT);
if (supports_heat_() && supports_cool_())
traits.add_supported_mode(climate::CLIMATE_MODE_HEAT_COOL);
traits.set_supports_action(true);
return traits;
}
void PIDClimate::dump_config() {
LOG_CLIMATE("", "PID Climate", this);
ESP_LOGCONFIG(TAG, " Control Parameters:");
ESP_LOGCONFIG(TAG, " kp: %.5f, ki: %.5f, kd: %.5f", controller_.kp, controller_.ki, controller_.kd);
if (this->autotuner_ != nullptr) {
this->autotuner_->dump_config();
}
}
void PIDClimate::write_output_(float value) {
this->output_value_ = value;
// first ensure outputs are off (both outputs not active at the same time)
if (this->supports_cool_() && value >= 0)
this->cool_output_->set_level(0.0f);
if (this->supports_heat_() && value <= 0)
this->heat_output_->set_level(0.0f);
// value < 0 means cool, > 0 means heat
if (this->supports_cool_() && value < 0)
this->cool_output_->set_level(std::min(1.0f, -value));
if (this->supports_heat_() && value > 0)
this->heat_output_->set_level(std::min(1.0f, value));
// Update action variable for user feedback what's happening
climate::ClimateAction new_action;
if (this->supports_cool_() && value < 0)
new_action = climate::CLIMATE_ACTION_COOLING;
else if (this->supports_heat_() && value > 0)
new_action = climate::CLIMATE_ACTION_HEATING;
else if (this->mode == climate::CLIMATE_MODE_OFF)
new_action = climate::CLIMATE_ACTION_OFF;
else
new_action = climate::CLIMATE_ACTION_IDLE;
if (new_action != this->action) {
this->action = new_action;
this->do_publish_ = true;
}
this->pid_computed_callback_.call();
}
void PIDClimate::update_pid_() {
float value;
if (std::isnan(this->current_temperature) || std::isnan(this->target_temperature)) {
// if any control parameters are nan, turn off all outputs
value = 0.0;
} else {
// Update PID controller irrespective of current mode, to not mess up D/I terms
// In non-auto mode, we just discard the output value
value = this->controller_.update(this->target_temperature, this->current_temperature);
// Check autotuner
if (this->autotuner_ != nullptr && !this->autotuner_->is_finished()) {
auto res = this->autotuner_->update(this->target_temperature, this->current_temperature);
if (res.result_params.has_value()) {
this->controller_.kp = res.result_params->kp;
this->controller_.ki = res.result_params->ki;
this->controller_.kd = res.result_params->kd;
// keep autotuner instance so that subsequent dump_configs will print the long result message.
} else {
value = res.output;
if (mode != climate::CLIMATE_MODE_HEAT_COOL) {
ESP_LOGW(TAG, "For PID autotuner you need to set AUTO (also called heat/cool) mode!");
}
}
}
}
if (this->mode == climate::CLIMATE_MODE_OFF) {
this->write_output_(0.0);
} else {
this->write_output_(value);
}
if (this->do_publish_)
this->publish_state();
}
void PIDClimate::start_autotune(std::unique_ptr<PIDAutotuner> &&autotune) {
this->autotuner_ = std::move(autotune);
float min_value = this->supports_cool_() ? -1.0f : 0.0f;
float max_value = this->supports_heat_() ? 1.0f : 0.0f;
this->autotuner_->config(min_value, max_value);
this->set_interval("autotune-progress", 10000, [this]() {
if (this->autotuner_ != nullptr && !this->autotuner_->is_finished())
this->autotuner_->dump_config();
});
}
void PIDClimate::reset_integral_term() { this->controller_.reset_accumulated_integral(); }
} // namespace pid
} // namespace esphome