踩了许多坑,看了许多教程,终于成功用ros_arduino桥的方法实现了对电机的控制,希望大家能通过我的教训少走一些弯路。
注:本教程的所有代码可以进入我主页下载
step1:
ubuntu下安装arduino IDE(具体方法可直接搜索,这里就暂时略过)
step2:
利用arduino IDE将下位机代码烧录到arduino中,注意我使用的是arduino uno,而很多教程使用的是arduino mega2560,不同的板子应该对代码进行适当的修改,好,话不多说,我们来看看具体步骤。
step2.1:进入catkin_ws/src git下来官方的包
cd ~/catkin_ws/src
git clone https:
//github.com/hbrobotics/ros_arduino_bridge.git
然后回到catkin_ws并编译
cd ..
catkin_make
step2.2:将上一步下载的包中的下位机代码复制入arduino的库(libraries)中。
先进入到sketchbook的libraries目录下,一般都在home里,很好找
cd ~/sketchbook/libraries
\cp -rp ~/catkin_ws/src/ros_arduino_bridge/ros_arduino_firmware/src/libraries/ROSArduinoBridge -T ROSArduinoBridge
step2.3:根据所选用的驱动和板子修改ROSArduinoBridge及相关代码
#define USE_BASE // Enable the base controller code
//#undef USE_BASE // Disable the base controller code
/* Define the motor controller and encoder library you are using */
#ifdef USE_BASE
/* The Pololu VNH5019 dual motor driver shield */
//#define POLOLU_VNH5019
/* The Pololu MC33926 dual motor driver shield */
//#define POLOLU_MC33926
/* The RoboGaia encoder shield */
//#define ROBOGAIA
/* Encoders directly attached to Arduino board */
#define ARDUINO_ENC_COUNTER
/* L298 Motor driver*/
#define L298_MOTOR_DRIVER
#endif
主要就是这个地方需要修改,我都标蓝了,这里千万要修改对,看清楚哪里注释了哪里没有注释
修改之后电机ide左上方向左的箭头,进行烧录
step3:
下一步我们查看编码器以及电机的相关接口的定义,一定要按照这个来连线!
首先是编码器,编码器的两个信号反馈线按照下列方式直接连到uno上:
#ifdef ARDUINO_ENC_COUNTER
//below can be changed, but should be PORTD pins;
//otherwise additional changes in the code are required
#define LEFT_ENC_PIN_A PD2 //pin 2
#define LEFT_ENC_PIN_B PD3 //pin 3
//below can be changed, but should be PORTC pins
#define RIGHT_ENC_PIN_A PC4 //pin A4
#define RIGHT_ENC_PIN_B PC5 //pin A5
#endif
由于我只拿了一个电机做实验,所以只连接了2和3这两个
如果有两路电机,左侧电机的编码输出接D2,D3;右侧电机的编码输出接A4,A5
然后是电机
#ifdef L298_MOTOR_DRIVER
#define RIGHT_MOTOR_BACKWARD 5
#define LEFT_MOTOR_BACKWARD 6
#define RIGHT_MOTOR_FORWARD 9
#define LEFT_MOTOR_FORWARD 10
#define RIGHT_MOTOR_ENABLE 12
#define LEFT_MOTOR_ENABLE 13
#endif
可以看到 5 6 9 10是四个控制电机转动的输出口,我只连了5和