《XS128PID控制》由会员分享,可在线阅读,更多相关《XS128PID控制(5页珍藏版)》请在金锄头文库上搜索。
1、精品文档,仅供学习与交流,如有侵权请联系网站删除xs128-PID控制 采用增量式PID控制/*/定义PID结构体/*typedef struct PID int SetPoint; /设定目标 Desired Value double Proportion; /比例常数 Proportional Const double Integral; /积分常数 Integral Const double Derivative; /微分常数 Derivative Const int LastError; /Error-1 int PrevError; /Error-2 PID;/*/定义相关宏/*#d
2、efine P_DATA 100#define I_DATA 0.6#define D_DATA 1#define HAVE_NEW_VELOCITY 0X01/*/声明PID实体/*static PID sPID;static PID *sptr = &sPID;/*/PID参数初始化/*void IncPIDInit(void) sptr-LastError = 0; /Error-1 sptr-PrevError = 0; /Error-2 sptr-Proportion = P_DATA; /比例常数 Proportional Const sptr-Integral = I_DATA;
3、 /积分常数Integral Const sptr-Derivative = D_DATA; /微分常数 Derivative Const sptr-SetPoint =100; /目标是100/*/增量式PID控制设计/*int IncPIDCalc(int NextPoint) int iError, iIncpid; /当前误差 iError = sptr-SetPoint - NextPoint; /增量计算 iIncpid = sptr-Proportion * iError /Ek项 - sptr-Integral * sptr-LastError /Ek1项 + sptr-Der
4、ivative * sptr-PrevError; /Ek2项 sptr-PrevError = sptr-LastError; /存储误差,用于下次计算 sptr-LastError = iError; return(iIncpid); /返回增量值Int g_CurrentVelocity;Int g_Flag;void main(void) DisableInterrupt InitMCu();IncPIDInit(); g_CurrentVelocity=0;/全局变量也初始化 g_Flag=0;/全局变量也初始化 EnableInterrupt;While(1)if (g_Flag& HAVE_NEW_VELOCITY)PWMOUT+= IncPIDCalc(CurrentVelocity);g_Flag&= HAVE_NEW_VELOCITY; /*/采样周期T/*Interrrupt TIME voidCurrentVelocity =GetCurrentVelocity;g_Flag|= HAVE_NEW_VELOCITY;采样周期后.时钟中断 获取当前速度.更新标志g_Flag.调用IncPIDClac()计算增量.更新PWM.【精品文档】第 页