【ROS2】初级:CLI工具-录制并回放数据

目标:记录发布在某个主题和服务上的数据,以便您可以随时回放和检查。

 教程级别:初学者

 时间:15 分钟

 目录

  •  背景

  •  先决条件

  •  管理主题数据

    • 1. 设置

    • 2. 选择一个话题

    • 3 .记录主题

    • 4..检查主题数据

    • 5 .播放主题数据

  •  管理服务数据

    • 1. 设置

    • 2.检查服务可用性

    •  3.记录服务

    • 4.检查服务数据

    • 5 .播放服务数据

  •  摘要

  •  下一步

  •  相关内容

 背景

ros2 bag 是一个命令行工具,用于记录 ROS 2 系统中发布在主题和服务上的数据。它可以累积任意数量的主题和服务传递的数据,然后将其保存在数据库中。您可以随后重放数据以复现您的测试和实验结果。记录主题和服务也是分享您的工作并让他人重现的好方法。

 先决条件

您应该将 ros2 bag 作为常规 ROS 2 设置的一部分进行安装。

如果您需要安装 ROS 2,请查看安装说明。

本教程讨论了前面教程中涉及的概念,如节点、主题和服务。它还使用了 turtlesim 包和服务内省演示。

始终不要忘记在您打开的每个新终端中获取 ROS 2 的源。

管理主题数据

1. 设置

您将在 turtlesim 系统中记录键盘输入,以便稍后保存和回放,因此请开始启动 /turtlesim 和 /teleop_turtle 节点。

打开一个新的终端并运行:

ros2 run turtlesim turtlesim_node

打开另一个终端并运行:

ros2 run turtlesim turtle_teleop_key

我们还应该新建一个目录来存储我们保存的录音,这是一个好习惯:

mkdir bag_files
cd bag_files

2. 选择一个话题

ros2 bag 可以记录从主题发布的消息中的数据。要查看系统主题的列表,请打开一个新的终端并运行以下命令:

ros2 topic list

将返回:

cxy@ubuntu2404-cxy:~/bag_files$ ros2 topic list
/parameter_events
/rosout
/turtle1/cmd_vel
/turtle1/color_sensor
/turtle1/pose

在主题教程中,您学到了 /turtle_teleop 节点在 /turtle1/cmd_vel 主题上发布命令,以使海龟在 turtlesim 中移动。

要查看 /turtle1/cmd_vel 发布的数据,请运行以下命令:

ros2 topic echo /turtle1/cmd_vel

刚开始时什么也不会显示,因为遥控器没有发布任何数据。返回到你运行遥控器的终端,并选择它以便它处于活动状态。使用箭头键移动乌龟,你将看到运行 ros2 topic echo 的终端上发布了数据。

cxy@ubuntu2404-cxy:~/bag_files$ ros2 topic echo /turtle1/cmd_vel
linear:
  x: 2.0
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 0.0
---
linear:
  x: 0.0
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: -2.0
---
linear:
  x: 2.0
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 0.0
---
linear:
  x: 2.0
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 0.0
---

4bb5873b623176661074d03393a4faeb.png

3 记录主题 

3.1 记录单一主题 

要记录发布到主题的数据,请使用以下命令语法:

ros2 bag record <topic_name>

在运行此命令之前,请打开一个新的终端,并移动到您之前创建的 bag_files 目录,因为 rosbag 文件将保存在您运行它的目录中

 执行命令:

ros2 bag record /turtle1/cmd_vel

您将在终端中看到以下消息(日期和时间会有所不同)

cxy@ubuntu2404-cxy:~/bag_files$ ros2 bag record /turtle1/cmd_vel
[INFO] [1720089680.740411870] [rosbag2_recorder]: Press SPACE for pausing/resuming
[INFO] [1720089680.938029175] [rosbag2_recorder]: Listening for topics...
[INFO] [1720089680.938098500] [rosbag2_recorder]: Event publisher thread: Starting
[INFO] [1720089680.978356037] [rosbag2_recorder]: Subscribed to topic '/turtle1/cmd_vel'
[INFO] [1720089680.978601776] [rosbag2_recorder]: Recording...
[INFO] [1720089680.982589064] [rosbag2_recorder]: All requested topics are subscribed. Stopping discovery...

现在 ros2 bag 正在记录发布在 /turtle1/cmd_vel 主题上的数据。返回到遥控终端,再次移动乌龟。移动本身并不重要,但尝试制作一个可识别的模式,以便稍后在回放数据时能看出来。

d824bcff3085a8330c00376856dacdb4.png

9ddf6cf7e8078e10264429bc0aae5674.png

按 Ctrl+C 停止记录。

