系统版本:MacOS 10.15.6(官方写的是支持 10.14)

ROS版本:Foxy

shell:zsh

Mac:MacBook Pro (13-inch, 2016, Four Thunderbolt 3 Ports)

该文章总结了 ROS2 官网的 CLI Tools 介绍,如果需要更为详细的介绍请移步官网。且 Windows 不在文本讨论范围内。

本文主要介绍如下:

  1. 配置 ROS2 环境
  2. ROS 2 nodes
  3. ROS 2 topics
  4. ROS 2 services
  5. ROS 2 actions
  6. ROS 2 parameters
  7. 创建 launch 文件
  8. 记录和回放数据
  9. ROS1 和 ROS2 命令行工具的差异

全文篇幅可能较长,尽可能使用更精简的方式描述。

配置 ROS2 环境

1.加载核心 ROS 配置文件

与 ROS1 一样,使用 ROS2 也需要加载 setup 配置文件

1
. ~/ros2_foxy/ros2-osx/setup.zsh

注意:我的 shellzsh,请使用你对应的 shellsetup,且路径请以你安装路径为准。

2. 添加核心配置文件到 shell 的启动脚本中

官网的做法如下:

1
echo ". ~/ros2_foxy/ros2-osx/setup.zsh" >> ~/.zshrc

我不太建议这种做法,如果是完全学习 ROS2,可以采取这种方式。这种方式的好处是你全局都可以使用 ROS2 的命令,坏处就是你也不知道何时你的其他程序会加载到 ROS2 环境中的库,然而实际情况这个库不是我想要的。所以我更建议下面这种方式:

1
echo "alias ros2_source='. ~/ros2_foxy/ros2-osx/setup.zsh'" >> ~/.zshrc    

当我需要加载 ROS2 环境的时候,手动使用 ros2_source 命令即可。

3. 检查环境变量

使用 printenv | grep -i ROS 命令查看当前的所有环境变量,如果设置正确至少应该会打印如下内容:

1
2
3
ROS_DISTRO=foxy
ROS_PYTHON_VERSION=3
ROS_VERSION=2

设置 ROS_DOMAIN_ID 确保局域网不同域的 ROS 数据不会误访问,我使用的 id 为 2。官网要求该数字必须是 0-232 之间的整数。

1
echo "export ROS_DOMAIN_ID=2" >> ~/.zshrc

ROS Node

ROS Node 包含的命令如下:

  1. list
  2. info

前提条件运行了如下命令:

1
2
. ~/ros2_foxy/ros2-osx/setup.zsh
ros2 run turtlesim turtlesim_node

list

ros2 node list 显示所有正在运行的节点。

当前运行 ros2 node list 显示如下

1
/turtlesim

info

用法:ros2 node info <node_name>

例如当前有一个 /turtlesim 节点,使用如下命令:

1
ros2 node info /turtlesim

显示的结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/turtlesim
Subscribers:
/parameter_events: rcl_interfaces/msg/ParameterEvent
/turtle1/cmd_vel: geometry_msgs/msg/Twist
Publishers:
/parameter_events: rcl_interfaces/msg/ParameterEvent
/rosout: rcl_interfaces/msg/Log
/turtle1/color_sensor: turtlesim/msg/Color
/turtle1/pose: turtlesim/msg/Pose
Service Servers:
/clear: std_srvs/srv/Empty
/kill: turtlesim/srv/Kill
/reset: std_srvs/srv/Empty
/spawn: turtlesim/srv/Spawn
/turtle1/set_pen: turtlesim/srv/SetPen
/turtle1/teleport_absolute: turtlesim/srv/TeleportAbsolute
/turtle1/teleport_relative: turtlesim/srv/TeleportRelative
/turtlesim/describe_parameters: rcl_interfaces/srv/DescribeParameters
/turtlesim/get_parameter_types: rcl_interfaces/srv/GetParameterTypes
/turtlesim/get_parameters: rcl_interfaces/srv/GetParameters
/turtlesim/list_parameters: rcl_interfaces/srv/ListParameters
/turtlesim/set_parameters: rcl_interfaces/srv/SetParameters
/turtlesim/set_parameters_atomically: rcl_interfaces/srv/SetParametersAtomically
Service Clients:

