PID control algorithm notes arrangement 2: PID algorithm principle analysis, mathematical modeling

Thanks to the Internet gods for providing various learning materials

1. Purpose and basic idea of ​​control algorithm

1.1. Purpose of control

The fundamental purpose of control is to make the current state value of the control object the same as the user's set value (as close as possible).

1.2. Basic idea

Both the user's set value SV and the current value PV of the controlled object are simultaneously sent to the control algorithm logic composed of a specific hardware circuit model or specific algorithm software, and different control algorithms are used to analyze, judge and process SV and PV , so as to generate the control signal OUT that should be output at present, and the control signal is applied to the control object through the actuator, so as to produce the expected control effect.

2. Commonly used control algorithm: bit control

2.1. Two-position control algorithm

Features:

a. The output of the two-position control algorithm has only two states: high and low.

b. The executive agency makes the control object either not work at full capacity, or stop working. When PV is lower than SV, it will work at full capacity, and when PV is greater than or equal to SV, it will stop working completely. If the control object is a 1000W heater, it will run at full power of 1000W when the temperature is less than 1000W, and stop working when the temperature reaches.

c. Due to factors such as environmental factors or control system transmission delay or the inertia of the control object itself, the control effect is often that the PV has a large fluctuation in the SV.

d. When PV is close to the critical point of SV, the control output signal OUT often switches between H and L frequently, resulting in frequent switching of the output point of the actuator, which is prone to interference and shortens the life of the actuator.

2.2. Two-position control algorithm with hysteresis

Features:

a. Take the plus or minus 10% of SV as the upper and lower limits of hysteresis adjustment, output L when it is higher than the upper limit, and output H when it is lower than the lower limit;

b. Avoid the frequent action of the general two-position control algorithm at the critical point;

c. Because the control object has only two states of full operation or non-operation, there are still shortcomings of the general two-position control algorithm: PV always fluctuates around SV.

2.3. Three-position control algorithm

Features: On the basis of two-position control, the power of the control object is divided into three situations (ie, three positions) of zero power (stop working), half power and full power.

When the current value is lower than the set value by a certain percentage (generally %10), OUT1 and OUT2 play a controlling role at the same time, and the controlled object runs at full power.

When the current value is within plus or minus 10% of the set value, OUT1 acts alone and works in a half-power state.

When the current value reaches or exceeds the set value, both OUT1 and OUT2 stop outputting, and the control object stops working.

Compared with the general two-position control algorithm, the three-position control algorithm simply analyzes the current state value of the control object, and outputs different control signals according to different current state values. It can better control the output.

The main features of bit control:

@1. The control algorithm only focuses on controlling the difference between the current state value (PV) and the set value --- output a control signal if there is a difference between the two, and will not output a control signal if there is no difference between the two.

@2. The output signal state of the bit-type control algorithm is single, and only two states of high and low are output. These two states correspond to the working or non-working of the control object---if it is a temperature control system, it is the heater heating or not heating .

@3. Due to the delay of the control system itself or the inertia of the control object itself, the positional control algorithm can only make the current state value of the control object fluctuate around the set value, and cannot track well around the set value or even equal.

3. PID control algorithm

3.1 Basic idea of ​​PID control algorithm

PID algorithm is a predictive control algorithm, its core idea is:

@1. The PID algorithm not only considers the current state value (current state) of the control object, but also considers the state value (historical state) of the control object in the past period of time and the state value change (expected) in the most recent period. These three aspects are common Determine the current output control signal:

@2. The operation result of the PID control algorithm is a number, which is used to control the controlled object to work in various working states (such as various powers of heaters, various openings of valves, etc.), and the general output form is PWM , which basically meets the purpose of outputting control signals on demand and changing the output at any time according to the situation.

3.2. PID algorithm analysis

Assume a certain control system: the user set value is SV (that is, it is hoped that the state value of the controlled object will be kept near SV through the PID control algorithm).

3.2.1. Proportional item analysis (P item)

@1. Since the system is put into operation, the control algorithm samples the state value of the control object at regular intervals. Thus, the data sequence formed by the state value of the controlled object at each sampling time point since the startup can be obtained:

X1, X2, X3, X4, .... Xk-2, Xk-1, Xk

X1: The first sampling value since power on

Xk: the current sampling value (the latest sampling value)

@2. Extract three aspects of information from this sequence of sampled values:

<1> The difference between the current sampled value Xk and the user's set value SV: Ek

Ek = SV – Xk

AnalysisEk:

