|
在网上找了大概3个版本的PID控制程序,但逻辑不同,不知道那个版本是正确的?
版本1:应该是基于△Uk=A*e(k)+B*e(k-1)+C*e(k-2)
typedef struct PID
{
int SetPoint; //设定目标Desired Value
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;
int IncPIDCalc(int NextPoint)
{
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); //返回增量值
}
版本2:应该是基于△Uk=Kp[e(k)-e(k-1)] + Ki*e(k) + Kd[e(k)-2e(k-1)+e(k-2)]
typedef struct PID { //结构体定义
int SetPoint //设定值
int Proportion; // Proportion 比例系数
int Integral; // Integral 积分系数
int Derivative; // Derivative 微分系数
int LastError; // Error[-1] 前一拍误差
int PreError; // Error[-2] 前两拍误差
} PID;
//Title:增量式PID算法程序
//Description:给出一个误差增量
//Input: PID的P、I控制常数和之前的误差量(PID *pp)& 当前误差量(ThisError)
//Return: 误差增量templ
int PIDCal( PID *pp, int ThisError ){
//增量式PID算法(需要控制的不是控制量的绝对值,而是控制量的增量)
int pError,dError,iError;
long templ;
pError = ThisError-pp->LastError;
iError = ThisError;
dError = ThisError-2*(pp->LastError)+pp->PreError;
//增量计算
templ=pp->Proportion*pError + pp->Integral*iError+pp->Derivative*dError; //增量
//存储误差用于下次运算
pp->PreError = pp->LastError;
pp->LastError = ThisError;
return ((int)(templ>>8));
}
版本3:不知道基于什么公式
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 // 微分项
);
} |
|