ESPHome 2026.5.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 {
8namespace pid {
9
11 float update(float setpoint, float process_value);
12
13 void reset_accumulated_integral() { accumulated_integral_ = 0; }
14 void set_starting_integral_term(float in) { accumulated_integral_ = in; }
15
16 bool in_deadband();
17
18 friend class PIDClimate;
19
20 private:
22 float kp_ = 0;
24 float ki_ = 0;
26 float kd_ = 0;
27
28 // smooth the derivative value using an average over X samples
29 int derivative_samples_ = 1;
30
32 int output_samples_ = 1;
33
34 float threshold_low_ = 0.0f;
35 float threshold_high_ = 0.0f;
36 float kp_multiplier_ = 0.0f;
37 float ki_multiplier_ = 0.0f;
38 float kd_multiplier_ = 0.0f;
39 int deadband_output_samples_ = 1;
40
41 float min_integral_ = NAN;
42 float max_integral_ = NAN;
43
44 // Store computed values in struct so that values can be monitored through sensors
45 float error_;
46 float dt_;
47 float proportional_term_;
48 float integral_term_;
49 float derivative_term_;
50
51 void calculate_proportional_term_();
52 void calculate_integral_term_();
53 void calculate_derivative_term_(float setpoint);
54
56 float ring_buffer_average_(FixedRingBuffer<float> &buf, float new_value, int max_samples);
57
58 float calculate_relative_time_();
59
61 float previous_error_ = 0;
62 float previous_setpoint_ = NAN;
64 float accumulated_integral_ = 0;
65 uint32_t last_time_ = 0;
66
67 // Ring buffer for derivative smoothing
68 FixedRingBuffer<float> derivative_window_;
69
70 // Ring buffer for output smoothing (shared between normal and deadband modes)
71 FixedRingBuffer<float> output_window_;
72
73}; // Struct PIDController
74} // namespace pid
75} // namespace esphome
Fixed-capacity circular buffer - allocates once at runtime, never reallocates.
Definition helpers.h:383
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
static void uint32_t
float update(float setpoint, float process_value)
void set_starting_integral_term(float in)