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;
//=============================================================
// Function: IncPIDInit()
// Syntax: void IncPIDInit(void);
// Description: Initialization PID parameter.
// Notes:
// parameters: none
// returns: none
//=============================================================
void PID_Init(void)
{
sptr->LastError = 0; //Error[-1]
sptr->PrevError = 0; //Error[-2]
sptr->Proportion = 0.3; //比例常数 Proportional Const
sptr->Integral = 0.2; //3.5积分常数 Integral Const
sptr->Derivative = 0.1; //1.25微分常数 Derivative Const
sptr->SetPoint = 0;
sptr->SumError = 0;
}
//=============================================================
// ----Function: IncPIDSetPoint()
// ------Syntax: void IncPIDSetPoint(unsigned int setpoint);
// -Description: Set PID Desired Value
// -------Notes:
// --parameters: Desired Value
// -----returns: none
//=============================================================
void PIDSetPoint(int setpoint)
{ sptr->SetPoint = setpoint; }
int PIDGetSetpoint(void)
{ return(sptr->SetPoint); }
//=============================================================
// ----Function: IncPIDKp()
// ------Syntax: void IncPIDKp(double dKp);
// -Description: Set PID Proportion coefficient
// -------Notes:
// --parameters: Proportion Const
// -----returns: none
//=============================================================
void PIDSetKp(double dKpp)
{ sptr->Proportion = dKpp; }
//===================================//
// Get Proportion
//===================================//
double PIDGetKp(void)
{ return(sptr->Proportion); }
//=============================================================
// ----Function: IncPIDKi()
// ------Syntax: void IncPIDKi(double dKi);
// -Description: Set PID Integral coefficient
// -------Notes:
// --parameters: Integral Const
// -----returns: none
//=============================================================
void PIDSetKi(double dKii)
{ sptr->Integral = dKii; }
//===================================//
// Get Integral
//===================================//
double PIDGetKi(void)
{ return(sptr->Integral); }
//=============================================================
// ----Function: IncPIDKd()
// ------Syntax: void IncPIDKd(double dKd);
// -Description: Set PID Derivative coefficient
// -------Notes:
// --parameters: Derivative Const
// -----returns: none
//=============================================================
void PIDSetKd(double dKdd)
{ sptr->Derivative = dKdd; }
//===================================//
// Get Derivative
//===================================//
double PIDGetKd(void)
{ return(sptr->Derivative); }
void Self_ConfigPID(double uiKp)
{
double dKp;
dKp = uiKp;
PIDSetKp(2.45*dKp);
PIDSetKi(3.50*dKp);
PIDSetKd(1.25*dKp);
}
//=============================================================
// ----Function: IncPIDCalc()
// ------Syntax: int IncPIDCalc(unsigned int NextPoint);
// -Description: Increment Digital PID calculate
// -------Notes: Basic Increment Digital PID
// --parameters: Next Point
// -----returns: increase controls parameter
//=============================================================
int IncPIDCalc(int NextPoint)
{
register int iError, iIncpid;
iError = sptr->SetPoint - NextPoint;
iIncpid = sptr->Proportion * iError //E[0]
+ sptr->Integral * sptr->LastError //E[-1]
+ sptr->Derivative * sptr->PrevError; //E[-2]
sptr->PrevError = sptr->LastError;
sptr->LastError = iError;
return(iIncpid);
}
//=============================================================
// ----Function: LocPIDCalc()
// ------Syntax: unsigned int locPIDCalc(unsigned int NextPoint);
// -Description: Location Digital PID calculate
// -------Notes: Basic Location Digital PID
// --parameters: Next Point
// -----returns: Location controls parameter
//=============================================================
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);
}
//=============================================//
// *END*
//=============================================//
|