正文

比较典型的PID处理程序2006-05-05 16:10:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/vfdff/13588.html

分享到:

比较典型的PID处理程序 [日期:2005-2-1] 来源:21ICbbs 作者:lookuper [字体:大中小]/*====================================================================================================这是一个比较典型的PID处理程序,在使用单片机作为控制cpu时,请稍作简化,具体的PID参数必须由具体对象通过实验确定。由于单片机的处理速度和ram资源的限制,一般不采用浮点数运算,而将所有参数全部用整数,运算到最后再除以一个2的N次方数据(相当于移位),作类似定点数运算,可大大提高运算速度,根据控制精度的不同要求,当精度要求很高时,注意保留移位引起的“余数”,做好余数补偿。这个程序只是一般常用pid算法的基本架构,没有包含输入输出处理部分。=====================================================================================================*/#include#include/*====================================================================================================PID FunctionThe PID (比例、积分、微分) function is used in mainlycontrol applications. PIDCalc performs one iteration of the PIDalgorithm.While the PID function works, main is just a dummy program showinga typical usage.=====================================================================================================*/typedef struct PID {double SetPoint; // 设定目标Desired valuedouble Proportion; // 比例常数Proportional Constdouble Integral; // 积分常数Integral Constdouble Derivative; // 微分常数Derivative Constdouble LastError; // Error[-1]double PrevError; // Error[-2]double SumError; // Sums of Errors} PID;/*====================================================================================================PID计算部分=====================================================================================================*/double PIDCalc( PID *pp, double NextPoint ){double dError,Error;Error = pp->SetPoint - NextPoint; // 偏差pp->SumError += Error; // 积分dError = pp->LastError - pp->PrevError; // 当前微分pp->PrevError = pp->LastError;pp->LastError = Error;return (pp->Proportion * Error // 比例项+ pp->Integral * pp->SumError // 积分项+ pp->Derivative * dError // 微分项);}/*====================================================================================================Initialize PID Structure=====================================================================================================*/void PIDInit (PID *pp){memset ( pp,0,sizeof(PID));}/*====================================================================================================Main Program=====================================================================================================*double sensor (void) // Dummy Sensor Function{return 100.0;}void actuator(double rDelta) // Dummy Actuator Function{}void main(void){PID sPID; // PID Control Structuredouble rOut; // PID Response (Output)double rIn; // PID Feedback (Input)PIDInit ( &sPID ); // Initialize StructuresPID.Proportion = 0.5; // Set PID CoefficientssPID.Integral = 0.5;sPID.Derivative = 0.0;sPID.SetPoint = 100.0; // Set PID Setpointfor (;;) { // Mock Up of PID ProcessingrIn = sensor (); // Read InputrOut = PIDCalc ( &sPID,rIn ); // Perform PID Interationactuator ( rOut ); // Effect Needed Changes}

阅读(5575) | 评论(2)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册