cpubbs论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

LabVIEW+单片机学习套件全套教程资料下载[免费]LabVIEW论坛精华列表贴USB0816数据采集卡《LabVIEW宝典》
LabWindows/CVI论坛精华贴NET0816以太网数据采集卡RC0210远程设备授权系统 关闭关停锁定打开设备 户外分布式数据采集
NET1624低速高精度以太网数据采集卡WIFI0824SD无线WIFI网络数据采集卡脱机运行 SD存储 小尺寸微型 串口采集远程采集 安卓 手持移动采集 纪录仪
查看: 2129|回复: 2

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

[复制链接]
发表于 2004-11-24 20:02:48 | 显示全部楼层 |阅读模式
<>/*====================================================================================================
    这是从网上找来的一个比较典型的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>
<>    While the PID function works, main is just a dummy program showing
    a typical usage.
=====================================================================================================*/</P>
<>typedef struct PID {</P>
<P>        double  SetPoint;           //  设定目标 Desired Value</P>
<P>        double  Proportion;         //  比例常数 Proportional Const
        double  Integral;           //  积分常数 Integral Const
        double  Derivative;         //  微分常数 Derivative Const</P>
<P>        double  LastError;          //  Error[-1]
        double  PrevError;          //  Error[-2]
        double  SumError;           //  Sums of Errors</P>
<P>} PID;</P>
<P>/*====================================================================================================
   PID计算部分
=====================================================================================================*/</P>
<P>double PIDCalc( PID *pp, double NextPoint )
{
    double  dError,
            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>
发表于 2004-11-24 21:00:27 | 显示全部楼层
好!兄弟发的贴子都很好的!精华!支持你!谢谢![em08][em29][em31]
发表于 2007-5-14 12:16:08 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|cpubbs论坛. ( 粤ICP备09171248号 )

GMT+8, 2025-5-4 12:49 , Processed in 2.100592 second(s), 8 queries , Gzip On, File On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

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