[ROS Q&A] 188 – How do you extract multiple variables from a ROS message

[ROS Q&A] 188 - How do you extract multiple variables from a ROS message

Written by Alberto Ezquerro

17/05/2019

Learn how to extract multiple variables from a ROS message, through a very simple example. This video answers the following question found on the ROS Answers forum: https://answers.ros.org/question/322896/how-do-you-extract-multiple-variables-from-a-message/. Let’s go!


Related Resources


Step 1: Get your development environment ready

Either of the following will do:

  1. Use the ROS Development Studio (ROSDS), an online platform for developing for ROS within a PC browser. Easy-peasy. I’m using this option for this post
    1. Once you log in, click on the New ROSject button to create a project that will host your code. Then Click on Open ROSject to launch the development/simulation environment.
    2. To open a “terminal” on ROSDS, pick the Shell app from the Tools menu.
    3. You can find the IDE app on the Tools menu.
  2. You have ROS installed on a local PC. Okay, skip to Step 2.

Next step!

Step 2: Launch a simulation

We are going to use a topic from a simulation of a robot. You may use any robot that has Odometry (/odom topic). For this post, we are using Turtlebot 2.

On ROSDS, start a Turtlebot 2 simulation from the simulations menu:

  • Simulations -> Use a provided simulation -> (Empty world) + Select a robot -> Turtlebot 2 > Start simulation.

Step 3: Find out the kind and structure of the message the Odometry topic uses

Open a terminal and run the following:

user:~$ rostopic info /odom
Type: nav_msgs/Odometry

Publishers:
 * /gazebo (http://rosdscomputer:37377/)

Subscribers: None


user:~$ rosmsg show nav_msgs/Odometry
std_msgs/Header header
  uint32 seq
  time stamp
  string frame_id
string child_frame_id
geometry_msgs/PoseWithCovariance pose
  geometry_msgs/Pose pose
    geometry_msgs/Point position
      float64 x
      float64 y
      float64 z
    geometry_msgs/Quaternion orientation
      float64 x
      float64 y
      float64 z
      float64 w
  float64[36] covariance
geometry_msgs/TwistWithCovariance twist
  geometry_msgs/Twist twist
    geometry_msgs/Vector3 linear
      float64 x
      float64 y
      float64 z
    geometry_msgs/Vector3 angular
      float64 x
      float64 y
      float64 z
  float64[36] covariance
  • First command (rostopic info): we found out the kind of message  – nav_msgs/Odometry.
  • Second command (rosmsg show): we found out the structure of the message.

Next, let’s examine the structure of the ROS message nav_msgs/Odometry:

  • It’s a fairly complex message as it contains a lot of nested variables.
  • The variables are arranged in hierarchical or tree form, with parent variable appearing without indentation. “Parent variables” here include
    • header
    • child_frame_id
    • pose
    • twist.
  • Some of the parent variables have children, grandchildren, and so on, which are also variables. These are indicated by the visual indents. For example,
    • header has three children.
    • pose has two children.
      • pose, which also has children position and orientation, which both have children too.
      • covariance, which has no children.
  • Every variable has a type, indicated just before the variable name. Type for …
    • header is std_msgs/Header.
    • pose -> pose -> position -> x is float64.
    • child_frame_id is string.

Next, let create a Python package and see how to access these variables in code.

Step 4: Create a package and extract these multiple variables from the message std_msgs/Odometry

Create a package and create a Python file. Please note that we are making the Python file executable with chmod +x, otherwise it won’t run!

user:~$ cd catkin_ws/src/
user:~/catkin_ws/src$ catkin_create_pkg extract_msg rospy
Created file extract_msg/CMakeLists.txt
Created file extract_msg/package.xml
Created folder extract_msg/src
Successfully created files in /home/user/catkin_ws/src/extract_msg. Please adjust the values in package.xml.
user:~/catkin_ws/src$ cd extract_msg/src/
user:~/catkin_ws/src/extract_msg/src$ touch extract_msg_node.py
user:~/catkin_ws/src/extract_msg/src$ chmod +x extract_msg_node.py
user:~/catkin_ws/src/extract_msg/src$

Open the extract_msg_node.py using the IDE and paste in the following code that extracts some of the multiple variables from the ROS message.

#! /usr/bin/env python

import rospy # the ROS api for python. We need it to create a node, 
             # a subscriber and access other ROS-specific program control
from nav_msgs.msg import Odometry # Python message class for Odometry


rospy.init_node("extra_message_node")
msg = rospy.wait_for_message("/odom", Odometry)
print "Full message: \n"
print msg # that's the whole Odometry message. It should be something like
            # what was printed out by `rosmsg show nav_msgs/Odometry`

# print out each of the parent variables
print "\n Parent variables: \n"
print msg.header
print msg.child_frame_id
print msg.pose
print msg.twist

# print some children
print "\nSome children: \n"
print msg.header.frame_id
print msg.pose.pose
print msg.twist.twist

# print out some grandchildren
print "\nSome grandchildren: \n"
print msg.pose.pose.position
print msg.twist.twist.linear

# print out some great grandchildren :)
print "\nSome great grandchildren: \n"
print msg.pose.pose.orientation.w
print msg.twist.twist.angular.z

# print other (great grand) children below this line
print "\nOther ones you have specified: \n"

Run the program:

user:~$ rosrun extract_msg extract_msg_node.py

The program should run once and print out the extracted variables. Try adding your own variables.

And so that was it! I hope you were able to learn a thing or two from this post.

Extra: Video of the post

Here below you have a “sights and sounds” version of this post, just in case you prefer it that way. Enjoy!

Feedback

Did you like this post? Do you have any questions about the explanations? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS or ROS2 topics, please let us know in the comments area and we will do a video or post about it.


Edited by Bayode Aderinola

Topics: ROS Q&A
Masterclass 2023 batch2 blog banner

Check Out These Related Posts

129. ros2ai

129. ros2ai

I would like to dedicate this episode to all the ROS Developers who believe that ChatGPT or...

read more

0 Comments

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This