summaryrefslogtreecommitdiffstats
path: root/firmware/src/pi.rs
blob: 6f7b160bd90472d55f0e0aed1296a4aa0f654660 (plain)
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
use crate::fixpt::Fixpt;

#[derive(Clone)]
pub struct PiParams {
    pub kp: Fixpt,
    pub ki: Fixpt,
    pub ilim: Fixpt,
}

pub struct Pi {
    i: Fixpt,
}

impl Pi {
    pub const fn new() -> Self {
        Self {
            i: Fixpt::from_int(0),
        }
    }

    pub fn run(&mut self, params: &PiParams, sp: Fixpt, r: Fixpt) -> Fixpt {
        // deviation
        let e = sp - r;

        // P term
        let p = params.kp * e;

        // I term
        let i = self.i + (params.ki * e);
        let i = i.min(params.ilim);
        let i = i.max(-params.ilim);
        self.i = i;

        p + i
    }
}

// vim: ts=4 sw=4 expandtab
bues.ch cgit interface