第一个ROS程序
案例简介
1.需求
编写一个ROS2程序,可以在终端输出"Helo World"。
2.准备
无论是使用C++还是Python编写ROS2程序,都需要依赖于工作空间,在此,我们先实现工作空间的创建与编译,打开终端,输入如下指令:
mkdir -p 00_helloworld/src #创建工作空间以及子级目录 src,工作空间名称可以自定义
cd 00_helloworld #进入工作空间
colcon build #编译
上述指令执行完毕,将创建00_helloworld目录,且该目录下包含build、install、log、src共四个子级目录。
3.流程简介
工作空间创建完毕后,我么可以在工作空间下的src目录中编写C++或Python程序,且两种语言的实现流程大致一致,主要包含如下步骤:
- 创建功能包;
- 编辑源文件;
- 编辑配置文件;
- 编译;
- 执行。
HelloWorld(C++)
1.创建功能包
终端下,进入00_helloworld/src目录,使用如下指令创建一个C++功能包:
ros2 pkg create pkg01_helloworld_cpp --build-type ament_cmake --dependencies rclcpp --node-name helloworld
# ros2 pkg create 创建一个功能包
# --build-type 指定构建类型 ament_cmake是C++类型 ament_python是python类型
# --dependencies rclcpp 添加依赖
# --node-name 生成一个可执行程序
执行完毕,在src目录下将生成一个名为pkg01_helloworld_cpp的目录,且目录中已经默认生成了一些子级文件与文件夹。
2.编辑源文件
进入pkg01_helloworld_cpp/src目录,该目录下有一helloworld.cpp文件,修改文件内容如下:
#include "rclcpp/rclcpp.hpp"
int main(int argc, char ** argv)
{
// 初始化 ROS2
rclcpp::init(argc,argv);
// 创建节点
auto node = rclcpp::Node::make_shared("helloworld_node");
// 输出文本
RCLCPP_INFO(node->get_logger(),"hello world!");
// 释放资源
rclcpp::shutdown();
return 0;
}
3.编辑配置文件
在步骤1创建功能包时所使用的指令已经默认生成且配置了配置文件,不过实际应用中经常需要自己编辑配置文件,所以在此对相关内容做简单介绍,所使用的配置文件主要有两个,分别是功能包下的package.xml与CMakeLists.txt。
1.package.xml
文件内容如下:
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>pkg01_helloworld_cpp</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="ros2@todo.todo">ros2</maintainer>
<license>TODO: License declaration</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<!-- 所需要依赖 -->
<depend>rclcpp</depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
注释部分以后需要根据实际的包依赖进行添加或修改。
2.CMakeLists.txt
文件内容如下:
cmake_minimum_required(VERSION 3.8)
project(pkg01_helloworld_cpp)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
# 引入外部依赖包
find_package(rclcpp REQUIRED)
# 映射源文件与可执行文件
add_executable(helloworld src/helloworld.cpp)
# 设置目标依赖库
ament_target_dependencies(
helloworld
"rclcpp"
)
# 定义安装规则
install(TARGETS helloworld
DESTINATION lib/${PROJECT_NAME})
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# comment the line when this package is in a git repo and when
# a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()
中文注释部分以后可能需要根据实际情况修改。
4.编译
终端下进入到工作空间,执行如下指令:
colcon build
5.执行
终端下进入到工作空间,执行如下指令:
. install/setup.bash # 加载文件
# 也可以使用 source install/setup.bash
ros2 run pkg01_helloworld_cpp helloworld
程序执行,在终端下将输出文本:"hello world!"。