From 99cf17f7e451023a91c0e5f408e7b78aec86535b Mon Sep 17 00:00:00 2001 From: linuswck Date: Tue, 19 Mar 2024 13:10:23 +0800 Subject: [PATCH] temp_mon: Add support for constant current mode --- src/thermostat/temp_mon.rs | 53 ++++++++++++++++++++++++++++-------- src/thermostat/thermostat.rs | 2 +- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/thermostat/temp_mon.rs b/src/thermostat/temp_mon.rs index 4ca7f06..99b4685 100644 --- a/src/thermostat/temp_mon.rs +++ b/src/thermostat/temp_mon.rs @@ -11,6 +11,7 @@ pub enum TempStatusEnum { OverTemp, Unstable, Stable, + ConstantCurrentMode, } #[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)] @@ -56,7 +57,8 @@ impl Default for TempMon { #[derive(Default, PartialEq, Debug)] enum State { #[default] - PidOff, + PwrOff, + ConstantCurrentMode, PidStartUp, PidStable, OverTempAlarm, @@ -95,16 +97,38 @@ impl TempMon { self.state = State::default(); } - pub fn update_status(&mut self, pid_engaged: bool, temp: ThermodynamicTemperature) { + pub fn update_status(&mut self, pid_engaged: bool, pwr_on: bool, temp: ThermodynamicTemperature) { match self.state { - State::PidOff => { + State::PwrOff => { self.is_set_point_changed = false; self.status.status = TempStatusEnum::Off; self.count = 0; // State Transition - if pid_engaged { + if pwr_on { + if pid_engaged { + self.state = State::PidStartUp; + } else { + self.state = State::ConstantCurrentMode + } + } + } + + State::ConstantCurrentMode => { + let is_over_temp = temp > self.upper_limit || temp < self.lower_limit; + self.status.status = TempStatusEnum::ConstantCurrentMode; + + if is_over_temp { + self.state = State::OverTempAlarm; + self.status.status = TempStatusEnum::OverTemp; + } + else if !pwr_on { + self.state = State::PwrOff; + self.status.status = TempStatusEnum::Off; + } else if pid_engaged { self.state = State::PidStartUp; + self.status.status = TempStatusEnum::Unstable; + self.is_set_point_changed = false; } } State::PidStartUp | State::PidStable => { @@ -134,13 +158,20 @@ impl TempMon { } // State Transition - if self.status.status == TempStatusEnum::OverTemp { - self.state = State::OverTempAlarm; - } else if self.is_set_point_changed { - self.is_set_point_changed = false; - self.state = State::PidStartUp; - } else if self.status.status == TempStatusEnum::Stable { - self.state = State::PidStable; + if !pwr_on { + self.state = State::PwrOff; + } + else { + if self.status.status == TempStatusEnum::OverTemp { + self.state = State::OverTempAlarm; + } else if self.is_set_point_changed { + self.is_set_point_changed = false; + self.state = State::PidStartUp; + } else if self.status.status == TempStatusEnum::Stable { + self.state = State::PidStable; + } else if !pid_engaged { + self.state = State::ConstantCurrentMode + } } } State::OverTempAlarm => { diff --git a/src/thermostat/thermostat.rs b/src/thermostat/thermostat.rs index cfe6a90..9b1b394 100644 --- a/src/thermostat/thermostat.rs +++ b/src/thermostat/thermostat.rs @@ -186,7 +186,7 @@ impl Thermostat{ state.update(data); let pid_engaged = state.get_pid_engaged(); let temp = self.get_temperature(); - self.temp_mon.update_status(pid_engaged, temp); + self.temp_mon.update_status(pid_engaged, self.max1968.is_powered_on(), temp); debug!("state.get_pid_engaged(): {:?}", pid_engaged); debug!("Temperature: {:?} degree", temp.get::()); data_rdy = true;