PwmLimits: Use uom quantities for fields not f64s

This commit is contained in:
atse 2024-08-08 16:48:29 +08:00 committed by sb10q
parent 45eb55d36d
commit bd9ae997ae
3 changed files with 20 additions and 23 deletions

View File

@ -4,6 +4,7 @@ use crate::{
config::PwmLimits, config::PwmLimits,
pid, steinhart_hart as sh, pid, steinhart_hart as sh,
}; };
use num_traits::Zero;
use smoltcp::time::{Duration, Instant}; use smoltcp::time::{Duration, Instant};
use uom::si::{ use uom::si::{
electric_current::ampere, electric_current::ampere,
@ -47,9 +48,9 @@ impl ChannelState {
dac_value: ElectricPotential::new::<volt>(0.0), dac_value: ElectricPotential::new::<volt>(0.0),
i_set: ElectricCurrent::new::<ampere>(0.0), i_set: ElectricCurrent::new::<ampere>(0.0),
pwm_limits: PwmLimits { pwm_limits: PwmLimits {
max_v: 0.0, max_v: ElectricPotential::zero(),
max_i_pos: 0.0, max_i_pos: ElectricCurrent::zero(),
max_i_neg: 0.0, max_i_neg: ElectricCurrent::zero(),
}, },
pid_engaged: false, pid_engaged: false,
pid: pid::Controller::new(pid::Parameters::default()), pid: pid::Controller::new(pid::Parameters::default()),

View File

@ -364,15 +364,15 @@ impl Channels {
} }
pub fn get_max_v(&mut self, channel: usize) -> ElectricPotential { pub fn get_max_v(&mut self, channel: usize) -> ElectricPotential {
ElectricPotential::new::<volt>(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 { pub fn get_max_i_pos(&mut self, channel: usize) -> ElectricCurrent {
ElectricCurrent::new::<ampere>(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 { pub fn get_max_i_neg(&mut self, channel: usize) -> ElectricCurrent {
ElectricCurrent::new::<ampere>(self.channel_state(channel).pwm_limits.max_i_neg) self.channel_state(channel).pwm_limits.max_i_neg
} }
// Get current passing through TEC // Get current passing through TEC
@ -420,7 +420,7 @@ impl Channels {
let max_v = max_v.min(MAX_TEC_V).max(ElectricPotential::zero()); let max_v = max_v.min(MAX_TEC_V).max(ElectricPotential::zero());
let duty = (max_v / max).get::<ratio>(); let duty = (max_v / max).get::<ratio>();
let duty = self.set_pwm(channel, PwmPin::MaxV, duty); let duty = self.set_pwm(channel, PwmPin::MaxV, duty);
self.channel_state(channel).pwm_limits.max_v = max_v.get::<volt>(); self.channel_state(channel).pwm_limits.max_v = max_v;
(duty * max, max) (duty * max, max)
} }
@ -436,7 +436,7 @@ impl Channels {
Polarity::Normal => self.set_pwm(channel, PwmPin::MaxIPos, duty), Polarity::Normal => self.set_pwm(channel, PwmPin::MaxIPos, duty),
Polarity::Reversed => self.set_pwm(channel, PwmPin::MaxINeg, duty), Polarity::Reversed => self.set_pwm(channel, PwmPin::MaxINeg, duty),
}; };
self.channel_state(channel).pwm_limits.max_i_pos = max_i_pos.get::<ampere>(); self.channel_state(channel).pwm_limits.max_i_pos = max_i_pos;
(duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, max) (duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, max)
} }
@ -452,7 +452,7 @@ impl Channels {
Polarity::Normal => self.set_pwm(channel, PwmPin::MaxINeg, duty), Polarity::Normal => self.set_pwm(channel, PwmPin::MaxINeg, duty),
Polarity::Reversed => self.set_pwm(channel, PwmPin::MaxIPos, duty), Polarity::Reversed => self.set_pwm(channel, PwmPin::MaxIPos, duty),
}; };
self.channel_state(channel).pwm_limits.max_i_neg = max_i_neg.get::<ampere>(); self.channel_state(channel).pwm_limits.max_i_neg = max_i_neg;
(duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, max) (duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, max)
} }

View File

@ -6,11 +6,7 @@ use crate::{
}; };
use num_traits::Zero; use num_traits::Zero;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use uom::si::{ use uom::si::f64::{ElectricCurrent, ElectricPotential};
electric_current::ampere,
electric_potential::volt,
f64::{ElectricCurrent, ElectricPotential},
};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ChannelConfig { pub struct ChannelConfig {
@ -77,9 +73,9 @@ impl ChannelConfig {
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct PwmLimits { pub struct PwmLimits {
pub max_v: f64, pub max_v: ElectricPotential,
pub max_i_pos: f64, pub max_i_pos: ElectricCurrent,
pub max_i_neg: f64, pub max_i_neg: ElectricCurrent,
} }
impl PwmLimits { impl PwmLimits {
@ -88,15 +84,15 @@ impl PwmLimits {
let max_i_pos = channels.get_max_i_pos(channel); let max_i_pos = channels.get_max_i_pos(channel);
let max_i_neg = channels.get_max_i_neg(channel); let max_i_neg = channels.get_max_i_neg(channel);
PwmLimits { PwmLimits {
max_v: max_v.get::<volt>(), max_v,
max_i_pos: max_i_pos.get::<ampere>(), max_i_pos,
max_i_neg: max_i_neg.get::<ampere>(), max_i_neg,
} }
} }
pub fn apply(&self, channels: &mut Channels, channel: usize) { pub fn apply(&self, channels: &mut Channels, channel: usize) {
channels.set_max_v(channel, ElectricPotential::new::<volt>(self.max_v)); channels.set_max_v(channel, self.max_v);
channels.set_max_i_pos(channel, ElectricCurrent::new::<ampere>(self.max_i_pos)); channels.set_max_i_pos(channel, self.max_i_pos);
channels.set_max_i_neg(channel, ElectricCurrent::new::<ampere>(self.max_i_neg)); channels.set_max_i_neg(channel, self.max_i_neg);
} }
} }