理解ROS主题(rawmeat:http://wiki.ros.org/ROS/Tutorials/UnderstandingTopics)
注意:本教程翻译的日期是2018.9.12,wiki.ros.org可能会有更新,如语意有差异请以官网英文原版为准,我是不负泽任的。
描述:这个教程介绍了ROS主题,使用rostopic和rqt_plot命令行工具。
教程等级:初学者
下一个教程:理解ROS服务与参数
内容 |
---|
1.安装 |
1.roscore |
2.turtlesim |
3.turtle 键盘遥控操作 |
2.ROS主题 |
1.使用 rqt_graph |
2.介绍 rostopic |
3.使用 rostopic echo |
4.使用 rostopic list |
3.ROS消息 |
1.使用 rostopic type |
4.rostopic续 |
1.使用 rostopic pub |
2.使用 rostopic hz |
5.使用 rqt_plot |
6.视频教程 |
1.安装
1.1 roscore
让我们开始,保证我们有roscore在运行,在一个新的终端中输入:
$ roscore
如果你从上一个教程留着roscore运行,你可能得到错误的信息:
roscore cannot run as another roscore/master is already running.
Please kill other roscore/master processes before relaunching
这没关系,只能有一个roscore在运行。
1.2 turtlesim
在这个教程中我们也会使用turtlesim,请在一个新的终端中运行:
$ rosrun turtlesim turtlesim_node
1.3 turtle 键盘遥控操作
我们将会需要一些东西来驱动turtle四处运动,请在一个新的终端中运行:
$ rosrun turtlesim 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四处移动。如果你不能驱动它,选择turtle_teleop_key的终端窗口,确保你输入的按键可以被记录
现在你可以四处驱动turtle,然我们看看在这情景之后有什么在运行。
2.ROS主题
turtlesim_node 和 the turtle_teleop_key 在一个ROS topic相互沟通。turtle_teleop_key 正在一个话题上发布键位动作,同时 turtlesim 订阅相同的话题,接收键位动作。让我们使用rqt_graph,展示正在运行的节点与话题。
注意:如果你正在使用electric或更早的版本,rqt将不能使用,使用rxgraph替代。
2.1使用rqt_graph
Rqt_graph创建了一个在系统中正在运行的动力学图。rqt_graph是rqt package的一部分。除非你已经安装了,运行:
$ sudo apt-get install ros-< distro >-rqt
$ sudo apt-get install ros-< distro >-rqt-common-plugins
将< distro>替换成你的ROS版本(比如说indigo,jade,kenetic,lunar。。。等等)
在一个新的终端中:
$ rosrun rqt_graph rqt_graph
你将会看见类似于:
如果你把鼠标放置在 /turtle1/command_velocity,它将会高亮ROS节点(这儿的蓝色和绿色)和话题(这儿的红色)。正如你可见,turtlesim_node 和 the turtle_teleop_key节点正在/turtle1/command_velocity的话题上相互沟通。
2.2介绍rostopic
Rostopic工具允许你得到关于ROS**话题**的信息。
你可以使用help选项得到可用的rostopic的子命令
$ rostopic -h
rostopic bw display bandwidth used by topic
rostopic echo print messages to screen
rostopic hz display publishing rate of topic
rostopic list print information about active topics
rostopic pub publish data to topic
rostopic type print topic type
或者在rostopic打印出可能的子命令之后按tab键:
$ rostopic
bw echo find hz info list pub type
让我们使用这些子命令来检查turtlesim。
2.3使用rostopic echo
Rostopic echo展示一个话题中发布的数据。
用法:
rostopic echo [topic]
让我们看看turtle_teleop_key节点发布的命令速度数据。
对于ROS Hydro和之后的版本,这个数据在主题/turtle1/cmd_vel上发布。在一个新的终端中,运行:
$ rostopic echo /turtle1/cmd_vel
对于ROS Groovy和更早的版本,这个数据在/turtle1/command_velocity主题发布。在一个新的终端中,运行:
$ rostopic echo /turtle1/command_velocity
你可能不会看见任何发生,因为没有数据被发布在话题上。让我们通过按方向键让turtle_teleop_key发布数据。记住如果turtle没有移动那么你就要再次选择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。按下上左方的刷新键来展示新的节点。正如在这儿看见的红色rostopic echo,也是被turtle1/command_velocity主题订阅。
2.4使用rostopic list
Rostopic 列表 返回了现在发布和订阅的一系列话题。
让我们找出list的子命令需要的论据。在一个新终端中运行:
$ rostopic list -h
Usage: rostopic list [/topic]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
对于rostopic list使用冗长的选项:
$ rostopic list -v
这个展示了一个冗长的话题的列表,关于他们的发布于订阅,还有类型。
对于ROS Hydro和之后的类型,
Published topics:
* /turtle1/color_sensor [turtlesim/Color] 1 publisher
* /turtle1/cmd_vel [geometry_msgs/Twist] 1 publisher
* /rosout [rosgraph_msgs/Log] 2 publishers
* /rosout_agg [rosgraph_msgs/Log] 1 publisher
* /turtle1/pose [turtlesim/Pose] 1 publisherSubscribed topics:
* /turtle1/cmd_vel [geometry_msgs/Twist] 1 subscriber
* /rosout [rosgraph_msgs/Log] 1 subscriber
对于ROS Groovy和之前的版本,
Published topics:
* /turtle1/color_sensor [turtlesim/Color] 1 publisher
* /turtle1/command_velocity [turtlesim/Velocity] 1 publisher
* /rosout [roslib/Log] 2 publishers
* /rosout_agg [roslib/Log] 1 publisher
* /turtle1/pose [turtlesim/Pose] 1 publisherSubscribed topics:
* /turtle1/command_velocity [turtlesim/Velocity] 1 subscriber
* /rosout [roslib/Log] 1 subscriber
3.ROS 消息
话题上的交流通过节点之间发送ROS 消息。对于发布者(turtle_teleop_key)与订阅者(turtlesim_node)的交流,发布者和订阅者必须发送和接收相同类型的消息。这意味着一个主题类型是被在其上的消息类型定义的。发送给一个主题的消息的类型可以使用rostopic type决定。
3.1使用rostopic type
rostopic类型返回任何正在发布的消息类型
用法:
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 continued
既然我们已经知道了关于ROS**消息**,让我们用消息使用rostopic。
4.1使用rostopic pub
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.0和角速度1.8的移动。
这是一个相当复杂的实例,所以看每一个论据的细节。
对于ROS Hydro和之后的版本,
- 这个命令将会发布消息给一个给定的话题:
rostopic pub
这个选项(dash-one)造成了rostopic只会发布一个消息然后退出:
-1
这是发布向的话题的名称:
/turtle1/cmd_vel
这是发布给这个话题时使用的消息类型:
geometry_msgs/Twist
这个选项(双横线)告诉了选项解析器,下列参数中没有一个是选项。这在你的参数有一个引领-的时候是必须的,类似于负号。
–
- 正如之前记录的,一个 geometry_msgs/Twist msg有两个向量,每个都有三个浮点型元素:线速度与角速度。在这个例子中, ‘[2.0, 0.0, 0.0]’是线性值,数值是 x=2.0, y=0.0, and z=0.0,’[0.0, 0.0, 1.8]’ 是角速度值,数值是 x=0.0, y=0.0, and z=1.8。这些参数实际上是YAML语法的,在YAML command line documentation中有更多描述。
‘[2.0, 0.0, 0.0]’ ‘[0.0, 0.0, 1.8]’
对于ROS Groovy和之前的版本,
- 这个命令将会发布消息给一个给定的话题:
rostopic pub
这个选项(dash-one)造成了rostopic只会发布一个消息然后退出:
-1
这是发布向的话题的名称:
/turtle1/command_velocity
这是发布给这个话题时使用的消息类型:
turtlesim/Velocity
这个选项(双横线)告诉了选项解析器,下列参数中没有一个是选项。这在你的参数有一个引领-的时候是必须的,类似于负号。
–
- 正如之前记录的,一个 geometry_msgs/Twist msg有两个向量,每个都有三个浮点型元素:线速度与角速度。在这个例子中, ‘[2.0, 0.0, 0.0]’是线性值,数值是 x=2.0, y=0.0, and z=0.0,’[0.0, 0.0, 1.8]’ 是角速度值,数值是 x=0.0, y=0.0, and z=1.8。这些参数实际上是YAML语法的,在YAML command line documentation中有更多描述。
2.0 1.8
你可能会发现turtle停止了运动;这是因为turtle需要一个稳定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中发生了什么。按下上左方的刷新按钮。rostopic pub node(这儿的红色)正在与rostopic echo node(这儿的绿色)交流:
正如你可以看见turtle正在一个连续的圆中运动。在一个新的终端中,我们可以使用rostopic echo来看我们的turtlesim发布的数据:
rostopic echo /turtle1/pose
4.2使用rostopic hz
rostopic hz报告数据发布的速率。
用法:
rostopic hz [topic]
让我们看看turtlesim_node 发布/turtle1/pose有多快:
$ rostopic hz /turtle1/pose
你将会看见:
subscribed to [/turtle1/pose]
average rate: 59.354
min: 0.005s max: 0.027s std dev: 0.00284s window: 58
average rate: 59.459
min: 0.005s max: 0.027s std dev: 0.00271s window: 118
average rate: 59.539
min: 0.004s max: 0.030s std dev: 0.00339s window: 177
average rate: 59.492
min: 0.004s max: 0.030s std dev: 0.00380s window: 237
average rate: 59.463
min: 0.004s max: 0.030s std dev: 0.00380s window: 290
对于ROS Hydro和之后的版本,
$ rostopic type /turtle1/cmd_vel | rosmsg show
对于ROS Groovy 和之前的版本,
$ rostopic type /turtle1/command_velocity | rosmsg show
现在我们已经检查了使用rostopic 的话题,让我们使用另外一个工具来看看我们的turtlesim发布的数据:
5.使用rqt_plot
注意:如果你正在使用electric或之前的版本, rqt不可用. 使用rxplot代替。
rqt_plot展示了一个时间在主题上发布数据的滚动时间图。这儿我们使用rqt_plot来绘制被发布在/turtle1/pose话题上的数据。首先,在一个新的终端中输入以下内容来打开rqt_plot:
$ rosrun rqt_plot rqt_plot
在新的窗口应该弹出一个文字格在左上角落,给你增加任何话题到图中的能力。输入/turtle1/pose/x将会强调加号按键,之前是不能用的。按键然后对话题/turtle1/pose/y重复相同的过程。你将会看见turtle的x-y位置画在图中。
按减号按钮展示了一个菜单,让你可以在图中隐藏特定的主题。隐藏你刚刚添加的两个话题,添加/turtle1/pose/theta 将会展示下图所示的内容。
这个部分就这样,使用ctrl-c来终止rostopic终端,但是保持你的turtlesim运行。既然你理解了ROS topic是如何工作的,你可以看看如何 services and parameters work
6. 视频教程
下面的视频展示了一个小的教程,在ROS节点和ROS话题上使用turtlesim(完整的视频在my Udemy Course on ROS)