piper_sdk/robot/
command.rs1use crate::can::PiperFrame;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum CommandPriority {
12 RealtimeControl,
18
19 ReliableCommand,
25}
26
27#[derive(Debug, Clone, Copy)]
31pub struct PiperCommand {
32 pub frame: PiperFrame,
34 pub priority: CommandPriority,
36}
37
38impl PiperCommand {
39 pub fn realtime(frame: PiperFrame) -> Self {
41 Self {
42 frame,
43 priority: CommandPriority::RealtimeControl,
44 }
45 }
46
47 pub fn reliable(frame: PiperFrame) -> Self {
49 Self {
50 frame,
51 priority: CommandPriority::ReliableCommand,
52 }
53 }
54
55 pub fn frame(&self) -> PiperFrame {
57 self.frame
58 }
59
60 pub fn priority(&self) -> CommandPriority {
62 self.priority
63 }
64}
65
66impl From<PiperFrame> for PiperCommand {
67 fn from(frame: PiperFrame) -> Self {
69 Self::reliable(frame)
70 }
71}
72
73impl From<PiperCommand> for PiperFrame {
74 fn from(cmd: PiperCommand) -> Self {
75 cmd.frame
76 }
77}
78
79#[cfg(test)]
80mod tests {
81 use super::*;
82
83 #[test]
84 fn test_command_priority() {
85 let frame = PiperFrame::new_standard(0x123, &[1, 2, 3]);
86
87 let realtime_cmd = PiperCommand::realtime(frame);
88 assert_eq!(realtime_cmd.priority(), CommandPriority::RealtimeControl);
89
90 let reliable_cmd = PiperCommand::reliable(frame);
91 assert_eq!(reliable_cmd.priority(), CommandPriority::ReliableCommand);
92 }
93
94 #[test]
95 fn test_command_from_frame() {
96 let frame = PiperFrame::new_standard(0x123, &[1, 2, 3]);
97 let cmd: PiperCommand = frame.into();
98
99 assert_eq!(cmd.priority(), CommandPriority::ReliableCommand);
101 assert_eq!(cmd.frame().id, 0x123);
102 }
103
104 #[test]
105 fn test_command_to_frame() {
106 let frame = PiperFrame::new_standard(0x123, &[1, 2, 3]);
107 let cmd = PiperCommand::realtime(frame);
108
109 let converted_frame: PiperFrame = cmd.into();
110 assert_eq!(converted_frame.id, 0x123);
111 }
112}