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() {
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, "");
}
}

View File

@ -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<f64>
last_input: Option<f64>,
pub last_output: Option<f64>,
}
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<F: FnOnce(&mut Parameters)>(&mut self, f: F) {
f(&mut self.parameters);
}
#[allow(dead_code)]
pub fn reset(&mut self) {
self.integral = 0.0;