cruisecontrol/controller/src/sensor_manager.rs
Andy Killorin 971c57fa13
graceful decay on gyro failure
also switched gyro iic pins because the other ones were
shorted under the pico :|
2025-03-05 21:10:17 -05:00

117 lines
3.5 KiB
Rust

use core::sync::atomic::{AtomicU8, Ordering};
use core::task::Poll;
use core::{cell::RefCell, mem::transmute};
use common::SensorData;
use embassy_executor::Executor;
use embassy_rp::peripherals::{I2C0, I2C1};
use embassy_time::{Delay, Duration, Instant, Timer};
use embassy_rp::gpio::Output;
use embassy_rp::i2c::{Async, I2c};
use embedded_hal_bus::i2c::RefCellDevice;
use log::{error, info};
use mpu6050::Mpu6050;
use nalgebra::Vector3;
use crate::CHANNEL;
pub struct SensorHardware {
pub bus_tof: I2c<'static, I2C1, Async>,
pub bus_gyro: I2c<'static, I2C0, Async>,
pub tof_l_enable: Output<'static>,
pub tof_r_enable: Output<'static>,
}
pub fn sensor_manager(hardware: SensorHardware) -> ! {
let mut executor = Executor::new();
let executor: &'static mut Executor = unsafe{transmute(&mut executor)};
executor.run(move |spawner| {
spawner.spawn(init_sensors(hardware)).unwrap();
});
}
static POLL_STATE: AtomicU8 = AtomicU8::new(PollState::None as u8);
#[repr(u8)]
enum PollState {
None,
Tof,
Gyro,
}
#[embassy_executor::task]
async fn init_sensors(mut hardware: SensorHardware) {
let bus = RefCell::new(hardware.bus_tof);
Timer::after_millis(1).await;
let mut tof_s = vl53l0x::VL53L0x::new(RefCellDevice::new(&bus)).ok();
if let Some(ref mut tof_s) = tof_s {
tof_s.set_address(0x32).unwrap();
}
hardware.tof_l_enable.set_high();
Timer::after_micros(1200).await; // DS11555 3.2
let mut tof_l = vl53l0x::VL53L0x::new(RefCellDevice::new(&bus)).ok();
if let Some(ref mut tof_l) = tof_l {
tof_l.set_address(0x33).unwrap();
}
hardware.tof_r_enable.set_high();
Timer::after_micros(1200).await; // DS11555 3.2
let mut tof_r = vl53l0x::VL53L0x::new(RefCellDevice::new(&bus)).ok();
if let Some(ref mut tof_r) = tof_r {
tof_r.set_address(0x34).unwrap();
}
info!("initting gyro");
let mut gyro = Mpu6050::new_with_addr(hardware.bus_gyro,0x68);
let mut gyro_alive = true;
match gyro.init(&mut Delay) {
Ok(()) => {
gyro.set_gyro_range(mpu6050::device::GyroRange::D2000).unwrap_or_else(|_| gyro_alive = false);
gyro.set_accel_range(mpu6050::device::AccelRange::G16).unwrap_or_else(|_| gyro_alive = false);
info!("init gyro");
}
e => {
error!("gyro init err: {e:?}");
}
}
let mut data = SensorData {
tof_l: None,
tof_r: None,
tof_s: None,
gyro: None,
accel: None,
};
loop {
POLL_STATE.store(PollState::Tof as u8, Ordering::SeqCst);
for (tof, store) in
[(&mut tof_s, &mut data.tof_s), (&mut tof_l, &mut data.tof_l), (&mut tof_r, &mut data.tof_r)] {
if let Some(ref mut tof) = tof {
if let Ok(dist) = tof.read_range_single_millimeters_blocking() {
*store = Some(dist);
} else {
*store = None;
}
Timer::after_millis(3).await;
}
}
if gyro_alive {
POLL_STATE.store(PollState::Gyro as u8, Ordering::SeqCst);
if let Ok(gyro) = gyro.get_gyro() {
info!("rotation: {gyro}");
data.gyro = Some(gyro);
}
if let Ok(accel) = gyro.get_acc() {
data.accel = Some(accel);
}
}
POLL_STATE.store(PollState::None as u8, Ordering::SeqCst);
info!("sensors: {data:?}");
CHANNEL.send((data.clone(), Instant::now())).await;
}
}