1.3 ROS2快速体验
1.3 ROS2快速体验
ROS2中涉及的编程语言以C++和Python为主,ROS2中的大多数功能两者都可以实现,在本系列教程中,如无特殊情况,每一个案例也都会分别使用C++和Python两种方案演示。本节我们将介绍一个最基本的案例——ROS2版本的HelloWorld,通过学习本节内容,你可以了解ROS2程序的编写、编译与执行流程。
1.3.1 案例简介
1.需求
编写ROS2程序,要求程序运行时,可以在终端输出文本"Helo World"。
2.准备
无论是使用C++还是Python编写ROS2程序,都需要依赖于工作空间,在此,我们先实现工作空间的创建与编译,打开终端,输入如下指令:
mkdir -p ws00_helloworld/src #创建工作空间以及子级目录 src,工作空间名称可以自定义
cd ws00_helloworld #进入工作空间
colcon build #编译
上述指令执行完毕,将创建ws00_helloworld目录,且该目录下包含build、install、log、src共四个子级目录。
3.流程简介
工作空间创建完毕后,我么可以在工作空间下的src目录中编写C++或Python程序,且两种语言的实现流程大致一致,主要包含如下步骤:
创建功能包;
编辑源文件;
编辑配置文件;
编译;
执行。
下面两节我们会介绍具体的实现细节。
1.3.2 HelloWorld(C++)
1.创建功能包
终端下,进入ws00_helloworld/src目录,使用如下指令创建一个C++功能包:
ros2 pkg create pkg01_helloworld_cpp --build-type ament_cmake --dependencies rclcpp --node-name helloworld
执行完毕,在src目录下将生成一个名为pkg01_helloworld_cpp的目录,且目录中已经默认生成了一些子级文件与文件夹。
2.编辑源文件
进入pkg01_helloworld_cpp/src目录,该目录下有一helloworld.cpp文件,修改文件内容如下:

3.编辑配置文件
在步骤1创建功能包时所使用的指令已经默认生成且配置了配置文件,不过实际应用中经常需要自己编辑配置文件,所以在此对相关内容做简单介绍,所使用的配置文件主要有两个,分别是功能包下的package.xml与CMakeLists.txt。
1.package.xml
文件内容如下:
"1.0" xml version=
"http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema" xml-model href=
<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)
a
ment_lint_auto_find_test_dependencies()
endif()
ament_package()
中文注释部分以后可能需要根据实际情况修改。
4.编译
终端下进入到工作空间,执行如下指令:
colcon build
5.执行
终端下进入到工作空间,执行如下指令:
. install/setup.bash
ros2 run pkg01_helloworld_cpp helloworld
程序执行,在终端下将输出文本:"hello world!"。
1.3.3 HelloWorld(Python)
1.创建功能包
终端下,进入ws00_helloworld/src目录,使用如下指令创建一个python功能包:
ros2 pkg create pkg02_helloworld_py --build-type ament_python --dependencies rclpy --node-name helloworld
执行完毕,在src目录下将生成一个名为pkg02_helloworld_py的目录,且目录中已经默认生成了一些子级文件与文件夹。
2.编辑源文件
进入pkg02_helloworld_py/pkg02_helloworld_py目录,该目录下有一helloworld.py文件,修改文件内容如下:

3.编辑配置文件
与C++类似的,在步骤1创建功能包时所使用的指令也已经默认生成且配置了配置文件,不过实际应用中经常需要自己编辑配置文件,所以在此对相关内容做简单介绍,所使用的配置文件主要有两个,分别是功能包下的package.xml与setup.py。
1.package.xml
文件内容如下:
"1.0" xml version=
"http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema" xml-model href=
<package format="3">
<name>pkg02_helloworld_py</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="ros2@todo.todo">ros2</maintainer>
<license>TODO: License declaration</license>
<!-- 所需要依赖 -->
<depend>rclpy</depend>
<test_depend>ament_copyright</test_depend> <test_depend>ament_flake8</test_depend> <test_depend>ament_pep257</test_depend>
<test_depend>python3-pytest</test_depend>
<export>
<build_type>ament_python</build_type>
</export>
</package>
注释部分以后需要根据实际的包依赖进行添加或修改。
2.setup.py
文件内容如下:
from setuptools import setup
package_name = 'pkg02_helloworld_py'
setup(
name=package_name,
version='0.0.0',
packages=[package_name],
data_files=[
('share/ament_index/resource_index/packages', ['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='ros2',
maintainer_email='ros2@todo.todo',
description='TODO: Package description',
license='TODO: License declaration',
tests_require=['pytest'],
entry_points={
'console_scripts': [
# 映射源文件与可执行文件
'helloworld = pkg02_helloworld_py.helloworld:main'
],
},
)
注释部分以后可能需要根据实际情况修改。
4.编译
终端下进入到工作空间,执行如下指令:
colcon build
5.执行
终端下进入到工作空间,执行如下指令:
. install/setup.bash
ros2 run pkg02_helloworld_py helloworld
程序执行,在终端下将输出文本:"hello world!"。
1.3.4 运行优化
每次终端中执行工作空间下的节点时,都需要调用. install/setup.bash
指令,使用不便,优化策略是,可以将该指令的调用添加进~/setup.bash
,操作格式如下:
echo "source /{工作空间路径}/install/setup.bash" >> ~/.bashrc
示例:
echo "source /home/ros2/ws00_helloworld/install/setup.bash" >> ~/.bashrc
以后再启动终端时,无需手动再手动刷新环境变量,使用更方便。

B站有完整的ros系列教程视频,可以观看完整内容ros课程ROS2理论与实践
更多内容将在猛狮知识星球社区更新最新课程,后续将推出更多优质内容——详情可关注猛狮集训营公众号和猛狮集训营官方网站。