#include "pid.h"
float PP=0.5,II=0.5,DD=0;
double SumError=0,PrevError=0,LastError=0;
int dError=0,Error=0;
void PID_init(void)
{
Error=0;SumError=0;LastError=0;
}
void PIDcompute(unsigned int Target,unsigned int Real)
{
/*--------------------------------------------------------------------
根据设定及采集值进行计算PID调节,计算pwm输出值
--------------------------------------------------------------------*/
float j=0.0,i;
Error =(Target-Real);
SumError +=Error;
dError=Error-LastError;
LastError=Error;
i=PP;
j=Error*i;
i=II;
j=j+SumError*i;
i=DD;
j=j+dError*i;
j=j/100;
if(j>=hArrPwmVal)OutPwmValue=hArrPwmVal;
else if(j<1)OutPwmValue=1;
else OutPwmValue=j;
} |