simplify pid, keep last_output

This commit is contained in:
Astro 2020-03-19 22:00:22 +01:00
parent e5b4789304
commit 163bb38f68
2 changed files with 23 additions and 31 deletions

View File

@ -187,20 +187,25 @@ fn main() -> ! {
for (channel, state) in channel_states.iter().enumerate() { for (channel, state) in channel_states.iter().enumerate() {
let _ = writeln!(socket, "PID settings for channel {}", channel); let _ = writeln!(socket, "PID settings for channel {}", channel);
let pid = &state.pid; let pid = &state.pid;
let _ = writeln!(socket, "- target={:.4}", pid.get_target()); let _ = writeln!(socket, "- target={:.4}", pid.target);
let p = pid.get_parameters(); macro_rules! show_pid_parameter {
macro_rules! out {
($p: tt) => { ($p: tt) => {
let _ = writeln!(socket, "- {}={:.4}", stringify!($p), p.$p); let _ = writeln!(
socket, "- {}={:.4}",
stringify!($p), pid.parameters.$p
);
}; };
} }
out!(kp); show_pid_parameter!(kp);
out!(ki); show_pid_parameter!(ki);
out!(kd); show_pid_parameter!(kd);
out!(output_min); show_pid_parameter!(output_min);
out!(output_max); show_pid_parameter!(output_max);
out!(integral_min); show_pid_parameter!(integral_min);
out!(integral_max); show_pid_parameter!(integral_max);
if let Some(last_output) = pid.last_output {
let _ = writeln!(socket, "- output={:.4}", last_output);
}
let _ = writeln!(socket, ""); let _ = writeln!(socket, "");
} }
} }

View File

@ -25,10 +25,11 @@ impl Default for Parameters {
#[derive(Clone)] #[derive(Clone)]
pub struct Controller { pub struct Controller {
parameters: Parameters, pub parameters: Parameters,
target: f64, pub target: f64,
integral: f64, integral: f64,
last_input: Option<f64> last_input: Option<f64>,
pub last_output: Option<f64>,
} }
impl Controller { impl Controller {
@ -37,7 +38,8 @@ impl Controller {
parameters: parameters, parameters: parameters,
target: 0.0, target: 0.0,
last_input: None, last_input: None,
integral: 0.0 integral: 0.0,
last_output: None,
} }
} }
@ -68,25 +70,10 @@ impl Controller {
if output > self.parameters.output_max { if output > self.parameters.output_max {
output = self.parameters.output_max; output = self.parameters.output_max;
} }
self.last_output = Some(output);
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<F: FnOnce(&mut Parameters)>(&mut self, f: F) {
f(&mut self.parameters);
}
#[allow(dead_code)] #[allow(dead_code)]
pub fn reset(&mut self) { pub fn reset(&mut self) {
self.integral = 0.0; self.integral = 0.0;