firmware: clamp input against invalid value
This commit is contained in:
parent
7f9dc37c10
commit
e1ee3ec959
@ -161,9 +161,10 @@ impl LdDrive {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ld_set_i(&mut self, i: ElectricCurrent) {
|
pub fn ld_set_i(&mut self, i: ElectricCurrent) -> bool {
|
||||||
self.settings.ld_drive_current = i.min(Settings::LD_CURRENT_MAX);
|
self.settings.ld_drive_current = i.min(Settings::LD_CURRENT_MAX).max(ElectricCurrent::zero());
|
||||||
LdCurrentOutCtrlTimer::set_target_i_and_listen_irq(self.settings.ld_drive_current, self.ctrl.get_i_set());
|
LdCurrentOutCtrlTimer::set_target_i_and_listen_irq(self.settings.ld_drive_current, self.ctrl.get_i_set());
|
||||||
|
self.settings.ld_drive_current != i
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn poll_pd_mon_v(&mut self) -> ElectricPotential {
|
pub fn poll_pd_mon_v(&mut self) -> ElectricPotential {
|
||||||
|
@ -67,18 +67,20 @@ impl TempMon {
|
|||||||
const OVER_TEMP_COUNT_LIMIT: u32 = 25;
|
const OVER_TEMP_COUNT_LIMIT: u32 = 25;
|
||||||
const TEMP_STABLE_COUNT_LIMIT: u32 = 100;
|
const TEMP_STABLE_COUNT_LIMIT: u32 = 100;
|
||||||
|
|
||||||
pub fn set_upper_limit(&mut self, upper_limit: f32) {
|
pub fn set_upper_limit(&mut self, upper_limit: f32) -> bool {
|
||||||
self.upper_limit = upper_limit;
|
self.upper_limit = upper_limit.max(-273.15);
|
||||||
self.is_limit_changed = true;
|
self.is_limit_changed = true;
|
||||||
|
self.upper_limit != upper_limit
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_upper_limit(&mut self) -> f32 {
|
pub fn get_upper_limit(&mut self) -> f32 {
|
||||||
self.upper_limit
|
self.upper_limit
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_lower_limit(&mut self, lower_limit: f32) {
|
pub fn set_lower_limit(&mut self, lower_limit: f32) -> bool {
|
||||||
self.lower_limit = lower_limit;
|
self.lower_limit = lower_limit.max(-273.15);
|
||||||
self.is_limit_changed = true;
|
self.is_limit_changed = true;
|
||||||
|
self.lower_limit != lower_limit
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_lower_limit(&mut self) -> f32 {
|
pub fn get_lower_limit(&mut self) -> f32 {
|
||||||
|
@ -2,6 +2,7 @@ use core::{f32::NAN, marker::PhantomData};
|
|||||||
|
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use miniconf::Tree;
|
use miniconf::Tree;
|
||||||
|
use num_traits::Zero;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use uom::si::{electric_current::ampere,
|
use uom::si::{electric_current::ampere,
|
||||||
electric_potential::volt,
|
electric_potential::volt,
|
||||||
@ -257,42 +258,46 @@ impl Thermostat {
|
|||||||
self.tec_settings.default_pwr_on = pwr_on;
|
self.tec_settings.default_pwr_on = pwr_on;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_i(&mut self, i_tec: ElectricCurrent) -> ElectricCurrent {
|
pub fn set_i(&mut self, i_tec: ElectricCurrent) -> bool {
|
||||||
let voltage = i_tec * 10.0 * R_SENSE + self.tec_settings.center_pt;
|
let i_tec_set = i_tec.max(-TecSettings::MAX_I_NEG_CURRENT).min(TecSettings::MAX_I_POS_CURRENT);
|
||||||
|
let voltage = i_tec_set * 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 = i_tec;
|
self.tec_settings.i_set = i_tec_set;
|
||||||
self.tec_settings.i_set
|
i_tec_set != i_tec
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_max_v(&mut self, max_v: ElectricPotential) -> ElectricPotential {
|
pub fn set_max_v(&mut self, max_v: ElectricPotential) -> bool {
|
||||||
let duty = (max_v / TecSettings::MAX_V_DUTY_TO_VOLTAGE_RATE).get::<ratio>();
|
let max_v_set = max_v.min(TecSettings::MAX_V_MAX).max(ElectricPotential::zero());
|
||||||
|
let duty = (max_v_set / 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 = max_v;
|
self.tec_settings.max_v_set = max_v_set;
|
||||||
self.tec_settings.max_v_set
|
max_v_set != max_v
|
||||||
}
|
}
|
||||||
|
|
||||||
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) -> bool {
|
||||||
let duty = (max_i_pos / TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE).get::<ratio>();
|
let max_i_pos_set = max_i_pos.min(TecSettings::MAX_I_POS_CURRENT).max(ElectricCurrent::zero());
|
||||||
|
let duty = (max_i_pos_set / 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 = max_i_pos;
|
self.tec_settings.max_i_pos_set = max_i_pos_set;
|
||||||
self.tec_settings.max_i_pos_set
|
max_i_pos_set != max_i_pos
|
||||||
}
|
}
|
||||||
|
|
||||||
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) -> bool {
|
||||||
let duty = (max_i_neg / TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE).get::<ratio>();
|
let max_i_neg_set = max_i_neg.min(TecSettings::MAX_I_NEG_CURRENT).max(ElectricCurrent::zero());
|
||||||
|
let duty = (max_i_neg_set / 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 = max_i_neg;
|
self.tec_settings.max_i_neg_set = max_i_neg_set;
|
||||||
self.tec_settings.max_i_neg_set
|
max_i_neg_set != max_i_neg
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
@ -469,12 +474,19 @@ impl Thermostat {
|
|||||||
self.pid_ctrl_ch0.set_sh_t0(t0);
|
self.pid_ctrl_ch0.set_sh_t0(t0);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_temperature_setpoint(&mut self, t: f32) {
|
pub fn set_temperature_setpoint(&mut self, t: f32) -> bool {
|
||||||
|
let clamped = if self.temp_mon.get_upper_limit() < t ||
|
||||||
|
self.temp_mon.get_lower_limit() > t {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
let t = t
|
let t = t
|
||||||
.min(self.temp_mon.get_upper_limit())
|
.min(self.temp_mon.get_upper_limit())
|
||||||
.max(self.temp_mon.get_lower_limit());
|
.max(self.temp_mon.get_lower_limit());
|
||||||
self.pid_ctrl_ch0.set_pid_setpoint(t);
|
self.pid_ctrl_ch0.set_pid_setpoint(t);
|
||||||
self.temp_mon.set_setpoint(t);
|
self.temp_mon.set_setpoint(t);
|
||||||
|
clamped
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn apply_temp_mon_settings(&mut self, settings: TempMonSettings) {
|
pub fn apply_temp_mon_settings(&mut self, settings: TempMonSettings) {
|
||||||
@ -484,12 +496,12 @@ impl Thermostat {
|
|||||||
.set_lower_limit(settings.lower_limit);
|
.set_lower_limit(settings.lower_limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_temp_mon_upper_limit(&mut self, t: f32) {
|
pub fn set_temp_mon_upper_limit(&mut self, t: f32) -> bool {
|
||||||
self.temp_mon.set_upper_limit(t);
|
self.temp_mon.set_upper_limit(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_temp_mon_lower_limit(&mut self, t: f32) {
|
pub fn set_temp_mon_lower_limit(&mut self, t: f32) -> bool {
|
||||||
self.temp_mon.set_lower_limit(t);
|
self.temp_mon.set_lower_limit(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_temp_adc_sinc5_sinc1_filter(&mut self, index: u8, odr: ad7172::SingleChODR) {
|
pub fn set_temp_adc_sinc5_sinc1_filter(&mut self, index: u8, odr: ad7172::SingleChODR) {
|
||||||
|
Loading…
Reference in New Issue
Block a user