Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert csv to rosbag?

I have a csv file that I want to convert into rosbag as I am trying to use a package written in ROS. The goal is to perform sensor fusion using an extended kalman filter and my csv file has information of IMU and GPS data.

like image 855
Yukti Kathuria Avatar asked Oct 15 '25 19:10

Yukti Kathuria


1 Answers

There are a couple steps:

1. Read in the data from the CSV file.

There are a number of ways to do this in Python but I prefer using Pandas:

import pandas as pd
df = pd.read_csv('my_csv_file.csv')

This loads the data as a Pandas dataframe that you can read off columns asdf['timestamp'] which can then be indexed like an array.

2. Populate ROS messages and write to rosbag

There is a nice Python API for rosbag. I don't know exactly what the data elements are in your CSV file so I will leave the message population up to you, but it looks something like:

import rospy
import rosbag
from sensor_msgs.msg import Imu, NavSatFix

with rosbag.Bag('output.bag', 'w') as bag:
    for row in range(df.shape[0]):
        timestamp = rospy.Time.from_sec(df['timestamp'][row])
        imu_msg = Imu()
        imu_msg.header.stamp = timestamp

        # Populate the data elements for IMU
        # e.g. imu_msg.angular_velocity.x = df['a_v_x'][row]

        bag.write("/imu", imu_msg, timestamp)

        gps_msg = NavSatFix()
        gps_msg.header.stamp = timestamp

        # Populate the data elements for GPS

        bag.write("/gps", gpu_msg, timestamp)

Note that there seem to be other GPS message types you can use, e.g. from the gps_common package. /imu and /gps are the topic names and you can name them as desired.

like image 58
adamconkey Avatar answered Oct 18 '25 16:10

adamconkey