Learning how to interact with topics

To interact and get information about topics, we have the rostopic tool. This tool accepts the following parameters:

  • rostopic bw TOPIC: This displays the bandwidth used by topics
  • rostopic echo TOPIC: This prints messages to the screen
  • rostopic find TOPIC: This finds topics by their type
  • rostopic hz TOPIC: This displays the publishing rate of topics
  • rostopic info TOPIC: This prints information about active topics
  • rostopic list: This lists the active topics
  • rostopic pubs TOPIC: This publishes data to the topic
  • rostopic type TOPIC: This prints the topic type

If you want to see more information on these parameters, use -h, as follows:

    $ rostopic bw -h  

With the pub parameter, we can publish topics that can be subscribed to by any node. We only need to publish the topic with the correct name. We will do this test later; we are now going to use a node that will do this work for us:

    $ rosrun turtlesim turtle_teleop_key  

With this node, we can move the turtle using the arrow keys, as illustrated in the following screenshot:

Why does the turtle move when turtle_teleop_key is executed?

Checking the information provided by rosnode about the /teleop_turtle and /turtlesim nodes, we can see that there exists a topic called /turtle1/cmd_vel [geometry_msgs/Twist] in the Publications section of the node /teleop_turtle, and in the Subscriptions section of the /turtlesim node, there is /turtle1/cmd_vel [geometry_msgs/Twist]:

    $ rosnode info /teleop_turtle  

This means that the first node is publishing a topic that the second node can subscribe to. You can see the topic list using the following command line:

    $ rostopic list  

The output will be as follows:

    /rosout
      /rosout_agg
    /turtle1/colour_sensor
    /turtle1/cmd_vel
    /turtle1/pose  

With the echo parameter, you can see the information sent by the node. Run the following command line and use the arrow keys to see the data that is being sent:

    $ rostopic echo /turtle1/cmd_vel  

You will see something similar to the following output:

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

You can see the type of message sent by the topic using the following command line:

    $  rostopic type /turtle1/cmd_vel  

You will see something similar to the following output:

    Geometry_msgs/Twist  

If you want to see the message fields, you can do it with the following command:

    $ rosmsg show geometry_msgs/Twist  

You will see something similar to the following output:

    geometry_msgs/Vector3 linear
      float64 x
      float64 y
      float64 z
    geometry_msgs/Vector3 angular
      float64 x
      float64 y
      float64 z  

These tools are useful because, with this information, we can publish topics using the rostopic pub [topic] [msg_type] [args] command:

    $ rostopic pub /turtle1/cmd_vel  geometry_msgs/Twist -r 1 -- "linear:
    x: 1.0
    y: 0.0
    z: 0.0
    angular:
    x: 0.0
    y: 0.0
    z: 1.0"  

You will see the turtle doing a curve, as shown in the following screenshot:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.201.217