Compare commits

...

3 Commits

Author SHA1 Message Date
linuswck a76b8de994 thermostat: Save the user config-ed value directly
- the firmware recorded the actual value set with quantization errors or
    other errors
- this caused confusion for the user when they read back the settings
2024-07-22 17:37:25 +08:00
linuswck 3410a271fd thermostat: rst pid_state at pwr_up 2024-07-22 10:54:01 +08:00
linuswck 8f38acd0f2 thermostat: do not rst tec_i_out to 0 at pwr down 2024-07-22 10:52:32 +08:00
1 changed files with 15 additions and 11 deletions

View File

@ -213,13 +213,14 @@ impl Thermostat {
} }
pub fn power_up(&mut self) { pub fn power_up(&mut self) {
self.max1968.power_up(); if !self.max1968.is_powered_on() {
self.max1968.power_up();
self.pid_ctrl_ch0.reset_pid_state();
}
} }
pub fn power_down(&mut self) { pub fn power_down(&mut self) {
self.max1968.power_down(); self.max1968.power_down();
self.pid_ctrl_ch0.reset_pid_state();
self.set_i(ElectricCurrent::new::<ampere>(0.0));
} }
fn set_center_pt(&mut self, value: ElectricPotential) { fn set_center_pt(&mut self, value: ElectricPotential) {
@ -232,36 +233,39 @@ impl Thermostat {
pub fn set_i(&mut self, i_tec: ElectricCurrent) -> ElectricCurrent { pub fn set_i(&mut self, i_tec: ElectricCurrent) -> ElectricCurrent {
let voltage = i_tec * 10.0 * R_SENSE + self.tec_settings.center_pt; let voltage = i_tec * 10.0 * R_SENSE + self.tec_settings.center_pt;
let voltage = self.max1968.set_dac(voltage, self.max1968.dac_out_range); let _voltage = self.max1968.set_dac(voltage, self.max1968.dac_out_range);
self.tec_settings.i_set = (voltage - self.tec_settings.center_pt) / (10.0 * R_SENSE); self.tec_settings.i_set = i_tec;
self.tec_settings.i_set self.tec_settings.i_set
} }
pub fn set_max_v(&mut self, max_v: ElectricPotential) -> ElectricPotential { pub fn set_max_v(&mut self, max_v: ElectricPotential) -> ElectricPotential {
let duty = (max_v / TecSettings::MAX_V_DUTY_TO_VOLTAGE_RATE).get::<ratio>(); let duty = (max_v / TecSettings::MAX_V_DUTY_TO_VOLTAGE_RATE).get::<ratio>();
let duty = self let _duty = self
.max1968 .max1968
.set_pwm(PwmPinsEnum::MaxV, duty as f64, TecSettings::MAX_V_DUTY_MAX); .set_pwm(PwmPinsEnum::MaxV, duty as f64, TecSettings::MAX_V_DUTY_MAX);
self.tec_settings.max_v_set = duty as f32 * TecSettings::MAX_V_DUTY_TO_VOLTAGE_RATE;
self.tec_settings.max_v_set = max_v;
self.tec_settings.max_v_set self.tec_settings.max_v_set
} }
pub fn set_max_i_pos(&mut self, max_i_pos: ElectricCurrent) -> ElectricCurrent { pub fn set_max_i_pos(&mut self, max_i_pos: ElectricCurrent) -> ElectricCurrent {
let duty = (max_i_pos / TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE).get::<ratio>(); let duty = (max_i_pos / TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE).get::<ratio>();
let duty = self let _duty = self
.max1968 .max1968
.set_pwm(PwmPinsEnum::MaxPosI, duty as f64, TecSettings::MAX_I_POS_DUTY_MAX); .set_pwm(PwmPinsEnum::MaxPosI, duty as f64, TecSettings::MAX_I_POS_DUTY_MAX);
self.tec_settings.max_i_pos_set = duty as f32 * TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE;
self.tec_settings.max_i_pos_set = max_i_pos;
self.tec_settings.max_i_pos_set self.tec_settings.max_i_pos_set
} }
pub fn set_max_i_neg(&mut self, max_i_neg: ElectricCurrent) -> ElectricCurrent { pub fn set_max_i_neg(&mut self, max_i_neg: ElectricCurrent) -> ElectricCurrent {
let duty = (max_i_neg / TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE).get::<ratio>(); let duty = (max_i_neg / TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE).get::<ratio>();
let duty = self let _duty = self
.max1968 .max1968
.set_pwm(PwmPinsEnum::MaxNegI, duty as f64, TecSettings::MAX_I_NEG_DUTY_MAX); .set_pwm(PwmPinsEnum::MaxNegI, duty as f64, TecSettings::MAX_I_NEG_DUTY_MAX);
self.tec_settings.max_i_neg_set = duty as f32 * TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE;
self.tec_settings.max_i_neg_set = max_i_neg;
self.tec_settings.max_i_neg_set self.tec_settings.max_i_neg_set
} }