fix palntstate not correctly calculated
This commit is contained in:
		@@ -61,11 +61,12 @@ struct LightState {
 | 
			
		||||
    is_day: bool,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Copy, Clone, Debug, PartialEq, Default)]
 | 
			
		||||
#[derive(Clone, Copy, Debug, PartialEq, Default)]
 | 
			
		||||
struct PlantState {
 | 
			
		||||
    a: Option<u8>,
 | 
			
		||||
    b: Option<u8>,
 | 
			
		||||
    p: Option<u8>,
 | 
			
		||||
    consecutive_pump_count: u32,
 | 
			
		||||
    after_p: Option<u8>,
 | 
			
		||||
    do_water: bool,
 | 
			
		||||
    frozen: bool,
 | 
			
		||||
@@ -368,7 +369,6 @@ fn safe_main() -> anyhow::Result<()> {
 | 
			
		||||
        ..Default::default()
 | 
			
		||||
    }; PLANT_COUNT];
 | 
			
		||||
    let plant_to_pump = determine_next_plant(
 | 
			
		||||
        online_mode,
 | 
			
		||||
        &mut plantstate,
 | 
			
		||||
        europe_time,
 | 
			
		||||
        &tank_state,
 | 
			
		||||
@@ -386,9 +386,14 @@ fn safe_main() -> anyhow::Result<()> {
 | 
			
		||||
    let mut did_pump = false;
 | 
			
		||||
    match plant_to_pump {
 | 
			
		||||
        Some(plant) => {
 | 
			
		||||
            let mut state = plantstate[plant];
 | 
			
		||||
            let consecutive_pump_count = board.consecutive_pump_count(plant) + 1;
 | 
			
		||||
            board.store_consecutive_pump_count(plant, consecutive_pump_count);
 | 
			
		||||
            let state = &mut plantstate[plant];
 | 
			
		||||
            state.consecutive_pump_count = board.consecutive_pump_count(plant) + 1;
 | 
			
		||||
            board.store_consecutive_pump_count(plant, state.consecutive_pump_count);
 | 
			
		||||
            if state.consecutive_pump_count > config.max_consecutive_pump_count.into() {
 | 
			
		||||
                state.not_effective = true;
 | 
			
		||||
                board.fault(plant, true);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            let plant_config = config.plants[plant];
 | 
			
		||||
 | 
			
		||||
            if plant_config.sensor_p {
 | 
			
		||||
@@ -884,7 +889,6 @@ fn determine_state_target_moisture_for_plant(
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn determine_next_plant(
 | 
			
		||||
    online_mode: OnlineMode,
 | 
			
		||||
    plantstate: &mut [PlantState; PLANT_COUNT],
 | 
			
		||||
    cur: DateTime<Tz>,
 | 
			
		||||
    tank_state: &TankState,
 | 
			
		||||
@@ -954,12 +958,8 @@ fn determine_next_plant(
 | 
			
		||||
        {
 | 
			
		||||
            board.fault(plant, true);
 | 
			
		||||
        }
 | 
			
		||||
        if state.do_water {
 | 
			
		||||
            if board.consecutive_pump_count(plant) > config.max_consecutive_pump_count.into() {
 | 
			
		||||
                state.not_effective = true;
 | 
			
		||||
                board.fault(plant, true);
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
        if !state.dry {
 | 
			
		||||
            state.consecutive_pump_count = 0;
 | 
			
		||||
            board.store_consecutive_pump_count(plant, 0);
 | 
			
		||||
        }
 | 
			
		||||
        println!("Plant {} state is {:?}", plant, state);
 | 
			
		||||
@@ -1032,7 +1032,11 @@ fn update_plant_state(
 | 
			
		||||
                );
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        let _ = board.mqtt_publish(
 | 
			
		||||
            &config,
 | 
			
		||||
            format!("/plant{}/active", plant + 1).as_str(),
 | 
			
		||||
            state.active.to_string().as_bytes(),
 | 
			
		||||
        );
 | 
			
		||||
        let _ = board.mqtt_publish(
 | 
			
		||||
            &config,
 | 
			
		||||
            format!("/plant{}/Sensor A", plant + 1).as_str(),
 | 
			
		||||
@@ -1116,6 +1120,13 @@ fn update_plant_state(
 | 
			
		||||
            format!("/plant{}/Out of Work Hour", plant + 1).as_str(),
 | 
			
		||||
            state.out_of_work_hour.to_string().as_bytes(),
 | 
			
		||||
        );
 | 
			
		||||
        let _ = board.mqtt_publish(
 | 
			
		||||
            &config,
 | 
			
		||||
            format!("/plant{}/consecutive pump count", plant + 1).as_str(),
 | 
			
		||||
            state.consecutive_pump_count.to_string().as_bytes(),
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user