Carma-platform v4.2.0
CARMA Platform is built on robot operating system (ROS) and utilizes open source software (OSS) that enables Cooperative Driving Automation (CDA) features to allow Automated Driving Systems to interact and cooperate with infrastructure and other vehicles through communication.
process_traj_logs.py
Go to the documentation of this file.
1#!/usr/bin/python3
2
3# Copyright (C) 2021 LEIDOS.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy of
7# the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16
17import sys
18import csv
19from bisect import bisect_left
20from enum import Enum
21import matplotlib.pyplot as plt
22from matplotlib.widgets import Slider
23
24def binarySearch(a, x):
25 i = bisect_left(a, x)
26 if i:
27 return (i-1)
28 else:
29 return -1
30# Usage
31# process_traj_logs.py <file name> <start time> <end_time>
32#
33# Read the file until start_time is found.
34# Read the file until end_time is found
35# Grab all lines between start and end time
36# Print layered graphs of all trajectories between those times
37if len(sys.argv) < 4:
38 print("Need 3 arguments: process_traj_logs.py <file name> <start time> <end_time>")
39 exit()
40
41data = []
42with open(sys.argv[1], 'r') as infile, \
43 open(sys.argv[1] + ".clean", 'w') as outfile:
44 data = infile.read()
45 data = data.replace("\x1b[0m", "")
46 data = data.replace("\x1b[32m", "")
47 outfile.write(data)
48
49core_data = {
50 "times": [],
51 "content": []
52}
53with open(sys.argv[1] + ".clean", 'r') as csv_file:
54 traj_data = csv.reader(csv_file, delimiter='|')
55 i = 0
56
57 for row in traj_data:
58 if len(row) < 4:
59 continue
60 core_data["times"].append(float(row[0].strip()))
61 core_data["content"].append(row[3].strip())
62 #print(row)
63 i+=1
64 #if i == 15: # TODO remove
65 # print(core_data)
66 #
67 # break
68
69# Grab requested section
70start_index = max(binarySearch(core_data["times"], float(sys.argv[2])) - 1, 0)
71end_index = min(binarySearch(core_data["times"], float(sys.argv[3])) + 1, len(core_data["times"]))
72
73print("Start index: " + str(start_index))
74print("End index: " + str(end_index))
75
76core_data["times"] = core_data["times"][start_index:end_index]
77core_data["content"] = core_data["content"][start_index:end_index]
78
79# Identify required graphs
80class DataSource(Enum):
81 NONE = 0
82 RAW_POINTS = 1,
83 TIME_BOUND_POINTS = 2,
84 BACK_AND_FRONT_POINTS = 3,
85 SAMPLED_POINTS = 4,
86 RAW_CURVATURES = 5,
87 PROCESSED_CURVATURES = 6,
88 CURVATURE_CONSTRAINED_SPEEDS = 7,
89 FINAL_YAWS = 8,
90 SPEED_LIMIT_CONSTRAINED_SPEEDS = 9,
91 SPEED_OP_REVERSE_STEP = 10,
92 SPEED_OP_FORWARD = 11,
93 AFTER_SPEED_OP = 12,
94 AFTER_AVERAGE = 13,
95 AFTER_MIN_SPEED = 14,
96 FINAL_TIMES = 15
97
98data_source = DataSource.NONE
99
100core_data["time_steps"] = []
101
102for content in core_data["content"]:
103 if data_source == DataSource.NONE and "VehicleState" in content:
104 core_data["time_steps"].append({
105 DataSource.RAW_POINTS : [],
106 DataSource.TIME_BOUND_POINTS : [],
107 DataSource.BACK_AND_FRONT_POINTS : [],
108 DataSource.SAMPLED_POINTS : [],
109 DataSource.RAW_CURVATURES : [],
110 DataSource.PROCESSED_CURVATURES : [],
111 DataSource.CURVATURE_CONSTRAINED_SPEEDS : [],
112 DataSource.FINAL_YAWS : [],
113 DataSource.SPEED_LIMIT_CONSTRAINED_SPEEDS : [],
114 DataSource.SPEED_OP_REVERSE_STEP : [],
115 DataSource.SPEED_OP_FORWARD : [],
116 DataSource.AFTER_SPEED_OP : [],
117 DataSource.AFTER_AVERAGE : [],
118 DataSource.AFTER_MIN_SPEED : [],
119 DataSource.FINAL_TIMES : []
120 })
121
122 data_source = DataSource.RAW_POINTS
123
124 if data_source == DataSource.RAW_POINTS and "Point:" in content and "Speed:" in content:
125 point_speed = content.split(':')
126 point = point_speed[1].split('Speed')
127 xy = point[0].split(',')
128 x = float(xy[0])
129 y = float(xy[1])
130 core_data["time_steps"][-1][DataSource.RAW_POINTS].append((x,y))
131
132 if data_source == DataSource.RAW_POINTS and "Got time_bound_points with size:" in content:
133 data_source = DataSource.TIME_BOUND_POINTS
134
135 if data_source == DataSource.TIME_BOUND_POINTS and "Point:" in content and "Speed:" in content:
136 point_speed = content.split(':')
137 point = point_speed[1].split('Speed')
138 xy = point[0].split(',')
139 x = float(xy[0])
140 y = float(xy[1])
141 core_data["time_steps"][-1][DataSource.TIME_BOUND_POINTS].append((x,y))
142
143 if data_source == DataSource.TIME_BOUND_POINTS and "Got back_and_future points with size" in content:
144 data_source = DataSource.BACK_AND_FRONT_POINTS
145
146 if data_source == DataSource.BACK_AND_FRONT_POINTS and "Point:" in content and "Speed:" in content:
147 point_speed = content.split(':')
148 point = point_speed[1].split('Speed')
149 xy = point[0].split(',')
150 x = float(xy[0])
151 y = float(xy[1])
152 core_data["time_steps"][-1][DataSource.BACK_AND_FRONT_POINTS].append((x,y))
153
154 if data_source == DataSource.BACK_AND_FRONT_POINTS and "Got sampled points with size:" in content:
155 data_source = DataSource.SAMPLED_POINTS
156
157 if data_source == DataSource.SAMPLED_POINTS and "," in content and not ":" in content:
158 xy = content.split(',')
159 x = float(xy[0])
160 y = float(xy[1])
161 core_data["time_steps"][-1][DataSource.SAMPLED_POINTS].append((x,y))
162
163 if (False and data_source == DataSource.RAW_CURVATURES or data_source == DataSource.SAMPLED_POINTS) and "better_curvature[i]:" in content:
164 data_source = DataSource.RAW_CURVATURES
165 split = content.split(':')
166 c = float(split[1])
167 core_data["time_steps"][-1][DataSource.RAW_CURVATURES].append(c)
168
169 if (data_source == DataSource.PROCESSED_CURVATURES or data_source == DataSource.SAMPLED_POINTS) and "curvatures[i]:" in content:
170 data_source = DataSource.PROCESSED_CURVATURES
171 split = content.split(':')
172 c = float(split[1])
173 core_data["time_steps"][-1][DataSource.PROCESSED_CURVATURES].append(c)
174
175 if (data_source == DataSource.CURVATURE_CONSTRAINED_SPEEDS or data_source == DataSource.PROCESSED_CURVATURES) and "ideal_speeds:" in content:
176 data_source = DataSource.CURVATURE_CONSTRAINED_SPEEDS
177 split = content.split(':')
178 c = float(split[1])
179 core_data["time_steps"][-1][DataSource.CURVATURE_CONSTRAINED_SPEEDS].append(c)
180
181 if (data_source == DataSource.FINAL_YAWS or data_source == DataSource.CURVATURE_CONSTRAINED_SPEEDS) and "final_yaw_values[i]:" in content:
182 data_source = DataSource.FINAL_YAWS
183 split = content.split(':')
184 c = float(split[1])
185 core_data["time_steps"][-1][DataSource.FINAL_YAWS].append(c)
186
187 if (data_source == DataSource.SPEED_LIMIT_CONSTRAINED_SPEEDS or data_source == DataSource.FINAL_YAWS) and "constrained_speed_limits:" in content:
188 data_source = DataSource.SPEED_LIMIT_CONSTRAINED_SPEEDS
189 split = content.split(':')
190 c = float(split[1])
191 core_data["time_steps"][-1][DataSource.SPEED_LIMIT_CONSTRAINED_SPEEDS].append(c)
192
193 if (data_source == DataSource.SPEED_OP_REVERSE_STEP or data_source == DataSource.SPEED_LIMIT_CONSTRAINED_SPEEDS) and "only_reverse[i]:" in content:
194 data_source = DataSource.SPEED_OP_REVERSE_STEP
195 split = content.split(':')
196 c = float(split[1])
197 core_data["time_steps"][-1][DataSource.SPEED_OP_REVERSE_STEP].append(c)
198
199 if (data_source == DataSource.SPEED_OP_FORWARD or data_source == DataSource.SPEED_OP_REVERSE_STEP) and "after_forward[i]:" in content:
200 data_source = DataSource.SPEED_OP_FORWARD
201 split = content.split(':')
202 c = float(split[1])
203 core_data["time_steps"][-1][DataSource.SPEED_OP_FORWARD].append(c)
204
205 if (data_source == DataSource.AFTER_SPEED_OP or data_source == DataSource.SPEED_OP_FORWARD) and "postAccel[i]:" in content:
206 data_source = DataSource.AFTER_SPEED_OP
207 split = content.split(':')
208 c = float(split[1])
209 core_data["time_steps"][-1][DataSource.AFTER_SPEED_OP].append(c)
210
211 if (data_source == DataSource.AFTER_AVERAGE or data_source == DataSource.AFTER_SPEED_OP) and "post_average[i]:" in content:
212 data_source = DataSource.AFTER_AVERAGE
213 split = content.split(':')
214 c = float(split[1])
215 core_data["time_steps"][-1][DataSource.AFTER_AVERAGE].append(c)
216
217 if (data_source == DataSource.AFTER_MIN_SPEED or data_source == DataSource.AFTER_AVERAGE) and "post_min_speed[i]:" in content:
218 data_source = DataSource.AFTER_MIN_SPEED
219 split = content.split(':')
220 c = float(split[1])
221 core_data["time_steps"][-1][DataSource.AFTER_MIN_SPEED].append(c)
222
223 if (data_source == DataSource.FINAL_TIMES or data_source == DataSource.AFTER_MIN_SPEED) and "times[i]:" in content:
224 data_source = DataSource.FINAL_TIMES
225 split = content.split(':')
226 c = float(split[1])
227 core_data["time_steps"][-1][DataSource.FINAL_TIMES].append(c)
228
229 if data_source == DataSource.FINAL_TIMES and not ("times[i]:" in content):
230 data_source = DataSource.NONE
231
232
233print("DONE PROCESSING FILE")
234print("CREATING GRAPHS")
235
236# Takes list of dictionary
237def xy_scatter_with_slider(figure_num, data, key, title, xlabel, ylabel):
238
239 fig = plt.figure(figure_num)
240
241 plt.title(title)
242 plt.xlabel(xlabel)
243 plt.ylabel(ylabel)
244 time_step = data[0]
245 l, = plt.plot([xy[0] for xy in time_step[key]], [xy[1] for xy in time_step[key]], '.')
246
247 time_step_ax = plt.axes([0.20, 0.001, 0.65, 0.03])
248 time_step_sldr = Slider(time_step_ax, 'Time Step', 0.0, len(data) - 1.0, valinit=0, valstep=1)
249
250 def update_timestep(val):
251 time_step = data[int(time_step_sldr.val)]
252 l.set_xdata([xy[0] for xy in time_step[key]])
253 l.set_ydata([xy[1] for xy in time_step[key]])
254 fig.canvas.draw_idle()
255
256 time_step_sldr.on_changed(update_timestep)
257
258 return (fig, l, time_step_sldr)
259
260# Takes list of dictionary
261def index_plot_with_slider(figure_num, data, key, title, xlabel, ylabel):
262
263 fig = plt.figure(figure_num)
264
265 plt.title(title)
266 plt.xlabel(xlabel)
267 plt.ylabel(ylabel)
268 time_step = data[0]
269 l, = plt.plot(range(len(time_step[key])), time_step[key])
270
271 time_step_ax = plt.axes([0.20, 0.01, 0.65, 0.03])
272 time_step_sldr = Slider(time_step_ax, 'Time Step', 0.0, len(data) - 1.0, valinit=0, valstep=1)
273
274 def update_timestep(val):
275 time_step = data[int(time_step_sldr.val)]
276 l.set_xdata(range(len(time_step[key])))
277 l.set_ydata(time_step[key])
278 fig.canvas.draw_idle()
279
280 time_step_sldr.on_changed(update_timestep)
281
282 return (fig, l, time_step_sldr)
283
284plot1= xy_scatter_with_slider(1, core_data["time_steps"], DataSource.RAW_POINTS,
285 "Raw Downsampled Points from Lanelet Centerlines", "X (m)", "Y (m)")
286
287plot2= xy_scatter_with_slider(2, core_data["time_steps"], DataSource.TIME_BOUND_POINTS,
288 "Time Bound Points from Lanelet Centerlines", "X (m)", "Y (m)")
289
290plot3= xy_scatter_with_slider(3, core_data["time_steps"], DataSource.BACK_AND_FRONT_POINTS,
291 "Back and front points from Lanelet Centerlines", "X (m)", "Y (m)")
292
293plot4= xy_scatter_with_slider(4, core_data["time_steps"], DataSource.SAMPLED_POINTS,
294 "Sampled points from spline fitting", "X (m)", "Y (m)")
295
296plot5 = index_plot_with_slider(5, core_data["time_steps"], DataSource.RAW_CURVATURES,
297 "Raw Curvatures", "Index", "Curvature (1/r) (m)")
298
299plot6 = index_plot_with_slider(6, core_data["time_steps"], DataSource.PROCESSED_CURVATURES,
300 "Processed Curvatures", "Index", "Curvature (1/r) (m)")
301
302plot7 = index_plot_with_slider(7, core_data["time_steps"], DataSource.CURVATURE_CONSTRAINED_SPEEDS,
303 "Curvature constrained speeds", "Index", "Velocity (m/s)")
304
305plot8 = index_plot_with_slider(8, core_data["time_steps"], DataSource.FINAL_YAWS,
306 "Final Yaw values", "Index", "Yaw (rad)")
307
308plot9 = index_plot_with_slider(9, core_data["time_steps"], DataSource.SPEED_LIMIT_CONSTRAINED_SPEEDS,
309 "Speed Limit Constrained Speeds", "Index", "Velocity (m/s)")
310
311plot10 = index_plot_with_slider(10, core_data["time_steps"], DataSource.SPEED_OP_REVERSE_STEP,
312 "Speed Optimization Reverse Step", "Index", "Velocity (m/s)")
313
314plot11 = index_plot_with_slider(11, core_data["time_steps"], DataSource.SPEED_OP_FORWARD,
315 "Speed Optimization Forward Step", "Index", "Velocity (m/s)")
316
317plot12 = index_plot_with_slider(12, core_data["time_steps"], DataSource.AFTER_SPEED_OP,
318 "Speed Optimization Output", "Index", "Velocity (m/s)")
319
320plot13 = index_plot_with_slider(13, core_data["time_steps"], DataSource.AFTER_AVERAGE,
321 "Speed after Moving Average", "Index", "Velocity (m/s)")
322
323plot14 = index_plot_with_slider(14, core_data["time_steps"], DataSource.AFTER_MIN_SPEED,
324 "Speed after applying minimum speed (FINAL SPEED)", "Index", "Velocity (m/s)")
325
326plot15 = index_plot_with_slider(15, core_data["time_steps"], DataSource.FINAL_TIMES,
327 "Final Times", "Index", "Seconds (s)")
328
329plt.show()
330'''
331core_data["time_steps"].append({
332 DataSource.RAW_POINTS : [],
333 DataSource.TIME_BOUND_POINTS : [],
334 DataSource.BACK_AND_FRONT_POINTS : [],
335 DataSource.SAMPLED_POINTS : [],
336 DataSource.RAW_CURVATURES : [],
337 DataSource.PROCESSED_CURVATURES : [],
338 DataSource.CURVATURE_CONSTRAINED_SPEEDS : [],
339 DataSource.FINAL_YAWS : [],
340 DataSource.SPEED_LIMIT_CONSTRAINED_SPEEDS : [],
341 DataSource.SPEED_OP_REVERSE_STEP : [],
342 DataSource.SPEED_OP_FORWARD : [],
343 DataSource.AFTER_SPEED_OP : [],
344 DataSource.AFTER_AVERAGE : [],
345 DataSource.AFTER_MIN_SPEED : [],
346 DataSource.FINAL_TIMES : []
347 })
348'''
349
350# Proceeded by : VehicleState
351# Point: Speed:
352#
353# Proceeded by : Got time_bound_points with size:
354# Point: Speed:
355
356# Proceeded by : Got back_and_future points with size
357# Point: Speed:
358
359# Proceeded by : Got sampled points with size
360# (nothing ) -74.5649, 329.079
361
362# Proceeded by : nothing
363# better_curvature[i]:
364
365# Proceeded by : nothing
366# curvatures[i]:
367
368# Proceeded by : nothing
369# ideal_speeds[i]:
370
371# Proceeded by : nothing
372# final_yaw_values[i]:
373
374# Proceeded by : nothing
375# final_actual_speeds[i]:
376
377# Proceeded by : nothing
378# only_reverse[i]:
379
380# Proceeded by : nothing
381# after_forward[i]:
382
383# Proceeded by : nothing
384# postAccel[i]:
385
386# Proceeded by : nothing
387# post_average[i]:
388
389# Proceeded by : nothing
390# post_min_speed[i]:
391
392# Proceeded by : nothing
393# times[i]:
394
395# Proceeded by : nothing
396# times[i]:
397
398
399
400
401
402
403
404
405
406
407
408
409#with open('demofile.txt', "r") as f:
410# read_data = f.readline()
411
412
413#if __name__ == "__main__":
414# data = "My data read from the Web"
415# print(data)
416# modified_data = process_data(data)
417# print(modified_data)
def xy_scatter_with_slider(figure_num, data, key, title, xlabel, ylabel)
def index_plot_with_slider(figure_num, data, key, title, xlabel, ylabel)