diff --git a/.github/workflows/run-checks.yml b/.github/workflows/run-checks.yml index a52f6fa..a9a26e2 100644 --- a/.github/workflows/run-checks.yml +++ b/.github/workflows/run-checks.yml @@ -89,9 +89,12 @@ jobs: - name: Run cargo fmt run: cargo fmt --all -- --check - - name: Lint pod operation + - name: Lint pod operation (w/o rpi) run: cargo clippy -- -D warnings + - name: Lint pod operation (w/ rpi) + run: cargo clippy --features rpi -- -D warnings + - name: Build Pod Operation Program (debug) run: cargo build --target $TARGET --config target.$TARGET.linker=\"aarch64-linux-gnu-gcc\" diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..fc8f27d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "rust-analyzer.cargo.features": [ + // "rpi" + ] +} diff --git a/pod-operation/package.json b/pod-operation/package.json index fd044b2..36212a4 100644 --- a/pod-operation/package.json +++ b/pod-operation/package.json @@ -3,7 +3,7 @@ "version": "1.0.0", "description": "This Rust package is for the main program to be run on the pod. The program runs a finite-state machine to operate the pod components and acts as a Socket.IO server to communicate with the control station.", "scripts": { - "lint": "cargo clippy", + "lint": "cargo clippy && cargo clippy --features rpi", "format": "cargo fmt", "test": "cargo test" }, diff --git a/pod-operation/src/components/brakes.rs b/pod-operation/src/components/brakes.rs index ac74438..5834cc6 100644 --- a/pod-operation/src/components/brakes.rs +++ b/pod-operation/src/components/brakes.rs @@ -1,5 +1,5 @@ #[cfg(not(feature = "gpio"))] -use crate::utils::mock::OutputPin; +use crate::utils::mock::gpio::OutputPin; #[cfg(feature = "gpio")] use rppal::gpio::{Gpio, OutputPin}; diff --git a/pod-operation/src/components/gyro.rs b/pod-operation/src/components/gyro.rs index 284d099..f825b92 100644 --- a/pod-operation/src/components/gyro.rs +++ b/pod-operation/src/components/gyro.rs @@ -1,6 +1,8 @@ -use mpu6050::Mpu6050; #[cfg(feature = "mpu6050")] -use rppal::{hal::Delay, i2c::I2c}; +use { + mpu6050::Mpu6050, + rppal::{hal::Delay, i2c::I2c}, +}; use serde::Serialize; diff --git a/pod-operation/src/components/high_voltage_system.rs b/pod-operation/src/components/high_voltage_system.rs index 5a9dd24..4ee9ccd 100644 --- a/pod-operation/src/components/high_voltage_system.rs +++ b/pod-operation/src/components/high_voltage_system.rs @@ -1,5 +1,5 @@ #[cfg(not(feature = "gpio"))] -use crate::utils::mock::OutputPin; +use crate::utils::mock::gpio::OutputPin; #[cfg(feature = "gpio")] use rppal::gpio::{Gpio, OutputPin}; diff --git a/pod-operation/src/components/inverter_board.rs b/pod-operation/src/components/inverter_board.rs index a09e704..cba28ce 100644 --- a/pod-operation/src/components/inverter_board.rs +++ b/pod-operation/src/components/inverter_board.rs @@ -1,7 +1,7 @@ #[cfg(feature = "inverter")] use rppal::uart::{Parity, Uart}; -use tracing::info; +use tracing::debug; #[cfg(feature = "inverter")] mod serial_constants { @@ -31,6 +31,10 @@ impl InverterBoard { #[cfg(feature = "inverter")] pub fn send_control(&mut self, velocity: f32, throttle: f32) { let message = format!("{velocity} {throttle}\n"); + debug!( + "Sending inverter control message: {} {}", + velocity, throttle + ); self.uart.write(message.as_bytes()).unwrap(); } @@ -42,7 +46,7 @@ impl InverterBoard { /// Combine velocity and throttle into a space-separated string message #[cfg(not(feature = "inverter"))] pub fn send_control(&mut self, velocity: f32, throttle: f32) { - info!( + debug!( "Mocking inverter sending message: {} {}", velocity, throttle ); diff --git a/pod-operation/src/components/lidar.rs b/pod-operation/src/components/lidar.rs index 6d3b58e..2097c64 100644 --- a/pod-operation/src/components/lidar.rs +++ b/pod-operation/src/components/lidar.rs @@ -18,6 +18,7 @@ impl Lidar { .expect("Failed to initialize I2C device"); let lidar_lite = LidarLiteV3::new(i2cdev).expect("Failed to initialize LidarLiteV3"); + info!("Initialized LidarLite"); Lidar { lidar_lite } } diff --git a/pod-operation/src/components/lim_current.rs b/pod-operation/src/components/lim_current.rs index 2183033..03e7e35 100644 --- a/pod-operation/src/components/lim_current.rs +++ b/pod-operation/src/components/lim_current.rs @@ -16,9 +16,7 @@ const SENSITIVITY: f32 = 0.066; //Unit: vots/amp (v/a) fn voltage_to_current(voltage: i16) -> f32 { let voltage = f32::from(voltage) / 1000.0; - let current = (voltage - QUIESCENT_VOLTAGE) / SENSITIVITY; - println!("Voltage: {}", voltage); - current + (voltage - QUIESCENT_VOLTAGE) / SENSITIVITY } pub struct LimCurrent { @@ -33,12 +31,13 @@ impl LimCurrent { let mut adc = Ads1x1x::new_ads1015(i2cdev, device_address); adc.set_full_scale_range(FullScaleRange::Within4_096V) .unwrap(); + info!("Configured ADS1015 for for LimCurrent"); LimCurrent { ads1015: adc } } #[cfg(not(feature = "ads1015"))] pub fn new(device_address: SlaveAddr) -> Self { - info!("Mocking ADS at {:?} for LimCurrnt", device_address); + info!("Mocking ADS at {:?} for LimCurrent", device_address); LimCurrent {} } diff --git a/pod-operation/src/components/lim_temperature.rs b/pod-operation/src/components/lim_temperature.rs index 59c7b19..0f42253 100644 --- a/pod-operation/src/components/lim_temperature.rs +++ b/pod-operation/src/components/lim_temperature.rs @@ -41,6 +41,7 @@ impl LimTemperature { pub fn new(device_address: SlaveAddr) -> Self { let i2cdev = I2c::new().unwrap(); let adc = Ads1x1x::new_ads1015(i2cdev, device_address); + info!("Configured ADS1015 for for LimTemperature"); LimTemperature { // ads1015: Mutex::new(adc), ads1015: adc, diff --git a/pod-operation/src/components/pressure_transducer.rs b/pod-operation/src/components/pressure_transducer.rs index fd30001..4df2ca1 100644 --- a/pod-operation/src/components/pressure_transducer.rs +++ b/pod-operation/src/components/pressure_transducer.rs @@ -1,11 +1,11 @@ -#[cfg(feature = "ina219")] -use rppal::i2c::I2c; +use tracing::info; -use ina219::INA219; -use tracing::debug; +#[cfg(feature = "ina219")] +use {ina219::INA219, rppal::i2c::I2c}; // The calibration value is used to adjust the maximum current measurement // and precision of measurements. +#[cfg(feature = "ina219")] const INA219_CALIBRATION_VALUE: u16 = 0xffff; // The pod will be using two INA219s, so we'll need to differentiate them @@ -18,6 +18,7 @@ const INA219_DOWNSTREAM_ADDRESS: u8 = 0x41; // this is not provided in the INA219 library that we are using. Note that this // value changes according to the calibration value. The exact formula can be // found in the INA219 datasheet. +#[cfg(feature = "ina219")] const INA219_SCALING_VALUE: f32 = 160.0; struct Reference { @@ -55,10 +56,10 @@ fn init_ina(device_address: u8) -> INA219 { let device = I2c::new().unwrap(); let mut ina219 = INA219::new(device, device_address); - debug!("Initialized I2C and INA219"); + info!("Initialized I2C and INA219"); ina219.calibrate(INA219_CALIBRATION_VALUE).unwrap(); - debug!("Calibrating INA219"); + info!("Calibrating INA219"); ina219 } @@ -73,6 +74,11 @@ impl PressureTransducer { // This constructor should be used for INA219s where the address pins are // grounded. That is, the device address is 0x40. pub fn upstream() -> Self { + #[cfg(not(feature = "ina219"))] + info!( + "Mocking upstream pressure transducer at {}", + INA219_UPSTREAM_ADDRESS + ); Self { #[cfg(feature = "ina219")] ina: init_ina(INA219_UPSTREAM_ADDRESS), @@ -83,6 +89,11 @@ impl PressureTransducer { // This constructor should be used for INA219s where the address pin A0 is // jumped. That is, the device address is 0x41. pub fn downstream() -> Self { + #[cfg(not(feature = "ina219"))] + info!( + "Mocking downstream pressure transducer at {}", + INA219_DOWNSTREAM_ADDRESS + ); Self { #[cfg(feature = "ina219")] ina: init_ina(INA219_DOWNSTREAM_ADDRESS), diff --git a/pod-operation/src/components/signal_light.rs b/pod-operation/src/components/signal_light.rs index d3d7d66..6550b9a 100644 --- a/pod-operation/src/components/signal_light.rs +++ b/pod-operation/src/components/signal_light.rs @@ -1,5 +1,5 @@ #[cfg(not(feature = "gpio"))] -use crate::utils::mock::OutputPin; +use crate::utils::mock::gpio::OutputPin; #[cfg(feature = "gpio")] use rppal::gpio::{Gpio, OutputPin}; diff --git a/pod-operation/src/components/wheel_encoder.rs b/pod-operation/src/components/wheel_encoder.rs index 19414d0..e455520 100644 --- a/pod-operation/src/components/wheel_encoder.rs +++ b/pod-operation/src/components/wheel_encoder.rs @@ -1,7 +1,7 @@ use std::{ops::Sub, time::Instant}; #[cfg(not(feature = "gpio"))] -use crate::utils::mock::{InputPin, Level}; +use crate::utils::mock::gpio::{InputPin, Level}; #[cfg(feature = "gpio")] use rppal::gpio::{Gpio, InputPin, Level}; diff --git a/pod-operation/src/utils/mock.rs b/pod-operation/src/utils/mock.rs index a3a0c42..5f31cc0 100644 --- a/pod-operation/src/utils/mock.rs +++ b/pod-operation/src/utils/mock.rs @@ -1,46 +1,50 @@ -use tracing::info; - -/// Pin logic levels, copied from rppal::gpio -#[derive(Debug, PartialEq, Eq, Copy, Clone)] -#[repr(u8)] -pub enum Level { - Low = 0, - High = 1, -} - -/// Mock for GPIO InputPin -pub struct InputPin { - pub(crate) pin: u8, -} - -impl InputPin { - pub fn read(&self) -> Level { - info!("Mocking reading pin {} as low", self.pin); - Level::Low +#[cfg(not(feature = "gpio"))] +pub mod gpio { + use tracing::{debug, info}; + + /// Pin logic levels, copied from rppal::gpio + #[derive(Debug, PartialEq, Eq, Copy, Clone)] + #[repr(u8)] + #[allow(dead_code)] + pub enum Level { + Low = 0, + High = 1, } - pub fn is_low(&self) -> bool { - info!("Mocking reading pin {} as low", self.pin); - true + /// Mock for GPIO InputPin + pub struct InputPin { + pub(crate) pin: u8, } - pub fn is_high(&self) -> bool { - info!("Mocking reading pin {} as low", self.pin); - false + impl InputPin { + pub fn read(&self) -> Level { + debug!("Mocking reading pin {} as low", self.pin); + Level::Low + } + + pub fn is_low(&self) -> bool { + debug!("Mocking reading pin {} as low", self.pin); + true + } + + pub fn is_high(&self) -> bool { + debug!("Mocking reading pin {} as low", self.pin); + false + } } -} - -/// Mock for GPIO OutputPin -pub struct OutputPin { - pub(crate) pin: u8, -} -impl OutputPin { - pub fn set_low(&self) { - info!("Mocking pin {} to low", self.pin); + /// Mock for GPIO OutputPin + pub struct OutputPin { + pub(crate) pin: u8, } - pub fn set_high(&self) { - info!("Mocking pin {} to high", self.pin); + impl OutputPin { + pub fn set_low(&self) { + info!("Mocking pin {} to low", self.pin); + } + + pub fn set_high(&self) { + info!("Mocking pin {} to high", self.pin); + } } }