《XS128PID控制(6页).doc》由会员分享,可在线阅读,更多相关《XS128PID控制(6页).doc(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、- 采用增量式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;/*/定义相关宏/*#define P_DATA 100#define I_DATA 0.6
2、#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; /积分常数Integral Const sptr-Derivati
3、ve = 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-Derivative * sptr-PrevError; /Ek2项 sp
4、tr-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.第 6 页-