# Learning notes and understanding of PID algorithm

Posted by harshilshah on Wed, 02 Feb 2022 18:56:02 +0100

### PID algorithm type

There are two forms of PID algorithm in engineering

First define a structure

```typedef struct PID
{
float  Proportion;//P
float  Integral;  //I
float  Derivative;//D

float  PrevError;//Last error
float  LastError;//Last error
float  Error;	 //Current error
float  DError;
float  SumError; //Cumulative sum of historical errors

float  Integralmax;
float  output;	   //output
float  outputmax;
float  errormax;
u8 first_flag;
}PID;
```
• Positional type

```void PID_position_PID_calculation(PID *pp, float CurrentPoint, float NextPoint)
{
if(pp->first_flag == 1)
{
pp->LastError = NextPoint - CurrentPoint;
pp->PrevError = NextPoint - CurrentPoint;
pp->first_flag = 0;
}
pp->Error =  NextPoint -  CurrentPoint;
pp->SumError += pp->Error;
pp->DError = pp->Error - pp->LastError;

pp->output =  pp->Proportion * pp->Error + PID_abs_limit(pp->Integral * pp->SumError, pp->Integralmax ) + pp->Derivative * pp->DError ;

//Output limiting
if(pp->output > pp->outputmax )  pp->output = pp->outputmax;
if(pp->output < - pp->outputmax )  pp->output = -pp->outputmax;
pp->LastError = pp->Error;

{
pp->output = 0;
}
}
```
• Incremental

```void PID_incremental_PID_calculation(PID *pp, float CurrentPoint, float NextPoint)
{
//Error calculation
pp->Error =  NextPoint - CurrentPoint;
pp->DError = pp->Error - pp->LastError;

//PID algorithm
pp->output +=  pp->Proportion * (pp->DError)+ PID_abs_limit(pp->Integral * pp->Error, pp->Integralmax ) + pp->Derivative * ( pp->Error +  pp->PrevError - 2*pp->LastError);

//Output limiting
if(pp->output > pp->outputmax )  pp->output = pp->outputmax;
if(pp->output < - pp->outputmax )  pp->output = -pp->outputmax;
pp->PrevError = pp->LastError;
pp->LastError = pp->Error;

{
pp->output = 0;
}
}
```

### The difference between incremental and positional

• The incremental algorithm does not need to accumulate, and the determination of the increment of the control quantity is only related to the recent deviation sampling values, and the calculation error has little influence on the calculation of the control quantity;

The positional algorithm uses the cumulative value of past deviation, which is easy to produce large cumulative error.

• The incremental algorithm obtains the increment of control quantity. For example, in valve control, only the change part of valve opening is output, and the impact of misoperation is small. If necessary, this output can be limited or prohibited through logical judgment, which will not seriously affect the work of the system;

The output of position type directly corresponds to the output of the object, so it has a great impact on the system.

• The output of incremental PID control is the increment of control quantity, which has no integral effect. Therefore, this method is suitable for objects with integral parts of actuator, such as stepping motor, etc;

The position PID is suitable for objects with actuator without integral parts, such as electro-hydraulic servo valve.

• In PID control, position PID needs integral limiting and output limiting;

The incremental PID only needs to output amplitude limiting.

• Advantages: position PID is a non recursive algorithm, which can directly control the actuator (such as the balance trolley). The value of u(k) corresponds to the actual position of the actuator (such as the current angle of the trolley), so it can be well used in the object without integral parts of the actuator.
• Disadvantages: each output is related to the past state, e(k) should be accumulated during calculation, and the calculation workload is large.

• The impact of misoperation is small, and the wrong data can be removed by logical judgment when necessary
• The impact is small during manual / automatic switching, which is convenient for undisturbed switching. When the computer fails, it can still maintain the original value.
• There is no need to accumulate in the formula. Control increment Δ The determination of u(k) is only related to the last three sampling values.