搜索
bottom↓
回复: 30

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

[复制链接]

出0入0汤圆

发表于 2009-5-9 16:26:23 | 显示全部楼层 |阅读模式
这段时间搞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 {  

        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  
    }  
}  
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,

阿莫论坛20周年了!感谢大家的支持与爱护!!

曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……

出0入0汤圆

发表于 2009-6-23 11:38:57 | 显示全部楼层
MARK

出0入0汤圆

发表于 2010-4-21 19:36:28 | 显示全部楼层
赞一个

出0入0汤圆

发表于 2010-5-7 14:24:01 | 显示全部楼层
mark

出0入0汤圆

发表于 2010-5-7 14:54:52 | 显示全部楼层
mark

出0入0汤圆

发表于 2010-5-7 16:38:20 | 显示全部楼层
现在发现高数TMD太重要了。以前的高数也不知道怎么考的,真有种马上冲出去买本高数的冲动。

出0入0汤圆

发表于 2010-5-7 17:38:15 | 显示全部楼层
Mark

出0入0汤圆

发表于 2010-5-7 17:51:47 | 显示全部楼层
学习了~

出0入0汤圆

发表于 2010-5-8 12:17:56 | 显示全部楼层
谢谢

出0入0汤圆

发表于 2010-5-8 16:50:52 | 显示全部楼层
MARK一下。

出0入0汤圆

发表于 2010-5-9 10:31:59 | 显示全部楼层
MARK

出0入0汤圆

发表于 2011-1-24 12:39:51 | 显示全部楼层
mark

出0入0汤圆

发表于 2011-1-24 16:34:13 | 显示全部楼层
mark

出0入0汤圆

发表于 2011-1-26 00:03:43 | 显示全部楼层
mark

出0入0汤圆

发表于 2011-1-26 09:14:48 | 显示全部楼层
mark

出0入0汤圆

发表于 2011-1-26 09:17:44 | 显示全部楼层
mark

出0入0汤圆

发表于 2011-1-26 13:50:22 | 显示全部楼层
ark

出0入0汤圆

发表于 2011-1-29 09:44:43 | 显示全部楼层
先收藏,以后参考下啊。。。。

出0入0汤圆

发表于 2011-9-1 13:31:23 | 显示全部楼层
mark

出0入0汤圆

发表于 2011-9-1 13:36:15 | 显示全部楼层
PIDo ^

出0入0汤圆

发表于 2011-9-1 13:55:44 | 显示全部楼层
dError =pp->SumError-2*pp->LastError + pp->PrevError在这里。

出0入0汤圆

发表于 2011-9-2 14:46:52 | 显示全部楼层
mark

出0入0汤圆

发表于 2012-1-2 23:46:48 | 显示全部楼层
温习温习

出0入0汤圆

发表于 2012-1-9 09:24:59 | 显示全部楼层
mark

出0入0汤圆

发表于 2012-1-16 11:44:58 | 显示全部楼层
mark

出0入85汤圆

发表于 2012-1-16 11:48:12 | 显示全部楼层
mark

出0入0汤圆

发表于 2012-1-16 17:05:36 | 显示全部楼层
mark

出0入0汤圆

发表于 2014-2-11 09:31:56 | 显示全部楼层
mark.............

出0入0汤圆

发表于 2014-2-17 10:25:17 | 显示全部楼层
留个标记。。

出0入0汤圆

发表于 2014-2-19 17:15:14 | 显示全部楼层

mark            
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-8-26 17:26

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表