forked from cbruiz/printhor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_temperature.rs
248 lines (231 loc) · 9.24 KB
/
task_temperature.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//! The Temperature controller task
//! Currently, this is a very naive approach using a simple control pid,
//! but willing to improve with a formal method.
//!
//!
//! AS-IS:
//!
//! ```
//! --+--> power -> pid -> diff --+
//! | |
//! +---------------------------+
//! ```
//!
//! TO-BE:
//!
//! <dl>
//! <dt>1. Mathematical model</dt>
//! <dd>
//! The differential equation describing the temperature $\(T(t)\)$ respecting the time $\(t\)$ is the following:
//! <p>$ \[C \frac{dT(t)}{dt} = P(t) - R \cdot [T(t) - T_{\text{amb}}]\] $</p>
//! <p>To solve the differential equation and obtain an expression for \(T(t)\), we separate variables and integrate the first order differential equation.</p>
//! <p>Assuming initial conditions $\(T(0) = T_0\)$, The general solution:</p>
//! <p>$ \[T(t) = T_{\text{amb}} + [T_0 - T_{\text{amb}}] \cdot e^{-\frac{t}{\tau}} + \frac{P_{\text{max}}}{R} \cdot \left(1 - e^{-\frac{t}{\tau}}\right)\] $</p>
//! <p>
//! Where:
//! <ol>
//! <li>\(T_{\text{amb}}\) is the ambient temperature.</li>
//! <li>\(T_0\) is the initial temperature of the block.</li>
//! <li>\(P_{\text{max}}\) is the maximum power of heating.</li>
//! <li>\(\tau = \frac{C}{R}\) is the system time constant, which characterizes the speed of thermal response.</li>
//! </ol>
//! The equation provides a description of how the temperature evolves \(T(t)\) with the time \(t\) in response of the heating power \(P(t)\)
//! </p>
//! </dd>
//! <dt>2. Temperature sensor</dt>
//! <dd>
//! <p>
//! <dl>
//! <dt>2.1.</dt>
//! <dd>ADC mV Sampling with vref callibration if harware supports it</dd>
//! <dt>2.2.</dt>
//! <dd>Computation of thermistor resistive value (R_0) depending on circuit model.
//! <p>Asuming a rload (R_1) in serie with thermisthor (R_0) connected to ground:</p>
//! <p>V_{adc} = \frac{R_0}{R_0 + R_1}*V_{ref}</p>
//! <p>Then:</p>
//! <p>R_0 = \frac{V_{adc}*R_1}{V_{ref}-V_{adc}}</p>
//! </dd>
//! <dt>2.3.</dt>
//! <dd>
//! <p>With the measured resistive value and thermistor factors, convert to degrees leverating the Steinhart-Hart equation https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation:</p>
//! <p>\frac{1}{T}=\frac{1}{T_0}+\frac{1}{B}*\ln{\frac{R}{R_0}}</p>
//! <p>where:</p>
//! <dl>
//! <dt>T</dt><dd>Target temperature in Kelvin degrees</dd>
//! <dt>T_0</dt><dd>Nominal temperature of thermistor at 25ºC</dd>
//! <dt>B</dt><dd>Beta factor of the thermistor</dd>
//! <dt>R</dt><dd>Nominal resistance of the thermistor</dd>
//! <dt>R_0</dt><dd>Measured resistive value of thermisthor</dd>
//! </dl>
//! </dd>
//! </dl>
//! </p>
//! </dd>
//! <dt>3. Control model</dt>
//! <dd>
//! <p>
//! Control system uses a continuous feedback PID to adjust
//! the amount of heating supplied by a electric resistor to maintain the temperature of the heating block
//! The POD controller computes \(P(t)\) respecting to the error (\(e(t) = T_{\text{ref}} - T(t)\)),
//! The general equation of the PWM power control is:
//! </p>
//! <p>$\[P(t) = \text{PWM}(e(t)) \cdot P_{\text{max}}\]$</p>
//! <p>where:</p>
//! <dl>
//! <dt>\(\text{PWM}(e(t))\)</dt><dd>The function that converts the error in a PWM duty cycle (between 0 and 1).</dd>
//! <dt>\(P_{\text{max}}\)</dt><dd>The maximum power that electric resitor can supply.</dd>
//! </dl>
//! </dd>
//! </dl>
use crate::hwa;
use hwa::{ControllerRef, DeferAction, EventBusRef};
use hwa::{EventStatus, EventFlags};
use embassy_time::{Duration, Ticker};
use crate::hwa::controllers::HeaterController;
#[cfg(not(feature = "native"))]
use num_traits::float::FloatCore;
use num_traits::ToPrimitive;
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "with-defmt", derive(defmt::Format))]
enum State {
Duty,
Targeting,
Maintaining,
}
struct HeaterStateMachine {
pid: pid::Pid<f32>,
current_temp: f32,
last_temp: f32,
state: State,
#[cfg(feature = "native")]
t0: embassy_time::Instant,
}
impl HeaterStateMachine {
fn new() -> Self {
let mut pid: pid::Pid<f32> = pid::Pid::new(0.0f32, 100.0f32);
pid.p(5.0f32, 100.0f32);
pid.i(0.01f32, 100.0f32);
pid.d(1.5f32, 100.0f32);
Self {
#[cfg(feature = "native")]
t0: embassy_time::Instant::now(),
pid,
current_temp: 0f32,
last_temp: 0f32,
state: State::Duty,
}
}
async fn update<AdcPeri, AdcPin, PwmHwaDevice>(
&mut self, ctrl: &ControllerRef<HeaterController<AdcPeri, AdcPin, PwmHwaDevice>>,
event_bus: &EventBusRef,
temperature_flag: EventFlags,
action: DeferAction,
)
where
AdcPeri: hwa::device::AdcTrait + 'static,
AdcPin: hwa::device::AdcPinTrait<AdcPeri>,
PwmHwaDevice: embedded_hal_02::Pwm<Duty=u16> + 'static,
<PwmHwaDevice as embedded_hal_02::Pwm>::Channel: Copy,
crate::hwa::device::VrefInt: crate::hwa::device::AdcPinTrait<AdcPeri>
{
let mut m = ctrl.lock().await;
self.current_temp = m.read_temp().await;
#[cfg(feature = "native")]
{
if m.is_on() {
if self.t0.elapsed() > Duration::from_secs(5) {
self.current_temp = m.get_target_temp();
}
}
}
let new_state = {
hwa::debug!("MEASURED_TEMP[{:?}] {}", action, self.current_temp);
if m.is_on() {
let target_temp = m.get_target_temp();
self.pid.setpoint(target_temp);
self.last_temp = self.current_temp;
let delta = self.pid.next_control_output(self.current_temp).output;
m.set_current_temp(self.current_temp);
let power = if delta > 0.0f32 {
if delta < 100.0f32 {
delta / 100.0f32
}
else {
1.0f32
}
} else {
0.0f32
};
hwa::debug!("TEMP {} -> {}, {} P={} [{}]", self.last_temp, self.current_temp, delta, power, target_temp);
m.set_power((power * 100.0f32).to_u8().unwrap_or(0)).await;
if (self.current_temp - m.get_target_temp()).abs() / target_temp < 0.25 {
State::Maintaining
} else {
State::Targeting
}
} else {
self.current_temp = 0.0;
if self.last_temp != self.current_temp {
self.last_temp = self.current_temp;
}
State::Duty
}
};
if new_state != self.state {
hwa::trace!("Temp changed to {:?}", new_state);
match new_state {
State::Duty => {
#[cfg(feature = "native")]
{
self.t0 = embassy_time::Instant::now();
}
event_bus.publish_event(EventStatus::not_containing(temperature_flag)).await;
}
State::Maintaining => {
#[cfg(feature = "native")]
hwa::debug!("Temperature reached. Firing {:?}.", temperature_flag);
m.flush_notification(action).await;
event_bus.publish_event(EventStatus::containing(temperature_flag)).await;
}
State::Targeting => {
#[cfg(feature = "native")]
{
self.t0 = embassy_time::Instant::now();
}
event_bus.publish_event(EventStatus::not_containing(temperature_flag)).await;
}
}
self.state = new_state;
}
else if m.is_awaited() {
match new_state {
State::Maintaining => {
m.flush_notification(action).await;
}
_ => {}
}
}
}
}
#[embassy_executor::task(pool_size=1)]
pub async fn task_temperature(
event_bus: EventBusRef,
#[cfg(feature = "with-hot-end")]
hotend_controller: hwa::controllers::HotendControllerRef,
#[cfg(feature = "with-hot-bed")]
hotbed_controller: hwa::controllers::HotbedControllerRef,
) -> ! {
hwa::debug!("temperature_task started");
let mut ticker = Ticker::every(Duration::from_secs(2));
#[cfg(feature = "with-hot-end")]
let mut hotend_sm = HeaterStateMachine::new();
#[cfg(feature = "with-hot-bed")]
let mut hotbed_sm = HeaterStateMachine::new();
loop {
ticker.next().await;
#[cfg(feature = "with-hot-end")]
hotend_sm.update(&hotend_controller, &event_bus, EventFlags::HOTEND_TEMP_OK, DeferAction::HotendTemperature).await;
#[cfg(feature = "with-hot-bed")]
hotbed_sm.update(&hotbed_controller, &event_bus, EventFlags::HOTBED_TEMP_OK, DeferAction::HotbedTemperature).await;
}
}