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

Is there a way to save all rosbag data into a .csv or text file ?

asked 2013-02-12 05:18:37 -0500

Hi all!

I would like to extract the data from the rosbag and convert it into text or .csv file to use the data into Matlab and Excel. Is this possible ?

edit retag flag offensive close merge delete

Comments

good question

mannish123123 gravatar image mannish123123  ( 2022-01-23 21:51:44 -0500 )edit

5 Answers

Sort by ยป oldest newest most voted
20

answered 2013-02-12 07:22:23 -0500

Mani gravatar image

This is taken from this answer for a similar question:

For simple message formats, you can convert directly to CSV from the command line:

rostopic echo -b file.bag -p /topic

This won't work well if your bag file contains arrays/images/pointclouds/etc... as it renders the entire message to text.

edit flag offensive delete link more

Comments

This is so awsome

zkytony gravatar image zkytony  ( 2017-02-10 16:50:44 -0500 )edit

What are the meaning of the -b and -p flags ?

tonyParker gravatar image tonyParker  ( 2018-06-04 05:49:13 -0500 )edit

-b Display messages in a bag file.

-p Display messages in a matlab/octave-friendly plotting format.

From here.

IanCol gravatar image IanCol  ( 2019-07-23 12:35:02 -0500 )edit
6

answered 2013-02-12 05:42:11 -0500

Here's a quick way:

rosbag play mybag.bag
rostopic echo /foo > output.txt

This will output all message in the topic /foo to a yaml-formatted text file. You can then parse the file. It might be useful to output to multiple files for different fields. Let's say the message type on /foo is Pose:

rosbag play mybag.bag
rostopic echo /foo/position/x > output_x.txt
rostopic echo /foo/position/y > output_y.txt
rostopic echo /foo/position/z > output_z.txt

You can also look into bagys, which are an extension of the concept above.

edit flag offensive delete link more

Comments

Hi there! Is the other way around possible. i.e., convert from a .txt to a .bag? Thanks in advance!

TSC gravatar image TSC  ( 2014-08-26 13:45:29 -0500 )edit
2

answered 2013-05-29 11:35:26 -0500

NickSpeal gravatar image

updated 2014-04-29 01:34:25 -0500

fivef gravatar image

Please see this answer for a simple script to save all topics in the bag file as a seperate csv.

Please let me know if you found this useful or if you have any suggestions for how to improve it.

edit flag offensive delete link more
2

answered 2020-07-21 17:50:58 -0500

cwillia109 gravatar image

updated 2020-07-21 18:15:06 -0500

A more flexible way of doing this could be with Pandas. After you record your data, you can sift through it to gather the information you need, add it to a dataframe, perform any operations on it, and save it to a .csv. For example, lets consider some points.

import rosbag
from geometry_msgs.msg import Point
import pandas as pd

# The bag file should be in the same directory as your terminal
bag = rosbag.Bag('./recorded-data.bag')
topic = '/your_topic'
column_names = ['x', 'y']
df = pd.DataFrame(columns=column_names)

for topic, msg, t in bag.read_messages(topics=topic):
    x = msg.x
    y = msg.y

    df = df.append(
        {'x': x,
         'y': y},
        ignore_index=True
    )

df.to_csv('out.csv')
edit flag offensive delete link more

Comments

This is a fantastic answer :). I think being able to convert rosbag into csv should not depend on rosbag play like the other answer. One thing though: I found that import rosbag works inside your ros environment (there may be a specific py package outside ros, unsure) but geometry_msgs.msg should come with ros The last thing you may need is 'pip install pycryptodomex' and 'pip install gnupg' if you get issues with these libraries when trying to 'import rosbag'

redsapph gravatar image redsapph  ( 2020-07-23 13:15:08 -0500 )edit
0

answered 2022-01-23 07:23:47 -0500

Asan A. gravatar image

Take a look for this package which provide a GUI interface to convert topics into csv -> rosbag_to_csv

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2013-02-12 05:18:37 -0500

Seen: 67,256 times

Last updated: Jan 23 '22