summaryrefslogtreecommitdiffstats
path: root/firmware/src/speedo.rs
blob: f373fe9c60ebaae99227229a5f11eb56b47f7b2f (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
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
use crate::{
    analog::AcCapture,
    fixpt::Fixpt,
    mutex::MainCtx,
    timer::{timer_get, RelTimestamp, Timestamp, TIMER_TICK_US},
};

/// 2 edge (rising falling) in AC capture.
/// 4 speedometer edges per motor revolution
const SPEEDO_FACT: u32 = 4 * 2;

const OK_THRES: u8 = 4;

pub struct MotorSpeed(Fixpt);

impl MotorSpeed {
    const FACT_16HZ: u16 = 16;

    fn from_period_dur(dur: RelTimestamp) -> Self {
        let dur: i8 = dur.into();
        let dur: u8 = dur as _;

        // fact 2 to avoid rounding error.
        let num = (1_000_000 / (TIMER_TICK_US as u32 * (SPEEDO_FACT / 2))) as u16;
        let denom = dur as u16 * Self::FACT_16HZ * 2;

        Self(Fixpt::from_decimal(num as i16, denom as i16))
    }

    pub fn as_16hz(&self) -> Fixpt {
        self.0
    }
}

pub struct Speedo {
    ok_count: u8,
    prev_stamp: Timestamp,
    dur: [u8; 4],
}

impl Speedo {
    pub const fn new() -> Self {
        Self {
            ok_count: 0,
            prev_stamp: Timestamp::new(),
            dur: [0; 4],
        }
    }

    pub fn reset(&mut self) {
        self.ok_count = 0;
    }

    pub fn get_speed(&mut self) -> Option<MotorSpeed> {
        if self.ok_count < OK_THRES {
            None
        } else {
            Some(MotorSpeed::from_period_dur(self.get_dur()))
        }
    }

    pub fn get_dur(&self) -> RelTimestamp {
        let a = self.dur[0] as u16;
        let b = self.dur[1] as u16;
        let c = self.dur[2] as u16;
        let d = self.dur[3] as u16;
        let dur: u8 = ((a + b + c + d) / 4) as _;
        let dur: i8 = dur as _;
        dur.into()
    }

    fn new_duration(&mut self, dur: RelTimestamp) {
        let dur: i8 = dur.into();
        self.dur[0] = self.dur[1];
        self.dur[1] = self.dur[2];
        self.dur[2] = self.dur[3];
        self.dur[3] = dur as _;
        self.ok_count = self.ok_count.saturating_add(1);
    }

    pub fn update(&mut self, m: &MainCtx<'_>, ac: &AcCapture) {
        let now = timer_get(&m.to_any());
        if now < self.prev_stamp {
            // prev_stamp wrapped. Drop it.
            self.ok_count = 0;
        }
        if ac.is_new() {
            let ac_stamp = ac.stamp();
            if ac_stamp >= self.prev_stamp {
                let dur = ac_stamp - self.prev_stamp;
                self.new_duration(dur);
            } else {
                // prev_stamp wrapped.
                self.ok_count = 0;
            }
            self.prev_stamp = ac_stamp;
        }
    }
}

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