系统版本:MacOS 10.15.6(官方写的是支持 10.14)
ROS版本:Foxy
shell:zsh
Mac:
MacBook Pro (13-inch, 2016, Four Thunderbolt 3 Ports)
该文章总结了 ROS2 官网的 CLI Tools 介绍,如果需要更为详细的介绍请移步官网。且 Windows 不在文本讨论范围内。
本文主要介绍如下:
- 配置 ROS2 环境
- ROS 2 nodes
- ROS 2 topics
- ROS 2 services
- ROS 2 actions
- ROS 2 parameters
- 创建 launch 文件
- 记录和回放数据
- ROS1 和 ROS2 命令行工具的差异
全文篇幅可能较长,尽可能使用更精简的方式描述。
配置 ROS2 环境
1.加载核心 ROS 配置文件
与 ROS1 一样,使用 ROS2 也需要加载 setup 配置文件
1 |
. ~/ros2_foxy/ros2-osx/setup.zsh |
注意:我的 shell
是 zsh
,请使用你对应的 shell
的
setup
,且路径请以你安装路径为准。
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 |
ROS_DISTRO=foxy |
设置 ROS_DOMAIN_ID
确保局域网不同域的 ROS 数据不会误访问,我使用的 id 为 2。官网要求该数字必须是 0-232 之间的整数。
1 |
echo "export ROS_DOMAIN_ID=2" >> ~/.zshrc |
ROS Node
ROS Node
包含的命令如下:
- list
- 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 |
/turtlesim |
注:Subscribers
、Publishers
、Service Servers
、Service Clients
、Action Servers
和 Action Clients
的含义后面再进行介绍。
与 ROS1 的比较
命令 | ROS1 | ROS2 |
---|---|---|
show | rosnode list |
ros2 node list |
list | rosnode show <node_name> |
ros2 node show <node_name> |
使用起来几乎一致,但是有一些 ROS1 的命令 ROS2 没有,比如 kill
、ping
,在实际开发中这些命令也较少用到,不做讨论。
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 中的 rosmsg
和 rossrv
命令合并以后的命令,使用起来更为统一且简洁。先看看 rosmsg
和 rossrv
- 执行
rosmsg -v
1 |
rosmsg is a command-line tool for displaying information about ROS Message types. |
- 执行
rossrv -h
1 |
rossrv is a command-line tool for displaying information about ROS Service types. |
通过 -h
命令可以发现,这两个命令使用方式非常相似 ,结果相似也非常相似。我们在来看看ros2 interface -h
1 |
usage: ros2 interface [-h] Call `ros2 interface <command> -h` for more detailed usage. ... |
help
中已经将用法讲的很清楚了,这里就不对比了。记录不同类型的打印结果。
ros2 interface show std_msgs/msg/Int8
1 |
This was originally provided as an example message. |
ros2 interface show std_srvs/srv/Empty
1 |
--- |
ros2 interface show turtlesim/action/RotateAbsolute
1 |
The desired heading in radians |
注:msg、srv 和 action 消息的具体写法后面会在进行详细的介绍。
ROS Launch
在 ROS1 中 lauch 使用 xml 格式定义,使用的内容相对比较有限。然而在 ROS2 中 launch 重新设计,采用 Python 脚本的形式进行启动,大致模板可以使用如下:
1 |
from launch import LaunchDescription |
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 |
[INFO] [launch]: All log files can be found below /Users/askeynil/.ros/log/2020-09-02-19-33-18-140633-AskeyNildeMacBook-Pro.local-22416 |
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 操作 |