cu-pid 1.0.0

A PID controller for the Copper project.
Documentation
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
extern crate alloc;

use bincode::de::Decoder;
use bincode::enc::Encoder;
use bincode::error::{DecodeError, EncodeError};
use bincode::{Decode, Encode};
use core::marker::PhantomData;
use cu29::prelude::*;
use cu29::reflect::{Reflect, ReflectTypePath};
use serde::{Deserialize, Serialize};

#[cfg(not(feature = "std"))]
use alloc::format;

/// Output of the PID controller.
#[derive(Debug, Default, Clone, Encode, Decode, Serialize, Deserialize, Reflect)]
pub struct PIDControlOutputPayload {
    /// Proportional term
    pub p: f32,
    /// Integral term
    pub i: f32,
    /// Derivative term
    pub d: f32,
    /// Final output
    pub output: f32,
}

/// This is the underlying standard PID controller.
#[derive(Reflect)]
pub struct PIDController {
    // Configuration
    kp: f32,
    ki: f32,
    kd: f32,
    setpoint: f32,
    p_limit: f32,
    i_limit: f32,
    d_limit: f32,
    output_limit: f32,
    sampling: CuDuration,
    // Internal state
    integral: f32,
    last_error: f32,
    elapsed: CuDuration,
    last_output: PIDControlOutputPayload,
}

impl PIDController {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        kp: f32,
        ki: f32,
        kd: f32,
        setpoint: f32,
        p_limit: f32,
        i_limit: f32,
        d_limit: f32,
        output_limit: f32,
        sampling: CuDuration, // to avoid oversampling and get a bunch of zeros.
    ) -> Self {
        PIDController {
            kp,
            ki,
            kd,
            setpoint,
            integral: 0.0,
            last_error: 0.0,
            p_limit,
            i_limit,
            d_limit,
            output_limit,
            elapsed: CuDuration::default(),
            sampling,
            last_output: PIDControlOutputPayload::default(),
        }
    }

    pub fn reset(&mut self) {
        self.integral = 0.0f32;
        self.last_error = 0.0f32;
    }

    pub fn reset_integral(&mut self) {
        self.integral = 0.0f32;
    }

    pub fn init_measurement(&mut self, measurement: f32) {
        self.last_error = self.setpoint - measurement;
        self.elapsed = self.sampling; // force the computation on the first next_control_output
    }

    pub fn next_control_output(
        &mut self,
        measurement: f32,
        dt: CuDuration,
    ) -> PIDControlOutputPayload {
        self.elapsed += dt;

        if self.elapsed < self.sampling {
            // if we bang too fast the PID controller, just keep on giving the same answer
            return self.last_output.clone();
        }

        let error = self.setpoint - measurement;
        let CuDuration(elapsed) = self.elapsed;
        let dt = elapsed as f32 / 1_000_000f32; // the unit is kind of arbitrary.
        if dt == 0.0 {
            return self.last_output.clone();
        }

        // Proportional term
        let p_unbounded = self.kp * error;
        let p = p_unbounded.clamp(-self.p_limit, self.p_limit);

        // Integral term (accumulated over time)
        self.integral += error * dt;
        let i_unbounded = self.ki * self.integral;
        let i = i_unbounded.clamp(-self.i_limit, self.i_limit);

        // Derivative term (rate of change)
        let derivative = (error - self.last_error) / dt;
        let d_unbounded = self.kd * derivative;
        let d = d_unbounded.clamp(-self.d_limit, self.d_limit);

        // Update last error for next calculation
        self.last_error = error;

        // Final output: sum of P, I, D with output limit
        let output_unbounded = p + i + d;
        let output = output_unbounded.clamp(-self.output_limit, self.output_limit);

        let output = PIDControlOutputPayload { p, i, d, output };

        self.last_output = output.clone();
        self.elapsed = CuDuration::default();
        output
    }
}

