Understanding ROS Topics

From Lofaro Lab Wiki
Jump to: navigation, search

Understanding ROS Topics

If continuing from earlier, please close down all currently running terminals.

1) In a terminal, run roscore:


2) In a new terminal (terminal 2) run the turtle simulation:

  rosrun turtlesim turtlesim_node

3) In a new terminal (terminal 3) run a controller to be able to control the turtle with keystrokes:

  rosrun turtlesim turtle_teleop_key

You should be able to move the turtle. If not, make sure you have this terminal 3 selected, then try pressing the arrow keys again. (If on a vm, there might be a slight lag to start)

The turtlesim_node and the turtle_teleop_key node are communicating with each other over a ROS Topic. turtle_teleop_key is publishing the key strokes on a topic, while turtlesim subscribes to the same topic to receive the key strokes.

4) Install rqt_graph:

  sudo apt-get install ros-hydro-rqt
  sudo apt-get install ros-hydro-rqt-common-plugins

5) In a new terminal (terminal 4) run the rqt_graph to display a graphical representation of the system:

  rosrun rqt_graph rqt_graph

The nodes are circles, the topics are lines between them.

6) In a new terminal (terminal 5) use rostopic echo [topic] to view what data is being published to a topic:

  rostopic echo /turtle1/cmd_vel

Likely nothing happened. To see some data, select terminal 3 (with the teleop_key controller) and press the arrow keys. Terminal 5 should now print some data.

7) Go back to the graph of the nodes and topics. Hit refresh at the top-left. A new node will appear inside the graph.

8) In a new terminal (terminal 6) view the verbose list of topics:

  rostopic list -v

9) In terminal 6, use rostopic type [topic] to return the message type of any topic being published:

  rostopic type /turtle1/cmd_vel

10) In terminal 6, use rosmsg show geometry_msgs/Twist to see details of the message:

  rosmsg show geometry_msgs/Twist

11) In terminal 6, use rostopic pub [topic] [msg_type] [args] to publish data to a topic:

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

The turtle sim should now show a turtle going around in an arc for a time.

Break down of previous command:

  • rostopic pub  : published the information
  • -1  : has the publisher send one command then exit
  • /turtle1/cmd_vel  : name of topic to publish to
  • geometry_msgs/Twist : message type to use
  • --  : lets parser know following informations are not an option (important for negative numbers)
  • '[x, y, z] (2x)  : YAML velocity vectores. For more on YAML syntax, please see this link.

12) Likely the turtle has stopped. The simulation needs constant commands to keep going.

In terminal 6, use rostopic pub with the -r argument to send continuous commands:

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

The turtle should now be driving around in a circle. You can look at teh rqt_graph (hit refresh) to see the current hierarchy.

13) In terminal 6, use rostopic hz [topic] to see how fast the data is published to the topic:

  rostopic hz /turtle1/pose

Intervals of time with some information should print to the terminal.

14) In terminal 6, view in depth information on the topic:

  rostopic type /turtle1/cmd_vel | rosmsg show

15) In terminal 6, view a graph of the data being sent on the topic:

  rosrun rqt_plot rqt_plot