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_bag.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
23import rosbag # Imported to python env with pip install --extra-index-url https://rospypi.github.io/simple/ rospy rosbag
24import math
25import numpy as np
26
27# Usage
28# process_bag.py <file name>
29
30if len(sys.argv) < 2:
31 print("Need 1 arguments: process_bag.py <file name> ")
32 exit()
33
34print("Starting To Process Bag")
35bag = rosbag.Bag(sys.argv[1])
36
37plan_trajectory_time_steps = []
38for topic, msg, t in bag.read_messages(topics=['/guidance/plan_trajectory']):
39 # Create data to print for Plan Delegator -> Traj Executor
40 plan_trajectory_time_steps.append([])
41 for point in msg.trajectory_points:
42 plan_trajectory_time_steps[-1].append(point.target_time.to_sec())
43
44pure_pursuit_plan_trajectory_time_steps = []
45for topic, msg, t in bag.read_messages(topics=['/guidance/pure_pursuit/plan_trajectory']):
46 # Create data to print for Traj Executor -> Pure Pursuit Wrapper
47 pure_pursuit_plan_trajectory_time_steps.append([])
48 for point in msg.trajectory_points:
49 pure_pursuit_plan_trajectory_time_steps[-1].append(point.target_time.to_sec())
50
51carma_final_waypoints_times_steps = []
52first_point = []
53second_point = []
54third_point = []
55fourth_point = []
56for topic, msg, t in bag.read_messages(topics=['/guidance/carma_final_waypoints']):
57 # Create data to print for Pure Pursuit Wrapper -> Pure Pursuit
58 carma_final_waypoints_times_steps.append([])
59 i = 0
60 for point in msg.waypoints:
61 if i == 0:
62 first_point.append(point.twist.twist.linear.x)
63 elif i == 1:
64 second_point.append(point.twist.twist.linear.x)
65 elif i == 2:
66 third_point.append(point.twist.twist.linear.x)
67 elif i == 3:
68 fourth_point.append(point.twist.twist.linear.x)
69 carma_final_waypoints_times_steps[-1].append(point.twist.twist.linear.x)
70 i+=1
71
72ctrl_raw = []
73for topic, msg, t in bag.read_messages(topics=['/guidance/ctrl_raw']):
74 # Create data to print for Pure Pursuit -> Twist Filter
75 ctrl_raw.append(msg.cmd.linear_velocity)
76
77ctrl_cmd = []
78for topic, msg, t in bag.read_messages(topics=['/guidance/ctrl_cmd']):
79 # Create data to print for Twist Filter -> Twist Gate
80 ctrl_cmd.append(msg.cmd.linear_velocity)
81
82vehicle_cmd = []
83for topic, msg, t in bag.read_messages(topics=['/hardware_interface/vehicle_cmd']):
84 # Create data to print for Twist Gate -> SSC Interface (TODO double check)
85 if msg.ctrl_cmd.linear_velocity != 0.0:
86 vehicle_cmd.append(msg.ctrl_cmd.linear_velocity)
87
88print("Done Bag Processing")
89
90print("Graphing Data")
91# Takes list of dictionary
92def index_plot_with_slider(figure_num, data, title, xlabel, ylabel):
93
94 fig = plt.figure(figure_num)
95
96 plt.title(title)
97 plt.xlabel(xlabel)
98 plt.ylabel(ylabel)
99 time_step = data[0]
100 l, = plt.plot(range(len(data[0])), data[0], '.')
101
102 time_step_ax = plt.axes([0.20, 0.01, 0.65, 0.03])
103 time_step_sldr = Slider(time_step_ax, 'Time Step', 0.0, len(data) - 1.0, valinit=0, valstep=1)
104
105 def update_timestep(val):
106 time_step = data[int(time_step_sldr.val)]
107 if (int(time_step_sldr.val) < 66 and int(time_step_sldr.val) > 62):
108 print ("Trajectory Size: " + str(len(time_step)))
109 l.set_xdata(range(len(time_step)))
110 l.set_ydata(time_step)
111 fig.canvas.draw_idle()
112
113 time_step_sldr.on_changed(update_timestep)
114
115 return (fig, l, time_step_sldr)
116
117plot1= index_plot_with_slider(1, plan_trajectory_time_steps,
118 "/guidance/plan_trajectory", "Index", "Time (s)")
119
120plot2= index_plot_with_slider(2, pure_pursuit_plan_trajectory_time_steps,
121 "/guidance/pure_pursuit/plan_trajectory", "Index", "Time (s)")
122
123plot3= index_plot_with_slider(3, carma_final_waypoints_times_steps,
124 "/guidance/carma_final_waypoints", "Index", "Velocity (m/s)")
125
126
127fig = plt.figure(8)
128
129plt.title("Commands")
130plt.xlabel("Index")
131plt.ylabel("Velocity (m/s)")
132plt.plot(range(len(ctrl_raw)), ctrl_raw, 'r')
133plt.plot(range(len(ctrl_cmd)), ctrl_cmd, 'g')
134#plt.plot(range(len(vehicle_cmd)), vehicle_cmd, '.b')
135
136
137fig2 = plt.figure(9)
138
139plt.title("Points Waypoints")
140plt.xlabel("Timestep")
141plt.ylabel("Velocity (m/s)")
142plt.plot(range(len(first_point)), first_point, 'r')
143plt.plot(range(len(second_point)), second_point, 'g')
144plt.plot(range(len(third_point)), third_point, 'b')
145plt.plot(range(len(fourth_point)), fourth_point, 'y')
146plt.legend(["First", "Second", "Third", "Fourth"])
147
148# plot4= index_plot_with_slider(4, accel_time_steps,
149# "/guidance/carma_final_waypoints Accel", "Index - 1", "Acceleration (m/s^2)")
150
151# plot5= index_plot_with_slider(5, numpy_accels,
152# "Numpy Accel", "Index - 1", "Acceleration (m/s^2)")
153
154plt.show()
def index_plot_with_slider(figure_num, data, title, xlabel, ylabel)
Definition: process_bag.py:92