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)
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 withrostopic echo /pnc/relative_carstatus
?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.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.
@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
@kharkad how do you start all the nodes manually when you receive a bag file from someone else ?
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 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 ?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.