Incremental and positional PID

foreword

When I first came into contact with the PID controller, I was actually very confused about the incremental and positional types, and then there were speed loops, position loops, and current loops... a bunch of blah, blah, blah, blah, but now I understand In fact, it is just using some simple discrete data to calculate expectations.

First assume that we already know what Kp, Ki, and Kd are (if you don’t know, then read my last article about PID).

Algorithms about PID can be divided into two categories, one is positional, and the other is incremental. Let me start to talk about my understanding of these two algorithms.

Positional PID

The first is positional, which is also an algorithm I use most. Position PID is actually the PID control based on the deviation between the actual position of the current system and the position you expect to achieve. So what does it look like in a positional system?

Look at the formula to understand:

U k = K p ∗ e k + K i ∑ j = 0 k e k + K d ( e k − e k − 1 ) U_k= K_p*e_k+K_i\sum^k_{j=0}e_k+K_d(e_k-e_{k-1}) Uk=Kpek+Kij=0kek+Kd(ekek1)

It can be seen from the above formula that the output of each operation of PID is related to the past state, and the error of the integral term will be accumulated. If the deviation is always positive or negative, the position PID will always accumulate in the integral item. When the deviation starts to change in the opposite direction, it will take a while for the position PID to decrease from the maximum value, which will cause the position PID to control the output. lag. So we also need to limit the integral term (make a maximum value max and a minimum value min), and also limit the output. At the same time, we output U k U_kUkCorresponding to the actual position of the actuator, once the control output is wrong, that is to say, there is a problem with the current state value of the object we control, U k U_kUkLarge changes in the system will affect the large changes in the system.

Therefore, if we only use positional PID, we generally use PD control directly. Because of this, positional PID is used for objects without integral parts in the actuator, such as the upright control of the balance car, temperature control The system... wait.

The following is the code implementation:

#define HAVE_PID_INTEGRAL

#define LIMIT(TargetValue, LimitValue) \
if (TargetValue > LimitValue)\
{\
  TargetValue = LimitValue;\
}\
else if (TargetValue < -LimitValue)\
{\
  TargetValue = -LimitValue;\
}\

typedef struct{
  float Kp;
  float Ki;
  float Kd;
#ifdef HAVE_PID_INTEGRAL
  int index;            // 积分分离系数
  float Integral;       // 积分项
  float I_outputMax;    // 积分限幅
#endif
  float Last_Err;       // 上次误差
  float Output;         // PID输出
  float OutputMax;      // 位置式PID输出限幅
}Position_PID;

void PositionPID_Calculate(Position_PID *pid,const float Target,const float Measure)
{

  if(pid == NULL)
    return;
    
  float Err;
  
  Err = Target - Measure;
  
  pid->Output = pid->Kp * Err + pid->Kd * (Err - pid->Last_Err)
  
#ifdef HAVE_PID_INTEGRAL
    /* 积分分离 */
  if(abs(pid->Err) > Integraldead_zone)
  {
    pid->index=0;
  }else
  {
    pid->index = 1;
  }
  pid->Integral += pid->Ki * Err * pid->index;
  
  LIMIT(pid->Integral,I_outputMax);
  
  pid->Output += pid->Integral;
#endif
  
  LIMIT(Output,OutputMax);
  
  pid->Last_Err = Err;
  
}

advantage:

The position formula is a non-recursive algorithm, which can directly control the object. The value of U(k) corresponds to the actual equivalent of the object, so it can be well applied to the control object without integral components.

shortcoming:

Each output is related to the previous state, and the error value err must be accumulated, which requires a large amount of calculation.

Incremental PID

So what is an incremental PID? The output of the incremental PID is only the increment of the control quantity Δ U k \Delta U_kU _k. When the control amount required by the actuator is incremental, then we can use the incremental PID control algorithm for control. (The calculation output of incremental PID is incremental, not directly acting on the actuator)

(Incremental PID can be derived from the positional method. If you are interested, you can go to Baidu. I won’t waste space here.)

Look at the formula to understand:

Δ U k = K p ( e ( k ) − e ( k − 1 ) ) + K ie ( k ) + KD [ e ( k ) − 2 e ( k − 1 ) + e ( k − 2 ) ] \Delta U_k=K_p(e(k)-e(k-1))+K_i{e(k)}+K_D[e(k)-2e(k-1)+e(k-2)]U _k=Kp(e(k)e(k1))+Kie(k)+KD[e(k)2e ( k _1)+e(k2)]

For incremental PID, given an input quantity, the deviation between the quantity fed back by the system and the set quantity is Err, and the last deviation Last_Err and the previous deviation Previous_Err are saved in the system. These three input quantities Through the incremental PID, the above-mentioned control quantity increment Δ U k \Delta U_k can be calculatedU _k. And the obtained control quantity Δ U ( k ) \Delta U(k)Δ U ( k ) corresponds to the increment of the recent position error, rather than the deviation from the actual position, that is to say, there is no error accumulation. That is, the control amount needs to be increased on the basis of the last control amount.

The following is the code implementation:

#define LIMIT(TargetValue, LimitValue) \
if (TargetValue > LimitValue)\
{\
  TargetValue = LimitValue;\
}\
else if (TargetValue < -LimitValue)\
{\
  TargetValue = -LimitValue;\
}\

#define Integraldead_zone 100 // 积分死区 根据自己的需求定义
typedef struct{
  float Kp;
  float Ki;
  float Kd;
  float p_out;
  float i_out;
  float d_out;
  float Err;
  float Last_Err;       // 上次误差
  float Previous_Err;   // 上上次误差
  float Output;
  float OutputMax;      // 增量式式PID输出限幅
}Incremental_PID;

void IncrementalPID_Calculate(Incremental_PID *pid,const float Target,const float Measure)
{ 
  if(pid == NULL)
    return;
    
  pid->Err = Target - Measure;
  
  pid->p_out = pid->Kp * (Err - Last_Err);
  pid->i_out = pid->Ki * Err;
  pid->d_out = pid->Kd * (Err - 2.0f*Last_Err + Previous_Err);
  
  pid->Output += p_out + i_out + d_out; 
  
  LIMIT(pid->Output, pid->OutputMax); // 限幅
  
  pid->Previous_Err = pid->Last_Err;
  pid->Last_Err = Err;
  
}

advantage:

  1. The system has little influence in case of misoperation, and logical judgment can be used to remove erroneous data.
  2. Small impulse, easy to achieve non-disturbance switching.
  3. There is no need to accumulate error values, and the determination of the control increment is only related to the last few sampling values.

shortcoming:

  1. The stage effect of the integral is large, and there is a steady-state error.
  2. The impact of overflow is great, and it is not good to use incremental method for some controlled objects.

Summary (difference between incremental and positional)

  1. Incremental algorithm does not need to do accumulation, incremental PID calculates the increment required by the system, and the determination of increment is only related to the last few deviation sampling values, and the calculation error has little influence on the calculation of control quantity. Compared with the positional type, the positional type needs to use the cumulative value of the deviation (Ki*err), which is prone to cumulative errors.
  2. The output of incremental PID control is the increment of the control quantity, and there is no integral function, so this method is suitable for objects with integral components. The position type PID is suitable for the object that the actuator does not have an integral part.
  3. The above code also clearly shows that the positional PID needs integral limiter and output limiter, while the incremental PID only needs output limiter.

Guess you like

Origin blog.csdn.net/dbqwcl/article/details/125736272