数据将会累积在一个新的包目录中,该目录的名称模式为 rosbag2_year_month_day-hour_minute_second 。这个目录将包含一个 metadata.yaml 以及以录制格式的包文件。

3fb5d0699893b0f6e28dd7e338c9c19b.png

4. 记录多个主题

您也可以记录多个主题,以及更改文件 ros2 bag 保存为的名称。

执行以下命令:

ros2 bag record -o subset /turtle1/cmd_vel /turtle1/pose

“ -o ”选项允许您为您的包文件选择一个独特的名称。在这种情况下,接下来的字符串“ subset ”是文件名。

要同时记录多个主题,只需将每个主题用空格分开列出即可。

您将看到以下消息,确认这两个话题正在被记录。

cxy@ubuntu2404-cxy:~/bag_files$ ros2 bag record -o subset /turtle1/cmd_vel /turtle1/pose
[ERROR] [ros2bag]: Output folder 'subset' already exists.
cxy@ubuntu2404-cxy:~/bag_files$ ros2 bag record -o subset /turtle1/cmd_vel /turtle1/pose
[INFO] [1720090401.115912486] [rosbag2_recorder]: Press SPACE for pausing/resuming
[INFO] [1720090401.137248845] [rosbag2_recorder]: Listening for topics...
[INFO] [1720090401.137296446] [rosbag2_recorder]: Event publisher thread: Starting
[INFO] [1720090401.149507412] [rosbag2_recorder]: Subscribed to topic '/turtle1/pose'
[INFO] [1720090401.156447052] [rosbag2_recorder]: Subscribed to topic '/turtle1/cmd_vel'
[INFO] [1720090401.156567047] [rosbag2_recorder]: Recording...
[INFO] [1720090401.158847683] [rosbag2_recorder]: All requested topics are subscribed. Stopping discovery...
[INFO] [1720090431.756737649] [rosbag2_recorder]: Pausing recording.
[INFO] [1720090431.756979417] [rosbag2_cpp]: Writing remaining messages from cache to the bag. It may take a while
[INFO] [1720090431.766412358] [rosbag2_recorder]: Event publisher thread: Exiting
[INFO] [1720090431.766623008] [rosbag2_recorder]: Recording stopped
[INFO] [1720090431.845740280] [rclcpp]: signal_handler(signum=2)

eacd4f4ef7f905ea6ce8b13dffb33093.png

你可以移动乌龟,完成后按 Ctrl+C 。

4ec4c33f814dd44adaa2182a0fda4c70.png

cb445ba370b2c20dd58ec08aaf0d5d25.png

 便条

您可以向命令添加另一个选项, -a ,它会记录系统上的所有主题。

4 检查主题数据 

您可以通过运行以下内容查看记录的详细信息:

ros2 bag info <bag_file_name

在 subset 包文件上运行此命令将返回有关文件的信息列表:

ros2 bag info subset
cxy@ubuntu2404-cxy:~/bag_files$ ros2 bag info subset


Files:             subset_0.mcap
Bag size:          157.8 KiB
Storage id:        mcap
ROS Distro:        jazzy
Duration:          30.593s
Start:             Jul  4 2024 18:53:21.152 (1720090401.152)
End:               Jul  4 2024 18:53:51.746 (1720090431.746)
Messages:          2090
Topic information: Topic: /turtle1/cmd_vel | Type: geometry_msgs/msg/Twist | Count: 177 | Serialization Format: cdr
                   Topic: /turtle1/pose | Type: turtlesim/msg/Pose | Count: 1913 | Serialization Format: cdr
Service:           0
Service information:

b572afc1bf6f47cd508e35a5ecfd4620.png

5 播放主题数据

在重放 bag 文件之前,请在运行 teleop 的终端中输入 Ctrl+C 。然后确保你的 turtlesim 窗口是可见的,这样你就可以看到 bag 文件的动作。

 输入命令:

ros2 bag play subset

终端将返回消息:

cxy@ubuntu2404-cxy:~/bag_files$ ros2 bag play subset
[INFO] [1720090572.751144971] [rosbag2_player]: Set rate to 1
[INFO] [1720090572.763453123] [rosbag2_player]: Adding keyboard callbacks.
[INFO] [1720090572.763515820] [rosbag2_player]: Press SPACE for Pause/Resume
[INFO] [1720090572.763540845] [rosbag2_player]: Press CURSOR_RIGHT for Play Next Message
[INFO] [1720090572.763561584] [rosbag2_player]: Press CURSOR_UP for Increase Rate 10%
[INFO] [1720090572.763580601] [rosbag2_player]: Press CURSOR_DOWN for Decrease Rate 10%
[INFO] [1720090572.763945754] [rosbag2_player]: Playback until timestamp: -1
[INFO] [1720090581.169025007] [rosbag2_player]: Pausing play.
[INFO] [1720090583.232968938] [rosbag2_player]: Resuming play.

