原创 PID程序

2011-9-13 11:10 1660 11 11 分类: MCU/ 嵌入式

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


 sptr->SumError = sumerror;     //误差累计
 sptr->LastError = lasterror;  //Error[k-1]
 sptr->PrevError = preverror;  //Error[k-2]
 sptr->Proportion = proportion;  //比例常数 Proportional Const
 sptr->Integral = integral;  //积分常数Integral Const
 sptr->Derivative = derivative;  //微分常数 Derivative Const
 sptr->SetPoint = setpoint; //设定目标 Desired Value
}
//增量式PID控制设计
//输入参数:NextPoint  当前采样值
//返回值:  iIncpid    增量值
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控制设计
//输入参数:NextPoint  当前采样值
//返回值:  iIncpid    位置值
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); //微分项
}*/

文章评论0条评论)

登录后参与讨论
我要评论
0
11
关闭 站长推荐上一条 /2 下一条