新闻  |   论坛  |   博客  |   在线研讨会
比较典型的PID处理程序
tongxin | 2009-04-13 15:28:47    阅读:698   发布文章


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

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
最近文章
寂寞如雪
2009-05-19 19:01:18
夜色花
2009-05-19 18:56:22
没有爱可以重来
2009-05-19 18:54:59
推荐文章
最近访客