-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from UCI-HyperXite/feature/conditional-compil…
…ation Implement feature flags for conditional compilation
- Loading branch information
Showing
19 changed files
with
285 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rust-analyzer.cargo.features": [ | ||
// "rpi" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,54 @@ | ||
#[cfg(feature = "inverter")] | ||
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; | ||
use tracing::debug; | ||
|
||
#[cfg(feature = "inverter")] | ||
mod serial_constants { | ||
use super::Parity; | ||
pub const SERIAL_PATH: &str = "/dev/ttyACM0"; | ||
pub const BAUD_RATE: u32 = 9600; | ||
pub const PARITY: Parity = Parity::None; | ||
pub const DATA_BITS: u8 = 8; | ||
pub const STOP_BITS: u8 = 1; | ||
} | ||
|
||
pub struct InverterBoard { | ||
#[cfg(feature = "inverter")] | ||
uart: Uart, | ||
} | ||
|
||
impl InverterBoard { | ||
#[cfg(feature = "inverter")] | ||
pub fn new() -> Self { | ||
use serial_constants::*; | ||
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. | ||
#[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(); | ||
} | ||
|
||
#[cfg(not(feature = "inverter"))] | ||
pub fn new() -> Self { | ||
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) { | ||
debug!( | ||
"Mocking inverter sending message: {} {}", | ||
velocity, throttle | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,42 @@ | ||
use i2cdev::linux::LinuxI2CDevice; | ||
use lidar_lite_v3::LidarLiteV3; | ||
use tracing::info; | ||
|
||
#[cfg(feature = "lidar")] | ||
use {i2cdev::linux::LinuxI2CDevice, lidar_lite_v3::LidarLiteV3}; | ||
|
||
#[cfg(feature = "lidar")] | ||
const LIDAR_ADDRESS: u16 = 0x62; | ||
|
||
pub struct Lidar { | ||
#[cfg(feature = "lidar")] | ||
lidar_lite: LidarLiteV3<LinuxI2CDevice>, | ||
} | ||
|
||
impl Lidar { | ||
#[cfg(feature = "lidar")] | ||
pub fn new() -> Lidar { | ||
let i2cdev = LinuxI2CDevice::new("/dev/i2c-1", LIDAR_ADDRESS) | ||
.expect("Failed to initialize I2C device"); | ||
let lidar_lite = LidarLiteV3::new(i2cdev).expect("Failed to initialize LidarLiteV3"); | ||
|
||
info!("Initialized LidarLite"); | ||
Lidar { lidar_lite } | ||
} | ||
|
||
#[cfg(not(feature = "lidar"))] | ||
pub fn new() -> Lidar { | ||
info!("Mocking lidar device"); | ||
Lidar {} | ||
} | ||
|
||
/// Convert the distance from centimeters to meters | ||
#[cfg(feature = "lidar")] | ||
pub fn read_distance(&mut self) -> f32 { | ||
let distance = self.lidar_lite.read_distance(false).unwrap(); | ||
f32::from(distance) / 100.0 | ||
} | ||
|
||
#[cfg(not(feature = "lidar"))] | ||
pub fn read_distance(&mut self) -> f32 { | ||
100.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,61 @@ | ||
use ads1x1x::ic::{Ads1015, Resolution12Bit}; | ||
use ads1x1x::interface::I2cInterface; | ||
use ads1x1x::mode::OneShot; | ||
use ads1x1x::ChannelSelection::{SingleA0, SingleA1, SingleA2}; | ||
use ads1x1x::{Ads1x1x, DynamicOneShot, FullScaleRange, SlaveAddr}; | ||
use nb::block; | ||
use rppal::i2c::I2c; | ||
use ads1x1x::SlaveAddr; | ||
use tracing::info; | ||
#[cfg(feature = "ads1015")] | ||
use { | ||
ads1x1x::ic::{Ads1015, Resolution12Bit}, | ||
ads1x1x::interface::I2cInterface, | ||
ads1x1x::mode::OneShot, | ||
ads1x1x::ChannelSelection::{SingleA0, SingleA1, SingleA2}, | ||
ads1x1x::{Ads1x1x, DynamicOneShot, FullScaleRange}, | ||
nb::block, | ||
rppal::i2c::I2c, | ||
}; | ||
|
||
const QUIESCENT_VOLTAGE: f32 = 2.5; //Units: volts (v) | ||
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 { | ||
#[cfg(feature = "ads1015")] | ||
ads1015: Ads1x1x<I2cInterface<I2c>, Ads1015, Resolution12Bit, OneShot>, | ||
} | ||
|
||
impl LimCurrent { | ||
#[cfg(feature = "ads1015")] | ||
pub fn new(device_address: SlaveAddr) -> Self { | ||
let i2cdev = I2c::new().unwrap(); | ||
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 LimCurrent", device_address); | ||
LimCurrent {} | ||
} | ||
|
||
pub fn cleanup(self) { | ||
#[cfg(feature = "ads1015")] | ||
self.ads1015.destroy_ads1015(); | ||
} | ||
|
||
#[cfg(feature = "ads1015")] | ||
pub fn read_currents(&mut self) -> (f32, f32, f32) { | ||
[SingleA0, SingleA1, SingleA2] | ||
.map(|channel| block!(self.ads1015.read(channel)).unwrap() * 2) | ||
.map(voltage_to_current) | ||
.into() | ||
} | ||
|
||
#[cfg(not(feature = "ads1015"))] | ||
pub fn read_currents(&mut self) -> (f32, f32, f32) { | ||
[2500, 2500, 2500].map(voltage_to_current).into() | ||
} | ||
} |
Oops, something went wrong.