Embedded PID algorithm

In automatic control, PID and its derived algorithms are one of the most widely used algorithms. Basically, all manufacturers that do automatic control will implement this classic algorithm. In the process of doing projects, we often encounter similar requirements, so we want to implement this algorithm to apply to more application scenarios.

1 , PID algorithm basic principles

PID algorithm is the most classic and simplest algorithm in the control industry, and it can best reflect the feedback control idea. For general R&D personnel, designing and implementing PID algorithm is the basic requirement to complete the automatic control system. Although this algorithm is simple, it takes a certain amount of effort to implement it well. First, we begin to analyze and design this classic proposition from the most basic principle of PID algorithm.

The execution process of PID algorithm is very simple, that is, use feedback to detect the deviation signal, and control the controlled quantity through the deviation signal. The controller itself is the sum of the three links of proportional, integral, and derivative. Its functional block diagram is as follows:

 

According to the above figure, we consider that at a specific time t , the input is rin(t) and the output is rout(t) , so the deviation can be calculated as err(t)=rin(t)-rout(t ) . So the basic control law of PID can be expressed as the following formula:

 

Among them, Kp is the proportional band, TI is the integral time, and TD is the derivative time. This is the basic principle of PID control.

2 , PID discrete algorithms

The previous section briefly introduced the basic principles of the PID algorithm, but it must be discretized if it is to be implemented on a computer. Next, we will talk about the discretization of the PID algorithm. Before realizing discretization, we need to make a simple description of the characteristics of proportional, integral, and differential.

The ratio is used to react to the deviation of the system, so as long as there is a deviation, the ratio will work. Integral is mainly used to eliminate the static error. The so-called static error refers to the difference that still exists between the input and output after the system is stable, and the integral is to offset the static error of the system through the accumulation of deviations. The differentiation is to respond to the change trend of the deviation, realize the advance adjustment according to the change trend of the deviation, and improve the reaction speed.

Before implementing a discrete, we assume that the sampling period is T . Suppose we check the Kth sampling period, it is obvious that the system performs the Kth sampling. The deviation at this time can be expressed as err(K)=rin(K)-rout(K) , then the integral can be expressed as: err(K)+ err(K+1)+ ┈┈ , and the differential can be expressed as : (Err(K)- err(K-1))/T . So we can express the offline form of the PID algorithm at the Kth sampling as:

 

It can also be written as:

 

This is the discrete description formula of the so-called positional PID algorithm. We know that there is also an incremental PID algorithm, so next we will push to the formula of the incremental PID algorithm. The above formula describes the result of the k- th sampling period, so the previous moment, which is k-1 sampling period, is not difficult to express as:

 

Then let 's talk about the increment of the Kth sampling period, which is obviously U(k)-U(k-1) . So we use the k- th sampling period formula to subtract the k- 1th sampling period formula to get the expression formula of the incremental PID algorithm:

 

Of course, incremental PID must remember one thing, that is, remembering U(k)=U(k-1)+∆U(k) .

3 , PID basically Controller

After the discretization is completed, we can implement it. After it has been expressed in discretized data formulas, re-entry computer programming is no longer a problem. Next, we will use the C language to implement the positional formulas and incremental formulas respectively.

( 1 ) Simple realization of positional PID

The realization of positional PID is based on the previous positional formula. In this section, we just complete the simplest realization, that is , the computer language of the previous discrete position PID formula.

First define the structure of the PID object:

1 /* Define structure and common body */ 2 3 typedef struct 4 5 { 6 7 float setpoint; // set value 8 9 float proportiongain; // proportion coefficient 10 11 float integralgain; // integration coefficient 12 13 float derivativegain ; // Differential coefficient 14 15 float lasterror; // Previous beat deviation 16 17 float result; // Output value 18 19 float integral; // Integral value 20 21 }PID;

 

Next, implement the PID controller:

1 void PIDRegulation(PID *vPID, float processValue) 2 3 { 4 5 float thisError; 6 7 thisError=vPID->setpoint-processValue; 8 9 vPID->integral+=thisError; 10 11 vPID->result=vPID->proportiongain*thisError+vPID->integralgain*vPID->integral+vPID->derivativegain*(thisError-vPID->lasterror); 12 13 vPID->lasterror=thisError; 14 15 }

 

This achieves the simplest position-based PID controller, of course, without considering any interference conditions, only the computer language of mathematical formulas.

( 2 ) Simple realization of incremental PID

The realization of incremental PID is based on the previous incremental formula. In this section, we just complete the simplest implementation, that is , the computer language of the previous discrete incremental PID formula.

First define the structure of the PID object:

1 /* Define structure and common body */ 2 3 typedef struct 4 5 { 6 7 float setpoint; // set value 8 9 float proportiongain; // proportion coefficient 10 11 float integralgain; // integration coefficient 12 13 float derivativegain ; // Differential coefficient 14 15 float lasterror; //The deviation of the previous beat 16 17 float preerror; //The deviation of the previous two beats 18 19 float deadband; // dead zone 20 21 float result; // output value 22 23 }PID;

Next, implement the PID controller:

void PIDRegulation(PID *vPID, float processValue) { float thisError; float increment; float pError,dError,iError; thisError=vPID->setpoint-processValue; //得到偏差值 pError=thisError-vPID->lasterror; iError=thisError; dError=thisError-2*(vPID->lasterror)+vPID->preerror; increment=vPID->proportiongain*pError+vPID->integralgain*iError+vPID->derivativegain*dError; //增量计算 vPID->preerror=vPID->lasterror; //存放偏差用于下次运算 vPID->lasterror=thisError; vPID->result+=increment; }

This realizes one of the simplest incremental PID controllers, and does not consider any interference conditions, only the computer language of mathematical formulas.

4. Basic features

The PID controllers are described and implemented in the previous section , including positional PID controllers and incremental PID controllers. Let's make a brief description of the characteristics of these two types of controllers.

The basic characteristics of the positional PID controller:

  • The output of the positional PID control is related to the entire past state, and the accumulated value of the deviation is used, which is prone to accumulate deviation.
  • Position PID is suitable for objects without integral parts in the actuator.
  • The output of the position type directly corresponds to the output of the object, which has a greater impact on the system.

The basic characteristics of incremental PID controller:

  • Incremental PID algorithm does not need to be accumulated, and the determination of the increment of the control quantity is only related to the deviation value of the last few times, and the influence of the calculation deviation is small.
  • The incremental PID algorithm obtains the increment of the control quantity, and the impact on the system is relatively small.
  • The use of incremental PID algorithm is easy to realize the disturbance-free switching from manual to automatic.

 

Guess you like

Origin blog.csdn.net/zhuimeng_ruili/article/details/113487922