ESPHome  2022.6.3
pid_climate.cpp
Go to the documentation of this file.
1 #include "pid_climate.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace pid {
6 
7 static const char *const TAG = "pid.climate";
8 
10  this->sensor_->add_on_state_callback([this](float state) {
11  // only publish if state/current temperature has changed in two digits of precision
12  this->do_publish_ = roundf(state * 100) != roundf(this->current_temperature * 100);
13  this->current_temperature = state;
14  this->update_pid_();
15  });
16  this->current_temperature = this->sensor_->state;
17  // restore set points
18  auto restore = this->restore_state_();
19  if (restore.has_value()) {
20  restore->to_call(this).perform();
21  } else {
22  // restore from defaults, change_away handles those for us
23  if (supports_heat_() && supports_cool_()) {
25  } else if (supports_cool_()) {
27  } else if (supports_heat_()) {
29  }
31  }
32 }
34  if (call.get_mode().has_value())
35  this->mode = *call.get_mode();
36  if (call.get_target_temperature().has_value())
38 
39  // If switching to off mode, set output immediately
40  if (this->mode == climate::CLIMATE_MODE_OFF)
41  this->write_output_(0.0f);
42 
43  this->publish_state();
44 }
49 
51  if (supports_cool_())
53  if (supports_heat_())
55  if (supports_heat_() && supports_cool_())
57 
59  return traits;
60 }
62  LOG_CLIMATE("", "PID Climate", this);
63  ESP_LOGCONFIG(TAG, " Control Parameters:");
64  ESP_LOGCONFIG(TAG, " kp: %.5f, ki: %.5f, kd: %.5f", controller_.kp, controller_.ki, controller_.kd);
65 
66  if (this->autotuner_ != nullptr) {
67  this->autotuner_->dump_config();
68  }
69 }
70 void PIDClimate::write_output_(float value) {
71  this->output_value_ = value;
72 
73  // first ensure outputs are off (both outputs not active at the same time)
74  if (this->supports_cool_() && value >= 0)
75  this->cool_output_->set_level(0.0f);
76  if (this->supports_heat_() && value <= 0)
77  this->heat_output_->set_level(0.0f);
78 
79  // value < 0 means cool, > 0 means heat
80  if (this->supports_cool_() && value < 0)
81  this->cool_output_->set_level(std::min(1.0f, -value));
82  if (this->supports_heat_() && value > 0)
83  this->heat_output_->set_level(std::min(1.0f, value));
84 
85  // Update action variable for user feedback what's happening
86  climate::ClimateAction new_action;
87  if (this->supports_cool_() && value < 0) {
89  } else if (this->supports_heat_() && value > 0) {
91  } else if (this->mode == climate::CLIMATE_MODE_OFF) {
92  new_action = climate::CLIMATE_ACTION_OFF;
93  } else {
94  new_action = climate::CLIMATE_ACTION_IDLE;
95  }
96 
97  if (new_action != this->action) {
98  this->action = new_action;
99  this->do_publish_ = true;
100  }
101  this->pid_computed_callback_.call();
102 }
104  float value;
105  if (std::isnan(this->current_temperature) || std::isnan(this->target_temperature)) {
106  // if any control parameters are nan, turn off all outputs
107  value = 0.0;
108  } else {
109  // Update PID controller irrespective of current mode, to not mess up D/I terms
110  // In non-auto mode, we just discard the output value
111  value = this->controller_.update(this->target_temperature, this->current_temperature);
112 
113  // Check autotuner
114  if (this->autotuner_ != nullptr && !this->autotuner_->is_finished()) {
115  auto res = this->autotuner_->update(this->target_temperature, this->current_temperature);
116  if (res.result_params.has_value()) {
117  this->controller_.kp = res.result_params->kp;
118  this->controller_.ki = res.result_params->ki;
119  this->controller_.kd = res.result_params->kd;
120  // keep autotuner instance so that subsequent dump_configs will print the long result message.
121  } else {
122  value = res.output;
124  ESP_LOGW(TAG, "For PID autotuner you need to set AUTO (also called heat/cool) mode!");
125  }
126  }
127  }
128  }
129 
130  if (this->mode == climate::CLIMATE_MODE_OFF) {
131  this->write_output_(0.0);
132  } else {
133  this->write_output_(value);
134  }
135 
136  if (this->do_publish_)
137  this->publish_state();
138 }
139 void PIDClimate::start_autotune(std::unique_ptr<PIDAutotuner> &&autotune) {
140  this->autotuner_ = std::move(autotune);
141  float min_value = this->supports_cool_() ? -1.0f : 0.0f;
142  float max_value = this->supports_heat_() ? 1.0f : 0.0f;
143  this->autotuner_->config(min_value, max_value);
144  this->set_interval("autotune-progress", 10000, [this]() {
145  if (this->autotuner_ != nullptr && !this->autotuner_->is_finished())
146  this->autotuner_->dump_config();
147  });
148 }
149 
151 
152 } // namespace pid
153 } // namespace esphome
This class is used to encode all control actions on a climate device.
Definition: climate.h:33
The climate device is off (inactive or no power)
Definition: climate_mode.h:33
output::FloatOutput * cool_output_
Definition: pid_climate.h:62
bool supports_cool_() const
Definition: pid_climate.h:55
void add_on_state_callback(std::function< void(float)> &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition: sensor.cpp:83
void set_interval(const std::string &name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
Definition: component.cpp:50
void write_output_(float value)
Definition: pid_climate.cpp:70
float target_temperature
The target temperature of the climate device.
Definition: climate.h:183
const optional< ClimateMode > & get_mode() const
Definition: climate.cpp:260
void setup() override
Definition: pid_climate.cpp:9
This class contains all static data for climate devices.
The climate device is set to heat to reach the target temperature.
Definition: climate_mode.h:18
ClimateMode mode
The active mode of the climate device.
Definition: climate.h:175
float current_temperature
The current temperature of the climate device, as reported from the integration.
Definition: climate.h:179
bool has_value() const
Definition: optional.h:87
void start_autotune(std::unique_ptr< PIDAutotuner > &&autotune)
std::unique_ptr< PIDAutotuner > autotuner_
Definition: pid_climate.h:69
void set_level(float state)
Set the level of this float output, this is called from the front-end.
The climate device is set to cool to reach the target temperature.
Definition: climate_mode.h:16
float state
This member variable stores the last state that has passed through all filters.
Definition: sensor.h:132
float ki
Integral gain K_i.
float kp
Proportional gain K_p.
void set_supported_modes(std::set< ClimateMode > modes)
ClimateAction
Enum for the current action of the climate device. Values match those of ClimateMode.
Definition: climate_mode.h:31
float update(float setpoint, float process_value)
Definition: pid_controller.h:9
The climate device is set to heat/cool to reach the target temperature.
Definition: climate_mode.h:14
void control(const climate::ClimateCall &call) override
Override control to change settings of the climate device.
Definition: pid_climate.cpp:33
The climate device is actively heating.
Definition: climate_mode.h:37
const optional< float > & get_target_temperature() const
Definition: climate.cpp:261
float kd
Differential gain K_d.
void publish_state()
Publish the state of the climate device, to be called from integrations.
Definition: climate.cpp:384
The climate device is off.
Definition: climate_mode.h:12
climate::ClimateTraits traits() override
Return the traits of this controller.
Definition: pid_climate.cpp:45
void set_supports_action(bool supports_action)
sensor::Sensor * sensor_
The sensor used for getting the current temperature.
Definition: pid_climate.h:61
bool supports_heat_() const
Definition: pid_climate.h:56
void dump_config() override
Definition: pid_climate.cpp:61
output::FloatOutput * heat_output_
Definition: pid_climate.h:63
Definition: a4988.cpp:4
PIDController controller_
Definition: pid_climate.h:64
The climate device is idle (monitoring climate but no action needed)
Definition: climate_mode.h:39
void set_supports_two_point_target_temperature(bool supports_two_point_target_temperature)
optional< ClimateDeviceRestoreState > restore_state_()
Restore the state of the climate device, call this from your setup() method.
Definition: climate.cpp:321
void set_supports_current_temperature(bool supports_current_temperature)
float output_value_
Output value as reported by the PID controller, for PIDClimateSensor.
Definition: pid_climate.h:66
The climate device is actively cooling.
Definition: climate_mode.h:35
void add_supported_mode(ClimateMode mode)
CallbackManager< void()> pid_computed_callback_
Definition: pid_climate.h:67
ClimateAction action
The active state of the climate device.
Definition: climate.h:177
bool state
Definition: fan.h:34