From 3ec5e185ee553e0b4bff05153f31315a00454efa Mon Sep 17 00:00:00 2001 From: atse Date: Thu, 8 Aug 2024 17:39:55 +0800 Subject: [PATCH] Move PwmLimits into its own file --- src/channel_state.rs | 5 +++-- src/config.rs | 36 ++++-------------------------------- src/main.rs | 1 + src/pwm_limits.rs | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 34 deletions(-) create mode 100644 src/pwm_limits.rs diff --git a/src/channel_state.rs b/src/channel_state.rs index 9872e11..649c903 100644 --- a/src/channel_state.rs +++ b/src/channel_state.rs @@ -1,8 +1,9 @@ use crate::{ ad7172, command_parser::{CenterPoint, Polarity}, - config::PwmLimits, - pid, steinhart_hart as sh, + pid, + pwm_limits::PwmLimits, + steinhart_hart as sh, }; use smoltcp::time::{Duration, Instant}; use uom::si::{ diff --git a/src/config.rs b/src/config.rs index 43c5bb8..bad90b4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,15 +2,13 @@ use crate::{ ad7172::PostFilter, channels::Channels, command_parser::{CenterPoint, Polarity}, - pid, steinhart_hart, + pid, + pwm_limits::PwmLimits, + steinhart_hart, }; use num_traits::Zero; use serde::{Deserialize, Serialize}; -use uom::si::{ - electric_current::ampere, - electric_potential::volt, - f64::{ElectricCurrent, ElectricPotential}, -}; +use uom::si::f64::ElectricCurrent; #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct ChannelConfig { @@ -74,29 +72,3 @@ impl ChannelConfig { channels.set_polarity(channel, self.polarity.clone()); } } - -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -pub struct PwmLimits { - pub max_v: f64, - pub max_i_pos: f64, - pub max_i_neg: f64, -} - -impl PwmLimits { - pub fn new(channels: &mut Channels, channel: usize) -> Self { - let max_v = channels.get_max_v(channel); - 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::(), - } - } - - 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)); - } -} diff --git a/src/main.rs b/src/main.rs index edc028c..34ad204 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,6 +37,7 @@ mod command_parser; use command_parser::Ipv4Config; mod channels; mod pid; +mod pwm_limits; mod steinhart_hart; mod timer; use channels::{Channels, CHANNELS}; diff --git a/src/pwm_limits.rs b/src/pwm_limits.rs new file mode 100644 index 0000000..0ba911d --- /dev/null +++ b/src/pwm_limits.rs @@ -0,0 +1,33 @@ +use crate::channels::Channels; +use serde::{Deserialize, Serialize}; +use uom::si::{ + electric_current::ampere, + electric_potential::volt, + f64::{ElectricCurrent, ElectricPotential}, +}; + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct PwmLimits { + pub max_v: f64, + pub max_i_pos: f64, + pub max_i_neg: f64, +} + +impl PwmLimits { + pub fn new(channels: &mut Channels, channel: usize) -> Self { + let max_v = channels.get_max_v(channel); + 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::(), + } + } + + 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)); + } +}