diff --git a/src/channel_state.rs b/src/channel_state.rs index 4820131..7ee1d7f 100644 --- a/src/channel_state.rs +++ b/src/channel_state.rs @@ -16,6 +16,7 @@ use uom::si::{ use crate::{ ad7172, pid, + config::PwmLimits, steinhart_hart as sh, command_parser::CenterPoint, }; @@ -32,6 +33,7 @@ pub struct ChannelState { pub center: CenterPoint, pub dac_value: ElectricPotential, pub i_set: ElectricCurrent, + pub pwm_limits: PwmLimits, pub pid_engaged: bool, pub pid: pid::Controller, pub sh: sh::Parameters, @@ -48,6 +50,11 @@ impl ChannelState { center: CenterPoint::Vref, dac_value: ElectricPotential::new::(0.0), i_set: ElectricCurrent::new::(0.0), + pwm_limits: PwmLimits { + max_v: 0.0, + max_i_pos: 0.0, + max_i_neg: 0.0, + }, pid_engaged: false, pid: pid::Controller::new(pid::Parameters::default()), sh: sh::Parameters::default(), diff --git a/src/channels.rs b/src/channels.rs index a428dd1..c4eea9d 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -390,19 +390,19 @@ impl Channels { pub fn get_max_v(&mut self, channel: usize) -> (ElectricPotential, ElectricPotential) { let max = 4.0 * ElectricPotential::new::(3.3); let duty = self.get_pwm(channel, PwmPin::MaxV); - (duty * max, MAX_TEC_V) + (duty * max, ElectricPotential::new::(self.channel_state(channel).pwm_limits.max_v)) } pub fn get_max_i_pos(&mut self, channel: usize) -> (ElectricCurrent, ElectricCurrent) { let max = ElectricCurrent::new::(3.0); let duty = self.get_pwm(channel, PwmPin::MaxIPos); - (duty * max, MAX_TEC_I) + (duty * max, ElectricCurrent::new::(self.channel_state(channel).pwm_limits.max_i_pos)) } pub fn get_max_i_neg(&mut self, channel: usize) -> (ElectricCurrent, ElectricCurrent) { let max = ElectricCurrent::new::(3.0); let duty = self.get_pwm(channel, PwmPin::MaxINeg); - (duty * max, MAX_TEC_I) + (duty * max, ElectricCurrent::new::(self.channel_state(channel).pwm_limits.max_i_neg)) } // Get current passing through TEC @@ -444,22 +444,28 @@ impl Channels { pub fn set_max_v(&mut self, channel: usize, max_v: ElectricPotential) -> (ElectricPotential, ElectricPotential) { let max = 4.0 * ElectricPotential::new::(3.3); - let duty = (max_v.min(MAX_TEC_V).max(ElectricPotential::zero()) / max).get::(); + let max_v = max_v.min(MAX_TEC_V).max(ElectricPotential::zero()); + let duty = (max_v / max).get::(); let duty = self.set_pwm(channel, PwmPin::MaxV, duty); + self.channel_state(channel).pwm_limits.max_v = max_v.get::(); (duty * max, max) } pub fn set_max_i_pos(&mut self, channel: usize, max_i_pos: ElectricCurrent) -> (ElectricCurrent, ElectricCurrent) { let max = ElectricCurrent::new::(3.0); - let duty = (max_i_pos.min(MAX_TEC_I).max(ElectricCurrent::zero()) / max).get::(); + let max_i_pos = max_i_pos.min(MAX_TEC_I).max(ElectricCurrent::zero()); + let duty = (max_i_pos / max).get::(); let duty = self.set_pwm(channel, PwmPin::MaxIPos, duty); + self.channel_state(channel).pwm_limits.max_i_pos = max_i_pos.get::(); (duty * max, max) } pub fn set_max_i_neg(&mut self, channel: usize, max_i_neg: ElectricCurrent) -> (ElectricCurrent, ElectricCurrent) { let max = ElectricCurrent::new::(3.0); - let duty = (max_i_neg.min(MAX_TEC_I).max(ElectricCurrent::zero()) / max).get::(); + let max_i_neg = max_i_neg.min(MAX_TEC_I).max(ElectricCurrent::zero()); + let duty = (max_i_neg / max).get::(); let duty = self.set_pwm(channel, PwmPin::MaxINeg, duty); + self.channel_state(channel).pwm_limits.max_i_neg = max_i_neg.get::(); (duty * max, max) } @@ -518,7 +524,7 @@ impl Channels { PwmSummary { channel, center: CenterPointJson(self.channel_state(channel).center.clone()), - i_set: (self.get_i(channel), MAX_TEC_I).into(), + i_set: (self.get_i(channel), self.channel_state(channel).i_set).into(), max_v: self.get_max_v(channel).into(), max_i_pos: self.get_max_i_pos(channel).into(), max_i_neg: self.get_max_i_neg(channel).into(), @@ -606,12 +612,12 @@ impl Serialize for CenterPointJson { #[derive(Serialize)] pub struct PwmSummaryField { value: T, - max: T, + set_value: T, } impl From<(T, T)> for PwmSummaryField { - fn from((value, max): (T, T)) -> Self { - PwmSummaryField { value, max } + fn from((value, set_value): (T, T)) -> Self { + PwmSummaryField { value, set_value } } } diff --git a/src/config.rs b/src/config.rs index 1649dde..a7a3e7a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -72,10 +72,10 @@ impl ChannelConfig { } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -struct PwmLimits { - max_v: f64, - max_i_pos: f64, - max_i_neg: f64, +pub struct PwmLimits { + pub max_v: f64, + pub max_i_pos: f64, + pub max_i_neg: f64, } impl PwmLimits {