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

Using python for automatically writing data in bag to csv file

asked 2011-04-12 03:32:59 -0500

Tinne De Laet gravatar image

updated 2014-01-28 17:09:31 -0500

ngrennan gravatar image

Hi all,

I want to write python code to automatically analyse a bag file and write the contents to a csv file (to allow for easy plotting afterwards). So far I can analyse the topics in the bag file, ... but I still looking for a tool that can automatically tell me how a message looks like (for instance PoseStamped consist of a header and a pose). I know that this information is in the message definition but I did not find an efficient way yet to get it into python. Of course I also had a look at rxbag, but I did not immediately found what I need. Can someone give me a hint?



edit retag flag offensive close merge delete

5 Answers

Sort by ยป oldest newest most voted

answered 2011-04-12 03:54:17 -0500

rostopic echo -p may do what you want with no code required.

edit flag offensive delete link more

answered 2011-04-12 04:23:47 -0500

You might find the info on the Interoperability wiki page helpful (even if not, please consider adding to it if you find something later on that isn't there). In particular it mentions the BagLoader class from starmac_tools, which was written mainly for the plotting use case you have in mind. Good Luck!

edit flag offensive delete link more

answered 2011-04-12 04:17:07 -0500

kwc gravatar image

You can introspect on a message using _slots and _slot_types. Here are their values for Header.

  __slots__ = ['seq','stamp','frame_id']
  _slot_types = ['uint32','time','string']

They have different underscores because __slots__ is a Python builtin and has special meaning.

from std_msgs.msg import Header
for name, type_ in zip(Header.__slots__, Header._slot_types):
     print name, type_


seq uint32
stamp time
frame_id string
edit flag offensive delete link more

answered 2012-06-15 07:07:44 -0500

punching gravatar image

updated 2015-03-09 20:38:03 -0500

tfoote gravatar image

I was trying to figure out the same thing. Last few commands on this page worked perfectly for converting .bag to .csv.

edit flag offensive delete link more


I've ticketed the broken link here: , as it looks fine in preview but is broken here.

Eric Perko gravatar image Eric Perko  ( 2012-06-15 12:48:43 -0500 )edit

answered 2012-06-17 02:07:06 -0500

Thomas gravatar image

updated 2012-06-17 02:09:09 -0500

A non generic approach is rosbag_analysis (see I just check for some attribute names and I "guess" the type from the attribute name for a very narrow set of message types.

This is bad, but it works... :)

edit: rostopic -p does not do what I want as it works on topics "live" so it may drop messages... rosbag_analysis processes bag files offline using the bag Python API.

edit flag offensive delete link more

Question Tools



Asked: 2011-04-12 03:32:59 -0500

Seen: 2,514 times

Last updated: Mar 09 '15