From 163bb38f6858184227e7c8068f2cf7df96271394 Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 19 Mar 2020 22:00:22 +0100 Subject: [PATCH] simplify pid, keep last_output --- src/main.rs | 27 ++++++++++++++++----------- src/pid.rs | 27 +++++++-------------------- 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/main.rs b/src/main.rs index 41ef773..74632bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -187,20 +187,25 @@ fn main() -> ! { for (channel, state) in channel_states.iter().enumerate() { let _ = writeln!(socket, "PID settings for channel {}", channel); let pid = &state.pid; - let _ = writeln!(socket, "- target={:.4}", pid.get_target()); - let p = pid.get_parameters(); - macro_rules! out { + let _ = writeln!(socket, "- target={:.4}", pid.target); + macro_rules! show_pid_parameter { ($p: tt) => { - let _ = writeln!(socket, "- {}={:.4}", stringify!($p), p.$p); + let _ = writeln!( + socket, "- {}={:.4}", + stringify!($p), pid.parameters.$p + ); }; } - out!(kp); - out!(ki); - out!(kd); - out!(output_min); - out!(output_max); - out!(integral_min); - out!(integral_max); + show_pid_parameter!(kp); + show_pid_parameter!(ki); + show_pid_parameter!(kd); + show_pid_parameter!(output_min); + show_pid_parameter!(output_max); + show_pid_parameter!(integral_min); + show_pid_parameter!(integral_max); + if let Some(last_output) = pid.last_output { + let _ = writeln!(socket, "- output={:.4}", last_output); + } let _ = writeln!(socket, ""); } } diff --git a/src/pid.rs b/src/pid.rs index 04451ef..ebe92d7 100644 --- a/src/pid.rs +++ b/src/pid.rs @@ -25,10 +25,11 @@ impl Default for Parameters { #[derive(Clone)] pub struct Controller { - parameters: Parameters, - target: f64, + pub parameters: Parameters, + pub target: f64, integral: f64, - last_input: Option + last_input: Option, + pub last_output: Option, } impl Controller { @@ -37,7 +38,8 @@ impl Controller { parameters: parameters, target: 0.0, last_input: None, - integral: 0.0 + integral: 0.0, + last_output: None, } } @@ -68,25 +70,10 @@ impl Controller { if output > self.parameters.output_max { output = self.parameters.output_max; } + self.last_output = Some(output); output } - pub fn get_target(&self) -> f64 { - self.target - } - - pub fn set_target(&mut self, target: f64) { - self.target = target; - } - - pub fn get_parameters(&self) -> &Parameters { - &self.parameters - } - - pub fn update_parameters(&mut self, f: F) { - f(&mut self.parameters); - } - #[allow(dead_code)] pub fn reset(&mut self) { self.integral = 0.0;