# 6.2 ros topics

turtlesim\_node节点和turtle\_teleop\_key节点之间是通过一个**ROS话题**来互相通信的。

turtle\_teleop\_key在一个话题上发布按键输入消息，而turtlesim则订阅该话题以接收该消息。 下面让我们使用rqt\_graph来显示当前运行的节点和话题。

## 6.2.1 使用 rqt\_graph

rqt\_graph能够创建一个显示当前系统运行情况的动态图形。 rqt\_graph是rqt功能包中的一部分。 如果你没有安装，请通过以下命令来安装：

```
$ sudo apt-get install ros-indigo-rqt
$ sudo apt-get install ros-indigo-rqt-common-plugins
```

在一个新终端中运行:

```
$ rosrun rqt_graph rqt_graph
```

你会看到类似下图所示的图形：

![image](http://wiki.ros.org/ROS/Tutorials/UnderstandingTopics?action=AttachFile\&do=get\&target=rqt_graph_turtle_key.png)

如果你将鼠标放在/turtle1/command\_velocity上方，相应的ROS节点（蓝色和绿色）和话题（红色）就会高亮显示。

正如你所看到的turtlesim\_node和turtle\_teleop\_key节点正通过一个名为/turtle1/command\_velocity的话题来互相通信。

![image](http://wiki.ros.org/ROS/Tutorials/UnderstandingTopics?action=AttachFile\&do=get\&target=rqt_graph_turtle_key2.png)

## 6.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。

## 6.2.3 使用 rostopic echo

rostopicecho可以显示在某个话题上发布的数据。 用法：

```
rostopic echo [topic]
```

让我们在一个新终端中看一下turtle\_teleop\_key节点在/turtle1/command\_velocit话题上发布的数据。

```
$ rostopic echo /turtle1/cmd_vel
```

接下来我们通过按下方向键使turtle\_teleop\_key节点发布数据。

记住如果turtle没有动起来的话就需要你重新选中turtle\_teleop\_key节点运行时所在的终端窗口。

现在当你按下向上方向键时应该会看到下面的信息：

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

现在让我们再看一下rqt\_graph（你可能需要刷新一下ROS graph）。

正如你所看到的rostopic echo(红色显示部分）现在也订阅了turtle1/command\_velocity话题。 ![image](http://wiki.ros.org/ROS/Tutorials/UnderstandingTopics?action=AttachFile\&do=get\&target=rqt_graph_echo.png)

## 6.2.4 使用 rostopic list

rostopic list能够列出所有当前订阅和发布的话题。 让我们查看一下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中使用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] 2 publishers
 * /rosout_agg [rosgraph_msgs/Log] 1 publisher
 * /turtle1/pose [turtlesim/Pose] 1 publisher

Subscribed topics:
 * /turtle1/cmd_vel [geometry_msgs/Twist] 1 subscriber
 * /rosout [rosgraph_msgs/Log] 1 subscriber
```
