From 257e68b5062fe5f2f1fb6511a087afdece00dedf Mon Sep 17 00:00:00 2001 From: atse Date: Thu, 8 Aug 2024 16:48:29 +0800 Subject: [PATCH] PwmLimits: Use uom quantities for fields not f64s --- src/channel_state.rs | 7 ++++--- src/channels.rs | 12 ++++++------ src/config.rs | 24 +++++++++++------------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/channel_state.rs b/src/channel_state.rs index d4c46a1..747cf36 100644 --- a/src/channel_state.rs +++ b/src/channel_state.rs @@ -1,3 +1,4 @@ +use num_traits::Zero; use smoltcp::time::{Duration, Instant}; use uom::si::{ f64::{ @@ -52,9 +53,9 @@ impl ChannelState { 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, + max_v: ElectricPotential::zero(), + max_i_pos: ElectricCurrent::zero(), + max_i_neg: ElectricCurrent::zero(), }, pid_engaged: false, pid: pid::Controller::new(pid::Parameters::default()), diff --git a/src/channels.rs b/src/channels.rs index d8300c0..403bb61 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -358,15 +358,15 @@ impl Channels { } pub fn get_max_v(&mut self, channel: usize) -> ElectricPotential { - ElectricPotential::new::(self.channel_state(channel).pwm_limits.max_v) + self.channel_state(channel).pwm_limits.max_v } pub fn get_max_i_pos(&mut self, channel: usize) -> ElectricCurrent { - ElectricCurrent::new::(self.channel_state(channel).pwm_limits.max_i_pos) + self.channel_state(channel).pwm_limits.max_i_pos } pub fn get_max_i_neg(&mut self, channel: usize) -> ElectricCurrent { - ElectricCurrent::new::(self.channel_state(channel).pwm_limits.max_i_neg) + self.channel_state(channel).pwm_limits.max_i_neg } // Get current passing through TEC @@ -415,7 +415,7 @@ impl Channels { 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::(); + self.channel_state(channel).pwm_limits.max_v = max_v; (duty * max, max) } @@ -427,7 +427,7 @@ impl Channels { Polarity::Normal => self.set_pwm(channel, PwmPin::MaxIPos, duty), Polarity::Reversed => self.set_pwm(channel, PwmPin::MaxINeg, duty), }; - self.channel_state(channel).pwm_limits.max_i_pos = max_i_pos.get::(); + self.channel_state(channel).pwm_limits.max_i_pos = max_i_pos; (duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, max) } @@ -439,7 +439,7 @@ impl Channels { Polarity::Normal => self.set_pwm(channel, PwmPin::MaxINeg, duty), Polarity::Reversed => self.set_pwm(channel, PwmPin::MaxIPos, duty), }; - self.channel_state(channel).pwm_limits.max_i_neg = max_i_neg.get::(); + self.channel_state(channel).pwm_limits.max_i_neg = max_i_neg; (duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, max) } diff --git a/src/config.rs b/src/config.rs index 65b511d..9d76420 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,9 +1,7 @@ use num_traits::Zero; use serde::{Serialize, Deserialize}; -use uom::si::{ - electric_potential::volt, - electric_current::ampere, - f64::{ElectricCurrent, ElectricPotential}, +use uom::si::f64::{ + ElectricCurrent, ElectricPotential, }; use crate::{ ad7172::PostFilter, @@ -76,9 +74,9 @@ impl ChannelConfig { #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct PwmLimits { - pub max_v: f64, - pub max_i_pos: f64, - pub max_i_neg: f64, + pub max_v: ElectricPotential, + pub max_i_pos: ElectricCurrent, + pub max_i_neg: ElectricCurrent, } impl PwmLimits { @@ -87,15 +85,15 @@ impl PwmLimits { let max_i_pos = channels.get_max_i_pos(channel); let max_i_neg = channels.get_max_i_neg(channel); PwmLimits { - max_v: max_v.get::(), - max_i_pos: max_i_pos.get::(), - max_i_neg: max_i_neg.get::(), + max_v, + max_i_pos, + max_i_neg, } } pub fn apply(&self, channels: &mut Channels, channel: usize) { - channels.set_max_v(channel, ElectricPotential::new::(self.max_v)); - channels.set_max_i_pos(channel, ElectricCurrent::new::(self.max_i_pos)); - channels.set_max_i_neg(channel, ElectricCurrent::new::(self.max_i_neg)); + channels.set_max_v(channel, self.max_v); + channels.set_max_i_pos(channel, self.max_i_pos); + channels.set_max_i_neg(channel, self.max_i_neg); } }