PID controller algorithm - Simulink simulation

PID control algorithm

overview

Closed-loop control system block diagram
The figure above is a block diagram of a closed-loop control system:

Suppose it is to debug the speed of a motor, r(t) in the above figure is the target speed, y(t) is the speed output, e(t) is the speed error, u(t) is the output value sent to the motor after PID calculation , the controlled object is a motor, assuming that the PID controller is C ( s ) = U ( s ) E ( s ) C(s)=\frac{U(s)}{E(s)}C(s)=E(s)U(s), the transfer function is G ( s ) = Y ( s ) U ( s ) G(s)=\frac{Y(s)}{U(s)}G(s)=U(s)and ( s ), the detection device is H(s), which is the feedback function.

Then the closed-loop transfer function of the system is ϕ ( s ) = Y ( s ) R ( s ) = C ( s ) G ( s ) 1 + C ( s ) G ( s ) H ( s ) \phi(s)= \frac{Y(s)}{R(s)}=\frac{C(s)G(s)}{1+C(s)G(s)H(s)}ϕ ( s )=R(s)and ( s )=1+C(s)G(s)H(s)C(s)G(s)

PID transfer function form: G ( s ) = U ( s ) E ( s ) = K s [ 1 + 1 T is + T ds ] G(s)=\frac{U(s)}{E(s)} =K_s[1+\frac{1}{T_is}+T_ds]G(s)=E(s)U(s)=Ks[1+Tis1+Tds]
PID control example
PID的微分方程形式: u ( t ) = K c [ e ( t ) + 1 T i ∫ 0 t e ( t ) d t + T d d e ( t ) d t ] + u 0 u(t)=K_c[e(t)+\frac{1}{T_i}\int_0^te(t)dt+T_d\frac{de(t)}{dt}]+u_0 u(t)=Kc[e(t)+Ti10te(t)dt+Tddtd e ( t )]+u0

式中: K c 、 T i 、 T d K_c、T_i、T_d KcTiTdThey are the proportional gain, integral time and differential time of the analog regulator respectively , and u0 is the regulator output when the deviation e=0, also known as the steady-state operating point.

也可以写成 u ( t ) = [ K p e ( t ) + K i ∫ 0 t e ( t ) d t + K d d e ( t ) d t ] + u 0 u(t)=[K_pe(t)+K_i\int_0^te(t)dt+K_d\frac{de(t)}{dt}]+u_0 u(t)=[Kpe(t)+Ki0te(t)dt+Kddtd e ( t )]+u0

Among them:
proportional coefficient K p = K c K_p=K_cKp=Kc

Integral coefficient K i = K c T i K_i=\frac{K_c}{T_i}Ki=TiKc

Differential coefficient K d = K c T d K_d=\frac{K_c}{T_d}Kd=TdKc

error e(t)

For why we need PID

As for why closed-loop control is needed, because in practical applications we may need to require the dimension to remain constant within a certain period of time, so a system must continuously receive feedback from the output to control the size of the input. Take controlling a temperature as an example, a heating rod needs to be kept at 30 degrees Celsius, but due to the weather, there may be no way to naturally heat it to 30 degrees Celsius, so when a temperature control system receives a temperature that does not reach the target temperature, it will heat up. Increase its own power to heat, so that the heating rod reaches the target temperature. On the contrary, if the heating is too high, the system will automatically reduce its own input after receiving the feedback, so as to keep the temperature constant.
Closed Loop System Example
So we
generally
subtract the feedback (measured output) of the controlled object from our expected reference input , then we can get an error error value , after getting this error value, we can use the error as an input to the PID control Controller, the final output result of the controller control input is directly passed to the controlled object Plant

Analysis of K p K_pKpThe role of (with differential regulation)

Proportional term: K p × err ( t ) K_p×err(t)Kp×err(t)

  • Respond to the deviation e(t) instantaneously , only related to the current deviation, and generate the corresponding control value u(t).

  • The strength of the control action depends on the proportional coefficient K p K_pKp

    That is to say K p K_pKpThe larger the value , the stronger the effect , the faster the process and the smaller the static deviation.

  • But K p K_pKpThe larger the value is, the easier it is to oscillate , increasing the overshoot of the system and degrading the stability of the system.

Summary: If you blindly increase the proportional item, although the static error will decrease, the overshoot will also increase a lot. So we can see a contradictory problem encountered by pure proportional control. If the coefficient is too small, there will be static error, and if the coefficient is too large, there will be overshoot . Therefore, in order to solve this problem, we need to increase the order of the system and introduce an integral term .

SIMulator simulation diagram

K i 、 K d K_i、K_dKiKdWhen both are 0, K p K_pKpThe magnitude of affects the shape of the output curve.

Shallow analysis K i K_iKithe role of

其实 K i = K c T i K_i=\frac{K_c}{T_i} Ki=TiKc, integral term: K i × ∫ err ( t ) dt K_i×\int err(t)dtKi×err(t)dt

  • As long as the deviation e(t) exists, the integral action will continue to increase (the premise is that the controller is not saturated), and the deviation e(t) will continue to decrease. When the deviation e(t)=0, the integral control action will stop.
  • Integral control will reduce the response speed of the system .
  • If the integral action is too strong, the overshoot of the system will be increased, and the stability of the system will deteriorate.

