forked from M-Labs/thermostat
simplify pid, keep last_output
This commit is contained in:
parent
e5b4789304
commit
163bb38f68
27
src/main.rs
27
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, "");
|
||||
}
|
||||
}
|
||||
|
27
src/pid.rs
27
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<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;
|
||||
|
Loading…
Reference in New Issue
Block a user