Ek>0: Indicates that the current state value is not up to standard

Ek=0: Indicates that the current control state just meets the requirements

Ek>0: Indicates that the current state value has exceeded the standard

Conclusion: Ek reflects the degree of deviation between the current value of the control object and the set value, and the output signal OUT can be adjusted according to the size of Ek: the degree of deviation is large, OUT increases, and the degree of deviation is small, OUT decreases. That is, the strength of the output signal is proportional to the magnitude of the current deviation, so the algorithm that gives the current value of the control signal OUT according to the magnitude of Ek is called proportional control (proportion). The mathematical model can be expressed as:

POUT=(Kp*I) + Out0

Kp: It is generally called a proportional coefficient, which can be understood as an amplifier (or attenuator) on the hardware. Proper selection of Kp will amplify or reduce the current error value Ek according to a certain gain, so as to improve the corresponding speed of the control algorithm.

Out0: It is a constant, the purpose is to ensure that the output signal is not 0 when Ek is 0, and avoid the controller output signal OUT being 0 when the current value is equal to the set value, so that the system is in an uncontrolled state and out of control.

3.2.2. Integral item analysis (I item)

@1. Subtract each sampling value since it was put into operation from the set value, and you can get the deviation sequence data at each sampling moment since the startup:

E1,E2,E3......Ek-2,Ek-1,Ek

illustrate:

E1: The deviation between the first sampling point and the set value when starting up

E1 = SV - X1;

E2 = SV - X2;

......

Ek – 2 = SV – Ek – 2;

Ek – 1 = SV – Ek – 1;

Ek: the deviation between the current sampled value and the set value

Ek=SV-Xk

Analyze the error sequence since booting:

Each deviation value may have three possible values: >0, <0, =0, because the control algorithm has continuously output control signals to control the controlled object from the start-up to the present, resulting in sometimes Exceeding the standard (Ex<0), sometimes not reaching the standard (Ex>0), sometimes just meeting the requirement (Ex=0); if these deviation values ​​are accumulated and calculated algebraically, then we get

Sk, namely:

Sk=E1+E2+E3+......+Ek-2+Ek-1+Ek

Analysis Sk:

Sk>0: Most of the time in the past did not meet the standard

Sk=0: In the past, the control effect was ideal

Sk<0: Exceeded most of the time in the past

Conclusion 1:

Through the analysis of Sk, the past control effect of the control algorithm can be comprehensively evaluated. It reflects that the control signal output by the control algorithm in the original way leads to the current control result, so this value should be used to modify the current control signal OUT to be output, so as to ensure that the control object will reach the user as soon as possible in a short period of time in the future set value.

Conclusion 2:

Sk is actually the addition of errors at each time point in the past, which is similar to the definite integral operation in mathematics, so the algorithm for adjusting the output signal according to Sk is called an integral (integral) algorithm. So the mathematical model of integral control is:

IOUT=Kp*(1/Ti *∫Exdt) + Out0

Kp is a constant, its purpose is similar to a hardware amplifier, which is used to amplify or attenuate Sk;

Out0 is a constant, in order to ensure that the system has an output value when the historical integral deviation value is 0, so as to avoid loss of control;

Ti is the integral time constant, and the larger the value is, the smaller the output will be. It can be understood that the error value that has existed for a long time in history has affected the current output signal. The smaller the value, the stronger the output OUT will be. It can be understood that the integration only considers the error of the most recent period.

In practice, if the system has been running for a long time, those early sampling deviation values ​​can ignore their impact on the current control, so a reasonable Ti value should be selected according to the actual situation to obtain a good control effect.

3.2.3. Integral item analysis (D item)

@1. The difference between the last two deviations Dk

Dk=I-I-1

illustrate:

Ek: current bias

Ek-1: Based on the current deviation value at the previous sampling moment (that is, the last deviation value)

Analysis Dk:

Dk>0: Indicates that the error from the last sampling time to the current time has a tendency to increase

Dk=0: Indicates that the error from the previous moment to the current moment is stable

Dk<0: It shows that the error from the previous moment to the current moment has a decreasing trend

Conclusion 1: Dk can explain the change trend of the state of the controlled object during the period from the last sampling to the current sampling. This change trend is likely to continue to the next sampling time point to a certain extent, so it can be based on this change trend (The value of Dk) adjusts the output signal OUT to achieve the purpose of early control.

@2.Dk is like a mathematical differential operation, which reflects the changing trend and amount of the controlled object over a period of time, so the algorithm that uses Dk to adjust the output signal of the controller is called a differential algorithm. It can be expressed mathematically as:

