ROS入门-arduino

1.安装ROS_Serial

1.1 安装环境参数

sudo apt-get install ros-indigo-rosserial-arduino
sudo apt-get install ros-indigo-rosserial

1.2 下载程序节点数据

cd ~/joey_ws/src
git clone https://github.com/ros-drivers/rosserial.git
cd ~/joey_ws
catkin_make
catkin_make install
source ~/joey_ws/install/setup.bash

2.Ubuntu安装Aruino程序

2.1 下载Arduino程序

https://www.arduino.cc/en/Main/Software

2.2 解压安装包

安装包解压至/Desktop目录下

mv arduino***.tar.xz ~/Desktop/
xz -d arduino***.tar.xz
tar -xf arduino***.tar

3.安装ros-Arduino库文件

3.1 运行一次arduino

cd ~/arduino-1.6.7/
./arduino

3.2 安装库文件

cd ~/Arduino/libraries/
rosrun rosserial_arduino make_libraries.py ./ 尤其记得后面的点不要漏了

4.运行helloworld程序

4.1 运行arduino程序

cd ~/arduino-1.6.7/
./arduino

4.2 arduino程序烧录

Tools 选择 Board arduino/mega2560 Processor "mega 2560"
选择当前arduino端口 /dev/ACM0
分配端口权限 sudo chmod 777 /dev/ttyACM0
选择示例程序<helloworld> 烧录

4.3 ROS端测试

  • 开启ROS服务器:

roscore

  • 定位当前工作区:

source ~/joey_ws/install/setup.bash

  • 运行arduino串口程序节点:

rosrun rosserial_python serial_node.py /dev/ttyACM0

  • 监控当前主题信号:

rostopic echo chatter

5.运行Blink程序

5.1 运行arduino程序

cd ~/arduino-1.6.7/
./arduino

5.2 arduino程序烧录

Tools 选择 Board arduino/mega2560 Processor "mega 2560"
选择当前arduino端口 /dev/ACM0
分配端口权限 sudo chmod 777 /dev/ttyACM0
选择示例程序<blink> 烧录

5.3 ROS端测试

  • 开启ROS服务器:

roscore

  • 定位当前工作区:

source ~/joey_ws/install/setup.bash

  • 运行arduino串口程序节点:

rosrun rosserial_python serial_node.py /dev/ttyACM0

  • 查询话题类型:

rostopic info /toggle_led
结果显示 type:std_msgs/Empty

  • 给toggle_led话题发送消息:

rostopic pub -r 1 /toggle_led std_msgs/Empty

  • 其中rostopic的表达式:

rostopic pub -r rate-in-hz topic-name message-type message-content
其中-r rate-in-hz 可以用 -1代替表示发送一次

6.运行时间同步与TF变换程序

6.1 运行arduino程序

cd ~/arduino-1.6.7/
./arduino

6.2 arduino程序烧录

Tools 选择
Board arduino/mega2560 Processor "mega 2560"
选择当前arduino端口
/dev/ACM0
分配端口权限
sudo chmod 777 /dev/ttyACM0
选择示例程序<Time-TF> 烧录

6.3 ROS端测试

  • 开启ROS服务器:

roscore

  • 定位当前工作区:

source ~/joey_ws/install/setup.bash

  • 运行arduino串口程序节点:

rosrun rosserial_python serial_node.py /dev/ttyACM0

7.运行按钮button程序

7.1 运行arduino程序

cd ~/arduino-1.6.7/
./arduino

7.2 arduino程序烧录

Tools 选择 Board arduino/mega2560 Processor "mega 2560"
选择当前arduino端口 /dev/ACM0
分配端口权限 sudo chmod 777 /dev/ttyACM0
选择示例程序<Time-TF> 烧录

7.3 ROS端测试

  • 开启ROS服务器:

roscore

  • 定位当前工作区:

source ~/joey_ws/install/setup.bash

  • 运行arduino串口程序节点:

rosrun rosserial_python serial_node.py /dev/ttyACM0

  • 查看主题信息:

rostopic echo pushed

8. 使用ROS-CMake烧录helloworld代码

8.1 新建helloworld工程程序

cd ~/joey_ws/src
catkin_create_pkg helloworld rosserial_arduino rosserial_client std_msgs
cd ~/joey_ws/src/helloworld/
mkdir firmware
vim fireware/chatter.cpp

#include <ros.h>
#include <std_msgs/String.h>

#include <Arduino.h>

ros::NodeHandle nh;

std_msgs::String str_msg;
ros::Publisher chatter("chatter", &str_msg);

char hello[13] = "hello world!";

void setup()
{
nh.initNode();
nh.advertise(chatter);
}

void loop()
{
str_msg.data = hello;
chatter.publish( &str_msg );
nh.spinOnce();
delay(1000);
}

8.2 更新总CMakelists

