ESPHome 2025.5.0
Loading...
Searching...
No Matches
pid_simulator.h
Go to the documentation of this file.
1#pragma once
2
7
8#include <vector>
9
10namespace esphome {
11namespace pid {
12
14 public:
16
17 float surface = 1;
18 float mass = 3;
19 float temperature = 21;
20 float efficiency = 0.98;
22 float specific_heat_capacity = 4.182;
23 float heat_power = 500;
25 float update_interval = 1;
26 std::vector<float> delayed_temps;
27 size_t delay_cycles = 15;
28 float output_value = 0.0;
30
31 float delta_t(float power) {
32 // P = Q / t
33 // Q = c * m * 𝚫t
34 // 𝚫t = (P*t) / (c*m)
35 float c = this->specific_heat_capacity;
36 float t = this->update_interval;
37 float p = power / 1000; // in kW
38 float m = this->mass;
39 return (p * t) / (c * m);
40 }
41
42 float update_temp() {
43 float value = clamp(output_value, 0.0f, 1.0f);
44
45 // Heat
46 float power = value * heat_power * efficiency;
47 temperature += this->delta_t(power);
48
49 // Cool
50 // Q = k_w * A * (T_mass - T_ambient)
51 // P = Q / t
53 float cool_power = (thermal_conductivity * surface * dt) / update_interval;
54 temperature -= this->delta_t(cool_power);
55
56 // Delay temperature readings
57 delayed_temps.push_back(temperature);
58 if (delayed_temps.size() > delay_cycles)
59 delayed_temps.erase(delayed_temps.begin());
60 float prev_temp = this->delayed_temps[0];
61 float alpha = 0.1f;
62 float ret = (1 - alpha) * prev_temp + alpha * prev_temp;
63 return ret;
64 }
65
66 void setup() override { sensor->publish_state(this->temperature); }
67 void update() override {
68 float new_temp = this->update_temp();
69 sensor->publish_state(new_temp);
70 }
71
72 protected:
73 void write_state(float state) override { this->output_value = state; }
74};
75
76} // namespace pid
77} // namespace esphome
uint8_t m
Definition bl0906.h:1
This class simplifies creating components that periodically check a state.
Definition component.h:301
Base class for all output components that can output a variable level, like PWM.
float temperature
mass of simulated object in kg
size_t delay_cycles
storage of past temperatures for delaying temperature reading
float output_value
how many update cycles to delay the output
void write_state(float state) override
float delta_t(float power)
float efficiency
current temperature of object in °C
float mass
surface area in m²
float specific_heat_capacity
thermal conductivity of surface are in W/(m*K), here: steel
float heat_power
specific heat capacity of mass in kJ/(kg*K), here: water
float thermal_conductivity
heating efficiency, 1 is 100% efficient
std::vector< float > delayed_temps
The simulated updated interval in seconds.
float ambient_temperature
Heating power in W.
sensor::Sensor * sensor
Current output value of heating element.
float update_interval
Ambient temperature in °C.
Base-class for all sensors.
Definition sensor.h:57
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition helpers.h:101