ROS2教程-理解ROS2 topics

背景

ROS2将复杂的系统分解为很多模块节点.话题(Topics)是ROS图中的重要元素,是节点交换信息的途径.一个节点可以同步发布任意数量的话题,也可以同步接收任意数量的消息.话题(topics)是数据在节点以及系统不同部分之间交换的最重要的方式.

ROS 2 breaks complex systems down into many modular nodes. Topics are a vital element of the ROS graph that act as a bus for nodes to exchange messages. A node may publish data to any number of topics and simultaneously have subscriptions to any number of topics. Topics are one of the important ways that data moves between nodes, and therefore between different parts of the system.

要求

上一节教程提供了一些有关节点(node)的基础背景.与往常一样,不要忘记在打开的每个新终端中都source ROS 2。

任务

1. Setup

打开一个新的terminal然后运行

ros2 run turtlesim turtlesim_node

打开另一个terminal然后运行

ros2 run turtlesim turtle_teleop_key

前面的教程提到过这两个节点名称为/turtlesim和/teleop_turtle.

2. rqt_graph

通过这个教程,我们将使用rqt_graph去可视化节点和话题,并且可视化他们之间的连接关系.

打开一个terminal,输入以下命令

rqt_graph

你也可以通过打开rqt然后选择Plugin->Introspection->Nodes Graph 来打开rqt_graph.

你可以看到以上的节点和话题以及围绕action图形外围的两个操作(让我们暂时忽略它们)。如果将鼠标悬停在中间的主题上,则会看到颜色突出显示,如上图所示。

该图描述了/turulesim节点和/teleop_turtle节点如何通过主题相互通信./teleop_turtle节点将数据发布到/turtle1/cmd_vel话题,并且/turtlesim节点已订阅该主题以接收数据.

当检查具有许多节点和话题,并且许多不同方式连接的更复杂的系统时,rqt_graph的突出显示功能非常有用。

rqt_graph是一种图形化工具。现在,我们将介绍一些用于话题的命令行工具。

3. ros2 topic list

在一个新的terminal中运行ros2 topic list命令将会返回所有的在系统中激活状态的消息列表.

/parameter_events
/rosout
/turtle1/cmd_vel
/turtle1/color_sensor
/turtle1/pose

ros2 topic list -t 将会返回相同的列表,并且会在后面显示话题的消息类型.

话题具有名称和类型。节点通过这些属性(尤其是类型)是知道他们在使用相同信息。

如果您想知道所有这些主题在rqt_graph中的位置,可以取消选中“隐藏”下的所有复选框:

4.ros2 topic echo

可以通过以下方式来看相应话题中的数据:

因为我们知道/teleop_turtle通过/turtle1/cmd_vel发布相应的数据到/turtlesim,  我们可以echo相应的话题:

ros2 topic echo /turtle1/cmd_vel

最开始,这个命令将不会返回任何数据.因为他在等待/teleop_turtle发布命令.

返回到turtle_teleop_key运行的terminal,然后使用方向键移动屏幕上的乌龟.此时观察echo的terminal,你会看到同时乌龟的位置数据被发布出来.

linear:
  x: 2.0
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 0.0
  ---

Now return to rqt_graph and uncheck the Debug box.

/_ros2cli_26646是运行/echo后产生的节点.现在你可以看到这个发布器正在通过cmd_vel话题发布数据,同时两个接收器来接收相应消息.

5 ros2 topic info

消息不必要进行一对一通信,消息可以进行一对多或者多对一或者多对多通信.

可以用另一种方式来查看话题的详情:

ros2 topic info /turtle1/cmd_vel
Which will return:

Type: geometry_msgs/msg/Twist
Publisher count: 1
Subscriber count: 2

6 ros2 interface show

节点可以发送话题通过消息.发布器和接收器必须发布和接收相同类型的类型.

之前我们可以通过运行ros2 topic list -t来查看消息类型.这里可以查看到cmd_vel话题的类型.

geometry_msgs/msg/Twist

这意味着在geometry_msgs包中包含了一个消息叫作 Twist

现在我们可以运行run ros2 interface show <type>来查看这个类型的详细信息,尤其是这个消息的数据结构.

ros2 interface show geometry_msgs/msg/Twist
# This expresses velocity in free space broken into its linear and angular parts.

    Vector3  linear
    Vector3  angular

上面告诉我们/turtlesim节点是一个包含两个矢量的消息,linear和angular,每个消息包含三个元素.如果你echo /teleop_turtle的数据,可以看到相同的数据结构

linear:
  x: 2.0
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 0.0
  ---

7 ros2 topic pub

现在你已经了解消息的数据结构,你可以通过下面的命令来发布相应的数据到话题中:

ros2 topic pub <topic_name> <msg_type> '<args>'

<args>是您想要发布的数据,您可以根据前面提到的数据

“ <args>”自变量是您将传递给该主题的实际数据,位于您在上一节中刚刚发现的结构中。

请务必注意,此参数需要以YAML语法输入。 输入完整的命令,如下所示:

ros2 topic pub --once /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"

--once是一个可选参数,意思是“发布一条消息然后退出”。

您将在终端中收到以下消息:

publisher: beginning loop
publishing #1: geometry_msgs.msg.Twist(linear=geometry_msgs.msg.Vector3(x=2.0, y=0.0, z=0.0), angular=geometry_msgs.msg.Vector3(x=0.0, y=0.0, z=1.8))

您会看到乌龟像这样移动:


乌龟(通常是指要模拟的真正的机器人)需要源源不断的命令才能连续运行。 因此,要使乌龟继续运动,可以运行:

ros2 topic pub --rate 1 /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"

此处的区别是删除了--once选项,并添加了--rate 1选项,这告诉ros2 topic pub以1 Hz的稳定流发布命令。

您可以刷新rqt_graph以图形方式查看正在发生的消息和节点。 您将看到ros 2 topic pub ...节点(/ _ros2cli_30358)正在/ turtle1 / cmd_vel话题上发布,并且同时被ros2 topic echo ...节点(/ _ros2cli_26646)和/ turtlesim节点接收 。

最后,您可以在pose主题上运行echo并重新检查rqt_graph:

ros2 topic echo /turtle1/pose

在这种情况下,/ turtlesim现在将发布到位姿态消息,并发布了一个新的显示节点。

8 ros2 topic hz

对于此过程的最后一次介绍,您可以使用以下方法查看发布数据的速率:

ros2 topic hz /turtle1/pose

它将返回/ turtlesim节点发布的位姿话题数据的的频率。

average rate: 59.354
  min: 0.005s max: 0.027s std dev: 0.00284s window: 58

回想一下,您使用ros2 topic pub --rate 1设置了turtle1 / cmd_vel的发布速率为稳定的1 Hz。如果您使用turtle1 / cmd_vel而不是turtle1 / pose来运行以上命令,您将看到一个反映该速率的平均值 。

9 Clean up

At this point you’ll have a lot of nodes running. Don’t forget to stop them, either by closing the terminal windows or entering Ctrl+C in each terminal.

Summary

Nodes publish information over topics, which allows any number of other nodes to subscribe to and access that information. In this tutorial you examined the connections between several nodes over topics using rqt_graph and command line tools. You should now have a good idea of how data moves around a ROS 2 system.

Next steps

Next you’ll learn about another communication type in the ROS graph with the tutorial Understanding ROS 2 services

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值