From 58e648b5e094363d6c149d77792809ca2b1f0d14 Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 24 Sep 2020 20:45:07 +0200 Subject: [PATCH] pid::Parameters, CenterPoint: demote f32 fields to save config space --- src/channels.rs | 4 ++-- src/command_parser.rs | 4 ++-- src/config.rs | 4 ++-- src/main.rs | 14 +++++++------- src/pid.rs | 36 ++++++++++++++++++------------------ 5 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/channels.rs b/src/channels.rs index a2a7dea..c02364d 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -103,7 +103,7 @@ impl Channels { CenterPoint::Vref => state.vref, CenterPoint::Override(center_point) => - ElectricPotential::new::(center_point), + ElectricPotential::new::(center_point.into()), }; let r_sense = ElectricalResistance::new::(R_SENSE); let (voltage, max) = self.get_dac(channel); @@ -137,7 +137,7 @@ impl Channels { CenterPoint::Vref => state.vref, CenterPoint::Override(center_point) => - ElectricPotential::new::(center_point), + ElectricPotential::new::(center_point.into()), }; let r_sense = ElectricalResistance::new::(R_SENSE); let voltage = i_tec * 10.0 * r_sense + center_point; diff --git a/src/command_parser.rs b/src/command_parser.rs index fa0e994..6ed73c0 100644 --- a/src/command_parser.rs +++ b/src/command_parser.rs @@ -126,7 +126,7 @@ pub enum PwmPin { #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub enum CenterPoint { Vref, - Override(f64), + Override(f32), } #[derive(Debug, Clone, PartialEq)] @@ -312,7 +312,7 @@ fn center_point(input: &[u8]) -> IResult<&[u8], Result> { value(Ok(CenterPoint::Vref), tag("vref")), |input| { let (input, value) = float(input)?; - Ok((input, value.map(CenterPoint::Override))) + Ok((input, value.map(|value| CenterPoint::Override(value as f32)))) } ))(input)?; end(input)?; diff --git a/src/config.rs b/src/config.rs index 6e2f148..25c8dc8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -37,7 +37,7 @@ impl Config { pub struct ChannelConfig { center: CenterPoint, pid: pid::Parameters, - pid_target: f64, + pid_target: f32, sh: steinhart_hart::Parameters, // TODO: pwm limits } @@ -47,7 +47,7 @@ impl ChannelConfig { ChannelConfig { center: state.center.clone(), pid: state.pid.parameters.clone(), - pid_target: state.pid.target, + pid_target: state.pid.target as f32, sh: state.sh.clone(), } } diff --git a/src/main.rs b/src/main.rs index 1e6fa1f..a9f78f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -388,19 +388,19 @@ fn main() -> ! { pid.reset(); } KP => - pid.parameters.kp = value, + pid.parameters.kp = value as f32, KI => - pid.parameters.ki = value, + pid.parameters.ki = value as f32, KD => - pid.parameters.kd = value, + pid.parameters.kd = value as f32, OutputMin => - pid.parameters.output_min = value, + pid.parameters.output_min = value as f32, OutputMax => - pid.parameters.output_max = value, + pid.parameters.output_max = value as f32, IntegralMin => - pid.parameters.integral_min = value, + pid.parameters.integral_min = value as f32, IntegralMax => - pid.parameters.integral_max = value, + pid.parameters.integral_max = value as f32, } let _ = writeln!(socket, "PID parameter updated"); } diff --git a/src/pid.rs b/src/pid.rs index 198664c..73f9092 100644 --- a/src/pid.rs +++ b/src/pid.rs @@ -2,13 +2,13 @@ use serde::{Serialize, Deserialize}; #[derive(Clone, Serialize, Deserialize)] pub struct Parameters { - pub kp: f64, - pub ki: f64, - pub kd: f64, - pub output_min: f64, - pub output_max: f64, - pub integral_min: f64, - pub integral_max: f64 + pub kp: f32, + pub ki: f32, + pub kd: f32, + pub output_min: f32, + pub output_max: f32, + pub integral_min: f32, + pub integral_max: f32 } impl Default for Parameters { @@ -50,32 +50,32 @@ impl Controller { let error = self.target - input; // partial - let p = self.parameters.kp * error; + let p = f64::from(self.parameters.kp) * error; //integral self.integral += error; - if self.integral < self.parameters.integral_min { - self.integral = self.parameters.integral_min; + if self.integral < self.parameters.integral_min.into() { + self.integral = self.parameters.integral_min.into(); } - if self.integral > self.parameters.integral_max { - self.integral = self.parameters.integral_max; + if self.integral > self.parameters.integral_max.into() { + self.integral = self.parameters.integral_max.into(); } - let i = self.parameters.ki * self.integral; + let i = f64::from(self.parameters.ki) * f64::from(self.integral); // derivative let d = match self.last_input { None => 0.0, - Some(last_input) => self.parameters.kd * (last_input - input) + Some(last_input) => f64::from(self.parameters.kd) * (last_input - input) }; self.last_input = Some(input); // output let mut output = p + i + d; - if output < self.parameters.output_min { - output = self.parameters.output_min; + if output < self.parameters.output_min.into() { + output = self.parameters.output_min.into(); } - if output > self.parameters.output_max { - output = self.parameters.output_max; + if output > self.parameters.output_max.into() { + output = self.parameters.output_max.into(); } self.last_output = Some(output); output