PLC论坛-全力打造可编程控制器专业技术论坛

 找回密码
 注册哦

QQ登录

只需一步,快速开始

微信扫码登录

查看: 241|回复: 0

增量式和位置式PID代码

[复制链接]
发表于 2010-9-14 05:48:00 | 显示全部楼层

增量式和位置式PID代码

//数据结构
typedef struct PID
{
  int  SetPoint;     //设定目标 Desired Value
  long SumError;               //误差累计 
 
  double  Proportion;         //比例常数 Proportional Const
  double  Integral;           //积分常数 Integral Const
  double  Derivative;         //微分常数 Derivative Const
 
  int LastError;               //Error[-1]
  int PrevError;               //Error[-2]
} PID;
static PID  sPID;
static PID  *sptr = &sPID;
 
//PID 参数初始化
void IncPIDInit(void)
{
  sptr->SumError   = 0;    
   sptr->LastError = 0;    //Error[-1]
   sptr->PrevError = 0;    //Error[-2]
 
  sptr->Proportion = 0;    //比例常数 Proportional Const
   sptr->Integral   = 0;    //积分常数 Integral Const
   sptr->Derivative = 0;    //微分常数 Derivative Const
   sptr->SetPoint   = 0;        
}


//增量式 PID 控制设计
int IncPIDCalc(int NextPoint)
{
  register int iError, iIncpid;
   //当前误差
  iError = sptr->SetPoint - NextPoint;
   //增量计算
  iIncpid = sptr->Proportion * iError               //E[k]项
              - sptr->Integral   * sptr->LastError     //E[k-1]项
             + sptr->Derivative * sptr->PrevError;   //E[k-2]项
   //存储误差,用于下次计算
  sptr->PrevError = sptr->LastError;
  sptr->LastError = iError;
   //返回增量值
 return(iIncpid);
}
//位置式 PID 控制设计
unsigned int LocPIDCalc(int NextPoint)
{
    register int  iError,dError;
 
  iError = sptr->SetPoint - NextPoint;       //偏差
  sptr->SumError += iError;       //积分
  dError = iError - sptr->LastError;     //微分
  sptr->LastError = iError;
    
  return(sptr->Proportion * iError            //比例项
           + sptr->Integral * sptr->SumError   //积分项
           + sptr->Derivative * dError);        //微分项
}
以上代码是真正可以用的,很讨厌某些所谓的“学士”把书本中的公式用C写一遍,说这是某某理论。

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册哦

本版积分规则

QQ|小黑屋|手机版|Archiver|PLC技术网-PLC论坛 ( 粤ICP备17165530号 )|网站地图

GMT+8, 2024-9-21 01:26 , Processed in 0.049880 second(s), 22 queries .

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