ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
pid_controller.h
Go to the documentation of this file.
1#pragma once
2
3#include "esphome/core/hal.h"
5#include <cmath>
6
7namespace esphome::pid {
8
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 an average over X samples
28 int derivative_samples_ = 1;
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
55 float ring_buffer_average_(FixedRingBuffer<float> &buf, float new_value, int max_samples);
56
57 float calculate_relative_time_();
58
60 float previous_error_ = 0;
61 float previous_setpoint_ = NAN;
63 float accumulated_integral_ = 0;
64 uint32_t last_time_ = 0;
65
66 // Ring buffer for derivative smoothing
67 FixedRingBuffer<float> derivative_window_;
68
69 // Ring buffer for output smoothing (shared between normal and deadband modes)
70 FixedRingBuffer<float> output_window_;
71
72}; // Struct PIDController
73} // namespace esphome::pid
Fixed-capacity circular buffer - allocates once at runtime, never reallocates.
Definition helpers.h:390
static void uint32_t
float update(float setpoint, float process_value)
void set_starting_integral_term(float in)