Skip to content

Commit

Permalink
Check both feature sets during linting
Browse files Browse the repository at this point in the history
- Update checks workflow and precommit hooks
- Resolve clippy lint issues
- Create mod for GPIO mocks
  • Loading branch information
taesungh committed Jun 1, 2024
1 parent 58f1813 commit c82a42d
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 56 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/run-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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\"

Expand Down
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rust-analyzer.cargo.features": [
// "rpi"
]
}
2 changes: 1 addition & 1 deletion pod-operation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion pod-operation/src/components/brakes.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
6 changes: 4 additions & 2 deletions pod-operation/src/components/gyro.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
2 changes: 1 addition & 1 deletion pod-operation/src/components/high_voltage_system.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
8 changes: 6 additions & 2 deletions pod-operation/src/components/inverter_board.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(feature = "inverter")]
use rppal::uart::{Parity, Uart};

use tracing::info;
use tracing::debug;

#[cfg(feature = "inverter")]
mod serial_constants {
Expand Down Expand Up @@ -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();
}

Expand All @@ -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
);
Expand Down
1 change: 1 addition & 0 deletions pod-operation/src/components/lidar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand Down
7 changes: 3 additions & 4 deletions pod-operation/src/components/lim_current.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {}
}

Expand Down
1 change: 1 addition & 0 deletions pod-operation/src/components/lim_temperature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 17 additions & 6 deletions pod-operation/src/components/pressure_transducer.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -55,10 +56,10 @@ fn init_ina(device_address: u8) -> INA219<I2c> {
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
}
Expand All @@ -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),
Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion pod-operation/src/components/signal_light.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
2 changes: 1 addition & 1 deletion pod-operation/src/components/wheel_encoder.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
76 changes: 40 additions & 36 deletions pod-operation/src/utils/mock.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit c82a42d

Please sign in to comment.