[MM32生态] PID 算法C 语言

[复制链接]
 楼主| jimmhu 发表于 2024-7-31 11:30 | 显示全部楼层 |阅读模式


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);}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

20

主题

3776

帖子

4

粉丝
快速回复 返回顶部 返回列表