ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

How to extract values from a ros bag that has an internal structure

asked 2022-10-30 16:44:44 -0600

clarknator gravatar image


I have a rosbag with several topics and need help with one of them. My OS is Ubuntu 18 and Melodic install for ROS-1. I need to parse the values of x, y, and z from one topic that apears to be an object when I read the topic messages.

Normally this is straight forward. Rephrasing my question is "How do I read messages from the topic such that I can parse the values from a structure inside it?

Why do I know it is a structure? I can pull the topic from the bag and look at the cvs with pandas.

I can successfully do this in Matlab using their ros toolbox. I have clients who don't want to install ros or buy a MAtlab license but have a need to see the data from the ros bag. They could run a python script. That's where I could use your help.

The topic giving me issues is 'global_path'. I chose to use bagpy although I think importing rosbag would work as well.

The basic steps I followed were:

  • read bag

    b = bagreader('/home/bags/flight_1_2022-08-22-13-09-30.bag')

  • check available topics


      Topics         Types                   Message Count  Frequency
  • 0 /global_path >> planner_map_interfaces/Plan >> 2 >>0.001536
  • 1 /particle_filters_visualizer >> visualization_msgs/MarkerArray >> 820 >>0.999696
  • 2 /planner/planning_request >> planner_map_interfaces/PlanningRequest >> 2 >> 0.001537
  • 3 /search_belief >> visualization_msgs/Marker >> 1399 >> 2.000637
  • 4 /target_projection_node/filtered_targets >> planner_map_interfaces/FilteredTargets >> 343 >>0.496940
  • 5 /target_propagation/particle_filters_belief >> target_propagation/ParticleFiltersBelief >> 820 >> 0.999689
  • pull messages from topic /global_path >>>data = b.message_by_topic('/global_path')
  • print("File saved: {}".format(data)) :: creates a csv of global_path topic
  • result: /home//bags/flight_1_2022-08-22-13-09-30/global_path.csv

  • import pandas as pd

    df_plan = pd.read_csv(data)

    df_plan :: shows the table with 2 rows and 6 columns.'plan' is column 6

    df_plan.shape :: shows (2,6) This is 2 rows and 6 columns

  • an example of showing the first column of the csv, 'Time'.

  • Note the type of the first is float64. So are columns 2 thru 5


0 1.661199e+09

1 1.661200e+09

Name: Time, dtype: float64

  • Next we see what type is the 6th column..


    0 [stamp: \n seq: 10\n stamp: \n secs: 0\n ...

    1 [stamp: \n seq: 8\n stamp: \n secs: 0\n ...

    Name: plan, dtype: object

  • The data type of column 6 is an object with label 'plan''.

  • the resultant cvs is a single column with many embedded fields

    • How can I format this 6th column? Is it when I first read the bag or when I extract the topic?

    • How do I read this column to extract the x, y, z positions inside the object?

    • in Matlab, I have a line to say the data type is a structure.
    • is there an equivalent using bagpy's bagreader or a method in rosbag?
  • Note the MAtlab use of 'readMessages' to specify a data format of 'struct'

bag = rosbag('/path/to ...

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-11-05 16:27:16 -0600

Mike Scheutzow gravatar image

You do not need to use structure - there is a much simpler way. The rosbag API returns one ros message at a time, already parsed, and you can read any field(s) that are present in the msg object:

import rosbag
bag = rosbag.Bag("myfile.bag")
for topic, msg, msg_t in bag.read_messages(topics=["/topic1"]):
    # this trivial example assumes we know type of msg object e.g. nav_msgs.msg.Path

edit flag offensive delete link more

Question Tools



Asked: 2022-10-30 16:44:44 -0600

Seen: 233 times

Last updated: Nov 05 '22