增量式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)
2019-3-17 11:00 上傳
點擊文件名下載附件
增量式pid
|