yfgww 发表于 2012-9-12 15:32:44

电机控制,PID的问题

U8 mc_control_current(U8 cur_cmd)
{
U8 Duty = 0;
S32 increment = 0;

// Error calculation
cur_error = cur_cmd - (mci_get_measured_current());// value -255 <=> 255

// proportional term calculation
cur_proportional = Kp_cur*cur_error;

// integral term calculation
cur_integral = cur_integral + cur_error;

if(cur_integral >255) cur_integral =255;
if(cur_integral < -255) cur_integral = -255;

cur_integ = Ki_cur*cur_integral;

// derivative term calculation
/*cur_derivative = cur_error - last_cur_error;

if(cur_derivative >255) cur_derivative =255;
if(cur_derivative < -255) cur_derivative = -255;

cur_der = Kd_cur*cur_derivative;

last_cur_error = cur_error;*/

// Duty Cycle calculation
increment = cur_proportional + cur_integ;
//increment += cur_der;
increment = increment >> K_cur_scal;

// Variable saturation
if(increment >= (S16)(255)) Duty = 255;
else
{
    if(increment <= (S16)(0)) Duty =   0;
    else Duty = (U8)(increment);
}

// return Duty Cycle
return Duty;
}
这是AVR官方的一个电机控制例程。
主程序是每250us 调用一次此函数,返回值作为为PWM的占空比。当设置电流与参考电流一样时, cur_error=0;每次调用cur_integ = Ki_cur*cur_integral;值都会变化,怎么实现速度稳定的呢?

tiancaigao7 发表于 2012-9-12 18:38:24

你到底是要控制速度还是电流?速度环不需要这么高的控制频率。10ms一次足够了

yfgww 发表于 2012-9-12 19:03:49

tiancaigao7 发表于 2012-9-12 18:38 static/image/common/back.gif
你到底是要控制速度还是电流?速度环不需要这么高的控制频率。10ms一次足够了 ...

电流闭环
页: [1]
查看完整版本: 电机控制,PID的问题