高级会员
- 积分
- 855
- 威望
- 403
- 贡献
- 220
- 兑换币
- 259
- 注册时间
- 2016-12-27
- 在线时间
- 116 小时
- 毕业学校
- 中堂小学
|
电机如何闭环???真心不会了,搞了一阵子,电机现在不听预定速度的话,一直加到最大速度。单独测试编码器读数没问题,一直搞不懂编码器读回来的是啥数,直接就可以和电机占空比相减????
附上我的PID代码:
void motor_PID(int32 left_motor, int32 right_motor, float P, float I ,float D)
{
static uint32 left_ek_2 = 0, left_ek_1 = 0, right_ek_1 = 0, right_ek_2 = 0, first_run = 0;
int32 error_left = 0, error_right = 0;
int32 en_count_left = 0, en_count_right = 0;;
left_motor *= 0.02; right_motor *= 0.02;
en_count_left = ftm_quad_get(encode_left); //读速度
en_count_right = ftm_quad_get(encode_right; //取整
up= en_count_right;
if(first_run == 0)
{
first_run = 1;
left_ek_2 = 1; right_ek_2 = 1;
left_ek_1 = 1; right_ek_1 = 1;
}
error_left = left_motor - en_count_left; error_right = right_motor - en_count_right;
PWM_left = PWM_left_last + (int32)P * error_left + I * (error_left - (2 * left_ek_1 + left_ek_2)) + D *(error_left - left_ek_1);
PWM_right = PWM_right_last + (int32)P * error_right + I * (error_right - (2 * right_ek_1 + right_ek_2)) + D *(error_right - right_ek_1);
//____________________更新偏差_______________________//
left_ek_2 = left_ek_1; right_ek_2 = right_ek_1;
left_ek_1 = error_left; right_ek_2 = error_right;
//____________________电机限额______________________//
if(PWM_left > MAX_left_motor_PWM)
{
PWM_left = MAX_left_motor_PWM;
}
else if(PWM_left < MIN_left_motor_PWM)
{
PWM_left = MIN_left_motor_PWM;
}
if(PWM_right > MAX_right_motor_PWM)
{
PWM_right = MAX_right_motor_PWM;
}
else if(PWM_right < MIN_right_motor_PWM)
{
PWM_right = MIN_right_motor_PWM;
}
PWM_left_last = PWM_left; PWM_right_last = PWM_right;
//_____________________送给电机_________________________//
if(PWM_left > 0) //正转
{
PWM_left = PWM_left;
ftm_pwm_init(FTM0, FTM_CH4, 20000, PWM_left);
ftm_pwm_init(FTM0, FTM_CH5, 20000, 0);
}
else if(PWM_left < 0) //反转
{
PWM_left = -PWM_left;
ftm_pwm_init(FTM0, FTM_CH4, 20000, 0);
ftm_pwm_init(FTM0, FTM_CH5, 20000, PWM_left);
}
if(PWM_right > 0) //正转
{
PWM_right = PWM_right;
ftm_pwm_init(FTM0, FTM_CH6, 20000, PWM_right);
ftm_pwm_init(FTM0, FTM_CH7, 20000, 0);
}
else if(PWM_right < 0) //反转
{
PWM_right = -PWM_right;
ftm_pwm_init(FTM0, FTM_CH6, 20000, 0);
ftm_pwm_init(FTM0, FTM_CH7, 20000, PWM_right);
}
PWM_right_last = PWM_right;
PWM_left_last = PWM_left;
/*-------------------end-------------------------*/
}
求大神看看.....
|
|