How to extract data structure values from rosbag using python3
Greetings!
I have a rosbag with several topics and need help with one of them. My OS is Ubuntu 18 and Melodic install for ROS-1. I need to parse the values of x, y, and z from one topic that apears to be an object when I read the topic messages.
Normally this is straight forward. Rephrasing my question is "How do I read messages from the topic such that I can parse the values from a structure inside it?
Why do I know it is a structure? I can pull the topic from the bag and look at the cvs with pandas.
I can successfully do this in Matlab using their ros toolbox. I have clients who don't want to install ros or buy a MAtlab license but have a need to see the data from the ros bag. They could run a python script. That's where I could use your help.
The topic giving me issues is 'global_path'. I chose to use bagpy although I think importing rosbag would work as well.
The basic steps I followed were:
read bag
b = bagreader('/home/bags/flight12022-08-22-13-09-30.bag')
check available topics
b.topic_table
Topics Types Message Count Frequency
- 0 /globalpath >> plannermap_interfaces/Plan >> 2 >>0.001536
- 1 /particlefiltersvisualizer >> visualization_msgs/MarkerArray >> 820 >>0.999696
- 2 /planner/planningrequest >> plannermap_interfaces/PlanningRequest >> 2 >> 0.001537
- 3 /searchbelief >> visualizationmsgs/Marker >> 1399 >> 2.000637
- 4 /targetprojectionnode/filteredtargets >> plannermap_interfaces/FilteredTargets >> 343 >>0.496940
- 5 /targetpropagation/particlefiltersbelief >> targetpropagation/ParticleFiltersBelief >> 820 >> 0.999689
- pull messages from topic /globalpath >>>data = b.messagebytopic('/globalpath')
- print("File saved: {}".format(data)) :: creates a csv of global_path topic
result: /home//bags/flight12022-08-22-13-09-30/global_path.csv
import pandas as pd
dfplan = pd.readcsv(data)
df_plan :: shows the table with 2 rows and 6 columns.'plan' is column 6
df_plan.shape :: shows (2,6) This is 2 rows and 6 columns
an example of showing the first column of the csv, 'Time'.
Note the type of the first is float64. So are columns 2 thru 5
df_plan['Time']
0 1.661199e+09
1 1.661200e+09
Name: Time, dtype: float64
- Next we see what type is the 6th column..
>>df_plan['plan']
>>> 0 [stamp: \n seq: 10\n stamp: \n secs: 0\n ...
1 [stamp: \n seq: 8\n stamp: \n secs: 0\n ...
Name: plan, dtype: object
- The data type of column 6 is an object with label 'plan''.
the resultant cvs is a single column with many embedded fields
- How can I format this 6th column? Is it when I first read the bag or when I extract the topic?
- How do I read this column to extract the x, y, z positions inside the object?
- in Matlab, I have a line to say the data type is a structure.
- is there an equivalent using bagpy's bagreader or a method in rosbag?
Note the MAtlab use of 'readMessages' to specify a data format of 'struct'
bag = rosbag('/path/to/rosbag file');
bSel = select(bag,'Topic','/global_path');
msgStructs = readMessages(bSel,'DataFormat','struct');
msgStructs{1} ::this references plan 1 of 2
msgStructs{1}.Plan_
- for n = 1:11
- xEast(n) = msgStructs{1}.Plan_(n).Position(1).Position.X;
- yNorth(n)= msgStructs{1}.Plan_(n).Position(1).Position.Y;
- zUP(n) = msgStructs{1}.Plan_(n).Position(1).Position.Z;
- end
This has been challenging. I'm willing to share my bag or csv's. I tried rosbag, bagpy and pandas. I may have to parse the one-column result from the csv of the object represented by the 6th column.
Asked by clarknator on 2022-10-30 16:42:14 UTC
Comments
duplicate of #q408736
Asked by ravijoshi on 2022-11-02 07:54:56 UTC