您好,欢迎访问三七文档
当前位置:首页 > 建筑/环境 > 工程监理 > 模糊自适应整定PID控制程序FUZZY-PID
/******************************************************************************文件名:FUZZY_PID.c**功能:此文件是模糊自适应整定PID控制程序**作者:Sahara*****************************************************************************/#includeDSP28_Device.h#includestdlib.h#includemath.h#includefloat.h//浮点数处理floaterror=0.0;//是指当前温度和目标温度之差floatSumerror=0.0;//每次采样的温度差值之和floatDerror=0.0;//前后两次采样温差之差,就是所谓的温度变化率floatLasterror=0.0;floatPreverror=0.0;floatKp=1.0;//设置PID控制系数floatKi=1.0;floatKd=0.0;floatV_out=0.0;/******************************************************************************文件名:PID_Calc(floaterror)**功能:此文件是PID计算子程序,此程序采用位置式方式,*增量式得到的是变化增量,最终的结果需要加修正值*增量式:v(k)=V(k)-Vo(k-1)=Kp*[e(k)-e(k-1)]+Ki*e(k)+Kd*[e(k)-2e(k-1)+e(k-2)];e(k)-e(k-1)=error;e(k)=Sumerror;e(k)-2e(k-1)+e(k-2)=[e(k)-e(k-1)]-[e(k-1)-e(k-2)]=Derror;Lasterror=e(k-1)-e(k-2);Derror=error-Lasterror;**位置式:v(k)=Kp*e(k)+Ki*[e(k)+e(k-1)+e(k-2)+...]+Kd*[e(k)-e(k-1)];e(k)=error;e(k)+e(k-1)+e(k-2)+...=Sumerror;e(k)-e(k-1)=Derror;Lasterror=e(k-1);Preverror=e(k-2);Derror=error-Lasterror;*作者:Sahara**入口参数:floaterror**出口参数:V_out*****************************************************************************/voidPID_Calc(floaterror){Sumerror=Sumerror+error;Derror=error-Lasterror;Preverror=Lasterror;Lasterror=error;V_out=Kp*error+Ki*Sumerror+Kd*Derror;//return(V_out);}/******************************************************************************文件名:FUZZY_Calc_Kp(floate,floatec)**功能:模糊算法kp计算部分**作者:Sahara**入口参数:floate,floatec**出口参数:Kp*****************************************************************************/voidFUZZY_Calc_Kp(floate,floatec){floateRule[7]={-3,-2,-1,0,1,2,3};floatecRule[7]={-3,-2,-1,0,1,2,3};floatuRule[7]={-3,-2,-1,0,1,2,3};floateFuzzy[7];floateFuzzy_Out[6];floatecFuzzy[7];floatecFuzzy_Out[6];floatuFuzzy[7];intPe,Pec;floatRule[7][7];floata1,a2;//生成模糊规则表的可调因子/*intRule[6][6]={{3,2,2,1,0,0},{3,2,1,1,0,-1},{2,2,1,0,-1,-1},{2,1,0,-1,-2,-2},{1,0,-1,-1,-2,-2},{0,-1,-2,-2,-2,-3},};*//*intRule[7][7]={{3,3,2,2,1,0,0},{3,3,2,1,1,0,-1},{2,2,2,1,0,-1,-1},{2,2,1,0,-1,-2,-2},{1,1,0,-1,-1,-2,-2},{1,0,-1,-2,-2,-2,-3},{0,0,-2,-2,-2,-3,-3},};*/inti,j;for(i=0;i7;i++){eFuzzy[i]=0;}for(i=0;i7;i++){ecFuzzy[i]=0;}for(i=0;i7;i++){uFuzzy[i]=0;}for(i=0;i2;i++){eFuzzy_Out[i]=0;}for(i=0;i2;i++){ecFuzzy_Out[i]=0;}/******************************************************************************误差隶属函数描述*误差隶属函数的描述采用三角形隶属函数描述*****************************************************************************/if(eeRule[0]){eFuzzy_Out[0]=1.0;Pe=0;}elseif(eeRule[0]&&eeRule[1]){eFuzzy[0]=(e-eRule[1])/(eRule[0]-eRule[1]);eFuzzy[1]=(e-eRule[0])/(eRule[1]-eRule[0]);eFuzzy_Out[0]=eFuzzy[0]eFuzzy[1]?eFuzzy[0]:eFuzzy[1];Pe=0;}elseif(eeRule[1]&&eeRule[2]){eFuzzy[1]=(e-eRule[2])/(eRule[1]-eRule[2]);eFuzzy[2]=(e-eRule[1])/(eRule[2]-eRule[1]);eFuzzy_Out[1]=eFuzzy[1]eFuzzy[2]?eFuzzy[1]:eFuzzy[2];Pe=1;}elseif(eeRule[2]&&eeRule[3]){eFuzzy[2]=(e-eRule[3])/(eRule[2]-eRule[3]);eFuzzy[3]=(e-eRule[2])/(eRule[3]-eRule[2]);eFuzzy_Out[2]=eFuzzy[2]eFuzzy[3]?eFuzzy[2]:eFuzzy[3];Pe=2;}elseif(eeRule[3]&&eeRule[4]){eFuzzy[3]=(e-eRule[4])/(eRule[3]-eRule[4]);eFuzzy[4]=(e-eRule[3])/(eRule[4]-eRule[3]);eFuzzy_Out[3]=eFuzzy[3]eFuzzy[4]?eFuzzy[3]:eFuzzy[4];Pe=3;}elseif(eeRule[4]&&eeRule[5]){eFuzzy[4]=(e-eRule[5])/(eRule[4]-eRule[5]);eFuzzy[5]=(e-eRule[4])/(eRule[5]-eRule[4]);eFuzzy_Out[4]=eFuzzy[4]eFuzzy[5]?eFuzzy[4]:eFuzzy[5];Pe=4;}elseif(eeRule[5]&&eeRule[6]){eFuzzy[5]=(e-eRule[6])/(eRule[5]-eRule[6]);eFuzzy[6]=(e-eRule[5])/(eRule[6]-eRule[5]);eFuzzy_Out[5]=eFuzzy[5]eFuzzy[6]?eFuzzy[5]:eFuzzy[6];Pe=5;}else{eFuzzy_Out[5]=1.0;Pe=5;}//eFuzzy_Out[1]=1.0-eFuzzy_Out[0];/******************************************************************************误差变化隶属函数描述*误差变化隶属函数描述采用三角形隶属函数描述*****************************************************************************/if(ececRule[0]){ecFuzzy_Out[0]=1.0;Pec=0;}elseif(ececRule[0]&&ececRule[1]){ecFuzzy[0]=(ec-ecRule[1])/(ecRule[0]-ecRule[1]);ecFuzzy[1]=(ec-ecRule[0])/(ecRule[1]-ecRule[0]);ecFuzzy_Out[0]=ecFuzzy[0]ecFuzzy[1]?ecFuzzy[0]:ecFuzzy[1];Pec=0;}elseif(ececRule[1]&&ececRule[2]){ecFuzzy[1]=(ec-ecRule[2])/(ecRule[1]-ecRule[2]);ecFuzzy[2]=(ec-ecRule[1])/(ecRule[2]-ecRule[1]);ecFuzzy_Out[1]=ecFuzzy[1]ecFuzzy[2]?ecFuzzy[1]:ecFuzzy[2];Pec=1;}elseif(ececRule[2]&&ececRule[3]){ecFuzzy[2]=(ec-ecRule[3])/(ecRule[2]-ecRule[3]);ecFuzzy[3]=(ec-ecRule[2])/(ecRule[3]-ecRule[2]);ecFuzzy_Out[2]=ecFuzzy[2]ecFuzzy[3]?ecFuzzy[2]:ecFuzzy[3];Pec=2;}elseif(ececRule[3]&&ececRule[4]){ecFuzzy[3]=(ec-ecRule[4])/(ecRule[3]-ecRule[4]);ecFuzzy[4]=(ec-ecRule[3])/(ecRule[4]-ecRule[3]);ecFuzzy_Out[3]=ecFuzzy[3]ecFuzzy[4]?ecFuzzy[3]:ecFuzzy[4];Pec=3;}elseif(ececRule[4]&&ececRule[5]){ecFuzzy[4]=(ec-ecRule[5])/(ecRule[4]-ecRule[5]);ecFuzzy[5]=(ec-ecRule[4])/(ecRule[5]-ecRule[4]);ecFuzzy_Out[4]=ecFuzzy[4]ecFuzzy[5]?ecFuzzy[4]:ecFuzzy[5];Pec=4;}elseif(ececRule[5]&&ececRule[6]){ecFuzzy[5]=(ec-ecRule[6])/(ecRule[5]-ecRule[6]);ecFuzzy[6]=(ec-ecRule[5])/(ecRule[6]-ecRule[5]);ecFuzzy_Out[5]=ecFuzzy[5]ecFuzzy[6]?ec
本文标题:模糊自适应整定PID控制程序FUZZY-PID
链接地址:https://www.777doc.com/doc-4648093 .html