xuepeng2000 发表于 2004-11-24 20:02:48

这是从网上找来的一个比较典型的PID处理程序

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

cpubbs 发表于 2004-11-24 21:00:27

好!兄弟发的贴子都很好的!精华!支持你!谢谢!

fresh 发表于 2007-5-14 12:16:08

好东西啊
正想做 这个呢,居然在论坛里找到了,历史很久远呢
页: [1]
查看完整版本: 这是从网上找来的一个比较典型的PID处理程序