tasks: simulation: move sim data into a separate partition for flashing

This commit is contained in:
2025-11-05 11:56:10 +01:00
parent 4776227793
commit 41f69833e5
3 changed files with 138 additions and 9 deletions

View File

@@ -123,8 +123,21 @@ async fn main(spawner: Spawner) {
#[cfg(feature="simulation")]
{
spawner.must_spawn(renderbug_embassy::tasks::simulation::motion_simulation_task(garage.motion.dyn_sender()));
spawner.must_spawn(renderbug_embassy::tasks::simulation::location_simulation_task(garage.motion.dyn_sender()));
use core::{cell::RefCell, ops::DerefMut};
use alloc::rc::Rc;
use esp_storage::FlashStorage;
let storage = Rc::new(RefCell::new(FlashStorage::new()));
let mut buf = [8; 1024];
let partitions = esp_bootloader_esp_idf::partitions::read_partition_table(storage.borrow_mut().deref_mut(), &mut buf).unwrap();
let data_partition = partitions.find_partition(
esp_bootloader_esp_idf::partitions::PartitionType::Data(
esp_bootloader_esp_idf::partitions::DataPartitionSubType::Undefined
)).expect("Unable to read partition table").expect("Could not find data partition!");
let data_offset = data_partition.offset() as usize;
info!("Loading simulation data starting at {data_offset:#02x}");
spawner.must_spawn(renderbug_embassy::tasks::simulation::motion_simulation_task(Rc::clone(&storage), data_offset, garage.motion.dyn_sender()));
spawner.must_spawn(renderbug_embassy::tasks::simulation::location_simulation_task(storage, data_offset, garage.motion.dyn_sender()));
}
info!("Launching motion engine");

View File

@@ -1,17 +1,68 @@
use core::cell::RefCell;
use core::fmt::Formatter;
use alloc::rc::Rc;
use alloc::sync::Arc;
use embassy_embedded_hal::flash;
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use embassy_sync::channel::DynamicSender;
use embassy_sync::mutex::Mutex;
use embassy_time::{Duration, Timer};
use embedded_storage::ReadStorage;
use esp_storage::FlashStorage;
use nalgebra::{Vector2, Vector3};
use log::*;
use rmp::decode::{RmpRead, RmpReadErr};
use crate::events::{Measurement, SensorSource};
use crate::Breaker;
struct OffsetReader<S> {
storage: Rc<RefCell<S>>,
offset: usize
}
#[derive(Debug)]
struct RmpErr{}
impl RmpReadErr for RmpErr {
}
impl core::fmt::Display for RmpErr {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.write_str("RmpErr")
}
}
impl<S: ReadStorage> RmpRead for OffsetReader<S> {
type Error = RmpErr;
fn read_exact_buf(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
match self.storage.borrow_mut().read(self.offset as u32, buf) {
Ok(_) => {
self.offset += buf.len();
Ok(())
},
err => Err(RmpErr{})
}
}
}
#[embassy_executor::task]
pub async fn motion_simulation_task(events: DynamicSender<'static, Measurement>) {
let mut rd = rmp::decode::Bytes::new(include_bytes!("../../target/motion_test_data.msgpack"));
pub async fn motion_simulation_task(storage: Rc<RefCell<FlashStorage>>, offset: usize, events: DynamicSender<'static, Measurement>) {
let mut rd = OffsetReader { storage, offset };
let mut runtime_secs = Breaker::default();
let mut runtime = Duration::default();
events.send(Measurement::SensorOnline(SensorSource::IMU)).await;
loop {
let this_type = rmp::decode::read_ext_meta(&mut rd).unwrap();
if this_type.typeid != 1 {
rd.offset += this_type.size as usize;
} else {
break;
}
}
info!("Found motion data at {:#02x}", rd.offset);
while let Ok(size) = rmp::decode::read_array_len(&mut rd) {
assert_eq!(size, 7, "Expected 7 fields, but only found {size}");
let delay = embassy_time::Duration::from_millis((rmp::decode::read_f64(&mut rd).unwrap() * 1000.0) as u64);
@@ -38,11 +89,21 @@ pub async fn motion_simulation_task(events: DynamicSender<'static, Measurement>)
}
#[embassy_executor::task]
pub async fn location_simulation_task(events: DynamicSender<'static, Measurement>) {
let mut rd = rmp::decode::Bytes::new(include_bytes!("../../target/gps_test_data.msgpack"));
pub async fn location_simulation_task(storage: Rc<RefCell<FlashStorage>>, offset: usize, events: DynamicSender<'static, Measurement>) {
let mut rd = OffsetReader { storage, offset };
let mut runtime_secs = Breaker::default();
let mut runtime = Duration::default();
events.send(Measurement::SensorOnline(SensorSource::GPS)).await;
loop {
let this_type = rmp::decode::read_ext_meta(&mut rd).unwrap();
info!("Found type={this_type:?}");
if this_type.typeid != 2 {
rd.offset += this_type.size as usize;
} else {
break;
}
}
info!("Found GPS data at {:#02x}", rd.offset);
while let Ok(size) = rmp::decode::read_array_len(&mut rd) {
assert_eq!(size, 3, "Expected 3 fields, but only found {size}");
let delay = embassy_time::Duration::from_millis((rmp::decode::read_f64(&mut rd).unwrap() * 1000.0) as u64);