Process a command frame. Returns true if it was for this VESC.
(&mut self, frame: &CanFrame)
| 32 | |
| 33 | /// Process a command frame. Returns true if it was for this VESC. |
| 34 | pub fn process_command(&mut self, frame: &CanFrame) -> bool { |
| 35 | if !frame.extended { |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | let frame_id = (frame.id & 0xFF) as u8; |
| 40 | if frame_id != self.id { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | let cmd = (frame.id >> 8) as u8; |
| 45 | |
| 46 | match cmd { |
| 47 | // Set RPM |
| 48 | 3 if frame.data.len() >= 4 => { |
| 49 | self.target_erpm = |
| 50 | i32::from_be_bytes([frame.data[0], frame.data[1], frame.data[2], frame.data[3]]); |
| 51 | true |
| 52 | } |
| 53 | // Set current |
| 54 | 1 if frame.data.len() >= 4 => { |
| 55 | let current_ma = |
| 56 | i32::from_be_bytes([frame.data[0], frame.data[1], frame.data[2], frame.data[3]]); |
| 57 | self.target_erpm = (current_ma as f32 * 10.0) as i32; |
| 58 | true |
| 59 | } |
| 60 | // Set duty |
| 61 | 0 if frame.data.len() >= 4 => { |
| 62 | let duty_scaled = |
| 63 | i32::from_be_bytes([frame.data[0], frame.data[1], frame.data[2], frame.data[3]]); |
| 64 | const MAX_ERPM: f32 = 8000.0; |
| 65 | let duty_normalized = duty_scaled as f32 / 100000.0; |
| 66 | self.target_erpm = (duty_normalized * MAX_ERPM) as i32; |
| 67 | true |
| 68 | } |
| 69 | _ => false, |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /// Update simulation state. |
| 74 | pub fn tick(&mut self, dt: f64) { |
no test coverage detected