fix palntstate not correctly calculated

This commit is contained in:
2024-03-09 15:21:52 +01:00
parent 869a581242
commit b4ad668620
2 changed files with 28 additions and 36 deletions

View File

@@ -240,12 +240,11 @@ impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> {
let mut percent = r2 / 190_f32 * 100_f32;
percent = percent.clamp(0.0, 100.0);
let quantizised = quantize_to_next_5_percent(percent as f64) as u16;
println!(
"Tank sensor raw {} percent {} quantized {}",
median, percent, quantizised
"Tank sensor raw {} percent {}",
median, percent
);
return Ok(quantizised);
return Ok(percent as u16);
}
fn set_low_voltage_in_cycle(&mut self) {
@@ -723,7 +722,7 @@ impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> {
true,
message,
);
Delay::new(10).delay_ms(10);
Delay::new(10).delay_ms(50);
match publish {
OkStd(message_id) => {
println!("Published mqtt topic {} with message {:#?} msgid is {:?}",full_topic, String::from_utf8_lossy(message), message_id);
@@ -1029,21 +1028,3 @@ impl CreatePlantHal<'_> for PlantHal {
Ok(rv)
}
}
fn quantize_to_next_5_percent(value: f64) -> i32 {
// Multiply by 100 to work with integer values
let multiplied_value = (value * 100.0).round() as i32;
// Calculate the remainder when divided by 5
let remainder = multiplied_value % 5;
// If the remainder is greater than or equal to half of 5, round up to the next 5%
let rounded_value = if remainder >= 2 {
multiplied_value + (5 - remainder)
} else {
multiplied_value - remainder
};
// Divide by 100 to get back to a float
rounded_value / 100
}