fix tank.rs errors after intial implementation
This commit is contained in:
parent
e878a774ff
commit
48b0777d94
@ -472,7 +472,7 @@ impl PlantCtrlBoard<'_> {
|
||||
}
|
||||
|
||||
/// 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();
|
||||
self.tank_power.set_high()?;
|
||||
//let stabilize
|
||||
|
@ -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;
|
||||
|
||||
@ -24,14 +24,15 @@ pub enum TankError {
|
||||
SensorDisabled,
|
||||
SensorMissing(f32),
|
||||
SensorValueError { value: f32, min: f32, max: f32 },
|
||||
BoardError(anyhow::Error)
|
||||
}
|
||||
|
||||
pub enum TankState {
|
||||
TankSensorPresent(u16),
|
||||
TankSensorPresent(f32),
|
||||
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 {
|
||||
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(
|
||||
raw_value_mv: u16,
|
||||
raw_value_mv: f32,
|
||||
config: &TankConfig,
|
||||
) -> Result<f32, TankError> {
|
||||
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 {
|
||||
value: divider_percent,
|
||||
min: config.tank_empty_percent,
|
||||
max: config.tank_full_percent,
|
||||
min: config.tank_empty_percent.into(),
|
||||
max: config.tank_full_percent.into(),
|
||||
});
|
||||
}
|
||||
Ok((divider_percent - config.tank_empty_percent) * 100
|
||||
/ (config.tank_full_percent - config.tank_empty_percent))
|
||||
Ok((divider_percent - f32::from(config.tank_empty_percent)) * 100.
|
||||
/ f32::from(config.tank_full_percent - config.tank_empty_percent))
|
||||
}
|
||||
|
||||
|
||||
@ -72,7 +73,7 @@ impl TankState {
|
||||
match self {
|
||||
TankState::TankSensorDisabled => Err(TankError::SensorDisabled),
|
||||
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
|
||||
//println!(
|
||||
//"Tank sensor returned mv {} as {}% leaving {} ml useable",
|
||||
@ -86,8 +87,8 @@ impl TankState {
|
||||
match self {
|
||||
TankState::TankSensorDisabled => Err(TankError::SensorDisabled),
|
||||
TankState::TankSensorPresent(raw_value_mv) => {
|
||||
let tank_fill_percent = raw_voltage_to_tank_fill_percent(raw_value_mv, config)?;
|
||||
if tank_fill_percent > config.tank_empty_percent {
|
||||
let tank_fill_percent = raw_voltage_to_tank_fill_percent(*raw_value_mv, config)?;
|
||||
if tank_fill_percent > config.tank_empty_percent.into() {
|
||||
//TODO(judge) move logging to more sensible place
|
||||
//println!(
|
||||
//"Enough water, current percent is {}, minimum empty level is {}",
|
||||
@ -105,8 +106,8 @@ impl TankState {
|
||||
match self {
|
||||
TankState::TankSensorDisabled => Err(TankError::SensorDisabled),
|
||||
TankState::TankSensorPresent(raw_value_mv) => {
|
||||
let tank_fill_percent = raw_voltage_to_tank_fill_percent(raw_value_mv, config)?;
|
||||
if tank_fill_percent < config.tank_warn_percent {
|
||||
let tank_fill_percent = raw_voltage_to_tank_fill_percent(*raw_value_mv, config)?;
|
||||
if tank_fill_percent < config.tank_warn_percent.into() {
|
||||
//TODO(judge) move logging to more sensible place
|
||||
//println!(
|
||||
//"Low water, current percent is {}, minimum warn level is {}",
|
||||
@ -121,9 +122,11 @@ impl TankState {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[derive(serde::Serialize)]
|
||||
pub struct TankStateMQTT {
|
||||
enough_water: bool,
|
||||
warn_level: bool,
|
||||
@ -136,11 +139,11 @@ pub struct TankStateMQTT {
|
||||
pub fn determine_tank_state(
|
||||
board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>,
|
||||
config: &PlantControllerConfig,
|
||||
) -> TankState {
|
||||
) -> Result<TankState, TankError> {
|
||||
if config.tank.tank_sensor_enabled {
|
||||
let raw_sensor_value_mv = board.tank_sensor_voltage();
|
||||
TankState::TankSensorPresent(raw_sensor_value_mv)
|
||||
let raw_sensor_value_mv = board.tank_sensor_voltage().map_err(|err| TankError::BoardError(err))?;
|
||||
Ok(TankState::TankSensorPresent(raw_sensor_value_mv))
|
||||
} else {
|
||||
TankState::TankSensorDisabled
|
||||
Ok(TankState::TankSensorDisabled)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user