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