From 98ba59135797356a3482dd8d79a489911be8305e Mon Sep 17 00:00:00 2001 From: Philip Salmony <34550389+pms67@users.noreply.github.com> Date: Mon, 25 May 2020 00:47:58 +0200 Subject: [PATCH] Added note on derivative-on-measurement. If you want to use derivative-on-measurement, you will need to negate the 'D'-gain you would typically use in a 'standard derivative-on-error' PID controller - I did not mention that in the video. Since the 'error signal' effectively going into the differentiator does not depend on the setpoint: e[n] = 0 - measurement, and therefore (e[n] - e[n - 1]) = (0 - measurement) - (0 - prevMeasurement) = -Kd * (measurement - prevMeasurement). So, for example, if you require a controller with a D-gain of 10, you would set pid->Kd = -10. This is a slight quirk of the derivative-on-measurement form - however, the code can of course be adapted to include the minus sign. --- PID.h | 2 +- PID_Test.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/PID.h b/PID.h index bf6b885..4ecfe64 100644 --- a/PID.h +++ b/PID.h @@ -6,7 +6,7 @@ typedef struct { /* Controller gains */ float Kp; float Ki; - float Kd; + float Kd; /* Note: since using derivative-on-measurement, Kd needs to be negative (in contrast to conventional 'derivative-on'error') */ /* Derivative low-pass filter time constant */ float tau; diff --git a/PID_Test.c b/PID_Test.c index 5d20da9..042b8d8 100644 --- a/PID_Test.c +++ b/PID_Test.c @@ -24,7 +24,7 @@ float TestSystem_Update(float inp); int main() { /* Initialise PID controller */ - PIDController pid = { PID_KP, PID_KI, PID_KD, + PIDController pid = { PID_KP, PID_KI, -PID_KD, PID_TAU, PID_LIM_MIN, PID_LIM_MAX, SAMPLE_TIME_S };