config: add pwm limits

This commit is contained in:
Astro 2020-09-24 21:10:27 +02:00
parent 17e89b2041
commit daa398cb5e
1 changed files with 36 additions and 5 deletions

View File

@ -1,7 +1,10 @@
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use postcard::{from_bytes, to_slice}; use postcard::{from_bytes, to_slice};
use uom::si::{
electric_potential::volt,
electric_current::ampere,
};
use crate::{ use crate::{
channel_state::ChannelState,
channels::{CHANNELS, Channels}, channels::{CHANNELS, Channels},
command_parser::CenterPoint, command_parser::CenterPoint,
pid, pid,
@ -18,8 +21,8 @@ impl Config {
pub fn new(channels: &mut Channels) -> Self { pub fn new(channels: &mut Channels) -> Self {
Config { Config {
channels: [ channels: [
ChannelConfig::new(channels.channel_state(0usize)), ChannelConfig::new(channels, 0),
ChannelConfig::new(channels.channel_state(1usize)), ChannelConfig::new(channels, 1),
], ],
} }
} }
@ -39,21 +42,44 @@ pub struct ChannelConfig {
pid: pid::Parameters, pid: pid::Parameters,
pid_target: f32, pid_target: f32,
sh: steinhart_hart::Parameters, sh: steinhart_hart::Parameters,
// TODO: pwm limits pwm: PwmLimits,
} }
impl ChannelConfig { impl ChannelConfig {
pub fn new(state: &ChannelState) -> Self { pub fn new(channels: &mut Channels, channel: usize) -> Self {
let pwm = PwmLimits::new(channels, channel);
let state = channels.channel_state(channel);
ChannelConfig { ChannelConfig {
center: state.center.clone(), center: state.center.clone(),
pid: state.pid.parameters.clone(), pid: state.pid.parameters.clone(),
pid_target: state.pid.target as f32, pid_target: state.pid.target as f32,
sh: state.sh.clone(), sh: state.sh.clone(),
pwm,
} }
} }
} }
#[derive(Clone, Serialize, Deserialize)]
struct PwmLimits {
max_v: f32,
max_i_pos: f32,
max_i_neg: f32,
}
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>() as f32,
max_i_pos: max_i_pos.get::<ampere>() as f32,
max_i_neg: max_i_neg.get::<ampere>() as f32,
}
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
@ -65,6 +91,11 @@ mod test {
pid: pid::Parameters::default(), pid: pid::Parameters::default(),
pid_target: 93.7, pid_target: 93.7,
sh: steinhart_hart::Parameters::default(), sh: steinhart_hart::Parameters::default(),
pwm: PwmLimits {
max_v: 1.65,
max_i_pos: 2.1,
max_i_neg: 2.25,
},
}; };
let config = Config { let config = Config {
channels: [ channels: [