Files
PID/PID.h
Philip Salmony 98ba591357 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.
2025-05-23 12:36:19 +07:00

44 lines
926 B
C

#ifndef PID_CONTROLLER_H
#define PID_CONTROLLER_H
typedef struct {
/* Controller gains */
float Kp;
float Ki;
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;
/* Output limits */
float limMin;
float limMax;
/* Integrator limits */
float limMinInt;
float limMaxInt;
/* Sample time (in seconds) */
float T;
/* pid coefficient offset for start pid */
float offset;
/* Controller "memory" */
float integrator;
float prevError; /* Required for integrator */
float proportional;
float differentiator;
float prevMeasurement; /* Required for differentiator */
/* Controller output */
float out;
} PIDController;
void PIDController_Init(PIDController *pid);
float PIDController_Update(PIDController *pid, float setpoint, float measurement);
#endif