fix tank.rs errors after intial implementation

This commit is contained in:
ju6ge 2025-03-14 23:39:46 +01:00
parent e878a774ff
commit 48b0777d94
Signed by: judge
GPG Key ID: 6512C30DD8E017B5
2 changed files with 23 additions and 20 deletions

View File

@ -472,7 +472,7 @@ impl PlantCtrlBoard<'_> {
} }
/// return median tank sensor value in milli volt /// return median tank sensor value in milli volt
pub fn tank_sensor_voltage(&mut self) -> Result<u16> { pub fn tank_sensor_voltage(&mut self) -> Result<f32> {
let delay = Delay::new_default(); let delay = Delay::new_default();
self.tank_power.set_high()?; self.tank_power.set_high()?;
//let stabilize //let stabilize

View File

@ -1,4 +1,4 @@
use crate::config::{self, PlantControllerConfig, TankConfig}; use crate::{config::{self, PlantControllerConfig, TankConfig}, plant_hal::PlantCtrlBoard};
const OPEN_TANK_VOLTAGE: f32 = 3.0; const OPEN_TANK_VOLTAGE: f32 = 3.0;
@ -24,14 +24,15 @@ pub enum TankError {
SensorDisabled, SensorDisabled,
SensorMissing(f32), SensorMissing(f32),
SensorValueError { value: f32, min: f32, max: f32 }, SensorValueError { value: f32, min: f32, max: f32 },
BoardError(anyhow::Error)
} }
pub enum TankState { pub enum TankState {
TankSensorPresent(u16), TankSensorPresent(f32),
TankSensorDisabled, TankSensorDisabled,
} }
fn raw_volatge_to_divider_percent(raw_value_mv: u16) -> Result<f32, TankError> { fn raw_volatge_to_divider_percent(raw_value_mv: f32) -> Result<f32, TankError> {
if raw_value_mv > OPEN_TANK_VOLTAGE { if raw_value_mv > OPEN_TANK_VOLTAGE {
return Err(TankError::SensorMissing(raw_value_mv)); return Err(TankError::SensorMissing(raw_value_mv));
} }
@ -51,19 +52,19 @@ fn raw_volatge_to_divider_percent(raw_value_mv: u16) -> Result<f32, TankError> {
} }
fn raw_voltage_to_tank_fill_percent( fn raw_voltage_to_tank_fill_percent(
raw_value_mv: u16, raw_value_mv: f32,
config: &TankConfig, config: &TankConfig,
) -> Result<f32, TankError> { ) -> Result<f32, TankError> {
let divider_percent = raw_volatge_to_divider_percent(raw_value_mv)?; let divider_percent = raw_volatge_to_divider_percent(raw_value_mv)?;
if s < config.tank_empty_percent || s > config.tank_full_percent { if divider_percent < config.tank_empty_percent.into() || divider_percent > config.tank_full_percent.into() {
return Err(TankError::SensorValueError { return Err(TankError::SensorValueError {
value: divider_percent, value: divider_percent,
min: config.tank_empty_percent, min: config.tank_empty_percent.into(),
max: config.tank_full_percent, max: config.tank_full_percent.into(),
}); });
} }
Ok((divider_percent - config.tank_empty_percent) * 100 Ok((divider_percent - f32::from(config.tank_empty_percent)) * 100.
/ (config.tank_full_percent - config.tank_empty_percent)) / f32::from(config.tank_full_percent - config.tank_empty_percent))
} }
@ -72,7 +73,7 @@ impl TankState {
match self { match self {
TankState::TankSensorDisabled => Err(TankError::SensorDisabled), TankState::TankSensorDisabled => Err(TankError::SensorDisabled),
TankState::TankSensorPresent(raw_value_mv) => { TankState::TankSensorPresent(raw_value_mv) => {
let tank_fill_percent = raw_voltage_to_tank_fill_percent(raw_value_mv, config)?; let tank_fill_percent = raw_voltage_to_tank_fill_percent(*raw_value_mv, config)?;
//TODO(judge) move logging to more sensible place //TODO(judge) move logging to more sensible place
//println!( //println!(
//"Tank sensor returned mv {} as {}% leaving {} ml useable", //"Tank sensor returned mv {} as {}% leaving {} ml useable",
@ -86,8 +87,8 @@ impl TankState {
match self { match self {
TankState::TankSensorDisabled => Err(TankError::SensorDisabled), TankState::TankSensorDisabled => Err(TankError::SensorDisabled),
TankState::TankSensorPresent(raw_value_mv) => { TankState::TankSensorPresent(raw_value_mv) => {
let tank_fill_percent = raw_voltage_to_tank_fill_percent(raw_value_mv, config)?; let tank_fill_percent = raw_voltage_to_tank_fill_percent(*raw_value_mv, config)?;
if tank_fill_percent > config.tank_empty_percent { if tank_fill_percent > config.tank_empty_percent.into() {
//TODO(judge) move logging to more sensible place //TODO(judge) move logging to more sensible place
//println!( //println!(
//"Enough water, current percent is {}, minimum empty level is {}", //"Enough water, current percent is {}, minimum empty level is {}",
@ -105,8 +106,8 @@ impl TankState {
match self { match self {
TankState::TankSensorDisabled => Err(TankError::SensorDisabled), TankState::TankSensorDisabled => Err(TankError::SensorDisabled),
TankState::TankSensorPresent(raw_value_mv) => { TankState::TankSensorPresent(raw_value_mv) => {
let tank_fill_percent = raw_voltage_to_tank_fill_percent(raw_value_mv, config)?; let tank_fill_percent = raw_voltage_to_tank_fill_percent(*raw_value_mv, config)?;
if tank_fill_percent < config.tank_warn_percent { if tank_fill_percent < config.tank_warn_percent.into() {
//TODO(judge) move logging to more sensible place //TODO(judge) move logging to more sensible place
//println!( //println!(
//"Low water, current percent is {}, minimum warn level is {}", //"Low water, current percent is {}, minimum warn level is {}",
@ -121,9 +122,11 @@ impl TankState {
}, },
} }
} }
} }
#[derive(Serialize)] #[derive(serde::Serialize)]
pub struct TankStateMQTT { pub struct TankStateMQTT {
enough_water: bool, enough_water: bool,
warn_level: bool, warn_level: bool,
@ -136,11 +139,11 @@ pub struct TankStateMQTT {
pub fn determine_tank_state( pub fn determine_tank_state(
board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>, board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>,
config: &PlantControllerConfig, config: &PlantControllerConfig,
) -> TankState { ) -> Result<TankState, TankError> {
if config.tank.tank_sensor_enabled { if config.tank.tank_sensor_enabled {
let raw_sensor_value_mv = board.tank_sensor_voltage(); let raw_sensor_value_mv = board.tank_sensor_voltage().map_err(|err| TankError::BoardError(err))?;
TankState::TankSensorPresent(raw_sensor_value_mv) Ok(TankState::TankSensorPresent(raw_sensor_value_mv))
} else { } else {
TankState::TankSensorDisabled Ok(TankState::TankSensorDisabled)
} }
} }