-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextract_topics_from_rosbag.py
200 lines (174 loc) · 7.18 KB
/
extract_topics_from_rosbag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"""
This scrips extracts topics from rosbag files that are required for further analyis.
"""
import rosbag
import numpy as np
import os
import csv
from os.path import expanduser
from pathlib import Path
## Input
#Parameters
bag_name = 'vmp_0_4_term_alt_0_4_try_12_20230214_174800.bag'
bag_file_path = '/home/pgoldschmid/src/rl_multi_rotor_landing/rl_multi_rotor_landing_gcs/other_files'
file_base_path = 'rosbag_converts'
skipped_entries_at_start_of_episode = 5
## Calc
id_name = bag_name[:-4]
list_of_msg = []
rel_p_x_list = []
rel_p_y_list = []
rel_p_z_list = []
rel_v_x_list = []
rel_v_y_list = []
rel_v_z_list = []
t_rel_list = []
t_reset_list = []
#Read rosbag file and extract rel position and rel vel data
print("Start extracting observations...")
bag = rosbag.Bag(os.path.join(bag_file_path,bag_name))
for topic, msg, t in bag.read_messages(topics=['/copter/vicon_observation_interface/observations']):
rel_p_x_list.append(msg.rel_p_x)
rel_p_y_list.append(msg.rel_p_y)
rel_p_z_list.append(msg.rel_p_z)
rel_v_x_list.append(msg.rel_v_x)
rel_v_y_list.append(msg.rel_v_y)
rel_v_z_list.append(msg.rel_v_z)
t_rel_list.append(t.to_sec())
#Make dir and initialize file
Path(os.path.join(os.path.dirname(os.path.realpath(__file__)),file_base_path,bag_name[:-4],'Total')).mkdir(parents=True, exist_ok=True)
file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),file_base_path,bag_name[:-4],'Total',id_name+"_observations"+'.dat')
open(file_path,'w+').close()
#Fill with data
row_list = []
for j in range(skipped_entries_at_start_of_episode,len(t_rel_list)):
row_list = [t_rel_list[j],rel_p_x_list[j],rel_p_y_list[j],rel_p_z_list[j],rel_v_x_list[j],rel_v_y_list[j],rel_v_z_list[j]]
with open(file_path,'a+') as f:
writer = csv.writer(f)
writer.writerow(row_list)
print("Done extracting observations...")
#Read rosbag file and extract position and velocity data from state estimate
print("Start extracting state estimate...")
p_x_list =[]
p_y_list =[]
p_z_list =[]
v_x_list =[]
v_y_list =[]
v_z_list =[]
t_list =[]
bag = rosbag.Bag(os.path.join(bag_file_path,bag_name))
for topic, msg, t in bag.read_messages(topics=['/fc0/pose']):
p_x_list.append(msg.position.x)
p_y_list.append(msg.position.y)
p_z_list.append(msg.position.z)
v_x_list.append(msg.velocity.x)
v_y_list.append(msg.velocity.y)
v_z_list.append(msg.velocity.z)
t_list.append(t.to_sec())
#Make dir and initialize file
Path(os.path.join(os.path.dirname(os.path.realpath(__file__)),file_base_path,bag_name[:-4])).mkdir(parents=True, exist_ok=True)
file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),file_base_path,bag_name[:-4],'Total',id_name+"_state_estimate_pos_vel"+'.dat')
open(file_path,'w+').close()
#Fill with data
row_list = []
for j in range(skipped_entries_at_start_of_episode,len(t_list)):
row_list = [t_list[j],p_x_list[j],p_y_list[j],p_z_list[j],v_x_list[j],v_y_list[j],v_z_list[j]]
with open(file_path,'a+') as f:
writer = csv.writer(f)
writer.writerow(row_list)
print("Done extracting state estimate...")
#Read rosbag file and extract position of moving platform
print("Start extracting moving platform pose in ned...")
p_x_list =[]
p_y_list =[]
p_z_list =[]
t_list =[]
bag = rosbag.Bag(os.path.join(bag_file_path,bag_name))
for topic, msg, t in bag.read_messages(topics=['/vicon/moving_platform/pose']):
p_x_list.append(msg.pose.position.x)
p_y_list.append(msg.pose.position.y)
p_z_list.append(msg.pose.position.z)
t_list.append(t.to_sec())
#Make dir and initialize file
Path(os.path.join(os.path.dirname(os.path.realpath(__file__)),file_base_path,bag_name[:-4])).mkdir(parents=True, exist_ok=True)
file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),file_base_path,bag_name[:-4],'Total',id_name+"_mp_pose"+'.dat')
row_list = []
open(file_path,'w+').close()
#Fill with data
row_list = []
for j in range(skipped_entries_at_start_of_episode,len(t_list)):
row_list = [t_list[j],p_x_list[j],p_y_list[j],p_z_list[j]]
with open(file_path,'a+') as f:
writer = csv.writer(f)
writer.writerow(row_list)
print("Done extracting moving platform pose in ned...")
#Read rosbag file and extract position of drone
print("Start extracting drone pose in enu...")
p_x_list =[]
p_y_list =[]
p_z_list =[]
t_list =[]
bag = rosbag.Bag(os.path.join(bag_file_path,bag_name))
for topic, msg, t in bag.read_messages(topics=['/vicon/drone/pose_enu']):
p_x_list.append(msg.pose.position.x)
p_y_list.append(msg.pose.position.y)
p_z_list.append(msg.pose.position.z)
t_list.append(t.to_sec())
#Make dir and initialize file
Path(os.path.join(os.path.dirname(os.path.realpath(__file__)),file_base_path,bag_name[:-4])).mkdir(parents=True, exist_ok=True)
file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),file_base_path,bag_name[:-4],'Total',id_name+"_drone_pose_enu"+'.dat')
row_list = []
open(file_path,'w+').close()
#Fill with data
row_list = []
for j in range(skipped_entries_at_start_of_episode,len(t_list)):
row_list = [t_list[j],p_x_list[j],p_y_list[j],p_z_list[j]]
with open(file_path,'a+') as f:
writer = csv.writer(f)
writer.writerow(row_list)
print("Done extracting drone pose in enu...")
#Read rosbag file and extract velocity of moving platform
print("Start extracting moving platform velocity in ned...")
v_x_list =[]
v_y_list =[]
v_z_list =[]
t_list =[]
bag = rosbag.Bag(os.path.join(bag_file_path,bag_name))
for topic, msg, t in bag.read_messages(topics=['/vicon/moving_platform/twist']):
v_x_list.append(msg.twist.linear.x)
v_y_list.append(msg.twist.linear.y)
v_z_list.append(msg.twist.linear.z)
t_list.append(t.to_sec())
#Make dir and initialize file
Path(os.path.join(os.path.dirname(os.path.realpath(__file__)),file_base_path,bag_name[:-4])).mkdir(parents=True, exist_ok=True)
file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),file_base_path,bag_name[:-4],'Total',id_name+"_mp_twist"+'.dat')
row_list = []
open(file_path,'w+').close()
#Fill with data
row_list = []
for j in range(skipped_entries_at_start_of_episode,len(t_list)):
row_list = [t_list[j],v_x_list[j],v_y_list[j],v_z_list[j]]
with open(file_path,'a+') as f:
writer = csv.writer(f)
writer.writerow(row_list)
print("Done extracting moving platform velocity in ned...")
print("Start extracting ROSControlled status...")
ROSControlled_list = []
t_list =[]
bag = rosbag.Bag(os.path.join(bag_file_path,bag_name))
for topic, msg, t in bag.read_messages(topics=['/fc0/TransmitterInfo']):
ROSControlled_list.append(msg.ROSControlled)
t_list.append(t.to_sec())
#Make dir and initialize file
Path(os.path.join(os.path.dirname(os.path.realpath(__file__)),file_base_path,bag_name[:-4])).mkdir(parents=True, exist_ok=True)
file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),file_base_path,bag_name[:-4],'Total',id_name+"_ROSControlled"+'.dat')
row_list = []
open(file_path,'w+').close()
#Fill with data
row_list = []
for j in range(skipped_entries_at_start_of_episode,len(t_list)):
row_list = [t_list[j],ROSControlled_list[j]]
with open(file_path,'a+') as f:
writer = csv.writer(f)
writer.writerow(row_list)
print("Done extracting ROSControlled status...")