Describe roschina/教程/UnderstandingTopics here.

Note: This tutorial assumes that you have completed the previous tutorials: understanding ROS nodes.
(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Understanding ROS Topics

Description: This tutorial introduces ROS topics as well as using the rostopic and rxplot commandline tools.

Tutorial Level: BEGINNER

Next Tutorial: Understanding ROS services and parameters

准备

roscore

首先输入并确认 roscore 已经运行, 打开一个新的终端窗口:

$ roscore

如果roscore已经运行,会得到如下错误信息:

  • roscore cannot run as another roscore/master is already running. 
    Please kill other roscore/zenmaster processes before relaunching

turtlesim

本章我们仍使用 turtlesim 作为例子. 打开一个新的终端窗口运行如下命令:

$ rosrun turtlesim turtlesim_node

turtle keyboard teleoperation

我们试着让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动起来。如果终端窗口打开的不止一个,要保证当前激活的窗口是接收键盘命令的那个。

我们来看下这个过程怎么实现的.

ROS主题(Topics)

节点程序turtlesim_nodeturtle_teleop_key 之间通过一个ROS Topic来完成通信。 节点程序turtle_teleop_key 发布 publishing 按下的键盘命令数值到该主题上. 而同时节点程序 turtlesim 订阅subscribes 同一个topic来接收这些键盘命令数值. 我们使用命令工具 rxgraph 可以图形化显示出当前运行的节点程序间topic的通信过程。

关于 rxgraph

rxgraph 能对当前运行的系统中状态的图形化显示工具. rxgraph 存在于rxtools package中. 打开一个新的终端输入下面命令:

$ rxgraph

返回结果如下:

我们突出显示了ROS nodes 和 topics. 我们可以看到节点 turtlesim_nodeturtle_teleop_key 通过 topic /turtle1/command_velocity来通信.

关于命令 rostopic

命令工具 rostopic 用来查询ROS topics的相关信息.

通过-h参数可以获得该命令工具的帮助信息 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一些子命令的使用,以turtlesim为例.

关于 rostopic echo

rostopic echo 显示该主题发布的数据数值.

用法:

rostopic echo [topic]

要查看节点turtle_teleop_key中的topic(主题)/turtle1/command_velocity 发布的数据,打开一个终端窗口输入下面命令:

$ rostopic echo /turtle1/command_velocity

You probably won't see anything happen, this is because no data is being published on the topic. Let's make turtle_teleop_key publish data by pressing the arrow keys. Remember if the turtle isn't moving you need to select the turtle_teleop_key terminal again.

Now you should see the following when you press the up arrow key:

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

Now let's look at rxgraph again, as you can see rostopic echo is now also subscribed to the turtle1/command_velocity topic.

Using rostopic list

rostopic list returns a list of all topics currently subscribed to and published.

Lets figure out what argument the list sub-command needs. In a new terminal run:

$ 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

For rostopic list use the verbose option:

$ rostopic list -v

This displays a verbose list of topics to publish to and subscribe to and their type.

  • 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 publisher
    
    Subscribed topics:
     * /turtle1/command_velocity [turtlesim/Velocity] 1 subscriber
     * /rosout [roslib/Log] 1 subscriber

ROS Messages

Communication on topics happens by sending ROS messages between nodes. For the publisher (turtle_teleop_key) and subscriber (turtlesim_node) to communicate, the publisher and subscriber must send and receive the same type of message. This means that a topic type is defined by the message type published on it. The type of the message sent on a topic can be determined using rostopic type.

Using rostopic type

rostopic type returns the message type of any topic being published.

Usage:

rostopic type [topic]

Try:

$ rostopic type /turtle1/command_velocity
  • You should get:
    turtlesim/Velocity

We can look at the details of the message using rosmsg:

$ rosmsg show turtlesim/Velocity
  • float32 linear
    float32 angular

Now that we know what type of message turtlesim expects, we can publish commands to our turtle:

rostopic continued

Now that we have learned about ROS messages let's use rostopic with messages.

Using rostopic pub

rostopic pub publishes data on to a topic currently advertised.

Usage:

rostopic pub [topic] [msg_type] [args]

Example:

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

The previous command will send a single message to turtlesim telling it to move with an linear velocity of 2.0, and an angular velocity of 1.8 .

This is a pretty complicated example, so lets look at each argument in detail.

  • rostopic pub
    This command will publish messages to a given topic.
  •  -1 
    (dash-one) This option causes rostopic to only publish one message then exit.
  • /turtle1/command_velocity
    This is the name of the topic to publish to.
  • turtlesim/Velocity
    This is the message type to use when publishing the topic.
  • --

    (double-dash) This tells the option parser that none of the following arguments is an option. This is required in cases where your arguments have a leading dash - (such as with negative numbers).

  • 2.0 1.8 

    As noted before, a turtlesim/Velocity msg has two floating point elements : linear and angular. In this case, 2.0 becomes the linear value, and 1.8 is the angular value. These arguments are actually in YAML syntax, which is described more in the YAML command line documentation.

As you can see the turtle stopped moving, this is because the turtle requires a steady stream of commands at 1 Hz to keep moving. We can publish a steady stream of commands using rostopic pub -r command:

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

This publishes the velocity commands at a rate of 1 Hz on the velocity topic.

We can also look at what is happening in rxgraph:

As you can see the turtle is running in a continuous circle. In a new terminal, we can use rostopic echo to see the data published by our turtlesim:

Using rostopic hz

rostopic hz reports the rate at which data is published.

Usage:

rostopic hz [topic]

Let's see how fast the turtlesim_node is publishing /turtle1/pose:

$ rostopic hz /turtle1/pose

You will see:

  • 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

Now we can tell that the turtlesim is publishing data about our turtle at the rate of 60 Hz. We can also use rostopic type in conjunction with rosmsg show to get in depth information about a topic:

Now that we've examined the topics using rostopic let's use another tool to look at the data published by our turtlesim:

Using rxplot

rxplot displays a scrolling time plot of the data published on topics. Here we'll use rxplot to plot the data being published on the /turtle1/pose topic:

$ rxplot /turtle1/pose/x,/turtle1/pose/y /turtle1/pose/theta

You will see the turtle's x-y location plotted in the top graph while the turtles theta is displayed on the lower graph:

That's it for this section, use Ctrl-C to kill the rostopic terminals but keep your turtlesim running.

Now that you understand how ROS topics work, let's look at how services and parameters work.

Wiki: roschina/教程/UnderstandingTopics (last edited 2012-03-05 00:48:05 by LuZhiShen)