增量式PID代码设计实现

增量式PID代码设计实现

/结构体定义/
typedef struct
{
__IO int32_t SetPoint; //设定目标 Desired Value
__IO float Proportion; //比例常数 Proportional Const
__IO float Integral; //积分常数 Integral Const
__IO float Derivative; //微分常数 Derivative Const
__IO int LastError; //Error[-1]
__IO int PrevError; //Error[-2]
}PID_TypeDef;

/PID参数初始化/
PID_TypeDef sPID; // PID参数结构体
void PID_ParamInit()
{
sPID.LastError = 0; // Error[-1]
sPID.PrevError = 0; // Error[-2]
sPID.Proportion = 0; // 比例常数 Proportional Const
sPID.Integral = 0; // 积分常数 Integral Const
sPID.Derivative = 0; // 微分常数 Derivative Const
sPID.SetPoint = 0; // 设定目标Desired Value
}

/PID算法实现/
/**

  • 输入参数:当前控制量
  • 返 回 值:目标控制量
    */
    int32_t SpdPIDCalc(float NextPoint)
    {
    float iError,iIncpid;
    iError = (float)sPID.SetPoint - NextPoint; //误差

/* 消除抖动误差 */
if((iError<0.05f )&& (iError>-0.05f))
iError = 0.2f;

iIncpid=(sPID.Proportion *iError) //E[k]项
-(sPID.Integral * sPID.LastError) //E[k-1]项
+(sPID.Derivative * sPID.PrevError); //E[k-2]项

sPID.PrevError = sPID.LastError; //存储误差,用于下次计算
sPID.LastError = iError;
return(); //返回增量值

}

你可能感兴趣的:(开发语言,c语言)