├── .gitattributes ├── PID Controller Implementation in Software.pdf ├── README.md ├── PID.h ├── LICENSE ├── PID_Test.c └── PID.c /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /PID Controller Implementation in Software.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pms67/PID/HEAD/PID Controller Implementation in Software.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PID 2 | PID controller implementation written in C. 3 | 4 | Note on 'derivative-on-measurement': 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). (Note the minus sign compared to derivative-on-error!) 5 | I've included the minus sign in the code, so gains will have the effect as normal. 6 | -------------------------------------------------------------------------------- /PID.h: -------------------------------------------------------------------------------- 1 | #ifndef PID_CONTROLLER_H 2 | #define PID_CONTROLLER_H 3 | 4 | typedef struct { 5 | 6 | /* Controller gains */ 7 | float Kp; 8 | float Ki; 9 | float Kd; 10 | 11 | /* Derivative low-pass filter time constant */ 12 | float tau; 13 | 14 | /* Output limits */ 15 | float limMin; 16 | float limMax; 17 | 18 | /* Integrator limits */ 19 | float limMinInt; 20 | float limMaxInt; 21 | 22 | /* Sample time (in seconds) */ 23 | float T; 24 | 25 | /* Controller "memory" */ 26 | float integrator; 27 | float prevError; /* Required for integrator */ 28 | float differentiator; 29 | float prevMeasurement; /* Required for differentiator */ 30 | 31 | /* Controller output */ 32 | float out; 33 | 34 | } PIDController; 35 | 36 | void PIDController_Init(PIDController *pid); 37 | float PIDController_Update(PIDController *pid, float setpoint, float measurement); 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Philip Salmony 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /PID_Test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "PID.h" 5 | 6 | /* Controller parameters */ 7 | #define PID_KP 2.0f 8 | #define PID_KI 0.5f 9 | #define PID_KD 0.25f 10 | 11 | #define PID_TAU 0.02f 12 | 13 | #define PID_LIM_MIN -10.0f 14 | #define PID_LIM_MAX 10.0f 15 | 16 | #define PID_LIM_MIN_INT -5.0f 17 | #define PID_LIM_MAX_INT 5.0f 18 | 19 | #define SAMPLE_TIME_S 0.01f 20 | 21 | /* Maximum run-time of simulation */ 22 | #define SIMULATION_TIME_MAX 4.0f 23 | 24 | /* Simulated dynamical system (first order) */ 25 | float TestSystem_Update(float inp); 26 | 27 | int main() 28 | { 29 | /* Initialise PID controller */ 30 | PIDController pid = { PID_KP, PID_KI, PID_KD, 31 | PID_TAU, 32 | PID_LIM_MIN, PID_LIM_MAX, 33 | PID_LIM_MIN_INT, PID_LIM_MAX_INT, 34 | SAMPLE_TIME_S }; 35 | 36 | PIDController_Init(&pid); 37 | 38 | /* Simulate response using test system */ 39 | float setpoint = 1.0f; 40 | 41 | printf("Time (s)\tSystem Output\tControllerOutput\r\n"); 42 | for (float t = 0.0f; t <= SIMULATION_TIME_MAX; t += SAMPLE_TIME_S) { 43 | 44 | /* Get measurement from system */ 45 | float measurement = TestSystem_Update(pid.out); 46 | 47 | /* Compute new control signal */ 48 | PIDController_Update(&pid, setpoint, measurement); 49 | 50 | printf("%f\t%f\t%f\r\n", t, measurement, pid.out); 51 | 52 | } 53 | 54 | return 0; 55 | } 56 | 57 | float TestSystem_Update(float inp) { 58 | 59 | static float output = 0.0f; 60 | static const float alpha = 0.02f; 61 | 62 | output = (SAMPLE_TIME_S * inp + output) / (1.0f + alpha * SAMPLE_TIME_S); 63 | 64 | return output; 65 | } 66 | -------------------------------------------------------------------------------- /PID.c: -------------------------------------------------------------------------------- 1 | #include "PID.h" 2 | 3 | void PIDController_Init(PIDController *pid) { 4 | 5 | /* Clear controller variables */ 6 | pid->integrator = 0.0f; 7 | pid->prevError = 0.0f; 8 | 9 | pid->differentiator = 0.0f; 10 | pid->prevMeasurement = 0.0f; 11 | 12 | pid->out = 0.0f; 13 | 14 | } 15 | 16 | float PIDController_Update(PIDController *pid, float setpoint, float measurement) { 17 | 18 | /* 19 | * Error signal 20 | */ 21 | float error = setpoint - measurement; 22 | 23 | 24 | /* 25 | * Proportional 26 | */ 27 | float proportional = pid->Kp * error; 28 | 29 | 30 | /* 31 | * Integral 32 | */ 33 | pid->integrator = pid->integrator + 0.5f * pid->Ki * pid->T * (error + pid->prevError); 34 | 35 | /* Anti-wind-up via integrator clamping */ 36 | if (pid->integrator > pid->limMaxInt) { 37 | 38 | pid->integrator = pid->limMaxInt; 39 | 40 | } else if (pid->integrator < pid->limMinInt) { 41 | 42 | pid->integrator = pid->limMinInt; 43 | 44 | } 45 | 46 | 47 | /* 48 | * Derivative (band-limited differentiator) 49 | */ 50 | 51 | pid->differentiator = -(2.0f * pid->Kd * (measurement - pid->prevMeasurement) /* Note: derivative on measurement, therefore minus sign in front of equation! */ 52 | + (2.0f * pid->tau - pid->T) * pid->differentiator) 53 | / (2.0f * pid->tau + pid->T); 54 | 55 | 56 | /* 57 | * Compute output and apply limits 58 | */ 59 | pid->out = proportional + pid->integrator + pid->differentiator; 60 | 61 | if (pid->out > pid->limMax) { 62 | 63 | pid->out = pid->limMax; 64 | 65 | } else if (pid->out < pid->limMin) { 66 | 67 | pid->out = pid->limMin; 68 | 69 | } 70 | 71 | /* Store error and measurement for later use */ 72 | pid->prevError = error; 73 | pid->prevMeasurement = measurement; 74 | 75 | /* Return controller output */ 76 | return pid->out; 77 | 78 | } 79 | --------------------------------------------------------------------------------