[Documentation] [TitleIndex] [WordIndex

Note: This tutorial assumes that you have completed the previous tutorials: Um pouco mais sobre 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.

Entendendo tópicos no ROS

Description: Este tutorial apresenta tópicos no ROS, e também como utilizar as ferramentas rostopic e rqt_plot.

Tutorial Level: BEGINNER

Next Tutorial: Entndendo serviços e parâmetros no ROS

Configuração

roscore

Vamos começar verificando se temos o roscore rodando, em um novo terminal:

$ roscore

Se você deixou o roscore rodando do tutorial anterior, você receberá a seguinte messagem de erro:

Tudo bem. Somente um roscore precisa estar rodando.

Turtlesim

Neste tutorial vamos utilizar também o turtlesim. Favor rodar em um novo terminal:

$ rosrun turtlesim turtlesim_node

Turtle teleoperado pelo teclado

Precisamos também obter alguma forma de movimentar o turtle. Favor rodar em um novo terminal:

$ rosrun turtlesim turtle_teleop_key

Agora você pode usar as teclas do teclado para movimentar o turtle. Caso não consiga movimentar o turtle selecione a janela do terminal em que você executou o commando turtle_telop_key e verifique se as teclas que usou foram gravadas.

Agora que você consegue movimentar o turtle, vamos entender o funcionamento do código.

ROS Tópicos

Os nós turtlesim_node e o turtle_teleop_key estão comunicando entre si através de ROS Tópico. turtle_teleop_key está publicando a tecla digitada no tópico, enquanto o turtlesim subescreve o mesmo tópico para receber a tecla digitada. Vamos utilizar rqt_graph que mostra os nós e tópicos que estão rodando.

Nota: Se você tiver utilizando electric ou anterior, rqt não está disponível. Use rxgraph.

Using rqt_graph

rqt_graph creates a dynamic graph of what's going on in the system. rqt_graph is part of the rqt package. Unless you already have it installed, run:

replacing <distro> with the name of your rosdistribution (fuerte, groovy, etc.)

In a new terminal:

$ rosrun rqt_graph rqt_graph

You will see something similar to:

rqt_graph_turtle_key.png

If you place your mouse over /turtle1/command_velocity it will highlight the ROS nodes (here blue and green) and topics (here red). As you can see, the turtlesim_node and the turtle_teleop_key nodes are communicating on the topic named /turtle1/command_velocity.

rqt_graph_turtle_key2.png

Introducing rostopic

The rostopic tool allows you to get information about ROS topics.

You can use the help option to get the available sub-commands for rostopic

$ rostopic -h

Let's use some of these topic sub-commands to examine turtlesim.

Using rostopic echo

rostopic echo shows the data published on a topic.

Usage:

rostopic echo [topic]

Let's look at the command velocity data published by the turtle_teleop_key node.

For ROS Hydro and later, this data is published on the /turtle1/cmd_vel topic. In a new terminal, run:

$ rostopic echo /turtle1/cmd_vel

For ROS Groovy and earlier, this data is published on the /turtle1/command_velocity topic. In a new terminal, run:

$ rostopic echo /turtle1/command_velocity

You probably won't see anything happen 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.

For ROS Hydro and later, you should now see the following when you press the up key:

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

For ROS Groovy and earlier, you should now see the following when you press the up 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 rqt_graph again. Press the refresh button in the upper-left to show the new node. As you can see rostopic echo, shown here in red, is now also subscribed to the turtle1/command_velocity topic.

rqt_graph_echo.png

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

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.

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]

For ROS Hydro and later,

For ROS Groovy and earlier,

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]

For ROS Hydro and later, example:

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

For ROS Groovy and earlier, 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.

For ROS Hydro and later,

For ROS Groovy and earlier,

You may have noticed that the turtle has 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:

For ROS Hydro and later,

For ROS Groovy and earlier,

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

We can also look at what is happening in rqt_graph, The rostopic pub node (here in red) is communicating with the rostopic echo node (here in green):

rqt_graph_pub.png

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:

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:

For ROS Hydro and later,

For ROS Groovy and earlier,

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

Using rqt_plot

Note: If you're using electric or earlier, rqt is not available. Use rxplot instead.

rqt_plot displays a scrolling time plot of the data published on topics. Here we'll use rqt_plot to plot the data being published on the /turtle1/pose topic. First, start rqt_plot by typing

$ rosrun rqt_plot rqt_plot

in a new terminal. In the new window that should pop up, a text box in the upper left corner gives you the ability to add any topic to the plot. Typing /turtle1/pose/x will highlight the plus button, previously disabled. Press it and repeat the same procedure with the topic /turtle1/pose/y. You will now see the turtle's x-y location plotted in the graph.

rqt_plot.png

Pressing the minus button shows a menu that allows you to hide the specified topic from the plot. Hiding both the topics you just added and adding /turtle1/pose/theta will result in the plot shown in the next figure.

rqt_plot2.png

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.


2019-10-19 13:03