Enhance error handling and robustness in TWAI-based sensor detection and moisture measurement.

This commit is contained in:
2026-04-01 01:24:04 +02:00
parent f1dadd7e6e
commit eaa65637f1

View File

@@ -367,18 +367,26 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
let config = self.twai_config.take().expect("twai config not set"); let config = self.twai_config.take().expect("twai config not set");
let mut twai = config.into_async().start(); let mut twai = config.into_async().start();
let res = (async {
if twai.is_bus_off() {
bail!("Bus offline after start");
}
Timer::after_millis(10).await; Timer::after_millis(10).await;
let mut moistures = Moistures::default(); let mut moistures = Moistures::default();
let _ = wait_for_can_measurements(&mut twai, &mut moistures) let _ = wait_for_can_measurements(&mut twai, &mut moistures)
.with_timeout(Duration::from_millis(1000)) .with_timeout(Duration::from_millis(1000))
.await; .await;
Ok(moistures)
})
.await;
let config = twai.stop().into_blocking(); let config = twai.stop().into_blocking();
self.twai_config.replace(config); self.twai_config.replace(config);
self.can_power.set_low(); self.can_power.set_low();
Ok(moistures)
res
} }
async fn detect_sensors(&mut self, request: Detection) -> FatResult<Detection> { async fn detect_sensors(&mut self, request: Detection) -> FatResult<Detection> {
@@ -387,6 +395,11 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
let config = self.twai_config.take().expect("twai config not set"); let config = self.twai_config.take().expect("twai config not set");
let mut twai = config.into_async().start(); let mut twai = config.into_async().start();
let res = (async {
if twai.is_bus_off() {
bail!("Bus offline after start");
}
Timer::after_millis(1000).await; Timer::after_millis(1000).await;
info!("Sending info messages now"); info!("Sending info messages now");
// Send a few test messages per potential sensor node // Send a few test messages per potential sensor node
@@ -438,15 +451,18 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
.with_timeout(Duration::from_millis(3000)) .with_timeout(Duration::from_millis(3000))
.await; .await;
let config = twai.stop().into_blocking(); let result: Detection = moistures.into();
self.twai_config.replace(config);
self.can_power.set_low();
let result = moistures.into();
info!("Autodetection result: {result:?}"); info!("Autodetection result: {result:?}");
Ok(result) Ok(result)
})
.await;
let config = twai.stop().into_blocking();
self.twai_config.replace(config);
self.can_power.set_low();
res
} }
async fn general_fault(&mut self, enable: bool) { async fn general_fault(&mut self, enable: bool) {