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();
Timer::after_millis(10).await; let res = (async {
if twai.is_bus_off() {
bail!("Bus offline after start");
}
let mut moistures = Moistures::default(); Timer::after_millis(10).await;
let _ = wait_for_can_measurements(&mut twai, &mut moistures)
.with_timeout(Duration::from_millis(1000)) let mut moistures = Moistures::default();
.await; 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(); 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,66 +395,74 @@ 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();
Timer::after_millis(1000).await; let res = (async {
info!("Sending info messages now"); if twai.is_bus_off() {
// Send a few test messages per potential sensor node bail!("Bus offline after start");
for plant in 0..PLANT_COUNT { }
for sensor in [Sensor::A, Sensor::B] {
let detect = if sensor == Sensor::A { Timer::after_millis(1000).await;
request.plant[plant].sensor_a info!("Sending info messages now");
} else { // Send a few test messages per potential sensor node
request.plant[plant].sensor_b for plant in 0..PLANT_COUNT {
}; for sensor in [Sensor::A, Sensor::B] {
if !detect { let detect = if sensor == Sensor::A {
continue; request.plant[plant].sensor_a
} } else {
let target = StandardId::new(plant_id( request.plant[plant].sensor_b
IDENTIFY_CMD_OFFSET, };
sensor.into(), if !detect {
(plant + 1) as u16, continue;
)) }
.context(">> Could not create address for sensor! (plant: {}) <<")?; let target = StandardId::new(plant_id(
let can_buffer = [0_u8; 0]; IDENTIFY_CMD_OFFSET,
info!( sensor.into(),
"Sending test message to plant {} sensor {sensor:?} with id {}", (plant + 1) as u16,
plant + 1, ))
target.as_raw() .context(">> Could not create address for sensor! (plant: {}) <<")?;
); let can_buffer = [0_u8; 0];
if let Some(frame) = EspTwaiFrame::new(target, &can_buffer) { info!(
// Try a few times; we intentionally ignore rx here and rely on stub logic "Sending test message to plant {} sensor {sensor:?} with id {}",
let resu = twai plant + 1,
.transmit_async(&frame) target.as_raw()
.with_timeout(Duration::from_millis(500)) );
.await; if let Some(frame) = EspTwaiFrame::new(target, &can_buffer) {
match resu { // Try a few times; we intentionally ignore rx here and rely on stub logic
Ok(_) => {} let resu = twai
Err(err) => { .transmit_async(&frame)
info!( .with_timeout(Duration::from_millis(500))
"Error sending test message to plant {} sensor {sensor:?}: {err:?}", .await;
plant + 1 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 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(3000)) .with_timeout(Duration::from_millis(3000))
.await; .await;
let result: Detection = moistures.into();
info!("Autodetection result: {result:?}");
Ok(result)
})
.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();
let result = moistures.into(); res
info!("Autodetection result: {result:?}");
Ok(result)
} }
async fn general_fault(&mut self, enable: bool) { async fn general_fault(&mut self, enable: bool) {