【ROS2学习笔记】: TOPIC 篇

参考于ros2官方文档:Understanding topics — ROS 2 Documentation: Foxy documentation

目录

背景

先决条件

任务

1、启动小乌龟程序

2 rqt_graph

3 ros2 topic list

4 ros2 topic echo

5 ros2 topic info

6 ros2 interface

        


背景

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.——ROS2信息传递的一种方法,topic也叫话题

A node may publish data to any number of topics and simultaneously have subscriptions to any number of topics.——每个节点可以接受多个也可以发布多个话题

Topics are one of the main ways in which data is moved between nodes and therefore between different parts of the system.  ——话题通信很常用,多用于及时更新的场合

先决条件

对节点信息有一定的了解,使用小乌龟来学习

任务

1、启动小乌龟程序

ros2 run turtlesim turtlesim_node

再打开一个终端

ros2 run turtlesim turtle_teleop_key

顺利启动小乌龟

之后使用命令

ros2 node list

查看node 名称,一般默认的是/turtlesim/teleop_turtle by default.

2  rqt_graph

可视化工具,rqt中的一个插件,可以将节点信息和话题信息可视化

rqt_graph

还可以通过打开rqt并选择Plugins > Introspection > Node Graph来打开rqt_graph。

您应该可以看到上面的节点和主题,以及图形外围的两个操作(我们暂时忽略它们)。如果您将鼠标悬停在中心的主题上,您将看到颜色突出显示,如上图所示。

该图描述了/turtlesim节点和/teleop_turtle节点如何在主题上相互通信。/teleop_turtle节点正在向/turtle 1/cmd_vel主题发布数据(您输入的移动乌龟的按键),而/turtlesim节点订阅了该主题以接收数据。

rqt_graph的突出显示特性在检查具有许多节点和以许多不同方式连接的主题的更复杂的系统时非常有帮助。

3 ros2 topic list

在新终端中运行ros2 topic list命令将返回系统中当前活动的所有主题的列表:

ros2 topic list

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

4 ros2 topic echo

要查看话题发布的数据,可以使用echo指令

ros2 topic echo <topic_name>

我们知道/teleop_turtle通过/turtle 1/cmd_vel主题向/turtlesim发布数据,那么让我们使用echo来反思一下这个主题:

ros2 topic echo /turtle1/cmd_vel

当在控制台操纵小乌龟运动的时候,总段会打印出对应的信息

5 ros2 topic info

话题不一定只能是一对一的交流;它们可以是一对多、多对一或多对多

也可以通过info查看话题信息

ros2 topic info /turtle1/cmd_vel

得到的内容为

Type: geometry_msgs/msg/Twist          ——  话题传输数据的类型
Publisher count: 2                                  ——pub数量
Subscription count: 2                             ——sub数量

6 ros2 interface

节点使用消息在主题上发送数据。 发布者和订阅者必须发送和接收相同类型的消息才能通信。

我们前面在运行ros2 topic list -t后看到的主题类型让我们知道每个主题上使用的消息类型。 回想一下,cmd_vel主题的类型是:

geometry_msgs/msg/Twist

现在我们可以在这个类型上运行ros2 interface show <msg 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节点期待一条包含两个向量linearangular的消息,每个向量包含三个元素。

回想一下我们看到的/teleop_turtle通过/turtlesim命令传递给echo的数据,它的结构是相同的:

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

        

可以使用下列语法进行发布消息

ros2 topic pub <topic_name> <msg_type> '<args>'
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是一个可选参数,意思是“发布一条消息然后退出”。

之后乌龟像这样移动:

海龟需要稳定的命令流才能连续运行。 所以,要让乌龟继续移动,你可以运行:

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 改为了 --rata 1 即设置发送信息的频率是1hz 也即一秒一次。

8 ros2 topic hz

可以使用以下命令查看数据发布的速率

ros2 topic hz /turtle1/pose

它将返回关于/turtlesim节点向pose主题发布数据的速率的数据。

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值