From eadf30aff742deb80f89f9065936dc1fda4ec58d Mon Sep 17 00:00:00 2001 From: Sam Der Date: Wed, 29 May 2024 09:09:03 -0700 Subject: [PATCH] Create inverter board abstraction to send velocity and throttle to Raspberry Pi Pico (#83) * Add inverter board abstraction * InverterBoard refactoring - Extract arguments to Uart::with_path to constants - Simplify send_control to use format! rather than string concatenation --- .../src/components/inverter_board.rs | 25 +++++++++++++++++++ pod-operation/src/components/mod.rs | 1 + pod-operation/src/demo.rs | 8 ++++++ pod-operation/src/main.rs | 4 +++ 4 files changed, 38 insertions(+) create mode 100644 pod-operation/src/components/inverter_board.rs diff --git a/pod-operation/src/components/inverter_board.rs b/pod-operation/src/components/inverter_board.rs new file mode 100644 index 00000000..7a4c143f --- /dev/null +++ b/pod-operation/src/components/inverter_board.rs @@ -0,0 +1,25 @@ +use rppal::uart::{Parity, Uart}; + +const SERIAL_PATH: &str = "/dev/tty/ACM0"; +const BAUD_RATE: u32 = 9600; +const PARITY: Parity = Parity::None; +const DATA_BITS: u8 = 8; +const STOP_BITS: u8 = 1; + +pub struct InverterBoard { + uart: Uart, +} + +impl InverterBoard { + pub fn new() -> Self { + let uart = Uart::with_path(SERIAL_PATH, BAUD_RATE, PARITY, DATA_BITS, STOP_BITS).unwrap(); + Self { uart } + } + + /// Combine velocity and throttle into a space-separated string message and then send it over to + /// the Pico as bytes. + pub fn send_control(&mut self, velocity: f32, throttle: f32) { + let message = format!("{velocity} {throttle}\n"); + self.uart.write(message.as_bytes()).unwrap(); + } +} diff --git a/pod-operation/src/components/mod.rs b/pod-operation/src/components/mod.rs index 359e1a54..02e112a3 100644 --- a/pod-operation/src/components/mod.rs +++ b/pod-operation/src/components/mod.rs @@ -1,6 +1,7 @@ pub mod brakes; pub mod gyro; pub mod high_voltage_system; +pub mod inverter_board; pub mod lim_current; pub mod lim_temperature; pub mod pressure_transducer; diff --git a/pod-operation/src/demo.rs b/pod-operation/src/demo.rs index e4e766e3..1cabf1ab 100644 --- a/pod-operation/src/demo.rs +++ b/pod-operation/src/demo.rs @@ -3,6 +3,7 @@ use tracing::info; use crate::components::brakes::Brakes; use crate::components::gyro::Gyroscope; use crate::components::high_voltage_system::HighVoltageSystem; +use crate::components::inverter_board::InverterBoard; use crate::components::lim_current::LimCurrent; use crate::components::lim_temperature::LimTemperature; use crate::components::pressure_transducer::PressureTransducer; @@ -121,3 +122,10 @@ pub async fn high_voltage_system(mut high_voltage_system: HighVoltageSystem) { i += 1; } } + +pub async fn inverter_control(mut inverter_control: InverterBoard) { + loop { + inverter_control.send_control(0.0, 1.0); + tokio::time::sleep(std::time::Duration::from_secs(1)).await; + } +} diff --git a/pod-operation/src/main.rs b/pod-operation/src/main.rs index 6dc31212..198c8766 100644 --- a/pod-operation/src/main.rs +++ b/pod-operation/src/main.rs @@ -10,6 +10,7 @@ mod state_machine; use crate::components::brakes::Brakes; use crate::components::gyro::Gyroscope; use crate::components::high_voltage_system::HighVoltageSystem; +use crate::components::inverter_board::InverterBoard; use crate::components::lim_current::LimCurrent; use crate::components::lim_temperature::LimTemperature; use crate::components::pressure_transducer::PressureTransducer; @@ -56,6 +57,9 @@ async fn main() -> Result<(), Box> { let limcurrent = LimCurrent::new(ads1x1x::SlaveAddr::Default); tokio::spawn(demo::read_lim_current(limcurrent)); + let inverter_board = InverterBoard::new(); + tokio::spawn(demo::inverter_control(inverter_board)); + let app = axum::Router::new().layer(layer); info!("Starting server on port 5000");