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

Publish messages from a .csv file the appropriate way

asked 2021-06-09 02:38:12 -0500

congphase gravatar image

Hello all,

I extracted data from the GPS topic in my recorded bag file into a .csv file. I then modified the lat,lon values to create fake data as desire. Now I want to write a node to publish that modified data to mapviz.

What I expect to do is (with Python API):

  1. Parse the .csv file into a junk of NavSatFix data object
  2. Loop through that object to publish each messages (similar to each row in the .csv at first)

But I haven't found any tutorial with similar idea. Most of them performs something like

def talker():   
print('in talker')
pub = rospy.Publisher('GPS', NavSatFix, queue_size=10)
rospy.init_node('GPStalker', anonymous=True)
while not rospy.is_shutdown():
    #Assuming that parse will return these values
    time.sleep(1)
    # build navsat message
    fake_gps = NavSatFix()
    fake_gps.header.frame_id = ''
    fake_gps.header.stamp = rospy.Time.now()
    fake_gps.status.status = 1
    fake_gps.status.service = 2
    fake_gps.latitude = 3
    fake_gps.longitude = 4
    fake_gps.altitude = 5

    pub.publish(fake_gps)

My question is, is there a way to implement my idea? Is it efficient? Can you give me some documents or tutorials?

edit retag flag offensive close merge delete

Comments

Hello,

Can you please! tell me the columns and how many rows of your CSV file?

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2021-06-09 03:07:18 -0500 )edit

Hi @Ranjit,

My csv file has 15 columns:

rosbagTimestamp,header,seq,stamp,secs,nsecs,frame_id,status,status,service,latitude,longitude,altitude,position_covariance,position_covariance_type

and 352 rows, including the headers.

congphase gravatar image congphase  ( 2021-06-09 03:18:16 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-06-09 03:26:40 -0500

Ranjit Kathiriya gravatar image

updated 2021-06-09 03:34:03 -0500

Hello @congphase,

I think with this code you can able to read the csv file row one by one and publish it. Note: for example you want the data for rosbagTimestamp it is presented at 0 location so to obtain that data you need to make print(row[0]) similarly with others.

import csv
# Code inside your while loop
with open('csv_file_name.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

You can also use pandas, NumPy so many libraries. You just have to install this using the pip command.

I hope you got your answer if you have any questions or issue with the answer please! drop comment.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2021-06-09 02:38:12 -0500

Seen: 1,017 times

Last updated: Jun 09 '21