impl Freezable for PIDController {
    fn freeze<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
        Encode::encode(&self.integral, encoder)?;
        Encode::encode(&self.last_error, encoder)?;
        Encode::encode(&self.elapsed, encoder)?;
        Encode::encode(&self.last_output, encoder)?;
        Ok(())
    }

    fn thaw<D: Decoder>(&mut self, decoder: &mut D) -> Result<(), DecodeError> {
        self.integral = Decode::decode(decoder)?;
        self.last_error = Decode::decode(decoder)?;
        self.elapsed = Decode::decode(decoder)?;
        self.last_output = Decode::decode(decoder)?;
        Ok(())
    }
}

/// This is the Copper task encapsulating the PID controller.
#[derive(Reflect)]
pub struct GenericPIDTask<I>
where
    f32: for<'a> From<&'a I>,
{
    #[reflect(ignore)]
    _marker: PhantomData<fn() -> I>,
    pid: PIDController,
    first_run: bool,
    last_tov: CuTime,
    setpoint: f32,
    cutoff: f32,
}

impl<I> CuTask for GenericPIDTask<I>
where
    f32: for<'a> From<&'a I>,
    I: CuMsgPayload + ReflectTypePath + 'static,
{
    type Resources<'r> = ();
    type Input<'m> = input_msg!(I);
    type Output<'m> = output_msg!(PIDControlOutputPayload);

    fn new(config: Option<&ComponentConfig>, _resources: Self::Resources<'_>) -> CuResult<Self>
    where
        Self: Sized,
    {
        match config {
            Some(config) => {
                debug!("PIDTask config loaded");
                let setpoint: f32 = config
                    .get::<f64>("setpoint")?
                    .ok_or("'setpoint' not found in config")?
                    as f32;

                let cutoff: f32 = config.get::<f64>("cutoff")?.ok_or(
                    "'cutoff' not found in config, please set an operating +/- limit on the input.",
                )? as f32;

                // p is mandatory
                let kp = match config.get::<f64>("kp")? {
                    Some(kp) => Ok(kp as f32),
                    None => Err(CuError::from(
                        "'kp' not found in the config. We need at least 'kp' to make the PID algorithm work.",
                    )),
                }?;

                let p_limit = getcfg(config, "pl", 2.0f32)?;
                let ki = getcfg(config, "ki", 0.0f32)?;
                let i_limit = getcfg(config, "il", 1.0f32)?;
                let kd = getcfg(config, "kd", 0.0f32)?;
                let d_limit = getcfg(config, "dl", 2.0f32)?;
                let output_limit = getcfg(config, "ol", 1.0f32)?;

                let sampling = if let Some(value) = config.get::<u32>("sampling_ms")? {
                    CuDuration::from(value as u64 * 1_000_000u64)
                } else {
                    CuDuration::default()
                };

                let pid: PIDController = PIDController::new(
                    kp,
                    ki,
                    kd,
                    setpoint,
                    p_limit,
                    i_limit,
                    d_limit,
                    output_limit,
                    sampling,
                );

                Ok(Self {
                    _marker: PhantomData,
                    pid,
                    first_run: true,
                    last_tov: CuTime::default(),
                    setpoint,
                    cutoff,
                })
            }
            None => Err(CuError::from("PIDTask needs a config.")),
        }
    }

    fn process(
        &mut self,
        _ctx: &CuContext,
        input: &Self::Input<'_>,
        output: &mut Self::Output<'_>,
    ) -> CuResult<()> {
        output.tov = input.tov;
        match input.payload() {
            Some(payload) => {
                let tov = match input.tov {
                    Tov::Time(single) => single,
                    _ => return Err("Unexpected variant for a TOV of PID".into()),
                };

                let measure: f32 = payload.into();

                if self.first_run {
                    self.first_run = false;
                    self.last_tov = tov;
                    self.pid.init_measurement(measure);
                    output.clear_payload();
                    return Ok(());
                }
                let dt = tov - self.last_tov;
                self.last_tov = tov;

                // update the status of the pid.
                let state = self.pid.next_control_output(measure, dt);
                // But safety check if the input is within operational margins and cut power if it is not.
                let upper_limit = self.setpoint + self.cutoff;
                let lower_limit = self.setpoint - self.cutoff;
                if measure > upper_limit {
                    return Err(format!("{} > {} (cutoff)", measure, upper_limit).into());
                }
                if measure < lower_limit {
                    return Err(format!("{} < {} (cutoff)", measure, lower_limit).into());
                }
                output.metadata.set_status(format!(
                    "{:>5.2} {:>5.2} {:>5.2} {:>5.2}",
                    &state.output, &state.p, &state.i, &state.d
                ));
                output.set_payload(state);
            }
            None => output.clear_payload(),
        };
        Ok(())
    }

    fn stop(&mut self, _ctx: &CuContext) -> CuResult<()> {
        self.pid.reset();
        self.first_run = true;
        Ok(())
    }
}

/// Store/Restore the internal state of the PID controller.
impl<I> Freezable for GenericPIDTask<I>
where
    f32: for<'a> From<&'a I>,
{
    fn freeze<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
        self.pid.freeze(encoder)?;
        Encode::encode(&self.first_run, encoder)?;
        Encode::encode(&self.last_tov, encoder)?;
        Ok(())
    }

    fn thaw<D: Decoder>(&mut self, decoder: &mut D) -> Result<(), DecodeError> {
        self.pid.thaw(decoder)?;
        self.first_run = Decode::decode(decoder)?;
        self.last_tov = Decode::decode(decoder)?;
        Ok(())
    }
}

