ESPHome  2024.10.2
pid_climate.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/helpers.h"
9 #include "pid_controller.h"
10 #include "pid_autotuner.h"
11 
12 namespace esphome {
13 namespace pid {
14 
15 class PIDClimate : public climate::Climate, public Component {
16  public:
17  PIDClimate() = default;
18  void setup() override;
19  void dump_config() override;
20 
23  void set_cool_output(output::FloatOutput *cool_output) { cool_output_ = cool_output; }
24  void set_heat_output(output::FloatOutput *heat_output) { heat_output_ = heat_output; }
25  void set_kp(float kp) { controller_.kp_ = kp; }
26  void set_ki(float ki) { controller_.ki_ = ki; }
27  void set_kd(float kd) { controller_.kd_ = kd; }
28  void set_min_integral(float min_integral) { controller_.min_integral_ = min_integral; }
29  void set_max_integral(float max_integral) { controller_.max_integral_ = max_integral; }
30  void set_output_samples(int in) { controller_.output_samples_ = in; }
31  void set_derivative_samples(int in) { controller_.derivative_samples_ = in; }
32 
33  void set_threshold_low(float in) { controller_.threshold_low_ = in; }
34  void set_threshold_high(float in) { controller_.threshold_high_ = in; }
35  void set_kp_multiplier(float in) { controller_.kp_multiplier_ = in; }
36  void set_ki_multiplier(float in) { controller_.ki_multiplier_ = in; }
37  void set_kd_multiplier(float in) { controller_.kd_multiplier_ = in; }
39 
40  void set_deadband_output_samples(int in) { controller_.deadband_output_samples_ = in; }
41 
42  float get_output_value() const { return output_value_; }
43  float get_error_value() const { return controller_.error_; }
44  float get_kp() { return controller_.kp_; }
45  float get_ki() { return controller_.ki_; }
46  float get_kd() { return controller_.kd_; }
47  float get_min_integral() { return controller_.min_integral_; }
48  float get_max_integral() { return controller_.max_integral_; }
49  float get_proportional_term() const { return controller_.proportional_term_; }
50  float get_integral_term() const { return controller_.integral_term_; }
51  float get_derivative_term() const { return controller_.derivative_term_; }
52  int get_output_samples() { return controller_.output_samples_; }
53  int get_derivative_samples() { return controller_.derivative_samples_; }
54 
55  float get_threshold_low() { return controller_.threshold_low_; }
56  float get_threshold_high() { return controller_.threshold_high_; }
57  float get_kp_multiplier() { return controller_.kp_multiplier_; }
58  float get_ki_multiplier() { return controller_.ki_multiplier_; }
59  float get_kd_multiplier() { return controller_.kd_multiplier_; }
60  int get_deadband_output_samples() { return controller_.deadband_output_samples_; }
61  bool in_deadband() { return controller_.in_deadband(); }
62 
63  // int get_derivative_samples() const { return controller_.derivative_samples; }
64  // float get_deadband() const { return controller_.deadband; }
65  // float get_proportional_deadband_multiplier() const { return controller_.proportional_deadband_multiplier; }
66 
67  void add_on_pid_computed_callback(std::function<void()> &&callback) {
68  pid_computed_callback_.add(std::move(callback));
69  }
70  void set_default_target_temperature(float default_target_temperature) {
71  default_target_temperature_ = default_target_temperature;
72  }
73  void start_autotune(std::unique_ptr<PIDAutotuner> &&autotune);
74  void reset_integral_term();
75 
76  protected:
78  void control(const climate::ClimateCall &call) override;
80  climate::ClimateTraits traits() override;
81 
82  void update_pid_();
83 
84  bool supports_cool_() const { return this->cool_output_ != nullptr; }
85  bool supports_heat_() const { return this->heat_output_ != nullptr; }
86 
87  void write_output_(float value);
88 
100  std::unique_ptr<PIDAutotuner> autotuner_;
101  bool do_publish_ = false;
102 };
103 
104 template<typename... Ts> class PIDAutotuneAction : public Action<Ts...> {
105  public:
106  PIDAutotuneAction(PIDClimate *parent) : parent_(parent) {}
107 
108  void set_noiseband(float noiseband) { noiseband_ = noiseband; }
109  void set_positive_output(float positive_output) { positive_output_ = positive_output; }
110  void set_negative_output(float negative_output) { negative_output_ = negative_output; }
111 
112  void play(Ts... x) {
113  auto tuner = make_unique<PIDAutotuner>();
114  tuner->set_noiseband(this->noiseband_);
115  tuner->set_output_negative(this->negative_output_);
116  tuner->set_output_positive(this->positive_output_);
117  this->parent_->start_autotune(std::move(tuner));
118  }
119 
120  protected:
121  float noiseband_;
125 };
126 
127 template<typename... Ts> class PIDResetIntegralTermAction : public Action<Ts...> {
128  public:
129  PIDResetIntegralTermAction(PIDClimate *parent) : parent_(parent) {}
130 
131  void play(Ts... x) { this->parent_->reset_integral_term(); }
132 
133  protected:
135 };
136 
137 template<typename... Ts> class PIDSetControlParametersAction : public Action<Ts...> {
138  public:
139  PIDSetControlParametersAction(PIDClimate *parent) : parent_(parent) {}
140 
141  void play(Ts... x) {
142  auto kp = this->kp_.value(x...);
143  auto ki = this->ki_.value(x...);
144  auto kd = this->kd_.value(x...);
145 
146  this->parent_->set_kp(kp);
147  this->parent_->set_ki(ki);
148  this->parent_->set_kd(kd);
149  }
150 
151  protected:
152  TEMPLATABLE_VALUE(float, kp)
153  TEMPLATABLE_VALUE(float, ki)
154  TEMPLATABLE_VALUE(float, kd)
155 
156  PIDClimate *parent_;
157 };
158 
159 } // namespace pid
160 } // namespace esphome
float get_integral_term() const
Definition: pid_climate.h:50
This class is used to encode all control actions on a climate device.
Definition: climate.h:33
void add_on_pid_computed_callback(std::function< void()> &&callback)
Definition: pid_climate.h:67
output::FloatOutput * cool_output_
Definition: pid_climate.h:93
bool supports_cool_() const
Definition: pid_climate.h:84
void set_kp_multiplier(float in)
Definition: pid_climate.h:35
void set_threshold_low(float in)
Definition: pid_climate.h:33
void set_cool_output(output::FloatOutput *cool_output)
Definition: pid_climate.h:23
void write_output_(float value)
Definition: pid_climate.cpp:93
sensor::Sensor * humidity_sensor_
The sensor used for getting the current humidity.
Definition: pid_climate.h:92
void set_default_target_temperature(float default_target_temperature)
Definition: pid_climate.h:70
void setup() override
Definition: pid_climate.cpp:9
Base class for all output components that can output a variable level, like PWM.
Definition: float_output.h:31
This class contains all static data for climate devices.
uint16_t x
Definition: tt21100.cpp:17
void set_output_samples(int in)
Definition: pid_climate.h:30
void set_heat_output(output::FloatOutput *heat_output)
Definition: pid_climate.h:24
void set_negative_output(float negative_output)
Definition: pid_climate.h:110
void set_kd_multiplier(float in)
Definition: pid_climate.h:37
void set_positive_output(float positive_output)
Definition: pid_climate.h:109
void start_autotune(std::unique_ptr< PIDAutotuner > &&autotune)
void set_deadband_output_samples(int in)
Definition: pid_climate.h:40
void set_ki(float ki)
Definition: pid_climate.h:26
std::unique_ptr< PIDAutotuner > autotuner_
Definition: pid_climate.h:100
void set_humidity_sensor(sensor::Sensor *sensor)
Definition: pid_climate.h:22
void set_starting_integral_term(float in)
Definition: pid_climate.h:38
void set_threshold_high(float in)
Definition: pid_climate.h:34
void set_kd(float kd)
Definition: pid_climate.h:27
float get_error_value() const
Definition: pid_climate.h:43
float get_output_value() const
Definition: pid_climate.h:42
PIDSetControlParametersAction(PIDClimate *parent)
Definition: pid_climate.h:139
float get_derivative_term() const
Definition: pid_climate.h:51
void set_noiseband(float noiseband)
Definition: pid_climate.h:108
void control(const climate::ClimateCall &call) override
Override control to change settings of the climate device.
Definition: pid_climate.cpp:43
void set_max_integral(float max_integral)
Definition: pid_climate.h:29
climate::ClimateTraits traits() override
Return the traits of this controller.
Definition: pid_climate.cpp:55
void set_kp(float kp)
Definition: pid_climate.h:25
void set_starting_integral_term(float in)
sensor::Sensor * sensor_
The sensor used for getting the current temperature.
Definition: pid_climate.h:90
bool supports_heat_() const
Definition: pid_climate.h:85
void dump_config() override
Definition: pid_climate.cpp:74
output::FloatOutput * heat_output_
Definition: pid_climate.h:94
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_ki_multiplier(float in)
Definition: pid_climate.h:36
PIDController controller_
Definition: pid_climate.h:95
PIDResetIntegralTermAction(PIDClimate *parent)
Definition: pid_climate.h:129
void set_derivative_samples(int in)
Definition: pid_climate.h:31
Base-class for all sensors.
Definition: sensor.h:57
void set_min_integral(float min_integral)
Definition: pid_climate.h:28
float get_proportional_term() const
Definition: pid_climate.h:49
float output_value_
Output value as reported by the PID controller, for PIDClimateSensor.
Definition: pid_climate.h:97
CallbackManager< void()> pid_computed_callback_
Definition: pid_climate.h:98
esphome::sensor::Sensor * sensor
Definition: statsd.h:37
void set_sensor(sensor::Sensor *sensor)
Definition: pid_climate.h:21
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:168
PIDAutotuneAction(PIDClimate *parent)
Definition: pid_climate.h:106