Skip to content

Commit

Permalink
Create inverter board abstraction to send velocity and throttle to Ra…
Browse files Browse the repository at this point in the history
…spberry 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
  • Loading branch information
samderanova authored May 29, 2024
1 parent b5bed4a commit eadf30a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pod-operation/src/components/inverter_board.rs
Original file line number Diff line number Diff line change
@@ -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();
}
}
1 change: 1 addition & 0 deletions pod-operation/src/components/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
8 changes: 8 additions & 0 deletions pod-operation/src/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
4 changes: 4 additions & 0 deletions pod-operation/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,6 +57,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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");
Expand Down

0 comments on commit eadf30a

Please sign in to comment.