Action Servers:
/turtle1/rotate_absolute: turtlesim/action/RotateAbsolute
Action Clients:

注:SubscribersPublishersService ServersService ClientsAction ServersAction Clients 的含义后面再进行介绍。

与 ROS1 的比较

命令 ROS1 ROS2
show rosnode list ros2 node list
list rosnode show <node_name> ros2 node show <node_name>

使用起来几乎一致,但是有一些 ROS1 的命令 ROS2 没有,比如 killping,在实际开发中这些命令也较少用到,不做讨论。

ROS Topic

命令 ROS1 ROS2 含义
echo rostopic echo /topic ros2 topic echo /topic 打印某个 topic 的内容
find rostopic find /topic ros2 topic find /topic 查找使用某个 topic 的节点
info rostopic info /topic ros2 topic info /topic 显示某个 topic 的发布者和订阅者的数量
list rostopic list ros2 topic list 显示当前 topic 的清单
pub rostopic pub /topic "msg" ros2 topic pub /topic "msg" 发送消息到一个指定的 topic 中

ROS Service

命令 ROS1 ROS2 含义
call rosservice call /service_name /service_type [values] ros2 service call /service_name /service_type [values] 给指定的服务端发送特定消息
find rosservice find /service_type ros2 service find /service_type 查找使用某个 Service 的节点
list rosservice list ros2 service list 显示当前 Service 的清单

ROS Action

在 ROS1 中 ros action 的 cli 不存在。故不作对比,使用方式与前面两者非常相似。

命令 ROS Foxy 含义
info ros2 action info /action_name 显示某个 action 中 client 和 server 的数量
list ros2 action list 显示当前所有 action 的清单
send_goal ros2 action send_goal /action_name /action_type [goal] 发送一个 goal 到指定 action 中

ROS parameters

param 参数是节点所需的配置设置,可以是整数,浮点数,布尔值,字符串或列表。在 ros2 中参数是基于 service 构建的。依旧用表格的方式描述 ROS1 和 ROS2 中参数设计的差异。注:ROS2 的参数是存储在节点中的,ROS1 的参数是存储在 ROS Master 中的,所有在使用的时候一般会有一个节点名称的差距。

命令 ROS1 ROS2 含义
delete rosparam delete /parameter_name ros2 param delete /node_name /parameter_name 删除参数
dump rosparam dump file [namespace] ros2 param dump /node_name 保存参数
get rosparam get /parameter_name ros2 param get /node_name /parameter_name 获取参数
list rosparam list ros2 param list [node_name] 查看参数列表
set rosparam set /parameter_name value ros2 param set /node_name /parameter_name value 设置参数

ROS Interface

interface 是 ROS2 中将 ROS1 中的 rosmsgrossrv 命令合并以后的命令,使用起来更为统一且简洁。先看看 rosmsgrossrv

  1. 执行 rosmsg -v
1
2
3
4
5
6
7
8
9
10
11
rosmsg is a command-line tool for displaying information about ROS Message types.

Commands:
rosmsg show Show message description
rosmsg info Alias for rosmsg show
rosmsg list List all messages
rosmsg md5 Display message md5sum
rosmsg package List messages in a package
rosmsg packages List packages that contain messages

Type rosmsg <command> -h for more detailed usage
  1. 执行 rossrv -h
1
2
3
4
5
6
7
8
9
10
11
rossrv is a command-line tool for displaying information about ROS Service types.

Commands:
rossrv show Show service description
rossrv info Alias for rossrv show
rossrv list List all services
rossrv md5 Display service md5sum
rossrv package List services in a package
rossrv packages List packages that contain services