DOUT=Kp*(Td(de/dt))+Out0

Kp: is a constant, which can be understood as an amplifier or attenuator on the hardware, used to adjust the gain of the output signal OUT;

Out0: It is a constant, in order to ensure that OUT has a stable control value when Dk is 0, so as to avoid loss of control.

Td: differential time constant, (like the self-inductance coefficient of the inductor on the hardware) the larger the Td, the larger the OUT value, which has a strong impact on the output signal.

4. Analysis of the advantages and disadvantages of the three algorithms of proportional, integral and differential

POUT=(Kp*Ek) + Out0 ------ proportional algorithm

IOUT=Kp*(1/Ti *∫Exdt) + Out0 ------integral algorithm

DOUT=Kp*(Td(de/dt))+Out0 ------ differential algorithm

Proportional algorithm: only consider the current error of the control object, output the control signal if there is an error at present, and will not output the control signal if there is no error at present, that is to say, only when the deviation has already occurred, the proportional algorithm will take measures to adjust, so the separate proportional algorithm It is impossible to control the state value of the control object at the set value, and it will always fluctuate up and down the set value; but the proportional control is sensitive, and the error will be reflected to the output immediately.

Integral algorithm: Considering the historical error situation of the controlled object, the past error situation has participated in the current output control, but when the system has not yet reached the target, these historical errors often interfere with the current control (that is, drag the legs ), improper use will mess up the current output. But after the system enters a stable state, especially when there is no deviation between the current value and the set value, the integral algorithm can output a relatively stable control signal according to the past deviation value to prevent deviation from the target and play a preventive effect.

Differential algorithm: simply consider the recent rate of change. When the deviation of the system tends to a certain fixed value, the rate of change of the error is 0. The differential algorithm does not output the control signal to adjust the alignment deviation, so the differential algorithm cannot be used alone. He only cares about the rate of change of the deviation, regardless of whether there is a deviation (the deviation is not necessarily 0 when the rate of deviation change is 0). However, the differential algorithm can obtain the recent change trend of the control object, and it can assist the output signal to suppress the change of the control object as early as possible. It can be understood that when there will be a drastic change, the output signal will be greatly adjusted to suppress it, so as to avoid a large change of the control object.

The above three algorithms are combined to generate a current control quantity to control the control object, and their advantages and disadvantages complement each other to form a classic PID algorithm.

4. Mathematical model of PID algorithm

OUT=POUT+IOUT+DOUT

Right now:

OUT= POUT=((Kp*Ek) +Out0)+(Kp*(1/Ti *∫Exdt)+Out0)+(Kp*(Td(de/dt))+Out0)

Arrange the formula to get: Merge the Out0 of each item into OUT0

OUT=Kp(Ek+(1/Ti *∫Exdt)+(Td(de/dt)))+OUT0

5. Application of PID algorithm in embedded devices

5.1. Application of PID algorithm in single chip microcomputer

When the PID algorithm is applied in a single-chip microcomputer, the integral and differential terms can be approximated:

For the integral item, it can be changed to:

               n

     I =1/Ti∑Ek*T

               k=0

That is, the algebraic sum of the deviation values ​​of the sampling points in the past period of time is used instead of the integral.

T is the sampling period, also called the control period, and the PID calculation is performed every T time period.

For the differential term, it can be rewritten as:

D=Td*((Ek-Ek-1)/T)

Ek: current deviation, Ek-1 last deviation

5.2. Mathematical model of positional PID algorithm

From this, the expression of the PID algorithm in the microcontroller can be obtained:

OUT=Kp*(Ek+(1/Ti *∫Exdt)+(Td(de/dt)))+OUT0 -->

                             n

OUT=Kp*(I+1/Ti∑I*T+(Td*((I-I-1)/T)))+OUT0

                            k=0

Expanded further:

                                     n

OUT=(Kp*I)+(Kp*1/Ti∑I*T)+(Kp*Td*((I-I-1)/T))+OUT0

                                     k=0

Order Ki=Kp*(T/Ti)

Kd=(Kp*(Td/T))

Therefore:

                           n

OUT=Kp*I + Ki∑I+ Kd*(I-I-1) + OUT0

                          k=0

The calculation formula can be realized conveniently by using C language or other languages ​​during programming. OUT is the result of this calculation. Use OUT to drive the actuator to output the corresponding control signal. For example, temperature control can control the width of PWM, and the solenoid valve can change the current of the solenoid coil to change the opening of the valve. The conduction angle of the controlled silicon, etc.;

