# 6.4 继续学习 rostopic

现在我们已经了解了什么是ROS的消息，接下来我们开始结合消息来使用rostopic。

## 6.4.1 使用 rostopic pub

rostopicpub可以把数据发布到当前某个正在广播的话题上。 用法：

```
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大小的角速度开始移动。![image](http://wiki.ros.org/ROS/Tutorials/UnderstandingTopics?action=AttachFile\&do=get\&target=turtle\(rostopicpub\).png)

这是一个非常复杂的例子，因此让我们来详细分析一下其中的每一个参数。

* 这条命令将会发布消息到某个给定的话题。

  ```
  rostopic pub
  ```
* （单个破折号）这个参数选项使rostopic发布一条消息后马上退出。

```
-1
```

* 这是消息所发布到的话题名称。

```
/turtle1/cmd_vel
```

* 这是所发布消息的类型。

```
geometry_msgs/Twist
```

* （双破折号）这会告诉命令选项解析器接下来的参数部分都不是命令选项。

  这在参数里面包含有破折号-（比如负号）时是必须要添加的。

```
--
```

* 正如之前提到的，在一个turtlesim/Velocity消息里面包含有两个浮点型元素：linear和angular。

  在本例中2.0是linear的值，1.8是angula的值。

  这些参数其实是按照[YAML语法格式](http://wiki.ros.org/ROS/YAMLCommandLine)编写的，这在YAML文档中有更多的描述。

```
'[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'
```

你可能已经注意到turtle已经停止移动了。 这是因为turtle需要一个稳定的频率为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]'
```

这条命令以1Hz的频率发布速度命令到速度话题上。 ![image](http://wiki.ros.org/ROS/Tutorials/UnderstandingTopics?action=AttachFile\&do=get\&target=turtle\(rostopicpub\)2.png)

我们也可以看一下rqt\_graph中的情形 可以看到rostopic发布器节点（红色）正在与rostopic echo节点（绿色）进行通信：

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

正如你所看到的，turtle正沿着一个圆形轨迹连续运动。 我们可以在一个新终端中通过rostopic echo命令来查看turtlesim所发布的数据。

```
$ rostopic echo /turtle1/pose
```

## 6.4.2 使用 rostopic hz

rostopic hz命令可以用来查看数据发布的频率。 用法：

```
rostopic hz [topic]
```

我们看一下turtlesim\_node发布/turtle/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
```

现在我们可以知道了turtlesim正以大约60Hz的频率发布数据给turtle。 我们也可以结合rostopic type和rosmsg show命令来获取关于某个话题的更深层次的信息：

```
$ rostopic type /turtle1/cmd_vel | rosmsg show
```

到此我们已经完成了通过rostopic来查看话题相关情况的过程，接下来我将使用另一个工具来查看turtlesim发布的数据。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://songapore.gitbook.io/ros-tutorials/6-li-jie-ros-hua-ti-topics/6.4-ji-xu-xue-xi-rostopic.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
