如果本文章对您确实有用,还望不吝啬点赞收藏一下,您的认可是我最大的更新动力。
不管是哪个单片机IDE,stm也好,arduino什么的也好,在不借助单片机平台的内置函数情况下(其实我也没找到keil5的),调节舵机转速其实是可以通过算法来实现的,先上代码
double timestep = 0.009;
double a0; double a1; double a2; double a3;double a4; double a5;
double omegaf;double omegai; double acci; double accf;
double tf = 0.8;
double thetai=45; double thetaf=135;
double theta0;double omega0;double acc0;
double t=0;
int i = 0;
for ( t = 0; t <= tf; t +=timestep) { // goes from 0 degrees to 180 degrees
//a0 = 70;a1 = 0;a2 = 30 ;a3 = -10;
thetai=58; thetaf=98;
omegai = 35; omegaf = 125;
acci = 20; accf = 40;
a0 = thetai;
a1 = omegai;
a2 = acci/2;
a3 = (20*thetaf-20*thetai-(8*omegaf+12*omegai)*tf-(3*acci-accf)*(pow(tf,2))) / (2*(pow(tf,3)));
a4 = (30*thetai-30*thetaf+(14*omegaf+16*omegai)*tf+(3*acci-2*accf)*(pow(tf,2))) / (2*(pow(tf,4)));
a5 = (12*thetaf - 12*thetai - (6*omegaf+6*omegai)*tf-(acci-accf)*(pow(tf,2))) / (2*(pow(tf,5)));
theta0 = a0 +a1*t +a2*(pow(t,2))+ a3*(pow(t,3)) + a4*(pow(t,4)) + a5*(pow(t,5));
omega0 = a1 + 2*a2*t + 3*a3*(pow(t,2)) + 4*a4*(pow(t,3)) + 5*a5*(pow(t,4));
acc0 = 2*a2 + 6*a3*t + 12*a4*(pow(t,2)) + 20*a5*(pow(t,3));
//theta0 = (float)theta0;
Servo_Set_Angle(theta0); // tell servo to go to position in variable 'pos'
delay_ms(15); // waits 15 ms for the servo to reach the position
}
for ( t = 0; t <= tf; t +=timestep) { // goes from 0 degrees to 180 degrees
//a0 = 70;a1 = 0;a2 = 30 ;a3 = -10;
thetai=120; thetaf=60;
omegai = 50; omegaf = 40;
acci = 15; accf = 5;
a0 = thetai;
a1 = omegai;
a2 = acci/2;
a3 = (20*thetaf-20*thetai-(8*omegaf+12*omegai)*tf-(3*acci-accf)*(pow(tf,2))) / (2*(pow(tf,3)));
a4 = (30*thetai-30*thetaf+(14*omegaf+16*omegai)*tf+(3*acci-2*accf)*(pow(tf,2))) / (2*(pow(tf,4)));
a5 = (12*thetaf - 12*thetai - (6*omegaf+6*omegai)*tf-(acci-accf)*(pow(tf,2))) / (2*(pow(tf,5)));
theta0 = a0 +a1*t +a2*(pow(t,2))+ a3*(pow(t,3)) + a4*(pow(t,4)) + a5*(pow(t,5));
omega0 = a1 + 2*a2*t + 3*a3*(pow(t,2)) + 4*a4*(pow(t,3)) + 5*a5*(pow(t,4));
acc0 = 2*a2 + 6*a3*t + 12*a4*(pow(t,2)) + 20*a5*(pow(t,3));
//theta0 = (float)theta0;
Servo_Set_Angle(theta0); // tell servo to go to position in variable 'pos'
//Serial.print(theta0);
//Serial.println(t);
delay_ms(15); // waits 15 ms for the servo to reach the position
}
这个代码我是在keil5中运行stm32f103的速度调节代码,实际运行出来的效果如下图所示
可以看出舵机从48°转到108°是一条曲线关系,你可以通过修改Omega修改角速度,acc修改加速度,值得注意的是Servo_set_angle是指定转到的角度,其实这个算法的原理是把转动过程拆分成84步,相当于把转动过程离散化,一个一个片段拼接起来就能调节转速了,更顺滑,降低刚性碰撞,柔性碰撞。