您的乌龟将会跟随您在录制时输入的同一路径(尽管不是 100%精确;turtlesim 对系统时间的微小变化很敏感)。

7c0a18f77f846b06932cd0c8258caf40.png

因为 subset 文件记录了 /turtle1/pose 主题,所以只要你的 turtlesim 在运行,即使你没有移动, ros2 bag play 命令也不会退出。

这是因为只要 /turtlesim 节点处于活动状态,它就会定期在 /turtle1/pose 主题上发布数据。您可能已经注意到在上面的 ros2 bag info 示例结果中, /turtle1/cmd_vel 主题的 Count 信息只有17;那是我们在录制时按下箭头键的次数。

请注意, /turtle1/pose 的 Count 值超过 1913;在我们记录时,该主题的数据已发布了 3000 次。

要了解位置数据发布的频率,您可以运行以下命令:

ros2 topic hz /turtle1/pose

管理服务数据

1. 设置

您将在 introspection_client 和 introspection_service 之间记录服务数据,然后稍后显示并重放相同的数据。要在服务客户端和服务器之间记录服务数据,必须在节点上启用 Service Introspection 。

让我们启动 introspection_client 和 introspection_service 节点,并启用 Service Introspection 。您可以查看服务自省演示的更多详情。

打开一个新的终端并运行 introspection_service ,启用 Service Introspection :

ros2 run demo_nodes_cpp introspection_service --ros-args -p service_configure_introspection:=contents

打开另一个终端并运行 introspection_client ,启用 Service Introspection :

ros2 run demo_nodes_cpp introspection_client --ros-args -p client_configure_introspection:=contents

bed6deed59ced0d61ad6fe06f1fc14dd.png

2 检查服务可用性

ros2 bag 只能记录可用服务的数据。要查看系统服务列表,请打开一个新终端并运行命令:

ros2 service list

 将返回:

cxy@ubuntu2404-cxy:~$ ros2 service list
/add_two_ints
/introspection_client/describe_parameters
/introspection_client/get_parameter_types
/introspection_client/get_parameters
/introspection_client/get_type_description
/introspection_client/list_parameters
/introspection_client/set_parameters
/introspection_client/set_parameters_atomically
/introspection_service/describe_parameters
/introspection_service/get_parameter_types
/introspection_service/get_parameters
/introspection_service/get_type_description
/introspection_service/list_parameters
/introspection_service/set_parameters
/introspection_service/set_parameters_atomically

要检查 Service Introspection 是否在客户端和服务上启用,请运行以下命令:

ros2 service echo --flow-style /add_two_ints

您应该看到如下的服务通信:

cxy@ubuntu2404-cxy:~$ ros2 service echo --flow-style /add_two_ints
info:
  event_type: REQUEST_SENT
  stamp:
    sec: 1720099475
    nanosec: 143255056
  client_gid: [1, 15, 189, 33, 239, 105, 121, 147, 0, 0, 0, 0, 0, 0, 21, 3]
  sequence_number: 240
request: [{a: 2, b: 3}]
response: []
---
info:
  event_type: REQUEST_RECEIVED
  stamp:
    sec: 1720099475
    nanosec: 143700400
  client_gid: [1, 15, 189, 33, 239, 105, 121, 147, 0, 0, 0, 0, 0, 0, 20, 4]
  sequence_number: 240
request: [{a: 2, b: 3}]
response: []
---

3 记录服务

要记录服务数据,支持以下选项。服务数据可以与主题同时记录。

要记录特定服务:

ros2 bag record --service <service_names>

记录所有服务:

ros2 bag record --all-services

 执行命令:

ros2 bag record --service /add_two_ints

您将在终端中看到以下消息(日期和时间会有所不同):

cxy@ubuntu2404-cxy:~ros2 bag record --service /add_two_intsts
[INFO] [1720099698.274264497] [rosbag2_recorder]: Press SPACE for pausing/resuming
[INFO] [1720099698.281315607] [rosbag2_recorder]: Listening for topics...
[INFO] [1720099698.281318412] [rosbag2_recorder]: Event publisher thread: Starting
[INFO] [1720099698.393683310] [rosbag2_recorder]: Subscribed to topic '/add_two_ints/_service_event'
[INFO] [1720099698.394029292] [rosbag2_recorder]: Recording...

现在 ros2 bag 正在记录 /add_two_ints 服务上发布的服务数据。要停止录制,请在终端输入 Ctrl+C 。

