4、理解ros话题

来源:互联网 发布:深入浅出javascript 编辑:程序博客网 时间:2024/05/03 09:56

一、准备工作

1.roscore

首先启动ros:

roscore

已经启动过则无需此步。

2.turtlesim

启动一个小海龟

rosrun turtlesim turtlesim_node

3.远程操控小海龟

再一个新终端里运行:

rosrun turtlesim turtle_teleop_key

得到:

Reading from keyboard---------------------------Use arrow keys to move the turtle.

此时,通过方向键即可控制小海龟移动。

接下来让我们深入理解这个例子的原理。

二、ROS topics

此例中,turtlesim_node和turtle_teleop_key两个节点之间是通过一个ROS 话题(topic)互相沟通的。turtle_teleop_key捕捉键盘操作,发布击键信息到一个话题上,而turtlesim_node订阅了这个话题,从这个话题接收击键信息,进而由接收到的信息,对小海龟进行操作。

我们可以通过rqt_graph命令查看节点和话题的工作状况。

1.rqt_graph

rqt_graph命令创建一个动态图描述系统节点的关系,它是rqt包中的一个命令,未安装过rqt包的话需要使用以下命令安装:

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

< distro>是你的ros的版本名称,比如jade,indigo...

安装好之后,在终端内运行:

rosrun rqt_graph rqt_graph

你将看到:

rqt_graph GUI

移动你的鼠标,发现节点会被高亮。

2.rostopic命令

可以使用rostopic命令查看系统内的话题信息。

使用-h选项查看帮助:

rostopic -h
rostopic is a command-line tool for printing information about ROS Topics.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 typeType rostopic <command> -h for more detailed usage, e.g. 'rostopic echo -h'

3.使用rostopic echo

用法:

rostopic echo [topic]

让我们查看以下turtle_teleop_key发布击键信息的话题:

rostopic echo /turtle1/cmd_vel

在移动小海龟之前,不会看到任何输出。

现在切换到turtle_teleop_key所在的终端,随便敲击几下方向键,然后返回到本终端,可以看到此话题内发布遵循一定格式的信息:

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

4.使用rostopic 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

使用详细输出选项:

rostopic list -v

得到:

Published topics: * /turtle1/color_sensor [turtlesim/Color] 2 publishers * /turtle1/cmd_vel [geometry_msgs/Twist] 1 publisher * /rosout [rosgraph_msgs/Log] 5 publishers * /rosout_agg [rosgraph_msgs/Log] 1 publisher * /turtle1/pose [turtlesim/Pose] 2 publishersSubscribed topics: * /turtle1/cmd_vel [geometry_msgs/Twist] 3 subscribers * /rosout [rosgraph_msgs/Log] 1 subscriber * /statistics [rosgraph_msgs/TopicStatistics] 1 subscriber

三、ROS信息

ROS节点之间的交流,必须使用相同类型的信息,topic的类型由发布于topic之上的信息的类型定义。可以使用rostopic命令设定topic的信息类型。

1、使用rostopic type命令

用法:

rostopic type [topic]

终端下执行:

rostopic type /turtle1/cmd_vel

将得到:

geometry_msgs/Twist

我们可以进一步查看此类型的详细情况:

rosmsg show geometry_msgs/Twist

得到:

geometry_msgs/Vector3 linear  float64 x  float64 y  float64 zgeometry_msgs/Vector3 angular  float64 x  float64 y  float64 z

四、继续深入rostopic命令

现在我们已经理解了ROS的信息流,接下来我们使用rostopic对信息流进行进一步的操作

1.使用rostopic pub命令

rostopic pub可以向当前已有的话题上发布信息,用法:

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

现在我们逐个解释此命令用到的各参数:

  1. -1 表示发布一条信息然后推出
  2. /turtle1/cmd_vel 表示要发布到的话题阿
  3. geometry_msgs/Twist 表示发布的信息的类型
  4. -- 用来说明其后跟的字符串不表示命令参数(可以避免负数被解释成参数的情况)
  5. '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]' 表示具体的数据

执行此命令后我们可以看到小海龟移动一下然后停止,我们还可以发布持续的信息流使小海龟不停运动:

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

这条命令将会以1hz的频率发布信息,可以看到小海龟已经在不停地转圈了。

2.使用rostopic hz命令

rostopic hz命令可以查看信息发布的频率,用法:

rostopic hz [topic]

让我们看一下turtlesim_node在 /turtle1/pose话题上发布信息的频率:

rostopic hz /turtle1/pose

得到:

average rate: 62.508    min: 0.016s max: 0.016s std dev: 0.00011s window: 59average rate: 62.490    min: 0.016s max: 0.016s std dev: 0.00011s window: 121

可以看到频率为60Hz左右。

我们还可以组合两个命令查看详细信息:

rostopic type /turtle1/cmd_vel | rosmsg show

五、使用rqt_plot命令
rqt_plot命令可以将某话题下的数据按时间绘制出来。
首先需要启动rqt_plot:

rosrun rqt_plot rqt_plot

然后在输入框里输入任意话题查看其数据,比如/turtle1/pose

0 0
原创粉丝点击