Modern systems in engineering and software are inherently unstable due to their dynamic nature. An example would be a thermostat inside our homes that maintains the desired temperature during summers or winters. It achieves this by a PID controller that varies the temperature up or down to reach the target temperature. The unstable disturbance here would be the room temperature itself that doesn't want to stay constant. So a PID controller can help stabilize any dynamic system that is inherently unstable or could get unstable under certain circumstances.
The PID controller is a concept in control systems theory. It tries to minimize the error of a system. This error is equal to the difference between the desired state and the current state. So it helps the current state of the system closely match the desired state of the system. The PID controller consists of proportional, integral, and derivative controllers. They are different tuning constants for proportional, derivative, and integral controllers which should all be non-negative. The PID control system looks like this:
Then the control equation turns out to be:
Where e(t) = r(t) - y(t). Here r(t) represents the desired state and y(t) represents the current state.
The gif above shows a sample PID controller to demonstrate how different controller constants are tuned and how it affects the PID controller response for a given step function. A step function is the desired value. In this case, the desired value is 1 (red dashed lines), but in real applications, it can be a complex function. The blue line shows the PID controller response that is trying to match the desired value. The Zeigler-Nichols tuning method is a popular method to tune the PID controller constants. Find out more about the Zeigler-Nichols tuning method in this article.
Then the PID controller can be instantiated like this:
Finally the PID controller should be run in a closed loop to reach a desired value:
In the above sample code, the target temperature for the thermostat is 23ºC that is the desired value. The loop keeps running indefinitely. For each cycle, the temperature value is retrieved from the hardware sensor or an API. Then the current value in the PID controller is updated to calculate the error from the equation e(t) = r(t) - y(t). The controller output value is sent to the thermostat so that it may bring the current temperature to 23ºC. Finally, the room can always be close to 23ºC.