Enhance error handling and robustness in TWAI-based sensor detection and moisture measurement.
This commit is contained in:
@@ -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();
|
||||
|
||||
let res = (async {
|
||||
if twai.is_bus_off() {
|
||||
bail!("Bus offline after start");
|
||||
}
|
||||
|
||||
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<Detection> {
|
||||
@@ -387,6 +395,11 @@ 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();
|
||||
|
||||
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
|
||||
@@ -438,15 +451,18 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
.with_timeout(Duration::from_millis(3000))
|
||||
.await;
|
||||
|
||||
let config = twai.stop().into_blocking();
|
||||
self.twai_config.replace(config);
|
||||
|
||||
self.can_power.set_low();
|
||||
|
||||
let result = moistures.into();
|
||||
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();
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
async fn general_fault(&mut self, enable: bool) {
|
||||
|
||||
Reference in New Issue
Block a user