Summary: Although the integral term can eliminate the static error, it will contribute to the influence of overshoot to a certain extent, making the time for the system to stabilize longer.

SIMulator simulation diagram

When K p = 1, K d = 0 K_p=1, K_d=0Kp=1Kd=0 o'clock,K i K_iKiThe magnitude of affects the shape of the output curve.

Analysis of K d K_dKdthe role of

In fact, K d = K c T i K_d={K_c}{T_i}Kd=KcTi, the differential term: K d × derr ( t ) dt K_d×\frac{derr(t)}{dt}Kd×dtderr(t)

Differentiation is derivation, and derivation reflects the slope and rate of change, which is a trend . The greater the differential, the stronger the effect of the differential.

  • The differential link can give a corrective action in advance according to the change trend (change speed) of the deviation e(t), and can correct it before the deviation becomes large.
  • It helps to reduce the overshoot, overcome the shock, and make the system more stable . It speeds up the tracking speed of the system and reduces the adjustment time.
  • However, the differential action is very sensitive to the noise of the input signal, and generally does not need to be differentiated for those systems with large noise, or the input signal is filtered before the differential .

Summary: Whenever there is a sharp change in err(t)_ , a large derivative term is generated to counteract the sharp change. This just corresponds to our overshoot phenomenon , because the overshoot is just a sharp rise and then a sharp drop , and the differential term is to offset it.
SIMulator simulation diagram

K p = 1 、 K i = 1 K_p=1、K_i=1 Kp=1Ki=1 ,K d K_dKdThe magnitude of affects the shape of the output curve. (Kd=1 works well)

Convert continuous function to discrete function

What I mentioned above is actually an operation in the time domain. The inside of the microcontroller is a digital system, and the digital system must be discrete. Therefore, we need to convert continuous PID into discrete PID. Converting integral to discrete is actually summation and differentiation. Converting to discrete is just difference , namely:
U ( n ) = K p ∗ err ( n ) + K p TT i ∗ ∑ err ( n ) + K p T d T ∗ [ err ( n ) − err ( n − 1 ) ] U(n)=K_p*err(n)+\frac{K_pT}{T_i}*\sum err(n)+\frac{K_pT_d}{T}*[err(n)-err(n-1 )]U(n)=Kpe r r ( n )+TiKpTe r r ( n )+TKpTd[ e r r ( n )e r r ( n1 ) ]
But since T is a periodic constant, the formula can also be transformed into:
U ( n ) = K p ∗ err ( n ) + K i ∗ ∑ err ( n ) + K d ∗ [ err ( n ) − err ( n − 1 ) ] U(n)=K_p*err(n)+K_i*\sum err(n)+K_d*[err(n)-err(n-1)]U(n)=Kpe r r ( n )+Kie r r ( n )+Kd[ e r r ( n )e r r ( n1)]

Require:

  1. The PID operation must be completed within the periodic task (which can be understood as a timer interrupt).

  2. The sampling period and control period affect the coefficients.

Generally speaking, increasing the control period and sampling period can make our control smoother. The control period is the period of executing the control code. Generally, it is the period of the timer for placing the operation PID code. The sampling period is the period of obtaining sensor data. For example, the sampling cycle of the motor is the cycle of the CAN receiving interrupt.

Combined with official open source code

This is the pid_calculate function under pid.c, and the meaning can be easily understood by combining the formula. set is expectation, get is feedback, and the error err is obtained by subtracting the two, pid→pout is the proportional item , and err is directly amplified linearly, pid→iout is the integral item , and err is accumulated, pid→dout is the differential item , and err Make the difference. Finally, the three are superimposed to obtain the output of the pid controller.

/**
  * @brief     calculate delta PID and position PID
  * @param[in] pid: control pid struct
  * @param[in] get: measure feedback value
  * @param[in] set: target value
  * @retval    pid calculate output 
  */
float pid_calculate(struct pid *pid, float get, float set)
{
    
    
  pid->get = get;
  pid->set = set;
  pid->err = set - get;
  if ((pid->param.input_max_err != 0) && (fabs(pid->err) > pid->param.input_max_err))
    return 0;

  pid->pout = pid->param.p * pid->err;
  pid->iout += pid->param.i * pid->err;
  pid->dout = pid->param.d * (pid->err - pid->last_err);

  abs_limit(&(pid->iout), pid->param.inte_limit);
  pid->out = pid->pout + pid->iout + pid->dout;
  abs_limit(&(pid->out), pid->param.max_out);

  return pid->out;
}

Simulator simulation diagram

Simulator simulation diagram

For system identification and controller design based on the results of system identification, you can refer to the official open source tutorial:

https://bbs.robomaster.com/thread-4941-1-1.html

https://bbs.robomaster.com/thread-5059-1-1.html

Understand PID control in essence:

The principle of PID control algorithm (abandon the formula and truly understand PID control in essence) - Know (zhihu.com)

Guess you like

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