Skip to content Skip to sidebar Skip to footer

How To Write A Python Code To Read Data From ".bag" File And Write Data In Bag File Runtime

I have done following code to read data from .bag file import os f = open('/Volumes/aj/VLP16_Points_2017-10-24-11-21-21.bag', 'r') print (f.read()) f.close() I am getting the foll

Solution 1:

In Python 3 open() uses your environment to choose an appropriate encoding. If you sure, that file encoded with utf-8 you could ignore invalid byte sequence with

with open('/path/to/file', 'r', error='ignore') as f:
    print(f.read())

Or you could chose right encoding (if your file is non utf-8 encoded) with

withopen('/path/to/file', 'r', encoding='needed_encoding') as f:
    print(f.read())

Also, docs on open builtin could be useful.

Solution 2:

From https://wiki.ros.org/rosbag/Code%20API

import rosbag
bag = rosbag.Bag('test.bag')
for topic, msg, t in bag.read_messages(topics=['chatter', 'numbers']):
   print(msg)
bag.close()

Solution 3:

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

pip install bagpy

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 topicsprint(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')

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

Post a Comment for "How To Write A Python Code To Read Data From ".bag" File And Write Data In Bag File Runtime"