FOC算法中的电流环,速度环都涉及到PID控制 PID又分位置式PID控制算法和增量式PID控制算法,而个人认为在电机控制中使用增量式PID效果会更好一些。下面主要介绍几种增量式PID控制算法及C代码实现。 1.1AN1078文档中的PID PI结构框图和原理
C代码实现: //电流环D轴PI控制 void PI_Control_D( PID_ID_TypeDef *dParm)
{
int32_t U,Excess;
U = (dParm->Id_Sum + dParm->Id_Kp*dParm->Id_Err)>>12; if(U > dParm->Id_OutMax)
{
U = dParm->Id_OutMax;
} elseif(U < dParm->Id_OutMin)
{
U = dParm->Id_OutMin;
}else{
dParm->Id_Out = U;
}
Excess = U - dParm->Id_Out;
dParm->Id_Sum = dParm->Id_Sum + dParm->Id_Ki*dParm->Id_Err - dParm->Id_Kc*Excess;
}1234567891011121314151617181920 1.2传统增量式PID 离散化公式:
C代码实现: int16_t PIDControl_Q(PID_IQ_TypeDef * PID, int16_t Ref, int16_t Cur)
{
int32_t Kp_Out, Ki_Out, PID_Out,PID_Out_Last;if(!PID->Iq_Err)
{ PID->Iq_Err = Ref - Cur;// 初始化PID当前偏差 PID->Iq_Err_Err = PID->Iq_Err - PID->Iq_Err_Last1;// 初始化PID上次偏差和上上次偏差之差 PID->Iq_Err_Last1 = Ref - Cur;// 初始化PID上次偏差 PID->Iq_Err_Err_Last1 = PID->Iq_Err_Err;
}else{ PID->Iq_Err_Last1 = PID->Iq_Err;// 保存PID上次偏差 PID->Iq_Err = Ref - Cur;// 计算PID当前偏差 PID->Iq_Err_Err = PID->Iq_Err - PID->Iq_Err_Last1;// 计算PID上次偏差和上上次偏差之差}
Kp_Out = ((int32_t)PID->Iq_Kp * (int32_t)PID->Iq_Err_Err)>>12;
Ki_Out = ((int32_t)PID->Iq_Ki * (int32_t)PID->Iq_Err)>>12;
PID_Out = PID->Iq_Out;
PID_Out += Kp_Out + Ki_Out;if(PID_Out > PID->Iq_OutMax)
{
PID_Out = PID->Iq_OutMax;// PID最高输出} if(PID_Out < PID->Iq_OutMin)
{
PID_Out = PID->Iq_OutMin;// PID最低输出}
PID->Iq_Out = PID_Out;returnPID->Iq_Out;
} 1.3back-cal PID 结构框图:
C代码实现: /**
* PI controller
* */static float _pi(structBackCalPID*that, float err)
{
that->i_err = err;
own.m_pre_out = that->i_err * own.m_kp + own.m_sum;
that->o_out = own.m_pre_out;/*output clamp*/ if(that->o_out > own.m_out_up)
{
that->o_out = own.m_out_up;
}elseif(that->o_out < own.m_out_low)
{
that->o_out = own.m_out_low;
}/*back-cal dynamic Integrator clamp*/ own.m_sum += that->i_err * own.m_ki + own.m_kc * (that->o_out-own.m_pre_out); return that->o_out;
} 上述具体的PID.c/.h文件链接:https://download.csdn.net/download/strive3/87124205 以上几种PID控制算法均可以实现电机控制中的电流环,速度环PI控制。
|