From d574ccb5f4a66ac9f10106b6f4ff8780bef5f500 Mon Sep 17 00:00:00 2001 From: Astro Date: Sun, 11 Oct 2020 23:12:18 +0200 Subject: [PATCH] pid: change signedness from heating to cooling --- src/pid.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/pid.rs b/src/pid.rs index f25acb9..c955b3d 100644 --- a/src/pid.rs +++ b/src/pid.rs @@ -47,7 +47,7 @@ impl Controller { pub fn update(&mut self, input: f64) -> f64 { // error - let error = self.target - input; + let error = input - self.target; // partial let p = f64::from(self.parameters.kp) * error; @@ -65,7 +65,7 @@ impl Controller { // derivative let d = match self.last_input { None => 0.0, - Some(last_input) => f64::from(self.parameters.kd) * (last_input - input), + Some(last_input) => f64::from(self.parameters.kd) * (input - last_input), }; self.last_input = Some(input); @@ -129,7 +129,7 @@ mod test { #[test] fn test_controller() { const DEFAULT: f64 = 0.0; - const TARGET: f64 = 1234.56; + const TARGET: f64 = -1234.56; const ERROR: f64 = 0.01; const DELAY: usize = 10; @@ -144,8 +144,8 @@ mod test { let next_t = (t + 1) % DELAY; // Feed the oldest temperature let output = pid.update(values[next_t]); - // Overwrite oldest with previous temperature + output - values[next_t] = values[t] + output; + // Overwrite oldest with previous temperature - output + values[next_t] = values[t] - output; t = next_t; total_t += 1; }