/********************************************************************
模糊 PID 控制 C 程序代码
********************************************************************/
}
}
/***********************************************
函数功能:PID 参数 Kp 的计算
************************************************/
float fuzzy_kp(float e, float ec)
{
//e,ec,表示误差,误差变化率
float Kp_calcu;
uchar num,pe,pec;
float code eRule[7]={-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0};
float code ecRule[7]={-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0}; //误差变化率 EC 的模糊论
//误差 E 的模糊论域
域
float eFuzzy[2]={0.0,0.0};
float ecFuzzy[2]={0.0,0.0};
float code kpRule[4]={0.0,8.0,16.0,24.0};
float KpFuzzy[4]={0.0,0.0,0.0,0.0};
int code KpRule[7][7]=
{
//隶属于误差 E 的隶属程度
//隶属于误差变化率 EC 的隶属程度
//Kp 的模糊子集
//隶属于 Kp 的隶属程度
//Kp 的模糊控制表
3,3,3,3,3,3,3,
2,2,2,2,1,2,2,
1,1,1,1,1,1,1,
1,1,0,1,0,1,1,
0,0,1,0,0,1,0,
0,1,0,1,0,0,2,
3,3,3,3,3,3,3
};
/*****误差 E 隶属函数描述*****/
if(e
eFuzzy[0] = (eRule[2] -e)/(eRule[2]-eRule[1]);
pe = 1;
}
else if(eRule[2]<=e && e
else if(ecRule[2]<=ec && ec
0,0,0,0,0,0,0,
0,0,0,0,0,0,0,
0,0,0,0,0,0,0,
0,0,0,0,0,0,0,
0,0,0,0,0,0,0,
2,0,0,0,0,0,1,
3,3,3,3,3,3,3
};
/*****误差隶属函数描述*****/
if(e
}
else
{
eFuzzy[0] =0.0;
pe =5;
}
eFuzzy[1] =1.0 - eFuzzy[0];
/*****误差变化隶属函数描述*****/
if(ec
ecFuzzy[0] = (ecRule[6]-ec)/(ecRule[6]-ecRule[5]);
pec=5;
{
}
else
{
ecFuzzy[0] =0.0;
pec = 5;
}
ecFuzzy[1] = 1.0 - ecFuzzy[0];
/***********查询模糊规则表***************/
num =KiRule[pe][pec];
KiFuzzy[num] += eFuzzy[0]*ecFuzzy[0];
num =KiRule[pe][pec+1];
KiFuzzy[num] += eFuzzy[0]*ecFuzzy[1];
num =KiRule[pe+1][pec];
KiFuzzy[num] += eFuzzy[1]*ecFuzzy[0];
num =KiRule[pe+1][pec+1];
KiFuzzy[num] += eFuzzy[1]*ecFuzzy[1];
/******** 加 权 平 均 法 解 模 糊 ********/
Ki_calcu=KiFuzzy[0]*kiRule[0]+KiFuzzy[1]*kiRule[1]+KiFuzzy[2]*kiRule[2]
+KiFuzzy[3]*kiRule[3];
return(Ki_calcu);
}
/***********************************************
函数功能:PID 参数 Kd 的计算
************************************************/
float fuzzy_kd(float e, float ec)
{
float Kd_calcu;
uchar num,pe,pec;
float code eRule[7]={-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0};
float code ecRule[7]={-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0};
float eFuzzy[2]={0.0,0.0};
float ecFuzzy[2]={0.0,0.0};
float code kdRule[4]={0.0,1.0,2.0,3.0};
float KdFuzzy[4]={0.0,0.0,0.0,0.0};
int code KdRule[7][7]=
{
3,3,3,2,2,2,2,
2,2,2,1,1,1,1,
1,1,2,1,1,2,1,
1,1,0,1,0,1,1,
1,1,0,0,0,1,1,
2,2,1,0 ,1,1,1,
3,3,3,3,2,3,2
};
/*****误差隶属函数描述*****/
if(e
ecFuzzy[0] = (ecRule[1] - ec)/(ecRule[1]-ecRule[0]);
pec = 0 ;
}
else if(ecRule[1]<=ec && ec