ESPHome  2024.12.2
pid_simulator.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/helpers.h"
7 
8 #include <vector>
9 
10 namespace esphome {
11 namespace pid {
12 
14  public:
16 
17  float surface = 1;
18  float mass = 3;
19  float temperature = 21;
20  float efficiency = 0.98;
21  float thermal_conductivity = 15;
22  float specific_heat_capacity = 4.182;
23  float heat_power = 500;
24  float ambient_temperature = 20;
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
52  float dt = temperature - ambient_temperature;
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
Base class for all output components that can output a variable level, like PWM.
Definition: float_output.h:31
float efficiency
current temperature of object in °C
Definition: pid_simulator.h:20
void write_state(float state) override
Definition: pid_simulator.h:73
This class simplifies creating components that periodically check a state.
Definition: component.h:283
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: helpers.h:93
float heat_power
specific heat capacity of mass in kJ/(kg*K), here: water
Definition: pid_simulator.h:23
float output_value
how many update cycles to delay the output
Definition: pid_simulator.h:28
float temperature
mass of simulated object in kg
Definition: pid_simulator.h:19
float mass
surface area in m²
Definition: pid_simulator.h:18
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
size_t delay_cycles
storage of past temperatures for delaying temperature reading
Definition: pid_simulator.h:27
float delta_t(float power)
Definition: pid_simulator.h:31
float ambient_temperature
Heating power in W.
Definition: pid_simulator.h:24
float thermal_conductivity
heating efficiency, 1 is 100% efficient
Definition: pid_simulator.h:21
float update_interval
Ambient temperature in °C.
Definition: pid_simulator.h:25
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
float specific_heat_capacity
thermal conductivity of surface are in W/(m*K), here: steel
Definition: pid_simulator.h:22
std::vector< float > delayed_temps
The simulated updated interval in seconds.
Definition: pid_simulator.h:26
sensor::Sensor * sensor
Current output value of heating element.
Definition: pid_simulator.h:29
uint8_t m
Definition: bl0906.h:208
Base-class for all sensors.
Definition: sensor.h:57
bool state
Definition: fan.h:34