// Small helper befause we do this again and again
fn getcfg(config: &ComponentConfig, key: &str, default: f32) -> Result<f32, ConfigError> {
    Ok(config
        .get::<f64>(key)?
        .map(|value| value as f32)
        .unwrap_or(default))
}

#[cfg(test)]
mod tests {
    use super::*;
    use bincode::config::standard;
    use bincode::de::DecoderImpl;
    use bincode::de::read::SliceReader;
    use bincode::encode_to_vec;

    #[derive(Clone, Copy)]
    struct TestInput;

    impl From<&TestInput> for f32 {
        fn from(_: &TestInput) -> Self {
            0.0
        }
    }

    fn sample_task() -> GenericPIDTask<TestInput> {
        GenericPIDTask {
            _marker: PhantomData,
            pid: PIDController {
                kp: 1.0,
                ki: 2.0,
                kd: 3.0,
                setpoint: 4.0,
                p_limit: 5.0,
                i_limit: 6.0,
                d_limit: 7.0,
                output_limit: 8.0,
                sampling: CuDuration::from(9),
                integral: 10.0,
                last_error: 11.0,
                elapsed: CuDuration::from(12),
                last_output: PIDControlOutputPayload {
                    p: 13.0,
                    i: 14.0,
                    d: 15.0,
                    output: 16.0,
                },
            },
            first_run: false,
            last_tov: CuTime::from(17_u64),
            setpoint: 18.0,
            cutoff: 19.0,
        }
    }

    #[test]
    fn freeze_thaw_restores_pid_timekeeping_state() {
        let original = sample_task();
        let bytes =
            encode_to_vec(BincodeAdapter(&original), standard()).expect("encode pid task state");

        let mut restored = sample_task();
        restored.pid.integral = -1.0;
        restored.pid.last_error = -2.0;
        restored.pid.elapsed = CuDuration::from(999);
        restored.pid.last_output = PIDControlOutputPayload {
            p: -3.0,
            i: -4.0,
            d: -5.0,
            output: -6.0,
        };
        restored.first_run = true;
        restored.last_tov = CuTime::from(1_000_u64);

        let reader = SliceReader::new(&bytes);
        let mut decoder = DecoderImpl::new(reader, standard(), ());
        restored.thaw(&mut decoder).expect("thaw pid task state");

        assert_eq!(restored.pid.integral, original.pid.integral);
        assert_eq!(restored.pid.last_error, original.pid.last_error);
        assert_eq!(restored.pid.elapsed, original.pid.elapsed);
        assert_eq!(restored.pid.last_output.p, original.pid.last_output.p);
        assert_eq!(restored.pid.last_output.i, original.pid.last_output.i);
        assert_eq!(restored.pid.last_output.d, original.pid.last_output.d);
        assert_eq!(
            restored.pid.last_output.output,
            original.pid.last_output.output
        );
        assert_eq!(restored.first_run, original.first_run);
        assert_eq!(restored.last_tov, original.last_tov);
    }
}