ROS6—理解话题

这篇教程介绍ROS主题与使用rostopic和rqt_plot

设置

在新终端中运行rosrun,在另一个新终端中运行rosrun turtlesim turtlesim_node。
我们需要驱动turtle,由键盘远程操作,在一个新终端中运行:

$ rosrun turtle turtle_teleop_key

会得到类似这样的信息:

[ INFO] 1254264546.878445000: Started node [/teleop_turtle], pid [5528], bound on [aqy], xmlrpc port [43918], tcpros port [55936], logging to [~/ros/ros/log/teleop_turtle_5528.log], using [real] time
Reading from keyboard
---------------------------
Use arrow keys to move the turtle.

现在可以使用箭头键来驱动小龟运动。若不能驱动则在运行turtle_teleop_key的终端内确认你输入的键被记录。
Turtlesim_teleop
下面探究背后的机理。

ROS话题(topics)

上例中turtle_node和turtle_teleop_key通过ROS话题来通信。turtle_teleop_key向一个话题发布关键笔画消息,而turtlesimz则订阅同一个话题的笔画消息。可以使用rqt_graph来现实当前运行的节点和话题。
需注意的是若使用的是electric或更早的,使用rxgraph。
rqt_graph创建一个系统正在运行程序的动态图。rqt_graph是rat包的一部分,安装:

$ sudo apt-get install ros-<distro>-rqt
$ sudo apt-get install ros-<distro>-rqt-common-plugins

在新终端中输入:

$ rosrun rqt_graph rqt_graph

则会得到类似的图:

若将鼠标放在/turtle2/command_velocity则会高亮ROS节点和话题。可以看出turtlesim_node和turtle_teleopkey正在名为/turtle1/command——velocity上通信。

可以使用rostopic工具得到关于ROS话题的信息;使用帮助选项得到rostopic的子命令。

rostopic -h

会得到

Commands:
    rostopic bw display bandwidth used by topic
    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 type
Type rostopic <command> -h for more detailed usage, e.g. 'rostopic echo -h'

命令rostopics echo 显示向话题发布的消息

rostopic echo [topic]

查看turtle_teleop_key节点发布的command velocity数据。
对ROS版本Hydro和后面的版本,数据发布在/turtle1/cmd_vel话题上。在新终端中运行:

$ rostopic echo /turtleq/vmd_vel

对Groovy以及前面的版本,则在新终端中运行:

$ rostopics echo /turtle1/command_velocity

因为现在没有发送任何消息,所以什么也不会看到。现在通过按箭头键来使turtle_teleop_key发布消息。
对ROS Hydro以及之后的版本,会看到类似:

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
---

而对于ROS Groovy以及之前的版本,则会看到

---
linear: 2.0
angular: 0.0
---
linear: 2.0
angular: 0.0
---
linear: 2.0
angular: 0.0
---
linear: 2.0
angular: 0.0
---
linear: 2.0
angular: 0.0

现在按下rqt_graph图左上角refresh按钮来现实新的节点。可以看到rostopic echo现在也订阅了/turtle1/cmd_vel。
rsotopic list命令返回所有订阅的或发布的话题。通过帮助选项查看list子命令所需的参数。在新终端中运行:

$ 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

这里使用verbose选项:

$ rostopic list -v

会显示发布和订阅话题的冗长的列表以及它们的类型:

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

Subscribed topics:
 * /turtle1/cmd_vel [geometry_msgs/Twist] 2 subscribers
 * /rosout [rosgraph_msgs/Log] 1 subscriber
 * /statistics [rosgraph_msgs/TopicStatistics] 1 subscriber
3.ROS消息(Messages)

话题上的节点间通信通过发送消息发生。发布者和订阅者需发送和接受相同类型的消息。这就意味着一个话题的类型由发布的消息的类型定义。在话题上传递的消息可以使用rostopic type来确定。
rostopic type返回正在发布消息的类型。

rostopic type [topic]

对ROS Hydro或者更后的版本,使用:

$ rostopic type /turtle1/cmd_vel

会得到

geometry_msgs/Twist

使用rosmsg来看到更详细的细节:

$ 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

对ROS Groovy和更早的版本,使用:

$ rostopic type /turtle1/command_velocity

会得到:

turtlesim/Velocity