The result (OUT value) calculated by this PID algorithm indicates the control amount that the current controller should output, so it is called position PID (directly output the state value that the actuator should reach)

5.3. Incremental PID algorithm

The positional PID algorithm has a large amount of calculation and consumes processor resources. In some control systems, the actuator itself has no memory function. For example, whether the mos tube is turned on depends entirely on the gate voltage, whether the thyristor is turned on depends on the trigger signal, whether the relay is turned on depends on the coil current, etc., as long as the control signal Lost, the actuator stops, and positional PID should be used in these applications.

There is also the fact that the actuator itself has a memory function, such as a stepping motor. If the control signal is lost in time, because its own mechanical structure will remain in the original position, etc., in these control systems, it is not necessary for the PID algorithm to output the real value that should be achieved this time. Position, just need to explain how much correction should be made to the output signal on the basis of the last time (can be positive or negative), this is the incremental PID algorithm.

The incremental PID calculates the adjustment value that should be on the current control signal. If the calculated value is positive, the output signal will be enhanced, and if the calculated value is responsible for weakening the output signal.

Incremental PID algorithm mathematical model:

If OUTk-1 represents the last output control signal, then the current output value should be OUTk, and the relationship between the two is:

OUTk=OUTk-1+△OUT

△OUT is the incremental value that should be output;

The above formula is further obtained:

△OUT=OUTk-OUTk-1

The positional algorithm output this time:

n

OUT=Kp*I + Ki∑I+ Kd*(I-I-1) + OUT0 -----1式

k=0

The last positional algorithm output:

n-1

OUT=Kp*I-1 + Ki∑I+ Kd*(I-1-I-2) + OUT0 -----2式

k=0

The above formula 1 minus formula 2 can get the increment of two adjacent times:

As mentioned before:

Ki=Kp*(T/Ti)

Kd=(Kp*(Td/T))

△OUT=OUTk-OUTk-1=Kp(Ek-Ek-1)+((Kp*T)/Ti)Ek+(Kp*Td)/T*(Ek-2Ek-1+Ek-2)

Ek: current deviation

Ek-1: Last Deviation

Ek-2: Last Deviation

Kp: algorithm gain adjustment

Ti: integration time

Td: Derivative time constant

T: sampling period

in conclusion:

The calculation of incremental PID only needs the latest three deviations (this deviation, the last deviation, and the last deviation), and does not require the processor to store a large number of historical deviation values, and the amount of calculation is relatively small, which is easy to implement.

5.4. Understanding of Ti and Td

In the PID control algorithm, the current output signal is formed by the joint action of the proportional direction, the integral term and the differential term. When the proportional direction output is not 0, if the contribution of the integral term to the operation output and the contribution of the proportional direction operation to the output are When the same (both positive or negative). The integral term is equivalent to repeating the time when the proportional direction takes effect, and this time can be understood as the integral time.

When the proportional direction is not 0, if the calculated result of the differential term is the same as the contribution of the proportional direction to the output in a period of time (that is, both positive or negative), the differential term is equivalent to repeating the proportional direction in a period of time. The role of this time can be understood as the differential time.

In practical applications, Kp, Ti, and Td should be combined to ensure that the contribution of the three to the output is balanced, so that the control object is near the set value.

6. How can the PID algorithm be simplified in embedded devices?

The expression of the PID algorithm in the microcontroller:

OUT=Kp*(Ek+(1/Ti *∫Exdt)+(Td(de/dt)))+OUT0 -->

                             n

OUT=Kp*(I+1/Ti∑I*T+(Td*((I-I-1)/T)))+OUT0

                            k=0

Expanded further:

                                     n

OUT=(Kp*I)+(Kp*1/Ti∑I*T)+(Kp*Td*((I-I-1)/T))+OUT0

                                    k=0

Order Ki=Kp*(T/Ti)

Kd=(Kp*(Td/T))

Therefore:

                           n

OUT=Kp*I + Ki∑I+ Kd*(I-I-1) + OUT0

                          k=0

In actual project development, OUT0 can be removed to simplify the relationship to get

                           n

OUT=Kp*Ek + Ki∑Ek+ Kd*(Ek-Ek-1)

                          k=0

You only need to adjust the three parameters Kp, Ki, and Kd. These three items are the P item coefficients, I item coefficients, and D item coefficients that we usually understand.

Guess you like

Origin blog.csdn.net/qq_27568125/article/details/115376713