当从事大型软件项目时,Arduino IDE变得很笨拙。您经常希望能够从命令行编译项目,或者使用Eclipse之类的其他IDE、等这些可以自动编译的工具。最后,通过使用rosserial_client CMake基础结构,您可以使用ROS buildfarm构建和分发固件。
在本教程中,我们将创建一个简单的hello world固件。
在Indigo新增的功能:
您可以在常规ROS包中再次构建rosserial固件和其他客户端。
此功能可从Indigo开始使用。rosserial_arduino软件包的安装已经也安装了arduino-core,因此没有必要再增加额外的工作。
1 制作你的项目
启动rosserial_arduino项目就像创建任何其他包一样。在catkin工作区的src文件夹中:
catkin_create_pkg helloworld rosserial_arduino rosserial_client std_msgs
像往常一样,使用catkin_create_pkg创建一个名为helloworld的包。您必须依赖rosserial_arduino作为Arduino工具链,并使用rosserial_client作为客户端库生成宏。最后,由于我们将使用std_msgs / String消息,因此您必须依赖std_msgs。
2 源程序
复制下面的源代码,并在helloworld包中创建一个名为firmware / 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);
}
该程序几乎与Publisher Tutorial中介绍的hello world完全相同。当您在Arduino IDE之外编译cpp文件时,您需要明确包含一个包含所有Arduino函数的头文件(digitalRead,analogRead,delay等)。如果您不确定是否要使用Arduino IDE与CMake的文件,只需在文件顶部添加此行即可。反正不会有坏处,它确保您的文件始终与非Arduino IDE构建系统兼容。
3 CMakeLists.txt
打开包目录中的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)
使用rosserial_client的CMake脚本,实际上并没有直接构建固件,而是配置一个单独的CMake项目,并将目标从catkin包传递到子项目。
rosserial_generate_ros_lib函数创建一个名为helloworld_ros_lib目标,这将产生rosserial客户端库,包括消息的头文件。
rosserial_configure_client函数创建一个目标,它会在特定的子目录配置CMake项目,可以使用提供的工具链,在这种情况下。rosserial_arduino提供的Arduino工具链。
最后,rosserial_add_client_target调用每一个传递过来的目标,这样当你运行make命令编译helloworld_firmware_hello的catkin目标时,它将配置fireware目录并在其中构建hello目标。
现在,我们实际上需要第二个CMakeLists.txt,这是固件子项目的一个。在包中创建文件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 leonardo
PORT /dev/ttyACM0
)
该generate_arduino_firmware功能由提供Arduino的cmake的工具链,此用途。它处理定位Arduino的复杂性,链接您使用的任何库,等等。
你应该完成!
4 构建和测试
4.1 使用catkin_make
运行catkin_make时,默认情况下应该构建固件,但您也可以明确指定它:
catkin_make helloworld_firmware_hello
现在连接一个Arduino Leonardo,确认它出现为/ dev/ ttyACM0(或相应更改固件的CMakeLists.txt):
catkin_make helloworld_firmware_hello-upload
您可能需要按重置按钮重新启动Leonardo以允许上载代码,并且端口可能会在重置后更改。
4.2 使用catkin工具
运行catkin build时,默认情况下应该构建固件,但您也可以明确指定它:
catkin build helloworld
现在连接一个Arduino Leonardo,确认它出现为/dev/ ttyACM0(或相应更改固件的CMakeLists.txt),并对其进行编程:
catkin build --no-deps helloworld --make-args helloworld_firmware_hello-upload
您可能需要按重置按钮重新启动Leonardo以允许上载代码,并且端口可能会在重置后更改。
测试
现在你可以使用Arduino了!在单独的终端中启动以下内容以查看其运行情况:
roscore
rosrun rosserial_python serial_node.py / dev / ttyACM0
rostopic echo chatter
附加信息
提供这些功能的CMake脚本存在于此处,如果您想检查它以更好地了解正在发生的事情。
如果您不理解,那么第一步是清理工作区(删除构建和开发树),然后以详细模式重新运行:
catkin_make VERBOSE = 1
添加几张照片吧: