Move PwmLimits into its own file

This commit is contained in:
atse 2024-08-08 17:39:55 +08:00
parent 101a68fcfc
commit 3ec5e185ee
4 changed files with 41 additions and 34 deletions

View File

@ -1,8 +1,9 @@
use crate::{ use crate::{
ad7172, ad7172,
command_parser::{CenterPoint, Polarity}, command_parser::{CenterPoint, Polarity},
config::PwmLimits, pid,
pid, steinhart_hart as sh, pwm_limits::PwmLimits,
steinhart_hart as sh,
}; };
use smoltcp::time::{Duration, Instant}; use smoltcp::time::{Duration, Instant};
use uom::si::{ use uom::si::{

View File

@ -2,15 +2,13 @@ use crate::{
ad7172::PostFilter, ad7172::PostFilter,
channels::Channels, channels::Channels,
command_parser::{CenterPoint, Polarity}, command_parser::{CenterPoint, Polarity},
pid, steinhart_hart, pid,
pwm_limits::PwmLimits,
steinhart_hart,
}; };
use num_traits::Zero; use num_traits::Zero;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use uom::si::{ use uom::si::f64::ElectricCurrent;
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 {
@ -74,29 +72,3 @@ impl ChannelConfig {
channels.set_polarity(channel, self.polarity.clone()); 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::<volt>(),
max_i_pos: max_i_pos.get::<ampere>(),
max_i_neg: max_i_neg.get::<ampere>(),
}
}
pub fn apply(&self, channels: &mut Channels, channel: usize) {
channels.set_max_v(channel, ElectricPotential::new::<volt>(self.max_v));
channels.set_max_i_pos(channel, ElectricCurrent::new::<ampere>(self.max_i_pos));
channels.set_max_i_neg(channel, ElectricCurrent::new::<ampere>(self.max_i_neg));
}
}

View File

@ -37,6 +37,7 @@ mod command_parser;
use command_parser::Ipv4Config; use command_parser::Ipv4Config;
mod channels; mod channels;
mod pid; mod pid;
mod pwm_limits;
mod steinhart_hart; mod steinhart_hart;
mod timer; mod timer;
use channels::{Channels, CHANNELS}; use channels::{Channels, CHANNELS};

33
src/pwm_limits.rs Normal file
View File

@ -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::<volt>(),
max_i_pos: max_i_pos.get::<ampere>(),
max_i_neg: max_i_neg.get::<ampere>(),
}
}
pub fn apply(&self, channels: &mut Channels, channel: usize) {
channels.set_max_v(channel, ElectricPotential::new::<volt>(self.max_v));
channels.set_max_i_pos(channel, ElectricCurrent::new::<ampere>(self.max_i_pos));
channels.set_max_i_neg(channel, ElectricCurrent::new::<ampere>(self.max_i_neg));
}
}