Type rossrv <command> -h for more detailed usage

通过 -h 命令可以发现,这两个命令使用方式非常相似 ,结果相似也非常相似。我们在来看看ros2 interface -h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
usage: ros2 interface [-h] Call `ros2 interface <command> -h` for more detailed usage. ...

Show information about ROS interfaces

optional arguments:
-h, --help show this help message and exit

Commands:
list List all interface types available
package Output a list of available interface types within one package
packages Output a list of packages that provide interfaces
proto Output an interface prototype
show Output the interface definition

Call `ros2 interface <command> -h` for more detailed usage.

help 中已经将用法讲的很清楚了,这里就不对比了。记录不同类型的打印结果。

  1. ros2 interface show std_msgs/msg/Int8
1
2
3
4
5
6
# This was originally provided as an example message.
# It is deprecated as of Foxy
# It is recommended to create your own semantically meaningful message.
# However if you would like to continue using this please use the equivalent in example_msgs.

int8 data
  1. ros2 interface show std_srvs/srv/Empty
1
---
  1. ros2 interface show turtlesim/action/RotateAbsolute
1
2
3
4
5
6
7
8
# The desired heading in radians
float32 theta
---
# The angular displacement in radians to the starting position
float32 delta
---
# The remaining rotation in radians
float32 remaining

注:msg、srv 和 action 消息的具体写法后面会在进行详细的介绍。

ROS Launch

在 ROS1 中 lauch 使用 xml 格式定义,使用的内容相对比较有限。然而在 ROS2 中 launch 重新设计,采用 Python 脚本的形式进行启动,大致模板可以使用如下:

1
2
3
4
5
6
7
8
9
10
11
12
from launch import LaunchDescription
from launch_ros.actions import Node


def generate_launch_description():
return LaunchDescription([
Node(
package="turtlesim",
executable="turtlesim_node",
name="sim",
)
])

LaunchDescription 中传入了一个 Node 的数组,Node代表了所有的节点信息。参数的含义和使用列举如下:

参数名称 含义 类型 举例
package 包名 str turtlesim
executable 可执行文件的名称 str turtlesim_node
name 节点名称 str sim
namespace 工作空间名称 str turtlesim1
parameters 参数列表 [path 或 Map(key, value)] [{"background_r": 0}]["./sim.yaml"]
remappings 映射名称 [tuple] [('/turtlesim1/turtle1/pose', '/turtlesim1/turtle1/pose_remap')]

假设我们将之前的模板内容写入到 turtlesim_start.launch.py 中,在加载 ros 环境后,只需执行如下命令即可运行 launch 文件

1
ros2 launch turtlesim_start.launch.py

终端大致打印内容如下:

1
2
3
4
5
[INFO] [launch]: All log files can be found below /Users/askeynil/.ros/log/2020-09-02-19-33-18-140633-AskeyNildeMacBook-Pro.local-22416
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [turtlesim_node-1]: process started with pid [22418]
[turtlesim_node-1] [INFO] [1599046399.308158593] [sim]: Starting turtlesim with node name /_ros2cli_daemon_1
[turtlesim_node-1] [INFO] [1599046399.342564206] [sim]: Spawning turtle [turtle1] at x=[5.544445], y=[5.544445], theta=[0.000000]

ROS Bag

该命令可以指定录制一段时间内发送的 topic 数据,以便后面在查询问题的时候进行复现。

命令 ROS1 ROS2 含义
info rosbag info <bag_file_name> ros2 bag info <bag_file_name> 查看 bag 中的详细内容
play rosbag play <bag_file_name> ros2 bag play <bag_file_name> 播放 bag 中的操作
record rosbag record -O <output_name> [topics [topics ...]] ros2 bag record -o <output_name> [topics [topics ...]] 录制指定 topic 操作

评论