hetao7241 发表于 2009-5-9 16:26:23

整理一些PID程序,欢迎大家指正.

这段时间搞PID,整理了一些程序:
1.这个程序是本网站的,把PID部分复制到GCC可以编译通过,但是这个程序是位置式的,
=====================================================================================================*/
#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 {

      doubleSetPoint;         //设定目标 Desired Value

      doubleProportion;         //比例常数 Proportional Const
      doubleIntegral;         //积分常数 Integral Const
      doubleDerivative;         //微分常数 Derivative Const

      doubleLastError;          //Error[-1]
      doublePrevError;          //Error[-2]
      doubleSumError;         //Sums of Errors

} PID;

/*====================================================================================================
   PID计算部分
=====================================================================================================*/

double PIDCalc( PID *pp, double NextPoint )
{
    doubledError,
            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
    }
}
2.增量式的,参照这个程序:

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;
double PIDCalc (struct PID *pp,double NextPoint)

double dError,Error;
Error = pp->SetPoint - NextPoint; //偏差
pp->SumError += Error; //积分
dError =pp->SumError-2*pp->LastError + pp->PrevError;
//当前微分
pp->PrevError = pp->LastError;
pp->LastError = Error;
return (pp->Proportion * Error //比例项
+pp->Integral * pp->SumError //积分项
+pp->Derivative * dError // 微分项
);
其实一个是Uk,一个是Uk-Uk-1,

deepin 发表于 2009-6-23 11:38:57

MARK

gonghuashi 发表于 2010-4-21 19:36:28

赞一个

gxy508 发表于 2010-5-7 14:24:01

mark

avrwoo 发表于 2010-5-7 14:54:52

mark

robustman 发表于 2010-5-7 16:38:20

现在发现高数TMD太重要了。以前的高数也不知道怎么考的,真有种马上冲出去买本高数的冲动。

tangwei039 发表于 2010-5-7 17:38:15

Mark

jj.deng 发表于 2010-5-7 17:51:47

学习了~

qaswd 发表于 2010-5-8 12:17:56

谢谢

plc_avr 发表于 2010-5-8 16:50:52

MARK一下。

aohuahua 发表于 2010-5-9 10:31:59

MARK

zhq1989723 发表于 2011-1-24 12:39:51

mark

hanlinsong 发表于 2011-1-24 16:34:13

mark

waterloo100 发表于 2011-1-26 00:03:43

mark

jianchangd 发表于 2011-1-26 09:14:48

mark

shizt 发表于 2011-1-26 09:17:44

mark

myhonour 发表于 2011-1-26 13:50:22

ark

ZacharyGuo 发表于 2011-1-29 09:44:43

先收藏,以后参考下啊。。。。

xuyqhd 发表于 2011-9-1 13:31:23

mark

walshao 发表于 2011-9-1 13:36:15

PIDo ^

kim5257 发表于 2011-9-1 13:55:44

dError =pp->SumError-2*pp->LastError + pp->PrevError在这里。

hz77776666 发表于 2011-9-2 14:46:52

mark

zhz868 发表于 2012-1-2 23:46:48

温习温习

walux 发表于 2012-1-9 09:24:59

mark

tangwubing1988 发表于 2012-1-16 11:44:58

mark

bluefeel 发表于 2012-1-16 11:48:12

mark

dingliming 发表于 2012-1-16 17:05:36

mark

Rain0805 发表于 2014-2-11 09:31:56

mark.............

grs432 发表于 2014-2-17 10:25:17

{:tongue:}留个标记。。

ray1136 发表于 2014-2-19 17:15:14


mark            
页: [1]
查看完整版本: 整理一些PID程序,欢迎大家指正.