update: refactor and enhance CAN sensor initialization, reorganize GPIO assignments, improve error detection and logging, and streamline TWAI handling

This commit is contained in:
2026-01-31 00:06:42 +01:00
parent 355388aa62
commit ce10d084f8
8 changed files with 70 additions and 16 deletions

View File

@@ -140,4 +140,8 @@ impl<'a> BoardInteraction<'a> for Initial<'a> {
async fn get_mptt_current(&mut self) -> Result<Current, FatError> {
bail!("Please configure board revision")
}
async fn can_power(&mut self, state: bool) -> FatResult<()> {
bail!("Please configure board revision")
}
}

View File

@@ -162,6 +162,7 @@ pub trait BoardInteraction<'a> {
fn set_config(&mut self, config: PlantControllerConfig);
async fn get_mptt_voltage(&mut self) -> FatResult<Voltage>;
async fn get_mptt_current(&mut self) -> FatResult<Current>;
async fn can_power(&mut self, state: bool) -> FatResult<()>;
// Return JSON string with autodetected sensors per plant. Default: not supported.
async fn detect_sensors(&mut self) -> FatResult<DetectionResult> {

View File

@@ -387,22 +387,13 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
let mut twai = create_twai();
loop {
let rec = twai.receive();
match rec {
Ok(_) => {}
Err(err) => {
info!("Error receiving CAN message: {err:?}");
break;
}
}
}
Timer::after_millis(10).await;
let mut moistures = Moistures::default();
let _ = wait_for_can_measurements(&mut twai, &mut moistures)
.with_timeout(Duration::from_millis(2000))
.with_timeout(Duration::from_millis(5000))
.await;
teardown_twai(twai);
self.can_power.set_low();
@@ -413,7 +404,7 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
self.can_power.set_high();
let mut twai = create_twai();
// Give CAN some time to stabilize
Timer::after_millis(10).await;
Timer::after_millis(3000).await;
info!("Sending info messages now");
// Send a few test messages per potential sensor node
@@ -522,6 +513,17 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
async fn get_mptt_current(&mut self) -> FatResult<Current> {
self.charger.get_mppt_current()
}
async fn can_power(&mut self, state: bool) -> FatResult<()> {
if state && self.can_power.is_set_low(){
self.can_power.set_high();
create_twai();
} else {
teardown_twai(create_twai());
self.can_power.set_low();
}
Ok(())
}
}