如何调节舵机的转速?

本文介绍了一种无需借助单片机IDE内置函数的方法,通过编程算法在STM32F103的Keil5环境中调节舵机转速。作者通过示例代码展示了如何通过改变角速度和加速度参数实现平滑转速控制,将转动过程离散化以提高灵活性。
摘要由CSDN通过智能技术生成

如果本文章对您确实有用,还望不吝啬点赞收藏一下,您的认可是我最大的更新动力。

不管是哪个单片机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步,相当于把转动过程离散化,一个一个片段拼接起来就能调节转速了,更顺滑,降低刚性碰撞,柔性碰撞。

  • 13
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值