Ask Your Question

How can i read messages from a ROSbag file ?

asked 2018-04-11 10:20:22 -0500

aks gravatar image

I have a bag file in which some are custom messages whereas others are string messages. How can i display the contents of the topics which have message of type strings (without converting it to a .csv file) ?image description

for example, From the attached image, how can i read the messages of the topic /pnc/relative_carstatus

edit retag flag offensive close merge delete


I do have a question for you, how do you convert a rosbag file into a csv file? Anyway, why don't you use the rosbag play to play your bag file, and then in another terminal you just see the messages passing into the topic with rostopic echo /pnc/relative_carstatus?

kharkad gravatar image kharkad  ( 2018-04-11 10:31:51 -0500 )edit

Hi to convert a bag file to .csv format, use rostopic echo /topicname -b bagFileName.bag -p > file.csv And to the second part, I only have a bag file and when i play it, unfortunately no new nodes are created and no topics as well. That is why i cannot echo any topic.

aks gravatar image aks  ( 2018-04-11 10:39:00 -0500 )edit

Personally, whenever I use rosbag I start all nodes manually, and then play the bad file to publish on the wanted topics. I don't think rosbag starts nodes.

kharkad gravatar image kharkad  ( 2018-04-11 14:48:45 -0500 )edit

@aakash_sehgal please don't use an image to display text. Images are not searchable and people cannot copy and paste the text from the image. See

jayess gravatar image jayess  ( 2018-04-12 01:10:42 -0500 )edit

@kharkad how do you start all the nodes manually when you receive a bag file from someone else ?

aks gravatar image aks  ( 2018-04-12 06:16:41 -0500 )edit

I am confused: @aakash_sehgal: which nodes are you talking about? rosbag publishes msgs as if the original nodes were running. rosbag does not need the original nodes. If you're referring to "the rest" of the application (ie: consumers of msgs), that would be something else.

gvdhoorn gravatar image gvdhoorn  ( 2018-04-12 06:20:17 -0500 )edit

@gvdhoorn yes, that is correct. I wanted to reply to @kharkad 's previous comment and then realised that rosbag do not requires original nodes. Question : that means just by playing a bagfile i cannot access the messages. am i right ?

aks gravatar image aks  ( 2018-04-12 06:25:26 -0500 )edit

You can, as long as the system on which you are playing the bag has the necessary msgs. rostopic echo should be able to show msg content though, just as it would with 'normal' publications.

gvdhoorn gravatar image gvdhoorn  ( 2018-04-12 06:26:48 -0500 )edit

3 Answers

Sort by ยป oldest newest most voted

answered 2018-04-11 22:47:00 -0500

ZainMehdi gravatar image

You can use Rosbag API to access messages using a simple script

edit flag offensive delete link more



Can you please update your question with a minimum working example?

jayess gravatar image jayess  ( 2018-04-12 01:11:17 -0500 )edit

@ZainMehdi is it possible that i can execute this script in the terminal or do i need to specifically put this in the bagfiles directory and then execute it ?

aks gravatar image aks  ( 2018-04-12 06:19:27 -0500 )edit

@jayess ill try and do that and next time will take care about the image with text.

aks gravatar image aks  ( 2018-04-12 06:19:49 -0500 )edit

Sorry for the late response. I hope your problem has been resolved. and to answer your question yes you can execute it like a normal script

ZainMehdi gravatar image ZainMehdi  ( 2018-04-29 19:59:51 -0500 )edit

answered 2020-10-11 21:30:45 -0500

cybodroid gravatar image

updated 2020-10-12 15:40:32 -0500

Use the bagpy package to read the .bag file in Python. It can be installed using pip

pip install bagpy

It is a specialized package meant to extract data in a certain way only. Further, only a few message types are supported. However, the author of the package is very responsive, and you can request new message types to be included. Provide a test bag file for testing your request.

Brief documentation is at

Following are example code-snippets:

import bagpy
from bagpy import bagreader

b = bagreader('09-23-59.bag')

# get the list of topics

# get all the messages of type velocity
velmsgs   = b.vel_data()
veldf = pd.read_csv(velmsgs[0])
plt.plot(veldf['Time'], veldf['linear.x'])

# quickly plot velocities

# you can animate a timeseries data
bagpy.animate_timeseries(veldf['Time'], veldf['linear.x'], title='Velocity Timeseries Plot')

The package is still under development, suggestions are welcome.

Edit: A new function has been added to the package that can decode any ROS message. See below for the code-snippet:

b = bagreader('09-23-59.bag')
csvfiles = []
for t in b.topics:
    data = b.message_by_topic(t)
edit flag offensive delete link more



However, the author of the package is very responsive

are you describing yourself here?

However, it looks like the package is still under development.

If you are the author, please be less vague. You should know exactly what the state of the package is.

And as a general, high-level comment: I'm not sure I understand how maintainable this is. If I understand correctly, you'd have to add wrappers/getters for each and every message type you'd want to get out of the .bag?

gvdhoorn gravatar image gvdhoorn  ( 2020-10-12 02:55:29 -0500 )edit

I improved my language. The author is a colleague. This package has limited use, but it is helpful when you want to visualize a few most common messages in a few lines of code.

cybodroid gravatar image cybodroid  ( 2020-10-12 04:17:07 -0500 )edit

answered 2020-08-25 16:12:34 -0500

eRCaGuy gravatar image

updated 2020-08-25 16:14:11 -0500

I recently wrote ros_readbagfile. You can use it like this:

ros_readbagfile <mybagfile.bag> [topic1] [topic2] [topic3] [...] [topic1000]

I wrote a tutorial for it too, here:

See the tutorial for full instructions.

Thanks to this answer for getting me started:

See also:

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2018-04-11 10:20:22 -0500

Seen: 12,415 times

Last updated: Oct 12