|
1 |
| -use rppal::uart::{Parity, Uart}; |
2 |
| -use vesc_comm::{VescConnection, VescErrorWithBacktrace}; |
| 1 | +#[cfg(feature = "vesc")] |
| 2 | +use {rppal::uart::Uart, vesc_comm::VescConnection}; |
3 | 3 |
|
4 |
| -const BAUD_RATE: u32 = 115200; |
5 |
| -const PARITY: Parity = Parity::None; |
6 |
| -const DATA_BITS: u8 = 8; |
7 |
| -const STOP_BITS: u8 = 1; |
| 4 | +use tracing::debug; |
| 5 | +use tracing::info; |
| 6 | +use vesc_comm::VescErrorWithBacktrace; |
| 7 | + |
| 8 | +#[cfg(feature = "vesc")] |
| 9 | +mod serial_constants { |
| 10 | + use rppal::uart::Parity; |
| 11 | + |
| 12 | + pub const BAUD_RATE: u32 = 115200; |
| 13 | + pub const PARITY: Parity = Parity::None; |
| 14 | + pub const DATA_BITS: u8 = 8; |
| 15 | + pub const STOP_BITS: u8 = 1; |
| 16 | +} |
8 | 17 |
|
9 | 18 | const WHEEL_DIAMETER: f32 = 1.5; // inches
|
10 | 19 | const MPH_TO_IN_PER_MIN: f32 = 1056.0;
|
11 | 20 | const MPH_TO_RPM: f32 = MPH_TO_IN_PER_MIN / WHEEL_DIAMETER;
|
12 | 21 |
|
13 | 22 | pub struct Motors {
|
| 23 | + #[cfg(feature = "vesc")] |
14 | 24 | pub vesc: VescConnection<Uart>,
|
15 | 25 | }
|
16 | 26 |
|
17 | 27 | impl Motors {
|
| 28 | + #[cfg(feature = "vesc")] |
18 | 29 | pub fn new(serial_path: &str) -> Self {
|
| 30 | + use serial_constants::*; |
19 | 31 | let uart = Uart::with_path(serial_path, BAUD_RATE, PARITY, DATA_BITS, STOP_BITS).unwrap();
|
20 | 32 | let conn = VescConnection::new(uart);
|
| 33 | + info!("Initialized VESC on {}", serial_path); |
21 | 34 | Self { vesc: conn }
|
22 | 35 | }
|
23 | 36 |
|
| 37 | + #[cfg(not(feature = "vesc"))] |
| 38 | + pub fn new(serial_path: &str) -> Self { |
| 39 | + info!("Mocking VESC on {}", serial_path); |
| 40 | + Self {} |
| 41 | + } |
| 42 | + |
| 43 | + #[cfg(feature = "vesc")] |
24 | 44 | pub fn set_speed_mph(&mut self, new_speed_mph: f32) -> Result<(), VescErrorWithBacktrace> {
|
| 45 | + debug!("Driving motors at {}", new_speed_mph); |
25 | 46 | self.vesc
|
26 | 47 | .set_rpm((new_speed_mph * MPH_TO_RPM).round() as u32)
|
27 | 48 | }
|
| 49 | + |
| 50 | + #[cfg(not(feature = "vesc"))] |
| 51 | + pub fn set_speed_mph(&mut self, new_speed_mph: f32) -> Result<(), VescErrorWithBacktrace> { |
| 52 | + debug!("Mocking motors at {} RPM", new_speed_mph * MPH_TO_RPM); |
| 53 | + Ok(()) |
| 54 | + } |
28 | 55 | }
|
0 commit comments