ESPHome  2022.8.0
pid_autotuner.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "pid_controller.h"
6 #include "pid_simulator.h"
7 
8 namespace esphome {
9 namespace pid {
10 
11 class PIDAutotuner {
12  public:
13  struct PIDResult {
14  float kp;
15  float ki;
16  float kd;
17  };
19  float output;
21  };
22 
23  void config(float output_min, float output_max) {
26  }
27  PIDAutotuneResult update(float setpoint, float process_variable);
28  bool is_finished() const { return state_ != AUTOTUNE_RUNNING; }
29 
30  void dump_config();
31 
32  void set_noiseband(float noiseband) {
33  relay_function_.noiseband = noiseband;
34  // ZC detector uses 1/4 the noiseband of relay function (noise suppression)
35  frequency_detector_.noiseband = noiseband / 4;
36  }
37  void set_output_positive(float output_positive) { relay_function_.output_positive = output_positive; }
38  void set_output_negative(float output_negative) { relay_function_.output_negative = output_negative; }
39 
40  protected:
41  struct RelayFunction {
42  float update(float error);
43 
44  float current_target_error() const {
45  if (state == RELAY_FUNCTION_INIT)
46  return 0;
47  if (state == RELAY_FUNCTION_POSITIVE)
48  return -noiseband;
49  return noiseband;
50  }
51 
56  } state = RELAY_FUNCTION_INIT;
57  float noiseband = 0.5;
58  float output_positive = 1;
59  float output_negative = -1;
60  uint32_t phase_count = 0;
63  void update(uint32_t now, float error);
64 
65  bool has_enough_data() const;
66 
67  float get_mean_oscillation_period() const;
68 
69  bool is_increase_decrease_symmetrical() const;
70 
75  } state;
76  float noiseband = 0.05;
77  uint32_t last_zerocross{0};
78  std::vector<uint32_t> zerocrossing_intervals;
81  void update(float error, RelayFunction::RelayFunctionState relay_state);
82 
83  bool has_enough_data() const;
84 
85  float get_mean_oscillation_amplitude() const;
86 
87  bool is_amplitude_convergent() const;
88 
89  float phase_min = NAN;
90  float phase_max = NAN;
91  std::vector<float> phase_mins;
92  std::vector<float> phase_maxs;
95  PIDResult calculate_pid_(float kp_factor, float ki_factor, float kd_factor);
96  void print_rule_(const char *name, float kp_factor, float ki_factor, float kd_factor);
97  PIDResult get_ziegler_nichols_pid_() { return calculate_pid_(0.6f, 1.2f, 0.075f); }
98 
99  uint32_t enough_data_phase_ = 0;
100  float setpoint_ = NAN;
101  enum State {
105  float ku_;
106  float pu_;
107 };
108 
109 } // namespace pid
110 } // namespace esphome
void config(float output_min, float output_max)
Definition: pid_autotuner.h:23
struct esphome::pid::PIDAutotuner::OscillationAmplitudeDetector amplitude_detector_
const char * name
Definition: stm32flash.h:78
struct esphome::pid::PIDAutotuner::OscillationFrequencyDetector frequency_detector_
struct esphome::pid::PIDAutotuner::RelayFunction relay_function_
PIDResult calculate_pid_(float kp_factor, float ki_factor, float kd_factor)
PIDAutotuneResult update(float setpoint, float process_variable)
void set_output_positive(float output_positive)
Definition: pid_autotuner.h:37
void set_output_negative(float output_negative)
Definition: pid_autotuner.h:38
void print_rule_(const char *name, float kp_factor, float ki_factor, float kd_factor)
Definition: a4988.cpp:4
enum esphome::pid::PIDAutotuner::State state_
void set_noiseband(float noiseband)
Definition: pid_autotuner.h:32
bool state
Definition: fan.h:34
PIDResult get_ziegler_nichols_pid_()
Definition: pid_autotuner.h:97