标准PID源程序 点击:4190 | 回复:13



xiaolifeidao

    
  • 精华:28帖
  • 求助:0帖
  • 帖子:130帖 | 357回
  • 年度积分:0
  • 历史总积分:0
  • 注册:1900年1月01日
发表于:2005-09-29 09:45:00
楼主
--- 工业控制中常用算法 ---      /*====================================================================================================    这是从网上找来的一个比较典型的PID处理程序,在使用单片机作为控制cpu时,请稍作简化,具体的PID   参数必须由具体对象通过实验确定。由于单片机的处理速度和ram资源的限制,一般不采用浮点数运算,   而将所有参数全部用整数,运算到最后再除以一个2的N次方数据(相当于移位),作类似定点数运算,可   大大提高运算速度,根据控制精度的不同要求,当精度要求很高时,注意保留移位引起的“余数”,做好余   数补偿。这个程序只是一般常用pid算法的基本架构,没有包含输入输出处理部分。   =====================================================================================================*/   #include <string.h>   #include <stdio.h>   /*====================================================================================================    PID Function       The PID (比例、积分、微分) function is used in mainly    control applications. PIDCalc performs one iteration of the PID    algorithm.       While the PID function works, main is just a dummy program showing    a typical usage.   =====================================================================================================*/      typedef struct PID {       double SetPoint; // 设定目标 Desired value       double Proportion; // 比例常数 Proportional Const    double Integral; // 积分常数 Integral Const    double Derivative; // 微分常数 Derivative Const       double 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 Structure    double rOut; // PID Response (Output)    double rIn; // PID Feedback (Input)       PIDInit ( &sPID ); // Initialize Structure    sPID.Proportion = 0.5; // Set PID Coefficients    sPID.Integral = 0.5;    sPID.Derivative = 0.0;    sPID.SetPoint = 100.0; // Set PID Setpoint       for (;;) { // Mock Up of PID Processing       rIn = sensor (); // Read Input    rOut = PIDCalc ( &sPID,rIn ); // Perform PID Interation    actuator ( rOut ); // Effect Needed Changes    }   }



小迷糊神

  • 精华:0帖
  • 求助:0帖
  • 帖子:17帖 | 10回
  • 年度积分:0
  • 历史总积分:60
  • 注册:2005年7月06日
发表于:2005-09-29 11:00:00
1楼
程序中怎么没有涉及采样时间,微分项和积分项的计算正确吗?在计算它们的时候好象都没有用到采样时间嘛,本人有些不明白,望赐教

xiaolifeidao

  • 精华:28帖
  • 求助:0帖
  • 帖子:130帖 | 357回
  • 年度积分:0
  • 历史总积分:0
  • 注册:1900年1月01日
发表于:2005-09-29 11:15:00
2楼
这个程序只是一般常用pid算法的基本架构,只需要根据采样周期来调用PIDCalc 即可。

拉拉拉

  • 精华:0帖
  • 求助:0帖
  • 帖子:15帖 | 75回
  • 年度积分:0
  • 历史总积分:189
  • 注册:2001年8月12日
发表于:2005-09-30 03:19:00
3楼
需要请教的时候再和妮联系!

浪费青春

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 95回
  • 年度积分:0
  • 历史总积分:124
  • 注册:2004年6月01日
发表于:2005-12-15 11:38:00
4楼
这几天一直在找PID控制的东西,谢谢飞刀

拉拉拉

  • 精华:0帖
  • 求助:0帖
  • 帖子:15帖 | 75回
  • 年度积分:0
  • 历史总积分:189
  • 注册:2001年8月12日
发表于:2006-01-12 13:25:00
5楼
可以,值得借鉴!

lychang67

  • 精华:0帖
  • 求助:0帖
  • 帖子:15帖 | 163回
  • 年度积分:0
  • 历史总积分:419
  • 注册:2001年2月12日
发表于:2006-01-27 21:05:00
6楼
基本框架出来了,但还是不全面的,积分饱和问题,输入、输出限幅、参数有效性检查、输出跟踪等都没有体现出来,要想编好PID程序,必须对数字控制器有所了解

zlcwc

  • 精华:6帖
  • 求助:0帖
  • 帖子:52帖 | 160回
  • 年度积分:0
  • 历史总积分:2066
  • 注册:2003年12月26日
发表于:2006-11-10 01:44:00
7楼
支持,顶下。

ABCDEF

  • 精华:1帖
  • 求助:0帖
  • 帖子:17帖 | 138回
  • 年度积分:0
  • 历史总积分:1321
  • 注册:2006年9月16日
发表于:2006-11-10 10:07:00
8楼
谢谢,值得参考!

shenjl168

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 3回
  • 年度积分:0
  • 历史总积分:3
  • 注册:2007年4月29日
发表于:2007-04-29 14:09:00
9楼
顶!!
好呀!
我正在找也.
谢谢了.大家辛苦了!

club

  • 精华:0帖
  • 求助:0帖
  • 帖子:47帖 | 739回
  • 年度积分:0
  • 历史总积分:0
  • 注册:1900年1月01日
发表于:2007-06-15 14:04:00
10楼
支持,顶下。

hooko

  • 精华:1帖
  • 求助:0帖
  • 帖子:3帖 | 90回
  • 年度积分:0
  • 历史总积分:146
  • 注册:2005年3月03日
发表于:2007-06-26 22:41:00
11楼
值得借鉴!

happyoicq

  • 精华:0帖
  • 求助:0帖
  • 帖子:49帖 | 1382回
  • 年度积分:0
  • 历史总积分:3120
  • 注册:2005年10月16日
发表于:2007-07-25 15:18:00
12楼
谢谢

duonh

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 21回
  • 年度积分:0
  • 历史总积分:64
  • 注册:2008年3月30日
发表于:2008-11-06 17:35:27
13楼

这么好的资料,值得学习学习.


热门招聘
相关主题

官方公众号

智造工程师