| KNOWLEDGE BASE

A rich source of expert knowledge

Learn from experts in the world of embedded systems

PID Controller Implementation in Software - Phil's Lab #6

How to implement a PID controller in software using C, discussing theory and practical considerations. Demonstration of PID controller code using a custom flight simulator.

[NOTE] Something I forgot to mention in the video! 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! I've made the change in the Git repo - before you would have had to use a negative Kd gain to get the same result. Now you can, as normal with derivative-on-error, use a positive Kd gain as usual.

Additional note: The derivative low-pass filter can be controlled by the constant 'tau', which is the time constant of the filter (-3dB frequency in Hz, fc = 1 / (2*pi*tau)). A larger value of tau means the signal is filtered more heavily. As tau approaches zero, the differentiator approaches a 'pure differentiator' with no filtering.
Chapters:
  • 00:00Introduction
  • 00:39Control system basics
  • 02:40PID representation in continuous domain
  • 04:57Converting from the continuous to the discrete domain
  • 06:11PID controller difference equation
  • 07:35Practical considerations
  • 10:48Basic software structure
  • 11:53Implementation in C
  • 18:46Example: Flight simulator using PID controller code