步骤一:下载rosserial_arduino功能包
sudo apt-get install ros-melodic-rosserial-arduino
步骤二:下载和编译安装aduino-cmake (Tips:如果安装了rosserial就不用安装arduino-cmake了)
1、创建arduino_upload_ws工作空间:
cd ~
mkdir arduino_upload_ws/src -p
2、下载:
cd arduino_upload_ws
git clone https://github.com/queezythegreat/arduino-cmake.git
3、编译:
cd arduino-cmake
mkdir build
cmake ..
make
步骤三:为串口添加权限
cd /dev
sudo chmod 777 ttyACM*
步骤四:测试
cd ~/arduino_upload_ws/src
catkin_init_workspace
catkin_create_pkg helloworld rosserial_arduino rosserial_client std_msgs
cd helloworld
mkdir firmware
cd firemware
vim chatter.cpp
输入以下内容:
#include <ros.h>
#include <std_msgs/String.h>
//Arduino.h头文件,它包含了所有的Arduino函数(digitalRead, analogRead, delay, etc.)
#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);
}
接着创建CMakeLists.txt:
vim 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
)
回到上一级目录,添加CMakeLists.txt:
cd ..
mv CMakeLists.txt CMakeLists.back
vim 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)
- 1、编译:
cd ~/arduino_upload_ws
catkin_make helloworld_firmware_hello
- 2、上传:
cd ~/arduino_upload_ws
catkin_make helloworld_firmware_hello-upload
成功!
---------------------------------------------------分界线-----------------------------------------------
*可能遇到的问题
1、{nullptr} was not declared in this scope
这个问题为开始一直搞不明白,后来发现三c++11的规则,所以只需要在对应包中加入c++11规则就可以了
例如helloworld这个功能包,需要改的地方就是firmware里的CMakeLists.txt:
原内容:
```bash
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
)
修改后:
cmake_minimum_required(VERSION 2.8.3)
include_directories(${ROS_LIB_DIR})
#添加c++11的支持
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
# Remove this if using an Arduino without native USB (eg, other than Leonardo)
add_definitions(-DUSB_CON)
generate_arduino_firmware(art_table_arduino
SRCS Art_table_rosserial.cpp ${ROS_LIB_DIR}/time.cpp
BOARD mega2560
PORT /dev/ttyACM0
)
编译的时候最好是对单个烧写程序进行编译!所以我们这时编译使用的命令应该是:
catkin_make helloworld_firmware_hello
成功!
2、有可能遇到的rosserial问题
创建的工作空间在有rosserial功能包的情况下最好先单独编译rosserial然后再单独编译arduino烧写程序!