ESPHome  2021.11.3
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; }
22  void set_cool_output(output::FloatOutput *cool_output) { cool_output_ = cool_output; }
23  void set_heat_output(output::FloatOutput *heat_output) { heat_output_ = heat_output; }
24  void set_kp(float kp) { controller_.kp = kp; }
25  void set_ki(float ki) { controller_.ki = ki; }
26  void set_kd(float kd) { controller_.kd = kd; }
27  void set_min_integral(float min_integral) { controller_.min_integral = min_integral; }
28  void set_max_integral(float max_integral) { controller_.max_integral = max_integral; }
29 
30  float get_output_value() const { return output_value_; }
31  float get_error_value() const { return controller_.error; }
32  float get_kp() { return controller_.kp; }
33  float get_ki() { return controller_.ki; }
34  float get_kd() { return controller_.kd; }
36  float get_integral_term() const { return controller_.integral_term; }
38  void add_on_pid_computed_callback(std::function<void()> &&callback) {
39  pid_computed_callback_.add(std::move(callback));
40  }
41  void set_default_target_temperature(float default_target_temperature) {
42  default_target_temperature_ = default_target_temperature;
43  }
44  void start_autotune(std::unique_ptr<PIDAutotuner> &&autotune);
45  void reset_integral_term();
46 
47  protected:
49  void control(const climate::ClimateCall &call) override;
51  climate::ClimateTraits traits() override;
52 
53  void update_pid_();
54 
55  bool supports_cool_() const { return this->cool_output_ != nullptr; }
56  bool supports_heat_() const { return this->heat_output_ != nullptr; }
57 
58  void write_output_(float value);
59 
69  std::unique_ptr<PIDAutotuner> autotuner_;
70  bool do_publish_ = false;
71 };
72 
73 template<typename... Ts> class PIDAutotuneAction : public Action<Ts...> {
74  public:
75  PIDAutotuneAction(PIDClimate *parent) : parent_(parent) {}
76 
77  void set_noiseband(float noiseband) { noiseband_ = noiseband; }
78  void set_positive_output(float positive_output) { positive_output_ = positive_output; }
79  void set_negative_output(float negative_output) { negative_output_ = negative_output; }
80 
81  void play(Ts... x) {
82  auto tuner = make_unique<PIDAutotuner>();
83  tuner->set_noiseband(this->noiseband_);
84  tuner->set_output_negative(this->negative_output_);
85  tuner->set_output_positive(this->positive_output_);
86  this->parent_->start_autotune(std::move(tuner));
87  }
88 
89  protected:
90  float noiseband_;
94 };
95 
96 template<typename... Ts> class PIDResetIntegralTermAction : public Action<Ts...> {
97  public:
98  PIDResetIntegralTermAction(PIDClimate *parent) : parent_(parent) {}
99 
100  void play(Ts... x) { this->parent_->reset_integral_term(); }
101 
102  protected:
104 };
105 
106 template<typename... Ts> class PIDSetControlParametersAction : public Action<Ts...> {
107  public:
108  PIDSetControlParametersAction(PIDClimate *parent) : parent_(parent) {}
109 
110  void play(Ts... x) {
111  auto kp = this->kp_.value(x...);
112  auto ki = this->ki_.value(x...);
113  auto kd = this->kd_.value(x...);
114 
115  this->parent_->set_kp(kp);
116  this->parent_->set_ki(ki);
117  this->parent_->set_kd(kd);
118  }
119 
120  protected:
121  TEMPLATABLE_VALUE(float, kp)
122  TEMPLATABLE_VALUE(float, ki)
123  TEMPLATABLE_VALUE(float, kd)
124 
125  PIDClimate *parent_;
126 };
127 
128 } // namespace pid
129 } // namespace esphome
float get_integral_term() const
Definition: pid_climate.h:36
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:38
output::FloatOutput * cool_output_
Definition: pid_climate.h:62
bool supports_cool_() const
Definition: pid_climate.h:55
void set_cool_output(output::FloatOutput *cool_output)
Definition: pid_climate.h:22
void write_output_(float value)
Definition: pid_climate.cpp:69
void set_default_target_temperature(float default_target_temperature)
Definition: pid_climate.h:41
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.
void set_heat_output(output::FloatOutput *heat_output)
Definition: pid_climate.h:23
void set_negative_output(float negative_output)
Definition: pid_climate.h:79
void set_positive_output(float positive_output)
Definition: pid_climate.h:78
void start_autotune(std::unique_ptr< PIDAutotuner > &&autotune)
void set_ki(float ki)
Definition: pid_climate.h:25
std::unique_ptr< PIDAutotuner > autotuner_
Definition: pid_climate.h:69
float ki
Integral gain K_i.
float kp
Proportional gain K_p.
void set_kd(float kd)
Definition: pid_climate.h:26
float get_error_value() const
Definition: pid_climate.h:31
float get_output_value() const
Definition: pid_climate.h:30
PIDSetControlParametersAction(PIDClimate *parent)
Definition: pid_climate.h:108
float get_derivative_term() const
Definition: pid_climate.h:37
void set_noiseband(float noiseband)
Definition: pid_climate.h:77
void control(const climate::ClimateCall &call) override
Override control to change settings of the climate device.
Definition: pid_climate.cpp:32
void set_max_integral(float max_integral)
Definition: pid_climate.h:28
float kd
Differential gain K_d.
climate::ClimateTraits traits() override
Return the traits of this controller.
Definition: pid_climate.cpp:44
void set_kp(float kp)
Definition: pid_climate.h:24
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:60
output::FloatOutput * heat_output_
Definition: pid_climate.h:63
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
Definition: a4988.cpp:4
PIDController controller_
Definition: pid_climate.h:64
PIDResetIntegralTermAction(PIDClimate *parent)
Definition: pid_climate.h:98
Base-class for all sensors.
Definition: sensor.h:47
void set_min_integral(float min_integral)
Definition: pid_climate.h:27
float get_proportional_term() const
Definition: pid_climate.h:35
float output_value_
Output value as reported by the PID controller, for PIDClimateSensor.
Definition: pid_climate.h:66
CallbackManager< void()> pid_computed_callback_
Definition: pid_climate.h:67
void set_sensor(sensor::Sensor *sensor)
Definition: pid_climate.h:21
ClimateDevice - This is the base class for all climate integrations.
Definition: climate.h:167
PIDAutotuneAction(PIDClimate *parent)
Definition: pid_climate.h:75