Detailed explanation of PID core algorithm (c language)

        

    In the previous article, I introduced what the PID algorithm is, as well as the positional and incremental PID. This post will explain how to implement positional and incremental in a program.


Positional core algorithm


    The program is written by formula, so first list the positional formula of PID


    Find out the variables that need to be defined to prepare the program for writing.

The variables to be defined are: u_1 , kp , ts , ti , td , e_1 , e_2 , up_1 , ui_1 , ud_1 , x , x_1 (refer to programming for specific meanings)

    Special attention needs to be paid here, except for ts, others are generally floating-point types

uint ts=50; // sampling period is 50
float kp, ti, td;
float e_1 = 0, e_2 = 0; // Fixed e (k) sum e (k-1)
float u_1,up_1,ui_1=0,ud_1=0; // define u(k), and three components
float x_1; // current detection value
float x=40; // assume the temperature is controlled at 40°
while(1)
{
	x_1=AD_convert; // Get the current detection value from a sub-function, this function can be AD sampling or digital filtering, or even a sub-function of scale transformation

							     
	e_1=x-x_1; // Find the error e(k)
	
	if(e<0.01)
		break; //(When the temperature error is controlled within 0.01, the control ends)
	
	up_1=kp*e_1;
	
	ud_1 = kp * td * (e_1-e_2) / ts;
	
	ui_1=ui_1+kp*ts*e_1/ti; // means ui_1 at the previous moment plus ui_1 at this moment,
					  // At the next moment, it is the addition result of ui_1 at the next moment and ui_1 at this moment,
	                                  // That is, the purpose of accumulation is achieved by rolling addition.
// This sentence is the most difficult sentence in the whole program I think to analyze, // To really understand how to express the accumulation of e(i) in the positional expression. u_1=up_1+ud_1+ui_1; if(u_1>=10) u_1=10; if(u_1<=-10) u_1=-10; // The limit output of the control system acts as a protection system e_2=e_1 ; // Here is another very important variable rolling loop, changing e_1 at this moment to e_2 at the next moment }

    This program only shows the core algorithm. Although it cannot be used directly, it is actually written according to this core algorithm when the program is written, so it is particularly important to master the core algorithm.

    The positional defect mentioned in the previous PID article is a series of problems caused by accumulation. The incremental type eliminates the accumulation item, so the following is the core algorithm of the incremental type.

    The writing process is almost the same as the positional algorithm, so writing out the writing process just now helps to write incremental algorithms.


Incremental core algorithm

    Also list the increments first


Refer to the steps for writing a positional algorithm above.

uint ts=50; 				   
float kp, ti, td;
float e_1 = 0, e_2 = 0, e_3 = 0; // Fixed e (k) sum e (k-1)
float u_1, u_2, u_deta, up_deta, ui_deta = 0, ud_deta = 0; // Fixed u (k), u (k-1), u △, sum three pieces
float x_1;				      
float x=40; 				   
while(1)
{
	x_1=AD_convert;           
	e_1=x-x_1;      		   
	
	if(e<0.01)
		break;      		   
	
	up_deta=kp*(e_1-e_2);
	
	ud_deta = kp * td * (e_1-2 * e_2 + e_3) / ts;
	
	ui_deta=kp*ts*e_1/ti; // At this time, the ui does not have the accumulated amount of the position type just now
	
	u_deta=up_deta+ud_deta+ui_deta; //here is u△, the real u(k)=u△+u(k-1)
	
	u_1 = u_deta + u_2;          
	
	if(u_1>=10)
		u_1 = 10;
	
	if(u_1<=-10)
		u_1=-10; // The limit output of the control system acts as a protection system
	
	e_3 = e_2;
	e_2=e_1; // Pay attention to the order of variable loops here
	u_2=u_1; //Also be careful not to lose the variable loop of u(k)
}

The following comments only mark the incremental algorithms that require special attention when writing.

This article introduces the core algorithm of positional and incremental, and the next article will introduce the pitfalls of this standard PID algorithm and how to improve it.

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324828300&siteId=291194637