背景
【ROS 2 breaks complex systems down into many modular nodes.】
ROS2 将复杂的系统打散成为许多模块化的节点。
【Topics are a vital element of the ROS graph that act as a bus for nodes to exchange messages.】
Topic是ROS图中重要的元素,它像在节点之间交换信息的总线一样工作。
【A node may publish data to any number of topics and simultaneously have subscriptions to any number of topics.】
节点可以向任意数量的topics发布数据,同时可以订阅任意数量的topics。
【Topics are one of the important ways that data moves between nodes, and therefore between different parts of the system.】
Topics是一个数据在节点间乃至系统的不同部分之间流通重要的途径。
Tasks
1、Setup
#Open a new terminal and run:
ros2 run turtlesim turtlesim_node
#Open another terminal and run:
ros2 run turtlesim turtle_teleop_key
2、rqt_graph
在这个教程中,我们将使用rqt_graph来可视化的改变nodes和topics,以及他们之间的联系。
#打开rqt_graph
rqt_graph
rqt > selecting Plugins > Introspection > Nodes Graph.
打开后将会看到上面的nodes和topic,以及在图形外围的两个actions(暂不考虑)。当你把鼠标悬停在中间的topic上时,你将会看到像上图一样的高亮色彩。
上图描绘了/turtlesim节点和/teleop_turtle节点如何通过topic相互通信。 /teleop_turtle节点正在发布(publishing)数据(输入的用于移动乌龟的击键)到/ turtle1/cmd_vel主题,并且/turtlesim节点已订阅(subscribed)该主题以接收数据。
rqt_graph这个高亮的功能在 检查更复杂的有很多节点且主题的连接有很多方式的系统时 非常有效。
rqt_graph是一个图形化的自检的工具。下面看一下用于自检的命令行工具
3、ros2 topic list
ros2 topic list #return a list of all the topics currently active in the system
ros2 topic list -t
#will return the same list of topics, this time with the topic type appended in brackets after each
#将返回相同的主题列表,这次每个主题之后附有主题类型
topics 有名字红外类型。这些属性(尤其是类型)让节点知道他们正在谈论的信息是同一个话题。
将Hide中所有的选项全不勾选,可以看到rqt_graph中所有这些topices。
【很奇怪,我的图画出来之后会比教程中的图多了一个节点 /_ros2cli_daemon_0,但是检查了 node list 也是正确的,就迷惑20200731】
【20200803】再一次尝试时,没有执行 ros2 node list 这个命令,得到的结果如下图:
执行 ros2 node list后, /_ros2cli_daemon_0又出现了
这似乎说明,列举node导致这个节点的出现,理由未知。
在这个阶段先把他们隐藏,避免混淆
4、ros2 topic echo
【To see the data being published on a topic, use:】
通过以下命令来查看一个topic中被广播的数据
#ros2 topic echo <topic_name>
【Since we know that /teleop_turtle
publishes data to /turtlesim
over the /turtle1/cmd_vel
topic, let’s use echo
to introspect on that topic:】
既然我们已知/teleop_turtle通过/ turtle1/cmd_vel主题将数据发布到/turtlesim,那让我们使用echo对该主题进行自检:
ros2 topic echo /turtle1/cmd_vel
【At first, this command won’t return any data. That’s because it’s waiting for /teleop_turtle
to publish something.】
首先,这条指令并不会返回任何数据。这是因为它在等待/teleop_turtle发送些什么
【
Return to the terminal where turtle_teleop_key
is running and use the arrows to move the turtle around. Watch the terminal where your echo
is running at the same time, and you’ll see position data being published for every movement you make:】
回到turtle_teleop_key
正在运行的终端,通过箭头来试小乌龟移动。同时观察运行echo的终端,此时你将会看到每次你做出移动都会发布位置信息的数据。
【Now return to rqt_graph and uncheck the Debug box.】
现在回到rqt_graph ,并且取消勾选Debug框
【/_ros2cli_4290
is the node created by the echo
we just ran (the number will change). Now you can see that the publisher is publishing data over the cmd_vel
topic, and two subscribers are subscribed.】
/_ros2cli_4290就是我们刚才运行echo创建的节点(这个数值会变化)。现在我们可以看出发布者通过cmd_vel topic来发布数据,并且两个订阅者都会接收到(这个消息)。
5、ros2 topic info
【Topics don’t have to only be point-to-point communication; it can be one-to-many, many-to-one, or many-to-many.】
topic并不只有点对点的通讯,它还可以是 一对多,多对一,或者多对多(地通讯);
【Another way to look at this is running:】
另一种查看通讯的方法是运行下面的语句:
ros2 topic info /turtle1/cmd_vel
【Which will return:】
将会看到以下返回结果:
【Nodes send data over topics using messages. Publishers and subscribers must send and receive the same type of message to communicate.】
节点使用消息(messages)通过主题(topics)来传递数据。发布者和订阅者都必须发送和接收同一类型的消息进行通讯。
【The topic types we saw earlier after running ros2 topic list -t
let us know what type of messages each topic can send. Recall that the cmd_vel
topic has the type:】
通过运行 ros2 topic list -t
让我们之前看到的主题类型,让我们知道每个topic可以发送什么样的消息类型。回顾cmd_vel主题的类型:geometry_msgs/msg/Twist
【This means that in the package geometry_msgs
there is a msg
called Twist
.】
这意味着在geometry_msgs
包中,有一个msg 名字是 Twist
【Now we can run ros2 interface show <type>.msg
on this type to learn its the details, specifically, what structure of data the message expects.】
现在我们可以运行ros2 interface show <type>.msg在这个类型上来了解更多细节,特别是message需要什么样的数据结构。
ros2 interface show geometry_msgs/msg/Twist
【This tells you that the /turtlesim
node is expecting a message with two vectors, linear
and angular
, of three elements each. If you recall the data we saw /teleop_turtle
passing to /turtlesim
with the echo
command, it’s in the same structure:】
这告诉我们 /turtlesim
节点希望消息拥有两个维度,linear
和 angular
,每个维度有三个元素。如果我们回顾 使用echo命令得到的 /teleop_turtle
发送给 /turtlesim 的数据就会发现,这个和上面的要求拥有同样的数据结构。
【Now that you have the message structure, you can publish data onto a topic directly from the command line using:】
现在我们已经知道的消息的结构,我们可使用命令行以直接发布数据到一个主题:
#ros2 topic pub <topic_name> <msg_type> '<args>'
【The '<args>'
argument is the actual data you’ll pass to the topic, in the structure you just discovered in the previous section.】
<args>参数是我们传递给topic的实际数据,满足上一节我们刚发现的这个结构。
【It’s important to note that this argument needs to be input in YAML syntax. Input the full command like so:】
尤其重要的是要注意这个参数需要用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
is an optional argument meaning “publish one message then exit”.】
--once 参数是一个可选的参数,它的意思是 输出一个消息并退出
【
You will receive the following message in the terminal:】
我们将会在终端看到以下消息输出和海龟的运动:
【The turtle (and commonly the real robots which it is meant to emulate) require a steady stream of commands to operate continuously. So, to get the turtle to keep moving, you can run:】
海龟(和通常所指需要模拟的真正的机器人)需要一个连续的指令流来持续运动。所以想让小海龟持续运动可以输入以下指令:
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}}"
【The difference here is the removal of the --once
option and the addition of the --rate 1
option, which tells ros2 topic pub
to publish the command in a steady stream at 1 Hz.】
此处的区别是删除了--once选项,并增加了--rate 1选项,这告诉ros2 topic pub 发布1Hz的稳定的指令流。
【You can refresh rqt_graph to see what’s happening graphically. You will see the ros 2 topic pub ...
node (/_ros2cli_30358
) is publishing over the /turtle1/cmd_vel
topic, and is being received by both the ros2 topic echo ...
node (/_ros2cli_26646
) and the /turtlesim
node now.】
我们可以刷新rqt_graph来查看正在发生的事。我们将会看到 ros 2 topic pub ...
node(/_ros2cli_5447)通过
/turtle1/cmd_vel
主题来发布数据,它发布的数据被 ros2 topic echo ...
node(/_ros2cli_4290
)和(/turtlesim
)接收。
【Finally, you can run echo
on the pose
topic and recheck rqt_graph:】
最后,我们可以运行echo在pose主题上,并且重新查看rqt_graph:
ros2 topic echo /turtle1/pose
【In this case, /turtlesim
is now publishing to the pose
topic, and a new echo
node is subscribed.】
在这样的情况下,/turtlesim 现在发布到pose主题上,并且预定一个新的echo节点。
【For one last introspection on this process, you can report the rate at which data is published using:】
对于这个过程最后一个自检,我们可以通过使用下面的方法来报告发布数据的速率:
ros2 topic hz /turtle1/pose
【It will return data on the rate at which the /turtlesim
node is publishing data to the pose
topic.】
它将会返回一个数据表示 /turtlesim
节点发布数据到 pose 主题的速率。
【Recall that you set the rate of turtle1/cmd_vel
to publish at a steady 1 Hz using ros2 topic pub --rate 1
. If you run the above command with turtle1/cmd_vel
instead of turtle1/pose
, you will see an average reflecting that rate.】
回顾你设置的turtle1/cmd_vel发送的
速率固定为 1 Hz 使用了 ros2 topic pub --rate. 如果你运行上面的命令是使用了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.】
此时,您将有很多节点在运行。 不要忘记通过关闭终端窗口或在每个终端中输入Ctrl + C来停止它们。
总结
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.
节点通过主题来发布信息,它允许任何数量的其他节点来订阅和接收这些信息。在这个教程中我们使用rqt_graph和命令行练习了几个节点间通过主题来进行的交流。你现在应该对数据如何在ROS2 系统中移动有个更好的了解了。