增量式pid语言代码,初学者可以参考下
源程序如下:
- #define _PID_C
- #include "pid.h"
- #include <math.h>
- #define MAXOUT 1000 //输出最大值
- void IncPIDInit(void)
- {
- sptr = &sPID;
- sptr->SetPoint = 700; //设定值
- sptr->BitMove = 0; //返回结果比例
- sptr->LastError = 0; //前2次误差值
- sptr->PrevError = 0; //前1次误差值
- sptr->Proportion = 3; //比例
- sptr->Integral = 0; //积分
- sptr->Derivative = 0; //微分
- sptr->iError = 0; //当前误差
- sptr->iIncpid=0; //增量误差
- sptr->Uk = 0; //输出返回值
- }
- int IncPIDCalc(int NextPoint)
- {
- //当前误差
- sptr->iError = sptr->SetPoint - NextPoint;
- //增量误差
- sptr->iIncpid= sptr->Proportion * sptr->iError
- - sptr->Integral * sptr->LastError
- + sptr->Derivative * sptr->PrevError;
- //存储误差,用于下次计算
- sptr->PrevError = sptr->LastError;
- sptr->LastError = sptr->iError;
- sptr->Uk += sptr->iIncpid;
- //输出值限幅
- if ((sptr->Uk>>sptr->BitMove) >= MAXOUT)
- {
- sptr->Uk = MAXOUT;
- }
- else if((sptr->Uk>>sptr->BitMove) <= 0)
- {
- sptr->Uk = 0;
- }
- else sptr->Uk = sptr->Uk>>sptr->BitMove;
-
- return(sptr->Uk);
- }
复制代码- #ifndef _PID_H
- #ifndef _PID_H
- #ifdef _PID_C
- #define PID_EXT
- #else
- #define PID_EXT extern
- #endif
- typedef struct PID
- {
- int SetPoint;
-
- unsigned char BitMove;
-
- float Proportion;
- float Integral;
- float Derivative;
-
- int iError;
- int iIncpid;
-
- int LastError;
- int PrevError;
-
- int Uk;
- }PID,*pPID;
- PID_EXT PID sPID;
- PID_EXT pPID sptr;
- void IncPIDInit(void);
- int IncPIDCalc(int NextPoint);
- #endif
复制代码
所有资料51hei提供下载:
增量式PID代码C语言实现实现.rar
(1.19 KB, 下载次数: 45)
|