Skip to content

Commit d33bbea

Browse files
committed
Preliminary integration of VESC motors into FSM
1 parent e29bb6b commit d33bbea

File tree

3 files changed

+33
-56
lines changed

3 files changed

+33
-56
lines changed

pod-operation/src/components/motors.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,47 @@ const MPH_TO_RPM: f32 = MPH_TO_IN_PER_MIN / WHEEL_DIAMETER;
2222
pub struct Motors {
2323
#[cfg(feature = "vesc")]
2424
pub vesc: VescConnection<Uart>,
25+
#[cfg(feature = "vesc")]
26+
pub vesc2: VescConnection<Uart>,
2527
}
2628

2729
impl Motors {
2830
#[cfg(feature = "vesc")]
29-
pub fn new(serial_path: &str) -> Self {
31+
pub fn new(serial_path_one: &str, serial_path_two: &str) -> Self {
3032
use serial_constants::*;
31-
let uart = Uart::with_path(serial_path, BAUD_RATE, PARITY, DATA_BITS, STOP_BITS).unwrap();
33+
use vesc_comm::VescConnection;
34+
let uart =
35+
Uart::with_path(serial_path_one, BAUD_RATE, PARITY, DATA_BITS, STOP_BITS).unwrap();
36+
let uart2 =
37+
Uart::with_path(serial_path_two, BAUD_RATE, PARITY, DATA_BITS, STOP_BITS).unwrap();
3238
let conn = VescConnection::new(uart);
33-
info!("Initialized VESC on {}", serial_path);
34-
Self { vesc: conn }
39+
let conn2 = VescConnection::new(uart2);
40+
info!(
41+
"Initialized VESC on paths {} and {}",
42+
serial_path_one, serial_path_two
43+
);
44+
Self {
45+
vesc: conn,
46+
vesc2: conn2,
47+
}
3548
}
3649

3750
#[cfg(not(feature = "vesc"))]
38-
pub fn new(serial_path: &str) -> Self {
39-
info!("Mocking VESC on {}", serial_path);
51+
pub fn new(serial_path_one: &str, serial_path_two: &str) -> Self {
52+
info!(
53+
"Mocking VESC on {} and {}",
54+
serial_path_one, serial_path_two
55+
);
4056
Self {}
4157
}
4258

4359
#[cfg(feature = "vesc")]
4460
pub fn set_speed_mph(&mut self, new_speed_mph: f32) -> Result<(), VescErrorWithBacktrace> {
4561
debug!("Driving motors at {}", new_speed_mph);
46-
self.vesc
47-
.set_rpm((new_speed_mph * MPH_TO_RPM).round() as u32)
62+
let new_speed: u32 = (new_speed_mph * MPH_TO_RPM).round() as u32;
63+
self.vesc.set_rpm(new_speed)?;
64+
self.vesc2.set_rpm(new_speed)?;
65+
Ok(())
4866
}
4967

5068
#[cfg(not(feature = "vesc"))]

pod-operation/src/main.rs

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,6 @@ mod demo;
88
mod state_machine;
99
mod utils;
1010

11-
use crate::components::brakes::Brakes;
12-
use crate::components::gyro::Gyroscope;
13-
use crate::components::high_voltage_system::HighVoltageSystem;
14-
use crate::components::inverter_board::InverterBoard;
15-
use crate::components::lidar::Lidar;
16-
use crate::components::lim_current::LimCurrent;
17-
use crate::components::lim_temperature::LimTemperature;
18-
use crate::components::motors::Motors;
19-
use crate::components::pressure_transducer::PressureTransducer;
20-
use crate::components::signal_light::SignalLight;
21-
use crate::components::wheel_encoder::WheelEncoder;
2211
use crate::state_machine::StateMachine;
2312

2413
#[tokio::main]
@@ -30,43 +19,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
3019

3120
let (layer, io) = SocketIo::new_layer();
3221

33-
let signal_light = SignalLight::new();
34-
tokio::spawn(demo::blink(signal_light));
35-
36-
let upstream_pressure_transducer = PressureTransducer::upstream();
37-
tokio::spawn(demo::read_pressure_transducer(upstream_pressure_transducer));
38-
39-
let downstream_pressure_transducer = PressureTransducer::downstream();
40-
tokio::spawn(demo::read_pressure_transducer(
41-
downstream_pressure_transducer,
42-
));
43-
44-
let ads1015 = LimTemperature::new(ads1x1x::SlaveAddr::Default);
45-
tokio::spawn(demo::read_ads1015(ads1015));
46-
47-
let wheel_encoder = WheelEncoder::new();
48-
tokio::spawn(demo::read_wheel_encoder(wheel_encoder));
49-
50-
let gyro: Gyroscope = Gyroscope::new();
51-
tokio::spawn(demo::read_gyroscope(gyro));
52-
let brakes = Brakes::new();
53-
tokio::spawn(demo::brake(brakes));
54-
55-
let high_voltage_system = HighVoltageSystem::new();
56-
tokio::spawn(demo::high_voltage_system(high_voltage_system));
57-
58-
let lidar = Lidar::new();
59-
tokio::spawn(demo::read_lidar(lidar));
60-
61-
let limcurrent = LimCurrent::new(ads1x1x::SlaveAddr::Default);
62-
tokio::spawn(demo::read_lim_current(limcurrent));
63-
64-
let inverter_board = InverterBoard::new();
65-
tokio::spawn(demo::inverter_control(inverter_board));
66-
67-
let motors = Motors::new("/dev/ttyACM0");
68-
tokio::spawn(demo::vesc_motors(motors));
69-
7022
let mut state_machine = StateMachine::new(io);
7123
tokio::spawn(async move {
7224
state_machine.run().await;

pod-operation/src/state_machine.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::components::gyro::Gyroscope;
1414
use crate::components::high_voltage_system::HighVoltageSystem;
1515
use crate::components::lidar::Lidar;
1616
use crate::components::lim_temperature::LimTemperature;
17+
use crate::components::motors::Motors;
1718
use crate::components::pressure_transducer::PressureTransducer;
1819
use crate::components::signal_light::SignalLight;
1920
use crate::components::wheel_encoder::WheelEncoder;
@@ -55,6 +56,7 @@ pub struct StateMachine {
5556
lidar: Lidar,
5657
wheel_encoder: std::sync::Arc<std::sync::Mutex<WheelEncoder>>,
5758
gyro: Gyroscope,
59+
motors: Motors,
5860
}
5961

6062
impl StateMachine {
@@ -113,6 +115,7 @@ impl StateMachine {
113115
lidar: Lidar::new(),
114116
gyro: Gyroscope::new(),
115117
wheel_encoder: std::sync::Arc::new(std::sync::Mutex::new(WheelEncoder::new())),
118+
motors: Motors::new("/dev/ttyACM0", "/dev/ttyACM1"),
116119
}
117120
}
118121

@@ -225,24 +228,28 @@ impl StateMachine {
225228
self.high_voltage_system.enable(); // Enable high voltage system -- may move later
226229
self.signal_light.enable();
227230
self.brakes.disengage();
231+
self.motors.set_speed_mph(10.0).unwrap();
228232
}
229233

230234
fn _enter_stopped(&mut self) {
231235
info!("Entering Stopped state");
232236
self.signal_light.disable();
237+
self.motors.set_speed_mph(0.0).unwrap();
233238
self.brakes.engage();
234239
}
235240

236241
fn _enter_halted(&mut self) {
237242
info!("Entering Halted state");
238243
self.signal_light.disable();
244+
self.motors.set_speed_mph(0.0).unwrap();
239245
self.brakes.engage();
240246
self.high_voltage_system.disable();
241247
}
242248

243249
fn _enter_faulted(&mut self) {
244250
info!("Entering Faulted state");
245251
self.signal_light.disable();
252+
self.motors.set_speed_mph(0.0).unwrap();
246253
self.brakes.engage();
247254
self.high_voltage_system.disable();
248255
}

0 commit comments

Comments
 (0)