Article From:https://www.cnblogs.com/foxclever/p/9127104.html

From the basic principle of PID control, we know that the introduction of differential signals can improve the dynamic characteristics of the system, but there is also a problem, that is, it is easy to introduce high frequency interference and especially show the deficiency of differential term when the deviation disturbance is abrupt. In order to solve this problem, low-pass filtering is introduced to solve this problem.

1、The basic idea of incomplete differential

The differential term has the risk of introducing high frequency interference, but if the low-pass filter is added to the control algorithm, the performance of the system can be improved. One way is to add a first order low pass filter to the PID algorithm. This is called incomplete differential, and its structure is as follows:

Or in another form:

Here we consider the first form of structure. In this case, the differential formula can be expressed as follows:

Among them, the value of alpha is between 0 and 1, and the filtering constant and sampling period are determined. Based on this, we can get incremental formula for incomplete differential by increasing it.

Or expressed as:

These two expressions are equivalent, and the second representations are closer to our incremental differential PID algorithm, which is easy to understand. It is also more consistent with positional representation, so we choose second representations.

2、Algorithm implementation

After previous analysis and formula deduction, it is quite easy for us to achieve incomplete differentiation. To facilitate understanding, we keep the ratio and integral as the basic format, and only use incomplete differential algorithm to the differential part.

1）Positional typePIDAlgorithm implementation

The location PID implementation process is different from the previous one, and the difference is the need to increase the previous Ud value. First, the structure of the PID object is defined:

1 /*Definition of structure and public*/
2
3 typedef struct
4
5 {
6
7   float setpoint;       //Set value
8
9   float proportiongain;     //Ratio coefficient
10
11   float integralgain;      //Integral coefficient
12
13   float derivativegain;    //Differential coefficient
14
15   float lasterror;     //First beat deviation
16
17   float lastdev;      //The value of the differential term at the first beat
18
19   float alpha;       //Incomplete differential coefficient
20
21   float result; //Output value
22
23   float integral;//Integral value
24
25 }PID;

Next, the PID controller is implemented.

1 void PIDRegulation(PID *vPID, float processValue)
2
3 {
4
5   float thisError;
6
7   float thisDev;
8
9   thisError=vPID->setpoint-processValue;
10
11   vPID->integral+=thisError;
12
13   thisDev= vPID->derivativegain*(1- vPID-> alpha)*(thisError-vPID->lasterror)+ vPID-> alpha* vPID-> lastdev;
14
15
16
17   vPID->result=vPID->proportiongain*thisError+vPID->integralgain*vPID->integral+ thisDev;
18
19   vPID->lasterror=thisError;
20
21   vPID->lastdev = thisDev;
22
23 }

This has realized a position type PID controller with the simplest incomplete differential. Of course, it does not consider any interference conditions, only the computer language of the mathematical formula.

2）Incremental typePIDAlgorithm implementation

The realization of incremental PID controller is based on the incremental formula. First, the structure of the PID object is defined:

1 /*Definition of structure and public*/
2
3 typedef struct
4
5 {
6
7   float setpoint;       //Set value
8
9   float proportiongain;     //Ratio coefficient
10
11   float integralgain;      //Integral coefficient
12
13   float derivativegain;    //Differential coefficient
14
15   float lasterror;     //First beat deviation
16
17   float preerror;     //First two beat deviations
18
19   float lastdeltadev;      //The increment of the differential term at the first beat
20
21   float alpha;       //Incomplete differential coefficient
22
24
25   float result; //Output value
26
27 }PID;

Next, the PID controller is implemented.

1 void PIDRegulation(PID *vPID, float processValue)
2
3 {
4
5   float thisError;
6
7   float increment;
8
10
11   float pError,dError,iError;
12
13
14
15   thisError=vPID->setpoint-processValue; //Get the deviation value
16
17   pError=thisError-vPID->lasterror;
18
19   iError=thisError;
20
21   dError=thisError-2*(vPID->lasterror)+vPID->preerror;
22
23
24
26
28
29
30
31   vPID->preerror=vPID->lasterror;  //The storage deviation is used for the next operation
32
33   vPID->lasterror=thisError;
34