6_ROS话题消息(Topic)

这部分教程介绍ROS中Topic的概念以及与Topic相关的rostopic,rqt_plot等工具的使用。至于为什么选择deepin而不是ROS通用的ubuntu,也仅仅是为了支持国产系统。鉴于本人水平有限,如哪位攻城狮网友发现本文存在的问题,烦请留言指正,谢谢!

运行小乌龟

# 终端1--运行master节点
roscore 
# 终端2--运行turtlesim_node用于展示小乌龟
rosrun turtlesim turtlesim_node
# 终端3--运行turtle_teleop_key用于控制小乌龟运动
rosrun turtlesim  turtle_teleop_key

在上述三个节点都运行起来后,可以在turtle_teleop_key节点的终端中通过操作方向键来控制小乌龟运动。
在这里插入图片描述

ROS Topic

turtlesim_node和turtle_teleop_key两个节点使用Topic进行通信。turtle_teleop_key节点在Topic上发布方向键的点击消息,turtlesim节点订阅对应的消息。可以使用rqt_graph来显示正在运行的节点和Topic.

rqt_graph #或者rosrun rqt_graph rqt_graph

#如果命令不存在,可直接安装 <distro>--->代表你所使用的ros版本
$ sudo apt-get install ros-<distro>-rqt
$ sudo apt-get install ros-<distro>-rqt-common-plugins

在这里插入图片描述

rostopic

rostopic工具可方便你获取Topic相关信息

gaoy@msi:~$ rostopic -h
rostopic is a command-line tool for printing information about ROS Topics.

Commands:
        rostopic bw     display bandwidth used by topic
        rostopic delay  display delay of topic from timestamp in header
        rostopic echo   print messages to screen
        rostopic find   find topics by type
        rostopic hz     display publishing rate of topic    
        rostopic info   print information about active topic
        rostopic list   list active topics
        rostopic pub    publish data to topic
        rostopic type   print topic or field type

Type rostopic <command> -h for more detailed usage, e.g. 'rostopic echo -h'

rostopic echo

实时在终端展示对应topic的消息体,当然如果想看到对应的消息,需要在turtle_teleop_key终端中输入具体的操作指令。

gaoy@msi:~$ rostopic echo /turtle1/cmd_del
linear: 
  x: 2.0
  y: 0.0
  z: 0.0

添加了echo节点后的rqt_graph如下图所示
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
在这里插入图片描述

如上图所示多了一个rostopic_33509_xxx的节点也订阅了/turtle1/cmd_devl topic的消息。

rostopic list

列出当前正在运行的topic。

gaoy@msi:~$ rostopic list -h
Usage: rostopic list [/namespace]

Options:
  -h, --help            show this help message and exit
  -b BAGFILE, --bag=BAGFILE
                        list topics in .bag file
  -v, --verbose         list full details about each topic
  -p                    list only publishers
  -s                    list only subscribers
  --host                group by host name

# 运行rostopic list -v
gaoy@msi:~$ rostopic list -v

Published topics:
 * /rosout_agg [rosgraph_msgs/Log] 1 publisher
 * /rosout [rosgraph_msgs/Log] 5 publishers
 * /turtle1/pose [turtlesim/Pose] 1 publisher
 * /turtle1/color_sensor [turtlesim/Color] 1 publisher
 * /turtle1/cmd_vel [geometry_msgs/Twist] 1 publisher

Subscribed topics:
 * /rosout [rosgraph_msgs/Log] 1 subscriber
 * /turtle1/cmd_vel [geometry_msgs/Twist] 2 subscribers
 * /statistics [rosgraph_msgs/TopicStatistics] 2 subscribers

ROS Message

节点之间可以使用使用ROS消息进行Topic通信。对于publisher和subscrber来说两者必须发送和接收相同类型的消息。这意味着Topic的类型是由publiser发布的消息类型决定的。可以使用rostopic type来确定某个Topic所发送的消息的类型。

rostipc tyoe

# 查看topic列表
rostopic list
/rosout
/rosout_agg
/turtle1/cmd_vel
/turtle1/color_sensor
/turtle1/pose
# 查看某个topic的消息类型
rostopic type /turtle1/cmd_vel
geometry_msgs/Twist
# 查看消息的详细细节
rosmsg show geometry_msgs/Twist
geometry_msgs/Vector3 linear
  float64 x
  float64 y
  float64 z
geometry_msgs/Vector3 angular
  float64 x
  float64 y
  float64 z

通过以上方法我们已经知道发布节点所期望的消息类型.

使用rostopic 发布消息

rostopic将数据发送到指定Topic的方法如下

rostopic pub [topic] [msg_type] [args]
# 示例
rostopic pub -1 /turtle1/cmd_vel geometry_msgs/Twist -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'

效果如下:
在这里插入图片描述

上面的示例向turtlesim发送一条消息,告诉它以2.0的线速度和1.8的角速度移动。另外上面的示例也是一个相当复杂的例子。

  • rostopic pub:向指定topic发布消息
  • -1:此参数(单破折号),表示仅使用rostopic发布一条消息。然后退出
  • /turtle1/cmd_vel:topic名
  • geometry_msgs/Twist:待发布消息的消息类型
  • –:(双破折号)告诉选项解析器,以下参数都不是选项。在参数有前导破折号的情况下,这是必须的。
  • ‘[2.0, 0.0, 0.0]’ ‘[0.0, 0.0, 1.8]’:如前所述,geometry_msgs/Twist有两个包含3个浮点元素的向量:线性向量和角向量。在本例中使得线性向量的x=2.0,y=0.0,z=0.0;角向量中的x=0.0,y=0.0,z=1.8.这些参数实际上使用的YAML命令行

你应该注意到了海龟已经停止了运动,这是因为海龟需要1Hz的稳定指令流来保持运动,我们可以使用rostopic pub -r 来发布一系列稳定的命令:

rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'

效果如下:
在这里插入图片描述
小海龟在上次运动的终点,持续进行画圆运动。我们也可以使用rqt_graph来查看具体发生了什么
在这里插入图片描述

注意此处rqt_graph出来的图与上述有出入,这里只需要注意到有两个节点在向turtlesim发布/tutle1/cmd_vel即可。

rostopic hz

hz工具用于反馈具体topic的平均发布周期

rostopic hz [topic]
# 示例
rostopic hz /turtle1/pose
subscribed to [/turtle1/pose]
average rate: 62.509
        min: 0.016s max: 0.017s std dev: 0.00044s window: 62

从上面的打印可以看出/turtle1/pose的topic的发布周期是60Hz左右。另外我们可以将rostopic type和rosmsg show联合起来使用

 rostopic type /turtle1/cmd_vel | rosmsg show
geometry_msgs/Vector3 linear
  float64 x
  float64 y
  float64 z
geometry_msgs/Vector3 angular
  float64 x
  float64 y
  float64 z

目前为止我们使用rostopc分析了topic,可以使用rqt_plot来查看turtlesim发布的数据
rqt_plot显示关于topic上发布数据的滚动时间图。这里我们将使用rqt_plot来绘制在/turtle1/pose上发布的数据。

rqt_plot

效果图如下:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值