From eaa65637f157cf2b77d3391e8054b492a959f779 Mon Sep 17 00:00:00 2001 From: Empire Phoenix Date: Wed, 1 Apr 2026 01:24:04 +0200 Subject: [PATCH] Enhance error handling and robustness in TWAI-based sensor detection and moisture measurement. --- Software/MainBoard/rust/src/hal/v4_hal.rs | 132 ++++++++++++---------- 1 file changed, 74 insertions(+), 58 deletions(-) diff --git a/Software/MainBoard/rust/src/hal/v4_hal.rs b/Software/MainBoard/rust/src/hal/v4_hal.rs index 588fc12..5991e3d 100644 --- a/Software/MainBoard/rust/src/hal/v4_hal.rs +++ b/Software/MainBoard/rust/src/hal/v4_hal.rs @@ -367,18 +367,26 @@ impl<'a> BoardInteraction<'a> for V4<'a> { let config = self.twai_config.take().expect("twai config not set"); let mut twai = config.into_async().start(); - Timer::after_millis(10).await; + let res = (async { + if twai.is_bus_off() { + bail!("Bus offline after start"); + } - let mut moistures = Moistures::default(); - let _ = wait_for_can_measurements(&mut twai, &mut moistures) - .with_timeout(Duration::from_millis(1000)) - .await; + Timer::after_millis(10).await; + + let mut moistures = Moistures::default(); + let _ = wait_for_can_measurements(&mut twai, &mut moistures) + .with_timeout(Duration::from_millis(1000)) + .await; + Ok(moistures) + }) + .await; let config = twai.stop().into_blocking(); self.twai_config.replace(config); - self.can_power.set_low(); - Ok(moistures) + + res } async fn detect_sensors(&mut self, request: Detection) -> FatResult { @@ -387,66 +395,74 @@ impl<'a> BoardInteraction<'a> for V4<'a> { let config = self.twai_config.take().expect("twai config not set"); let mut twai = config.into_async().start(); - Timer::after_millis(1000).await; - info!("Sending info messages now"); - // Send a few test messages per potential sensor node - for plant in 0..PLANT_COUNT { - for sensor in [Sensor::A, Sensor::B] { - let detect = if sensor == Sensor::A { - request.plant[plant].sensor_a - } else { - request.plant[plant].sensor_b - }; - if !detect { - continue; - } - let target = StandardId::new(plant_id( - IDENTIFY_CMD_OFFSET, - sensor.into(), - (plant + 1) as u16, - )) - .context(">> Could not create address for sensor! (plant: {}) <<")?; - let can_buffer = [0_u8; 0]; - info!( - "Sending test message to plant {} sensor {sensor:?} with id {}", - plant + 1, - target.as_raw() - ); - if let Some(frame) = EspTwaiFrame::new(target, &can_buffer) { - // Try a few times; we intentionally ignore rx here and rely on stub logic - let resu = twai - .transmit_async(&frame) - .with_timeout(Duration::from_millis(500)) - .await; - match resu { - Ok(_) => {} - Err(err) => { - info!( - "Error sending test message to plant {} sensor {sensor:?}: {err:?}", - plant + 1 - ); - } + let res = (async { + if twai.is_bus_off() { + bail!("Bus offline after start"); + } + + Timer::after_millis(1000).await; + info!("Sending info messages now"); + // Send a few test messages per potential sensor node + for plant in 0..PLANT_COUNT { + for sensor in [Sensor::A, Sensor::B] { + let detect = if sensor == Sensor::A { + request.plant[plant].sensor_a + } else { + request.plant[plant].sensor_b + }; + if !detect { + continue; + } + let target = StandardId::new(plant_id( + IDENTIFY_CMD_OFFSET, + sensor.into(), + (plant + 1) as u16, + )) + .context(">> Could not create address for sensor! (plant: {}) <<")?; + let can_buffer = [0_u8; 0]; + info!( + "Sending test message to plant {} sensor {sensor:?} with id {}", + plant + 1, + target.as_raw() + ); + if let Some(frame) = EspTwaiFrame::new(target, &can_buffer) { + // Try a few times; we intentionally ignore rx here and rely on stub logic + let resu = twai + .transmit_async(&frame) + .with_timeout(Duration::from_millis(500)) + .await; + match resu { + Ok(_) => {} + Err(err) => { + info!( + "Error sending test message to plant {} sensor {sensor:?}: {err:?}", + plant + 1 + ); + } + } + } else { + info!("Error building CAN frame"); } - } else { - info!("Error building CAN frame"); } } - } - let mut moistures = Moistures::default(); - let _ = wait_for_can_measurements(&mut twai, &mut moistures) - .with_timeout(Duration::from_millis(3000)) - .await; + let mut moistures = Moistures::default(); + let _ = wait_for_can_measurements(&mut twai, &mut moistures) + .with_timeout(Duration::from_millis(3000)) + .await; + + let result: Detection = moistures.into(); + + info!("Autodetection result: {result:?}"); + Ok(result) + }) + .await; let config = twai.stop().into_blocking(); self.twai_config.replace(config); - self.can_power.set_low(); - let result = moistures.into(); - - info!("Autodetection result: {result:?}"); - Ok(result) + res } async fn general_fault(&mut self, enable: bool) {