Skip to content

Commit fe87f2f

Browse files
committed
Add conditional compilation for motor uart/vesc
1 parent 24ce05b commit fe87f2f

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

pod-operation/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ ina219 = ["dep:rppal"]
3232
ads1015 = ["dep:rppal"]
3333
mpu6050 = ["dep:rppal"]
3434
inverter = ["dep:rppal"]
35+
vesc = ["dep:rppal"]
3536
lidar = ["dep:lidar_lite_v3"]
36-
rpi = ["gpio", "ads1015", "ina219", "mpu6050", "inverter", "lidar"]
37+
rpi = ["gpio", "ads1015", "ina219", "mpu6050", "inverter", "vesc", "lidar"]
Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,55 @@
1-
use rppal::uart::{Parity, Uart};
2-
use vesc_comm::{VescConnection, VescErrorWithBacktrace};
1+
#[cfg(feature = "vesc")]
2+
use {rppal::uart::Uart, vesc_comm::VescConnection};
33

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+
}
817

918
const WHEEL_DIAMETER: f32 = 1.5; // inches
1019
const MPH_TO_IN_PER_MIN: f32 = 1056.0;
1120
const MPH_TO_RPM: f32 = MPH_TO_IN_PER_MIN / WHEEL_DIAMETER;
1221

1322
pub struct Motors {
23+
#[cfg(feature = "vesc")]
1424
pub vesc: VescConnection<Uart>,
1525
}
1626

1727
impl Motors {
28+
#[cfg(feature = "vesc")]
1829
pub fn new(serial_path: &str) -> Self {
30+
use serial_constants::*;
1931
let uart = Uart::with_path(serial_path, BAUD_RATE, PARITY, DATA_BITS, STOP_BITS).unwrap();
2032
let conn = VescConnection::new(uart);
33+
info!("Initialized VESC on {}", serial_path);
2134
Self { vesc: conn }
2235
}
2336

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")]
2444
pub fn set_speed_mph(&mut self, new_speed_mph: f32) -> Result<(), VescErrorWithBacktrace> {
45+
debug!("Driving motors at {}", new_speed_mph);
2546
self.vesc
2647
.set_rpm((new_speed_mph * MPH_TO_RPM).round() as u32)
2748
}
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+
}
2855
}

pod-operation/src/demo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub async fn vesc_motors(mut motors: Motors) {
141141
loop {
142142
motors.set_speed_mph(10.0).unwrap();
143143
tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
144-
println!("{:?}", motors.vesc.get_fw_version().unwrap());
144+
// println!("{:?}", motors.vesc.get_fw_version().unwrap());
145145
motors.set_speed_mph(0.0).unwrap();
146146
tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
147147
}

0 commit comments

Comments
 (0)