triple tof init

This commit is contained in:
Andy Killorin 2025-03-05 12:36:24 -05:00
commit 782fd891a1
Signed by: ank
GPG key ID: 23F9463ECB67FE8C
2 changed files with 384 additions and 36 deletions

View file

@ -88,7 +88,8 @@ async fn main(spawner: Spawner) {
let driver = Driver::new(p.USB, Irqs);
spawner.spawn(logger_task(driver)).unwrap();
let limit_switch = Input::new(p.PIN_22, Pull::Up);
let limit_switch = Input::new(p.PIN_16, Pull::Up);
let auto_status = Output::new(p.PIN_2, Level::High);
let mut d: pwm::Config = Default::default();
d.divider = 40.into();
@ -112,12 +113,14 @@ async fn main(spawner: Spawner) {
let scl = p.PIN_27;
let config = embassy_rp::i2c::Config::default();
let bus1 = embassy_rp::i2c::I2c::new_async(p.I2C1, scl, sda, Irqs, config);
let tof2enable = Output::new(p.PIN_16, Level::Low);
let tof_r_enable = Output::new(p.PIN_28, Level::Low);
let tof_l_enable = Output::new(p.PIN_22, Level::Low);
let hardware = SensorHardware {
bus_tof: unsafe {transmute(bus)},
bus_gyro: unsafe {transmute(bus1)},
tof2enable,
bus_tof: unsafe {transmute(bus1)},
bus_gyro: unsafe {transmute(bus)},
tof_l_enable,
tof_r_enable,
};
spawn_core1(

View file

@ -18,10 +18,11 @@ use crate::CHANNEL;
pub struct SensorHardware {
pub bus_tof: I2c<'static, I2C1, Async>,
pub bus_gyro: I2c<'static, I2C0, Async>,
pub tof2enable: Output<'static>,
pub tof_l_enable: Output<'static>,
pub tof_r_enable: Output<'static>,
}
pub fn sensor_manager(mut hardware: SensorHardware) -> ! {
pub fn sensor_manager(hardware: SensorHardware) -> ! {
let mut executor = Executor::new();
let executor: &'static mut Executor = unsafe{transmute(&mut executor)};
executor.run(move |spawner| {
@ -33,8 +34,9 @@ static POLL_STATE: AtomicU8 = AtomicU8::new(PollState::None as u8);
#[repr(u8)]
enum PollState {
None,
Tof1,
Tof2,
TofL,
TofR,
TofS,
Gyro,
}
@ -43,21 +45,29 @@ async fn init_sensors(mut hardware: SensorHardware) {
let bus = RefCell::new(hardware.bus_tof);
Timer::after_millis(1).await;
let mut tof = vl53l0x::VL53L0x::new(RefCellDevice::new(&bus)).unwrap();
tof.set_address(0x32).unwrap();
hardware.tof2enable.set_high();
Timer::after_micros(1200).await; // DS11555 3.2
let mut tof2 = vl53l0x::VL53L0x::new(RefCellDevice::new(&bus)).unwrap();
let mut tof_s = vl53l0x::VL53L0x::new(RefCellDevice::new(&bus)).unwrap();
tof_s.set_address(0x32).unwrap();
let mut gyro = Mpu6050::new_with_addr(hardware.bus_gyro,0x68);
gyro.init(&mut Delay).unwrap();
gyro.set_gyro_range(mpu6050::device::GyroRange::D2000).unwrap();
gyro.set_accel_range(mpu6050::device::AccelRange::G16).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)).unwrap();
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)).unwrap();
tof_r.set_address(0x34).unwrap();
//let mut gyro = Mpu6050::new_with_addr(hardware.bus_gyro,0x68);
//gyro.init(&mut Delay).unwrap();
//gyro.set_gyro_range(mpu6050::device::GyroRange::D2000).unwrap();
//gyro.set_accel_range(mpu6050::device::AccelRange::G16).unwrap();
let mut data = SensorData {
tof1: None,
tof2: None,
tof_l: None,
tof_r: None,
tof_s: None,
gyro: None,
accel: None,
};
@ -65,35 +75,43 @@ async fn init_sensors(mut hardware: SensorHardware) {
loop {
let mut updated = false;
POLL_STATE.store(PollState::Tof1 as u8, Ordering::SeqCst);
if let Ok(dist) = tof.read_range_single_millimeters_blocking() {
info!("dist1: {dist}mm");
data.tof1 = Some(dist);
POLL_STATE.store(PollState::TofS as u8, Ordering::SeqCst);
if let Ok(dist) = tof_s.read_range_single_millimeters_blocking() {
data.tof_s = Some(dist);
updated = true;
}
Timer::after_millis(3).await;
POLL_STATE.store(PollState::Tof2 as u8, Ordering::SeqCst);
if let Ok(dist) = tof2.read_range_single_millimeters_blocking() {
info!("dist2: {dist}mm");
data.tof2 = Some(dist);
POLL_STATE.store(PollState::TofL as u8, Ordering::SeqCst);
if let Ok(dist) = tof_l.read_range_single_millimeters_blocking() {
data.tof_l = Some(dist);
updated = true;
}
Timer::after_millis(3).await;
POLL_STATE.store(PollState::Gyro as u8, Ordering::SeqCst);
if let Ok(gyro) = gyro.get_gyro() {
info!("rotation: {gyro}");
data.gyro = Some(gyro);
updated = true;
}
if let Ok(accel) = gyro.get_acc() {
data.accel = Some(accel);
POLL_STATE.store(PollState::TofR as u8, Ordering::SeqCst);
if let Ok(dist) = tof_r.read_range_single_millimeters_blocking() {
data.tof_r = Some(dist);
updated = true;
}
Timer::after_millis(3).await;
//POLL_STATE.store(PollState::Gyro as u8, Ordering::SeqCst);
//if let Ok(gyro) = gyro.get_gyro() {
// info!("rotation: {gyro}");
// data.gyro = Some(gyro);
// updated = true;
//}
//if let Ok(accel) = gyro.get_acc() {
// data.accel = Some(accel);
// updated = true;
//}
if updated {
updated_ts = Instant::now();
}
POLL_STATE.store(PollState::None as u8, Ordering::SeqCst);
CHANNEL.send((data.clone(), updated_ts.clone())).await;
info!("sensors: {data:?}");
//CHANNEL.send((data.clone(), updated_ts.clone())).await;
}
}