ESPHome  2024.11.3
pid_controller.h
Go to the documentation of this file.
1 #pragma once
2 #include "esphome/core/hal.h"
3 #include <deque>
4 #include <cmath>
5 
6 namespace esphome {
7 namespace pid {
8 
9 struct PIDController {
10  float update(float setpoint, float process_value);
11 
12  void reset_accumulated_integral() { accumulated_integral_ = 0; }
13  void set_starting_integral_term(float in) { accumulated_integral_ = in; }
14 
15  bool in_deadband();
16 
17  friend class PIDClimate;
18 
19  private:
21  float kp_ = 0;
23  float ki_ = 0;
25  float kd_ = 0;
26 
27  // smooth the derivative value using a weighted average over X samples
28  int derivative_samples_ = 8;
29 
31  int output_samples_ = 1;
32 
33  float threshold_low_ = 0.0f;
34  float threshold_high_ = 0.0f;
35  float kp_multiplier_ = 0.0f;
36  float ki_multiplier_ = 0.0f;
37  float kd_multiplier_ = 0.0f;
38  int deadband_output_samples_ = 1;
39 
40  float min_integral_ = NAN;
41  float max_integral_ = NAN;
42 
43  // Store computed values in struct so that values can be monitored through sensors
44  float error_;
45  float dt_;
46  float proportional_term_;
47  float integral_term_;
48  float derivative_term_;
49 
50  void calculate_proportional_term_();
51  void calculate_integral_term_();
52  void calculate_derivative_term_(float setpoint);
53  float weighted_average_(std::deque<float> &list, float new_value, int samples);
54  float calculate_relative_time_();
55 
57  float previous_error_ = 0;
58  float previous_setpoint_ = NAN;
60  float accumulated_integral_ = 0;
61  uint32_t last_time_ = 0;
62 
63  // this is a list of derivative values for smoothing.
64  std::deque<float> derivative_list_;
65 
66  // this is a list of output values for smoothing.
67  std::deque<float> output_list_;
68 
69 }; // Struct PID Controller
70 } // namespace pid
71 } // namespace esphome
float update(float setpoint, float process_value)
void set_starting_integral_term(float in)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7