本人在对仪器进行调试的过程中,发现六个工位中的PID调节第一个出现了错误。在距离设定值还有20℃之间时,PID调节中占空比总会出现两、三个错误的数据。比如设定值为150℃时,当到达148.1℃时,其余工位的PWM为70左右,而第一工位的占空比却出现了几次200。等到温度到达设定值进行调节乃至稳定的过程中,PWM又不会出现任何错误。
请各位老大帮忙分析分析!(附程序代码)
void Adjust(uchar temp)
{
uint setpoint,nextpoint;
uint com;
setpoint = 0;
nextpoint = 0;
com = 0;
//设定值整数部分
// setpoint = (Para[2*temp] >> 4) * 100 + (Para[2*temp] & 0x0f) * 10 + (Para[2*temp+1] >> 4);
// setpoint = setpoint * 10;
setpoint = (Para[2*temp] >> 4) * 1000;
setpoint += (Para[2*temp] & 0x0f) * 100;
setpoint += (Para[2*temp+1] >> 4) * 10;
setpoint += (Para[2*temp+1] & 0x0f);
//当前温度值整数部分
// nextpoint = (Results[2*temp] >> 4) *100 + (Results[2*temp] & 0x0f) *10 +(Results[(2*temp)+1] >> 4);
// nextpoint = nextpoint * 10;
nextpoint = (Results[2*temp] >> 4) *1000;
nextpoint += (Results[2*temp] & 0x0f) *100;
nextpoint += (Results[(2*temp)+1] >> 4) * 10;
nextpoint += (Results[(2*temp)+1] & 0x0f);
if(setpoint > nextpoint)
{
com = setpoint - nextpoint;
if(com > 200)
{
PWM[temp] = 200;
}
else
{
PWM[temp] = PIDCalc(setpoint,nextpoint,temp);
}
}
else
{
com = nextpoint - setpoint;
if(com > 200)
{
PWM[temp] = 0;
}
else
{
PWM[temp] = PIDCalc(setpoint,nextpoint,temp);
}
}
}
/*-----------------------------------------------------------------------
【名称】 uchar PIDCalc(uint setpoint,uint nextpoint,uchar temp)
-----------------------------------------------------------------------*/
uchar PIDCalc(uint setpoint,uint nextpoint,uchar temp)
{
static int idata Error = 0;
static int idata dError = 0;
int idata freecont = 15000;
uchar idata com = 0;
uchar idata i;
//pid算法部分
if(setpoint < 1300) i = 0;
else if(setpoint < 1650) i = 1;
else if(setpoint < 1900) i = 2;
else if(setpoint < 2050) i = 3;
else i = 4;
Error = (int)(setpoint - nextpoint) ;
SumError[temp] = (int)(SumError[temp] + Error);
dError = (int)(lasrError[temp] - PrevError[temp]);
PrevError[temp] = lasrError[temp];
lasrError[temp] = Error;
freecont += (int)(Error * KPConst[i]);
freecont += (int)(SumError[temp] * KIConst[i]);
freecont += (int)(dError * KDConst[i]);
if(freecont <= 15000)
{
com = 0;
}
else if(freecont > 15200)
{
com = 200;
}
else
{
com = (freecont - 15000) & 0xff;
}
return (com);
} |