typedef struct PID{intSetPoint; //设定目标 DesiredValuelongSumError; //误差累计doubleProportion; //比例常数Proportional ConstdoubleIntegral; //积分常数 IntegralConstdoubleDerivative; //微分常数Derivative ConstintLastError; //Error[-1]intPrevError; //Error[-2]} PID;static PID sPID;static PID *sptr = &sPID;/*====================================================================InitializePID Structure PID 参数初始化===================================================================*/void IncPIDInit(void){sptr->SumError= 0;sptr->LastError= 0; //Error[-1]sptr->PrevError= 0; //Error[-2]sptr->Proportion= 0; //比例常数Proportional Constsptr->Integral= 0; //积分常数IntegralConstsptr->Derivative= 0; //微分常数Derivative Constsptr->SetPoint= 0;}/*==================================================================== 增量式PID 计算部分====================================================================*/int IncPIDCalc(int NextPoint){registerint iError, iIncpid; //当前误差iError= sptr->SetPoint - NextPoint; //增量计算iIncpid= sptr->Proportion * iError //E[k]项 -sptr->Integral * sptr->LastError //E[k-1]项 +sptr->Derivative * sptr->PrevError; //E[k-2]项//存储误差,用于下次计算sptr->PrevError= sptr->LastError;sptr->LastError= iError;//返回增量值return(iIncpid);}
|