ESPHome  2024.4.0
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 
21  void set_sensor(sensor::Sensor *sensor) { sensor_ = sensor; }
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_proportional_term() const { return controller_.proportional_term_; }
48  float get_integral_term() const { return controller_.integral_term_; }
49  float get_derivative_term() const { return controller_.derivative_term_; }
50  int get_output_samples() { return controller_.output_samples_; }
51  int get_derivative_samples() { return controller_.derivative_samples_; }
52 
53  float get_threshold_low() { return controller_.threshold_low_; }
54  float get_threshold_high() { return controller_.threshold_high_; }
55  float get_kp_multiplier() { return controller_.kp_multiplier_; }
56  float get_ki_multiplier() { return controller_.ki_multiplier_; }
57  float get_kd_multiplier() { return controller_.kd_multiplier_; }
58  int get_deadband_output_samples() { return controller_.deadband_output_samples_; }
59  bool in_deadband() { return controller_.in_deadband(); }
60 
61  // int get_derivative_samples() const { return controller_.derivative_samples; }
62  // float get_deadband() const { return controller_.deadband; }
63  // float get_proportional_deadband_multiplier() const { return controller_.proportional_deadband_multiplier; }
64 
65  void add_on_pid_computed_callback(std::function<void()> &&callback) {
66  pid_computed_callback_.add(std::move(callback));
67  }
68  void set_default_target_temperature(float default_target_temperature) {
69  default_target_temperature_ = default_target_temperature;
70  }
71  void start_autotune(std::unique_ptr<PIDAutotuner> &&autotune);
72  void reset_integral_term();
73 
74  protected:
76  void control(const climate::ClimateCall &call) override;
78  climate::ClimateTraits traits() override;
79 
80  void update_pid_();
81 
82  bool supports_cool_() const { return this->cool_output_ != nullptr; }
83  bool supports_heat_() const { return this->heat_output_ != nullptr; }
84 
85  void write_output_(float value);
86 
98  std::unique_ptr<PIDAutotuner> autotuner_;
99  bool do_publish_ = false;
100 };
101 
102 template<typename... Ts> class PIDAutotuneAction : public Action<Ts...> {
103  public:
104  PIDAutotuneAction(PIDClimate *parent) : parent_(parent) {}
105 
106  void set_noiseband(float noiseband) { noiseband_ = noiseband; }
107  void set_positive_output(float positive_output) { positive_output_ = positive_output; }
108  void set_negative_output(float negative_output) { negative_output_ = negative_output; }
109 
110  void play(Ts... x) {
111  auto tuner = make_unique<PIDAutotuner>();
112  tuner->set_noiseband(this->noiseband_);
113  tuner->set_output_negative(this->negative_output_);
114  tuner->set_output_positive(this->positive_output_);
115  this->parent_->start_autotune(std::move(tuner));
116  }
117 
118  protected:
119  float noiseband_;
123 };
124 
125 template<typename... Ts> class PIDResetIntegralTermAction : public Action<Ts...> {
126  public:
127  PIDResetIntegralTermAction(PIDClimate *parent) : parent_(parent) {}
128 
129  void play(Ts... x) { this->parent_->reset_integral_term(); }
130 
131  protected:
133 };
134 
135 template<typename... Ts> class PIDSetControlParametersAction : public Action<Ts...> {
136  public:
137  PIDSetControlParametersAction(PIDClimate *parent) : parent_(parent) {}
138 
139  void play(Ts... x) {
140  auto kp = this->kp_.value(x...);
141  auto ki = this->ki_.value(x...);
142  auto kd = this->kd_.value(x...);
143 
144  this->parent_->set_kp(kp);
145  this->parent_->set_ki(ki);
146  this->parent_->set_kd(kd);
147  }
148 
149  protected:
150  TEMPLATABLE_VALUE(float, kp)
151  TEMPLATABLE_VALUE(float, ki)
152  TEMPLATABLE_VALUE(float, kd)
153 
154  PIDClimate *parent_;
155 };
156 
157 } // namespace pid
158 } // namespace esphome
float get_integral_term() const
Definition: pid_climate.h:48
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:65
output::FloatOutput * cool_output_
Definition: pid_climate.h:91
bool supports_cool_() const
Definition: pid_climate.h:82
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:90
void set_default_target_temperature(float default_target_temperature)
Definition: pid_climate.h:68
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:108
void set_kd_multiplier(float in)
Definition: pid_climate.h:37
void set_positive_output(float positive_output)
Definition: pid_climate.h:107
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:98
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:137
float get_derivative_term() const
Definition: pid_climate.h:49
void set_noiseband(float noiseband)
Definition: pid_climate.h:106
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:88
bool supports_heat_() const
Definition: pid_climate.h:83
void dump_config() override
Definition: pid_climate.cpp:74
output::FloatOutput * heat_output_
Definition: pid_climate.h:92
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void set_ki_multiplier(float in)
Definition: pid_climate.h:36
PIDController controller_
Definition: pid_climate.h:93
PIDResetIntegralTermAction(PIDClimate *parent)
Definition: pid_climate.h:127
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:47
float output_value_
Output value as reported by the PID controller, for PIDClimateSensor.
Definition: pid_climate.h:95
CallbackManager< void()> pid_computed_callback_
Definition: pid_climate.h:96
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:104