Arduino 舵机控制小车运动代码解析
这段代码描述了使用 Arduino 控制小车运动的过程,通过控制两个舵机的转速来控制小车的方向和速度。
#include <servo.h>
servo servoLeft;
servo servoRight;
void setup ()
{
servoLeft.attach ( 13 );
servoRight.attach ( 12 );
maneuver(200, 200, 2000) ;
maneuver(-200, 200, 600) ;
maneuver(200, -200, 600) ;
maneuver (-200, -200, 2000) ;
maneuver ( 0, 0, -1);
}
void loop ( ){
}
void maneuver (int speedLeft, int speedRight, int msTime){
servoLeft.writeMicroseconds ( 1500 + speedLeft) ;
servoRight.writeMicroseconds (1500 - speedRight) ;
if (msTime == -1)
servoLeft.detach ( ) ;
servoRight.detach ( ) ;
delay (msTime) ;
}
在 setup 函数中,通过调用 maneuver 函数来实现小车的运动,具体运动过程如下:
- 先向前行驶,左右轮速度为 200,持续时间为 2000 毫秒;
- 向右转弯,左轮速度为 -200,右轮速度为 200,转动角度为 60 度,持续时间为 600 毫秒;
- 向左转弯,左轮速度为 200,右轮速度为 -200,转动角度为 60 度,持续时间为 600 毫秒;
- 向后行驶,左右轮速度为 -200,持续时间为 2000 毫秒;
- 停止运动,左轮速度为 0,右轮速度为 -1,持续时间为 1 毫秒。
在 maneuver 函数中,通过调用 writeMicroseconds 函数来设置左右轮的转速,同时通过 detach 函数来解除舵机的控制,以实现停止运动。 delay 函数用于控制小车运动的持续时间。
原文地址: https://www.cveoy.top/t/topic/oxxZ 著作权归作者所有。请勿转载和采集!