Skip to main content

motor_codec/
event.rs

1//! Decoded inbound-frame events.
2
3/// A decoded inbound message from a motor.
4#[derive(Copy, Clone, Debug, PartialEq)]
5#[non_exhaustive]
6pub enum Event {
7    /// Motor state response (the per-tick mechanical state).
8    State {
9        /// recv CAN ID of the responding motor.
10        motor_id: u32,
11        /// Position (radians).
12        q: f64,
13        /// Velocity (rad/s).
14        dq: f64,
15        /// Estimated torque (Nm).
16        tau: f64,
17        /// MOSFET temperature (degrees C).
18        t_mos: i16,
19        /// Rotor temperature (degrees C).
20        t_rotor: i16,
21    },
22    /// Reply to a parameter read or write (vendor-specific sub-protocol).
23    ParamReply {
24        /// recv CAN ID of the responding motor.
25        motor_id: u32,
26        /// Register identifier the reply refers to.
27        rid: u16,
28        /// Decoded value.
29        value: ParamValue,
30    },
31    /// A motor fault notification.
32    Fault {
33        /// recv CAN ID of the responding motor.
34        motor_id: u32,
35        /// Vendor-defined fault code.
36        code: u16,
37    },
38}
39
40/// Union of value encodings used by vendor parameter sub-protocols.
41#[derive(Copy, Clone, Debug, PartialEq)]
42#[non_exhaustive]
43pub enum ParamValue {
44    /// IEEE-754 float (typical for gains, limits).
45    Float(f64),
46    /// Unsigned integer (typical for IDs, mode selectors).
47    UInt(u32),
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn state_event_carries_all_five_fields() {
56        let e = Event::State {
57            motor_id: 0x11,
58            q: 0.5,
59            dq: 0.1,
60            tau: 0.2,
61            t_mos: 30,
62            t_rotor: 35,
63        };
64        match e {
65            Event::State {
66                motor_id,
67                q,
68                dq,
69                tau,
70                t_mos,
71                t_rotor,
72            } => {
73                assert_eq!(motor_id, 0x11);
74                assert_eq!(q, 0.5);
75                assert_eq!(dq, 0.1);
76                assert_eq!(tau, 0.2);
77                assert_eq!(t_mos, 30);
78                assert_eq!(t_rotor, 35);
79            }
80            _ => panic!("wrong variant"),
81        }
82    }
83
84    #[test]
85    fn param_value_union() {
86        let f = ParamValue::Float(1.5);
87        let u = ParamValue::UInt(42);
88        assert!(matches!(f, ParamValue::Float(_)));
89        assert!(matches!(u, ParamValue::UInt(_)));
90    }
91}