数据将会累积在一个新的包目录中,该目录的名称模式为 rosbag2_year_month_day-hour_minute_second 。这个目录将包含一个 metadata.yaml 以及以录制格式的包文件。

7bb2f77e82429151acd2c0bf7b50e449.png

4. 检查服务数据

您可以通过运行以下内容查看录音的详细信息:

ros2 bag info <bag_file_name>

执行此命令将返回有关文件的信息列表:

cxy@ubuntu2404-cxy:~$ ros2 bag info rosbag2_2024_07_04-21_28_18


Files:             rosbag2_2024_07_04-21_28_18_0.mcap
Bag size:          31.7 KiB
Storage id:        mcap
ROS Distro:        jazzy
Duration:          29.740s
Start:             Jul  4 2024 21:28:18.404 (1720099698.404)
End:               Jul  4 2024 21:28:48.144 (1720099728.144)
Messages:          0
Topic information: 
Service:           1
Service information: Service: /add_two_ints | Type: example_interfaces/srv/AddTwoInts | Event Count: 242 | Serialization Format: cdr

5 播放服务数据

在重放 bag 文件之前,请在运行 introspection_client 的终端中输入 Ctrl+C 。当 introspection_client 停止运行时, introspection_service 也会停止打印结果,因为没有传入的请求。

重播包文件中的服务数据将开始向 introspection_service 发送请求。

 输入命令:

ros2 bag play --publish-service-requests <bag_file_name>

终端将返回消息:

cxy@ubuntu2404-cxy:~$ ros2 bag play --publish-service-requests rosbag2_2024_07_04-21_28_18
[INFO] [1720100036.322875992] [rosbag2_player]: Set rate to 1
[INFO] [1720100036.334878718] [rosbag2_player]: Adding keyboard callbacks.
[INFO] [1720100036.334924436] [rosbag2_player]: Press SPACE for Pause/Resume
[INFO] [1720100036.334947912] [rosbag2_player]: Press CURSOR_RIGHT for Play Next Message
[INFO] [1720100036.334970134] [rosbag2_player]: Press CURSOR_UP for Increase Rate 10%
[INFO] [1720100036.334990811] [rosbag2_player]: Press CURSOR_DOWN for Decrease Rate 10%
[INFO] [1720100036.335640862] [rosbag2_player]: Playback until timestamp: -1

您的 introspection_service 终端将再次开始打印以下服务消息:

[INFO] [1713997478.090466075] [introspection_service]: Incoming request
a: 2 b: 3

这是因为 ros2 bag play 将包文件中的服务请求数据发送到 /add_two_ints 服务。

我们也可以内省服务通信,因为 ros2 bag play 正在回放以验证 introspection_service 。

在 ros2 bag play 之前运行此命令以查看 introspection_service :

ros2 service echo --flow-style /add_two_ints

您可以从 bag 文件中看到服务请求,从 introspection_service 中看到服务响应。

cxy@ubuntu2404-cxy:~$ ros2 service echo --flow-style /add_two_ints
info:
  event_type: REQUEST_RECEIVED
  stamp:
    sec: 1720100131
    nanosec: 122539133
  client_gid: [1, 15, 189, 33, 81, 112, 35, 20, 0, 0, 0, 0, 0, 0, 20, 4]
  sequence_number: 1
request: [{a: 2, b: 3}]
response: []
---
info:
  event_type: RESPONSE_SENT
  stamp:
    sec: 1720100131
    nanosec: 122703746
  client_gid: [1, 15, 189, 33, 81, 112, 35, 20, 0, 0, 0, 0, 0, 0, 20, 4]
  sequence_number: 1
request: []
response: [{sum: 5}]

摘要

您可以使用 ros2 bag 命令记录在 ROS 2 系统中传递的主题和服务数据。无论您是与他人分享工作还是内省自己的实验,这都是一个非常好的工具。

 下一步

您已完成“初学者:CLI 工具”教程!下一步是开始“初学者:客户端库”教程,从创建工作区开始https://docs.ros.org/en/jazzy/Tutorials/Beginner-Client-Libraries/Creating-A-Workspace/Creating-A-Workspace.html 。

 相关内容 

关于 ros2 bag 的更详细解释可以在这里 https://github.com/ros2/rosbag2 的自述文件中找到。有关服务记录和回放的更多信息可以在这里的设计文档https://docs.ros.org/en/jazzy/How-To-Guides/Overriding-QoS-Policies-For-Recording-And-Playback.html 中找到。有关 QoS 兼容性和 ros2 bag 的更多信息,请参阅 rosbag2:覆盖 QoS 策略 https://docs.ros.org/en/jazzy/How-To-Guides/Overriding-QoS-Policies-For-Recording-And-Playback.html。

  • 19
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值