-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbicycle_sim.py
193 lines (162 loc) · 6.86 KB
/
bicycle_sim.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
import numpy as np
import matplotlib.pyplot as plt
from carla_env.models.static.vehicle import KinematicBicycleModel, DynamicBicycleModel
import os
import pathlib
DELTA_T = 0.1
def plot_all():
pass
if __name__ == "__main__":
fname = "dynamic_kinematic_model_data_3.npz"
data = np.load("data/kinematic_model_data_train/dynamic_kinematic_model_data_3.npz")
vehicle_location = data["vehicle_location"]
vehicle_rotation = data["vehicle_rotation"]
vehicle_velocity = data["vehicle_velocity"]
vehicle_acceleration = data["vehicle_acceleration"]
vehicle_control = data["vehicle_control"]
elapsed_time = data["elapsed_time"]
print(f"Total Elapsed Time: {elapsed_time[-1]}")
print(f"Number of Steps: {elapsed_time.shape[0]}")
kinematic_bicycle_model = KinematicBicycleModel(
x=vehicle_location[0, 0],
y=vehicle_location[0, 1],
yaw=vehicle_rotation[0, 1],
v=np.linalg.norm(vehicle_velocity[0, :]),
)
dynamic_bicycle_model = DynamicBicycleModel(
x=vehicle_location[0, 0],
y=vehicle_location[0, 1],
yaw=vehicle_rotation[0, 1],
velocity_x=vehicle_velocity[0, 0],
velocity_y=vehicle_velocity[0, 1],
omega=0,
)
kinematic_bicycle_location = np.zeros((elapsed_time.shape[0], 2))
dynamic_bicycle_location = np.zeros((elapsed_time.shape[0], 2))
kinematic_bicycle_yaw = np.zeros((elapsed_time.shape[0], 1))
dynamic_bicycle_yaw = np.zeros((elapsed_time.shape[0], 1))
kinematic_bicycle_speed = np.zeros((elapsed_time.shape[0]))
dynamic_bicycle_speed = np.zeros((elapsed_time.shape[0]))
kinematic_bicycle_acceleration = np.zeros((elapsed_time.shape[0]))
dynamic_bicycle_acceleration = np.zeros((elapsed_time.shape[0]))
for k in range(elapsed_time.shape[0]):
action = vehicle_control[k, :]
if action[0] > 0 and action[2] == 0.0:
acceleration = action[0]
else:
acceleration = -action[2]
steer = action[1]
# kinematic_bicycle_model.step(acceleration, steer, DELTA_T)
# dynamic_bicycle_model.step(acceleration, steer, DELTA_T)
kinematic_bicycle_model.step(vehicle_acceleration[k, 0], steer, DELTA_T)
dynamic_bicycle_model.step(vehicle_acceleration[k, 0], steer, DELTA_T)
kinematic_bicycle_location[k, :] = (
kinematic_bicycle_model.x,
kinematic_bicycle_model.y,
)
dynamic_bicycle_location[k, :] = (
dynamic_bicycle_model.x,
dynamic_bicycle_model.y,
)
kinematic_bicycle_speed[k] = kinematic_bicycle_model.v
dynamic_bicycle_speed[k] = dynamic_bicycle_model.velocity_x
# acceleration
kinematic_bicycle_acceleration[k] = vehicle_acceleration[k, 0]
# acceleration
dynamic_bicycle_acceleration[k] = vehicle_acceleration[k, 0]
kinematic_bicycle_yaw[k] = kinematic_bicycle_model.yaw
dynamic_bicycle_yaw[k] = dynamic_bicycle_model.yaw
savedir = pathlib.Path(f"figures/{fname.split('.')[0]}")
os.makedirs(savedir, exist_ok=True)
plt.figure()
plt.plot(vehicle_location[:, 1], vehicle_location[:, 0], "r-", label="CARLA")
plt.plot(
kinematic_bicycle_location[:, 1],
kinematic_bicycle_location[:, 0],
"b-o",
label="Kinematic Bicycle",
)
# plt.plot(dynamic_bicycle_location[:, 1], dynamic_bicycle_location[:, 0], "g-", label="Dynamic Bicycle")
plt.legend()
plt.xlabel("y")
plt.ylabel("x")
plt.title("CARLA vs. Bicycle Model")
plt.savefig(savedir / "figure1.png")
# plt.show()
plt.figure()
plt.plot(elapsed_time, vehicle_location[:, 0], "r-", label="CARLA")
plt.plot(
elapsed_time, kinematic_bicycle_location[:, 0], "b-", label="Kinematic Bicycle"
)
# plt.plot(elapsed_time, dynamic_bicycle_location[:, 0], "g-", label="Dynamic Bicycle")
plt.legend()
plt.xlabel("Time")
plt.ylabel("x")
plt.title("CARLA vs. Bicycle Model")
plt.savefig(savedir / "figure2.png")
plt.figure()
plt.plot(elapsed_time, vehicle_location[:, 1], "r-", label="CARLA")
plt.plot(
elapsed_time, kinematic_bicycle_location[:, 1], "b-", label="Kinematic Bicycle"
)
# plt.plot(elapsed_time, dynamic_bicycle_location[:, 1], "g-", label="Dynamic Bicycle")
plt.legend()
plt.xlabel("Time")
plt.ylabel("y")
plt.title("CARLA vs. Bicycle Model")
plt.savefig(savedir / "figure3.png")
plt.figure()
# plt.plot(elapsed_time, np.array([np.linalg.norm(vehicle_velocity[k, :]) for k in range(vehicle_velocity.shape[0])]), "r-", label="CARLA")
plt.plot(elapsed_time, vehicle_velocity[:, 0], "r-", label="CARLA")
plt.plot(elapsed_time, kinematic_bicycle_speed, "b-", label="Kinematic Bicycle")
# plt.plot(elapsed_time, dynamic_bicycle_speed, "g-", label="Dynamic Bicycle")
plt.legend()
plt.xlabel("Time")
plt.ylabel("Speed")
plt.title("CARLA vs. Bicycle Model")
plt.savefig(savedir / "figure4.png")
# plt.show()
plt.figure()
# plt.plot(elapsed_time, np.array([np.linalg.norm(vehicle_acceleration[k, :]) for k in range(vehicle_acceleration.shape[0])]), "r-", label="CARLA")
plt.plot(elapsed_time, vehicle_acceleration[:, 0], "r-", label="CARLA")
# plt.plot(elapsed_time, kinematic_bicycle_acceleration, "b-", label="Kinematic Bicycle")
# plt.plot(elapsed_time, dynamic_bicycle_acceleration, "g-", label="Dynamic Bicycle")
plt.legend()
plt.xlabel("Time")
plt.ylabel("Acceleration-x")
plt.title("Vehicle Acceleration Response in CARLA")
plt.savefig(savedir / "figure5.png")
# plt.show()
plt.figure()
# plt.plot(elapsed_time, np.array([np.linalg.norm(vehicle_acceleration[k, :]) for k in range(vehicle_acceleration.shape[0])]), "r-", label="CARLA")
plt.plot(elapsed_time, vehicle_acceleration[:, 1], "r-", label="CARLA")
plt.plot(
elapsed_time, kinematic_bicycle_acceleration, "b-", label="Kinematic Bicycle"
)
# plt.plot(elapsed_time, dynamic_bicycle_acceleration, "g-", label="Dynamic Bicycle")
plt.legend()
plt.xlabel("Time")
plt.ylabel("Acceleration-y")
plt.title("Vehicle Acceleration Response in CARLA")
plt.savefig(savedir / "figure6.png")
# plt.show()
plt.figure()
plt.plot(elapsed_time, vehicle_control)
plt.xlabel("Time")
plt.ylabel("Control")
plt.legend(["Throttle", "Steer", "Brake"])
plt.title("Control Actions")
plt.savefig(savedir / "figure7.png")
# plt.show()
plt.figure()
plt.plot(elapsed_time, np.rad2deg(vehicle_rotation[:, 1]), "r-", label="CARLA")
plt.plot(
elapsed_time, np.rad2deg(kinematic_bicycle_yaw), "b-", label="Kinematic Bicycle"
)
# plt.plot(elapsed_time, np.rad2deg(dynamic_bicycle_yaw), "g-", label="Dynamic Bicycle")
plt.xlabel("Time")
plt.ylabel("Yaw")
plt.title("Vehicle Yaw")
plt.legend()
plt.savefig(savedir / "figure8.png")
# plt.show()