使用rosmsg:

$ rosmsg show turtlesim/Velocity

得到:

float32 linear
float32 angular

知道turtlesim所期待的数据类型后,可以想turtle发布命令。

4.rostopic 持续运动

rostopic pub发布数据到当前正在广告的主题上:

rostopic pub [topic] [msg_type] [args]

对ROS Hydro及以后版本,例如:

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

对ROS Groovy以及前面的版本,相应的则是:

$ rostopic pub -1 /turtle1/command_velocity turtlesim/Velocity  -- 2.0  1.8

前面的命令会发送一个命令到turtlesim告诉其以线速度2、角速度1.8移动:

再详细来看每个参数的含义。
对ROS Hydro及以后版本,
rostipic pub 会将消息发布到给定的话题;
参数-1会使rostopic仅发布消息然后推出;
/turtle1/cmd_vel 是发向的消息的名称;
geometry_msgs/Twist是发布的消息的类型;
参数- -告诉参数解析器后面的参数不是选项;这在以-开始的参数时必须用,比如负数;
如前面提到的,一个geometry_msgs/Twist msg有两个三维实向量:前者是线速度,后者是角速度。这些参数实际上遵循YAML语法。
ROS Groovy及其之前的版本类似。
注意到小龟停了下来;这是因为小龟需要以稳定的1Hz的命令流类似保持运动。使用rostopic pub -r可以发布稳定的命令流。
ROS Hydro及其以后版本:

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

ROS Groovy及其之前版本:

$ rostopic pub /turtle1/command_velocity turtlesim/Velocity -r 1 -- 2.0  -1.8

这会以1Hz频率发布速度命令:

也可以通过rqt_graph查看发生了什么,topic pub节点正在与rostopic echo节点通信:

小龟会一直沿着圆运动。在新终端中,可以使用rostopic echo来查看ruttlesim发布的数据

rostopic echo /turtle1/cmd_vel

rostopic hz会报告数据发布的速度:

rostopic hz [topic]

查看turtlesim_node发布/turtle1/pose的速度:

$ rostopic hz /turtle1/pose

会得到:

subscribed to [/turtle1/pose]
average rate: 62.501
    min: 0.016s max: 0.016s std dev: 0.00000s window: 62
average rate: 62.500
    min: 0.016s max: 0.016s std dev: 0.00000s window: 124
average rate: 62.500
    min: 0.016s max: 0.016s std dev: 0.00001s window: 187
average rate: 62.500
    min: 0.016s max: 0.016s std dev: 0.00001s window: 249
average rate: 62.500
    min: 0.016s max: 0.016s std dev: 0.00001s window: 312
average rate: 62.500
    min: 0.016s max: 0.016s std dev: 0.00001s window: 374
average rate: 62.500
    min: 0.016s max: 0.016s std dev: 0.00001s window: 437
average rate: 62.500
    min: 0.016s max: 0.016s std dev: 0.00001s window: 500
average rate: 62.500
    min: 0.016s max: 0.016s std dev: 0.00000s window: 562
                        ......

可以看出turtlesim正以60Hz的速率发布数据。也可以将rostopic type和rosmsg show结合起来使用来更深入地得到关于话题的信息。
ROS Hydro及以后版本:

$ rostopic type /turtle1/cmd_vel | rosmsg show

ROS Groovy及之前:

$ rostopic type /turtle1/command_velocity | rosmsg show

现在已经使用rostopic测试了话题,接下来用另一种工具查看turtle产生的数据。

5.使用rqt_plot

若使用electric及更早的版本,使用rxplot。
rqt_plot显示话题发布的数据的一段滚动间的绘图。这里使用rqt_plot来绘制关于话题/turtle1/pose发布的数据。先启动rqt_plot:

rosrun rqt_plot rqt_plot

弹出一个新窗口。在左上角的文本框可以让你添加任何话题来绘制。输入/turtle1/pose/x会使加号按钮高亮,按下它;对/turtle1/pose/y重复同样的过程,就能看到小龟的x-y地址在图中绘制出来。

按下负号键会显示一个选择隐藏特定话题的菜单。
对这个会话,使用Ctrl-C来杀死rostopic终端但使turtlesim继续运行。
了解了ROS话题工作原理之后,再看服务和参数工作。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值