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

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

Comments

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
1

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 http://wiki.ros.org/Support#Do

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
2

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

ZainMehdi gravatar image

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

http://wiki.ros.org/rosbag/Code%20API...

edit flag offensive delete link more

Comments

2

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
2

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: http://wiki.ros.org/ROS/Tutorials/rea...

See the tutorial for full instructions.

Thanks to this answer for getting me started: https://answers.ros.org/question/2408....

See also: https://answers.ros.org/question/2408...

edit flag offensive delete link more

Comments

BTW, it is only a very few changes to get it to work with python3. Important to noetic users. I don't know how to contact you. I am at stephen@missouri.edu

Stephen Montgomery-Smith gravatar image Stephen Montgomery-Smith  ( 2020-12-07 18:39:56 -0500 )edit

@Stephen Montgomery-Smith, see my latest version of the code here: https://github.com/ElectricRCAircraft.... I upgraded it to Python3 a while back. Please ensure it works with Python3 now. I've also updated the ROS tutorial linked-to in my answer.

eRCaGuy gravatar image eRCaGuy  ( 2021-09-21 13:36:31 -0500 )edit
0

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 https://jmscslgroup.github.io/bagpy/

Following are example code-snippets:

import bagpy
from bagpy import bagreader

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

# get the list of topics
print(b.topic_table)

# 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
b.plot_vel(save_fig=True)

# 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)
    csvfiles.append(data)
edit flag offensive delete link more

Comments

1

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

@cybodroid@eRCaGuy I have also used the standard way to extract bag data:

rostopic echo -b bag_name.bag -p/topic_desired > name_of_topic.csv

B I used the option:

rosbag record -a

Thus I have recorded about 200 topics of bag data not just one. And it would be very exhaustive type the 1st command above one by one to convert all the 200 topic's data to csv. It there a command or script that I run only once and convert all my topics to different csvs? For example something like this:

rostopic echo -b bag_name.bag -p/ALLTOPICS desired > Separated_topics_filenames.csv

And have:

planning.csv odometry.csv pose.csv etc

I have tried this:

$ rostopic echo -b all_topics_bag.bag -p / current_pose > current_pose.csv

Ok It works and created the csv, then I tried this to get all data

$ rostopic echo -b all_topics_bag.bag -p / current_pose > current_pose ...
(more)
Vini71 gravatar image Vini71  ( 2021-09-22 14:25:23 -0500 )edit

Question Tools

2 followers

Stats

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

Seen: 23,111 times

Last updated: Oct 12 '20