PID Controller in Embedded System
PID Introduction
The model of PID controller
Where u is the output of PID controller and e is the error of reference and feedback. Kp, Ki and Kd are the parameters of controller, which represents propotion, integral and derivative.
Discretization of PID controller
There are many methods to discretize a controller. We deploy the backward discretization method to the PID controller as following equation:
where Ui represent the integral or sum of the error of past. And the controller after discretization as equation (4) is called “Position type PID controller”. As the equation show, the output of controller is relevant with all of the past errors, so it’s not convenient to implement this controller in embedded software.
From integral to increment
To simplifiy the controller’s implementation, we calculate the difference of output of kth step and (k-1)th step as following equation:
According to above equation, we get:
Where:
As equation (14) shows, we got a controller model and the output is only relevant to the output of last step and the error of current step and last 2 steps. Wc can call it “increment type pid controller” We don’t need to calculate the sum of error of all past steps, so it’s simple and costless to impement this controller in our embedded software.
PID controller implementation
Interface
We can define a PID controller module in C:
1 |
|
And we need to implement following operation interface of PID controller:
1 | /** |
We can encapsulate the definition and interface in a header file which may be named “pid_regu.h”. And then we can implement those interface in a source file “pid_regu.c”. Finnally we got a pid contoller module for common use.
May me we can use the PID controller like following:
1 |
|
Go further
We derive the increment type PID controller and implement it in C language. However, many real controllers are based on PID control, but more complicated than simple PID control. There may be more limitations or requirements in different applications. So we can inherit PID controller module to other controllers where the PID controller is the base class of a real controller. Although inheritance can be implemented in C, it’s more simple and natural to use it in C++. So we can implement the PID controller as a base class of real controllers in c++.