void pid_calc(void) //pid计算
{
float dk1;
// float Iout;
// float Pout;
// float Dout;
float t1;
float t2;
float t3;
//if(pidcalcms<(pid.T)) { return ;}//计算周期未到
pid.Curr = Get18BitMcp3422();
pid.En = pid.Set - pid.Curr; //得到当前的偏差值
t1 = pid.Kp * pid.En; //比例输出
pid.SEn += pid.En; //历史偏差总和
dk1 = pid.En - pid.En_1; //最近两次偏差之差
t2 =0;// (pid.T * pid.Kp) / pid.Ki;
t2 = t2 * pid.SEn * pid.Kp; //积分输出
t3 = (pid.Kp * pid.Kd) / pid.T;
t3 = t3 * dk1; //微分输出
pid.Dout = (t1 + t2 + t3)/1000;
pid.Currpwm += pid.Dout; //本次应该输出的PWM
//////////////////////////////////////////////////////////
if(pid.Dout > pid.Pwm_Cycle)
{
pid.Currpwm = pid.Pwm_Cycle;
}
else if(pid.Dout < 1)
{
pid.Currpwm = 0;
}
//pid.OUT+=; //本次的计算结果
pid.En_1 = pid.En; //更新偏差
pidcalcms = 0;
}
void Tim1_CH1_DutyCycle_Change(u16 Pulse)
{
TIM1->CCR1 = Pulse;
}
这两个要如何关联在一起 |