vim ~/joey_ws/src/helloworld/CMakelists.txt

cmake_minimum_required(VERSION 2.8.3)
project(helloworld)

find_package(catkin REQUIRED COMPONENTS
  rosserial_arduino
  rosserial_client
)

catkin_package()

rosserial_generate_ros_lib(
  PACKAGE rosserial_arduino
  SCRIPT make_libraries.py
)

rosserial_configure_client(
  DIRECTORY firmware
  TOOLCHAIN_FILE ${ROSSERIAL_ARDUINO_TOOLCHAIN}
)

rosserial_add_client_target(firmware hello ALL)
rosserial_add_client_target(firmware hello-upload)

vim ~/joey_ws/src/helloworld/firmware/CMakelists.txt

cmake_minimum_required(VERSION 2.8.3)

include_directories(${ROS_LIB_DIR})

# Remove this if using an Arduino without native USB (eg, other than Leonardo)
add_definitions(-DUSB_CON)

generate_arduino_firmware(hello
  SRCS chatter.cpp ${ROS_LIB_DIR}/time.cpp
  BOARD mega2560
  PORT /dev/ttyACM0
)

8.3 烧录程序至arduino mega2560

  • 编译:

cd ~/joey_ws/
catkin_make helloworld_firmware_hello

  • 下载:

catkin_make helloworld_firmware_hello-upload

8.4 验证arduino节点通信

  • 开启ROS服务器:

roscore

  • 定位当前工作区:

source ~/joey_ws/install/setup.bash

  • 运行arduino串口程序节点:

rosrun rosserial_python serial_node.py /dev/ttyACM0

  • 监控当前主题信号:

rostopic echo chatter

9 使用ROS-make烧录ADC-arduino检测程序

9.1 新建adc_arduino工程程序

cd ~/joey_ws/src
catkin_create_pkg adc_arduino rosserial_arduino rosserial_client rosserial_server
cd ~/joey_ws/src/adc_arduino/
mkdir firmware && cd firmware
vim adc_arduino.cpp

#if (ARDUINO >= 100)
 #include <Arduino.h>
#else
 #include <WProgram.h>
#endif
#include <ros.h>
#include <rosserial_arduino/Adc.h>
#include <Arduino.h>

ros::NodeHandle nh;
rosserial_arduino::Adc adc_msg;
ros::Publisher p("adc", &adc_msg);

void setup()
{ 
  pinMode(13, OUTPUT);
  nh.initNode();

  nh.advertise(p);
}
//We average the analog reading to elminate some of the noise
int averageAnalog(int pin){
  int v=0;
  for(int i=0; i<4; i++) v+= analogRead(pin);
  return v/4;
}
long adc_timer;
void loop()
{
  adc_msg.adc0 = averageAnalog(0);
  adc_msg.adc1 = averageAnalog(1);
  adc_msg.adc2 = averageAnalog(2);
  adc_msg.adc3 = averageAnalog(3);
  adc_msg.adc4 = averageAnalog(4);      
  p.publish(&adc_msg);
  delay(100);
  nh.spinOnce();
}

8.2 更新总CMakelists

vim ~/joey_ws/src/adc_arduino/CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)
project(adc_arduino)

find_package(catkin REQUIRED COMPONENTS
  rosserial_arduino
  rosserial_client
  rosserial_server
)

catkin_package()

rosserial_generate_ros_lib(
  PACKAGE rosserial_arduino
  SCRIPT make_libraries.py
)

rosserial_configure_client(
  DIRECTORY firmware
  TOOLCHAIN_FILE ${ROSSERIAL_ARDUINO_TOOLCHAIN}
)

rosserial_add_client_target(firmware adc_arduino ALL)
rosserial_add_client_target(firmware adc_arduino-upload)

vim ~/joey_ws/src/adc_arduino/firmware/CMakelists.txt

cmake_minimum_required(VERSION 2.8.3)

include_directories(${ROS_LIB_DIR})

# Remove this if using an Arduino without native USB (eg, other than Leonardo)
add_definitions(-DUSB_CON)

generate_arduino_firmware(adc_arduino
  SRCS adc_arduino.cpp ${ROS_LIB_DIR}/time.cpp
  BOARD mega2560
  PORT /dev/ttyACM0
)

9.3 烧录程序至arduino mega2560

  • 编译:

cd ~/joey_ws/
catkin_make adc_arduino_firmware_adc_arduino

  • 下载:

catkin_make adc_arduino_firmware_adc_arduino-upload

9.4 验证arduino节点通信

  • 开启ROS服务器:

roscore

  • 定位当前工作区:

source ~/joey_ws/install/setup.bash

  • 运行arduino串口程序节点:

rosrun rosserial_python serial_node.py /dev/ttyACM0

  • 监控当前主题信号:

rostopic echo adc_arduino

推荐阅读更多精彩内容