- //======================================================================
- //Adjust these three factor ,to achieve the best control effect
- //P:1~10之間 I:0-5之間 D:0.1~1
- float P_Coefficient=4.75;
- float I_Coefficient=0.55;
- float D_Coefficient=0.2;
- #define Diff_Order 4 /* Differential order*/
- int Temp,DestTemp,HeatPower;
- int Set_Distant;
- long int Integral=5; // Points accumulated
- float Prev_Error[10]; // Record ten times before the error
- float P,I,D;
- float Ek,E;
- unsigned char FirstFlag=1;
- /*******************************************************************************
- * Funtion name:PID Control
- * Time:2013/3/5
- * Author:zhuhao
- *******************************************************************************/
- float PID_Control(float Error)
- {
- int i;
- float Output;
- float Ture;
- if(FirstFlag)//The first execution
- {
- FirstFlag=0;
- for(i=0;i<10;i++)
- Prev_Error=Error;
- }
- for(i=0;i<10;i++)
- Prev_Error[i+1]=Prev_Error;// Buffer queue
- Prev_Error[0]=Error;//
- Ek=Error-Prev_Error[Diff_Order];//
- E=0.8+Ek*0.2;//IIR
- P=P_Coefficient*Error;// Calculate the proportional component
- I=I_Coefficient*Integral;// Calculate the integral component
- D=D_Coefficient*E;//Calculate the derivative component
- Output=(P+I+D);
- if(Output>0)
- {
- Ture=Output;
- }
- if(Output>=100||Output<=0||Error<-40||Error>40)//Saturated or large deviation integral
- {
- if(Integral>0&&Error<0)
- Integral+=Error;
- if(Integral<0&&Error>0)
- Integral+=Error;
- }
- else
- Integral+=Error;
- if(Integral<-10) Integral=-10;
- if(Integral>10) Integral=10;
- if (Output>=100)
- Output=99;
- else if(Output<=0)
- Output=Ture;
- return Output;
- }
復制代碼
初次調試速度PID全過程(分享PID函數)
http://www.zg4o1577.cn/bbs/dpj-42514-1.html |