-
Notifications
You must be signed in to change notification settings - Fork 4
/
plot_sim.py
290 lines (232 loc) · 13.3 KB
/
plot_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import math
import numpy as np
import numpy.matlib
import matplotlib.pyplot as plt
import matplotlib.transforms as transforms
import matplotlib.patches as patches
from scipy import ndimage
from PIL import Image
from PIL import ImageChops
def plot_sim(X_old, params, step, Level_ratio, fig):
w_lane = params.w_lane
l_car = params.l_car
w_car = params.w_car
num_lanes = params.num_lanes
l_road = params.l_road
plot_format = params.plot_format
car_rect_lw = 0.25
x_lim_min = min(X_old[0, :]) - 3 * l_car
x_lim_max = max(X_old[0, :]) + 3 * l_car
x_lim = np.array([x_lim_min, x_lim_max])
l_car_safe = params.l_car_safe_fac * l_car
w_car_safe = params.w_car_safe_fac * w_car
color = ['b','r','m','g']
plt.figure(fig.number)
plt.cla()
ax = plt.gca()
img_blue = plt.imread('blue_car.jpg')
img_red = plt.imread('red_car.jpg')
# img_grass = plt.imread('grass.jpg')
#
#
#
# img = Image.open('grass.jpg')
# img_w, img_h = img.size
# background = Image.new('RGBA', (4010, 900), (255, 255, 255, 255))
# bg_w, bg_h = background.size
# offset = (-100, -50)
# background.paste(img, offset)
# offset = (-10+img_w, -50)
# background.paste(img, offset)
#
# ImageChops.offset(background,-100)
#
# ax.imshow(background)
# Road bound
Upper_RoadBound_rectangle = np.array(
[[-3*l_car, w_lane*num_lanes],
[-3*l_car, w_lane*num_lanes*1.5],
[l_road, w_lane*num_lanes*1.5],
[l_road, w_lane*num_lanes]])
Lower_RoadBound_rectangle = np.array(
[[-3*l_car, 0],
[-3*l_car, -w_lane*num_lanes/2],
[l_road, -w_lane*num_lanes/2],
[l_road, 0]])
y_lim = [min(Lower_RoadBound_rectangle[:, 1]), max(Upper_RoadBound_rectangle[:, 1])]
# Plotting grass (green patch)
plt.fill(np.squeeze(Upper_RoadBound_rectangle[:,0]),np.squeeze(Upper_RoadBound_rectangle[:,1]),color=(0,0.4,0,0.6), LineWidth = 2)
plt.fill(np.squeeze(Lower_RoadBound_rectangle[:,0]),np.squeeze(Lower_RoadBound_rectangle[:,1]),color=(0,0.4,0,0.6), LineWidth = 2)
fig = plt.gcf()
# Road bound lines
Lanes = np.array([
[-3*l_car, 0],
[l_road, 0],
[-3*l_car, w_lane*num_lanes],
[l_road, w_lane*num_lanes]])
plt.plot(np.squeeze(Lanes[0:2,0]),np.squeeze(Lanes[0:2,1]),color=(0,0,0),LineWidth = 1.75, linestyle='-')
plt.plot(np.squeeze(Lanes[2:4,0]),np.squeeze(Lanes[2:4,1]),color=(0,0,0),LineWidth = 1.75, linestyle='-')
# Lanes
Lanes = np.array([
[-3*l_car, w_lane],
[l_road, w_lane],
[-3*l_car, w_lane*2],
[l_road, w_lane*2]])
if step % 2 == 0:
plt.plot(x_lim, np.squeeze(Lanes[0:2,1]),color=(0,0,0),LineWidth = 1.25, linestyle='--')
plt.plot(x_lim, np.squeeze(Lanes[2:4,1]),color=(0,0,0),LineWidth = 1.25, linestyle='--')
else:
plt.plot(x_lim+np.array([-l_car*3,l_car*3]), np.squeeze(Lanes[0:2, 1]), color=(0, 0, 0), LineWidth=1.25, linestyle='--')
plt.plot(x_lim+np.array([-l_car*3,l_car*3]), np.squeeze(Lanes[2:4, 1]), color=(0, 0, 0), LineWidth=1.25, linestyle='--')
count = 0
for id in range(0,len(X_old[0,:])):
rect = np.array(
[[X_old[0, id] - l_car / 2 * math.cos(X_old[2, id]) - w_car / 2 * math.sin(X_old[2, id]),
X_old[1, id] - l_car / 2 * math.sin(X_old[2, id]) + w_car / 2 * math.cos(X_old[2, id])],
[X_old[0, id] - l_car / 2 * math.cos(X_old[2, id]) + w_car / 2 * math.sin(X_old[2, id]),
X_old[1, id] - l_car / 2 * math.sin(X_old[2, id]) - w_car / 2 * math.cos(X_old[2, id])],
[X_old[0, id] + l_car / 2 * math.cos(X_old[2, id]) - w_car / 2 * math.sin(X_old[2, id]),
X_old[1, id] + l_car / 2 * math.sin(X_old[2, id]) + w_car / 2 * math.cos(X_old[2, id])],
[X_old[0, id] + l_car / 2 * math.cos(X_old[2, id]) + w_car / 2 * math.sin(X_old[2, id]),
X_old[1, id] + l_car / 2 * math.sin(X_old[2, id]) - w_car / 2 * math.cos(X_old[2, id])],
[X_old[0, id] + (l_car / 2 - 1) * math.cos(X_old[2, id]) - w_car / 2 * math.sin(X_old[2, id]),
X_old[1, id] + (l_car / 2 - 1) * math.sin(X_old[2, id]) + w_car / 2 * math.cos(X_old[2, id])],
[X_old[0, id] + (l_car / 2 - 1) * math.cos(X_old[2, id]) + w_car / 2 * math.sin(X_old[2, id]),
X_old[1, id] + (l_car / 2 - 1) * math.sin(X_old[2, id]) - w_car / 2 * math.cos(X_old[2, id])]])
coll_rect = np.array(
[[X_old[0, id] - l_car_safe / 2 * math.cos(X_old[2, id]) + w_car_safe / 2 * math.sin(X_old[2, id]),
X_old[1, id] - l_car_safe / 2 * math.sin(X_old[2, id]) - w_car_safe / 2 * math.cos(X_old[2, id])],
[X_old[0, id] - l_car_safe / 2 * math.cos(X_old[2, id]) - w_car_safe / 2 * math.sin(X_old[2, id]),
X_old[1, id] - l_car_safe / 2 * math.sin(X_old[2, id]) + w_car_safe / 2 * math.cos(X_old[2, id])],
[X_old[0, id] + l_car_safe / 2 * math.cos(X_old[2, id]) - w_car_safe / 2 * math.sin(X_old[2, id]),
X_old[1, id] + l_car_safe / 2 * math.sin(X_old[2, id]) + w_car_safe / 2 * math.cos(X_old[2, id])],
[X_old[0, id] + l_car_safe / 2 * math.cos(X_old[2, id]) + w_car_safe / 2 * math.sin(X_old[2, id]),
X_old[1, id] + l_car_safe / 2 * math.sin(X_old[2, id]) - w_car_safe / 2 * math.cos(X_old[2, id])]])
l_car_safe_front = 1.1 * l_car
l_car_safe_back = 1.1 * l_car
w_car_safe = 1.1 * w_car
safe_rect = np.array(
[[X_old[0,id]-l_car_safe_back/2*math.cos(X_old[2,id])+w_car_safe/2*math.sin(X_old[2,id]),
X_old[1,id]-l_car_safe_back/2*math.sin(X_old[2,id])-w_car_safe/2*math.cos(X_old[2,id])],
[X_old[0,id]-l_car_safe_back/2*math.cos(X_old[2,id])-w_car_safe/2*math.sin(X_old[2,id]),
X_old[1,id]-l_car_safe_back/2*math.sin(X_old[2,id])+w_car_safe/2*math.cos(X_old[2,id])],
[X_old[0,id]+l_car_safe_front/2*math.cos(X_old[2,id])-w_car_safe/2*math.sin(X_old[2,id]),
X_old[1,id]+l_car_safe_front/2*math.sin(X_old[2,id])+w_car_safe/2*math.cos(X_old[2,id])],
[X_old[0,id]+l_car_safe_front/2*math.cos(X_old[2,id])+w_car_safe/2*math.sin(X_old[2,id]),
X_old[1,id]+l_car_safe_front/2*math.sin(X_old[2,id])-w_car_safe/2*math.cos(X_old[2,id])]])
# Create an inset axes to plot the car images
newax = ax.inset_axes([X_old[0,id]-l_car/2, X_old[1,id]-2.5, 5, 5], transform=ax.transData)
if X_old[4,id]==1:
color_id = 0
img_rot = ndimage.rotate(img_blue, (X_old[2,id]) * 180 / math.pi, reshape=False, cval=255)
else:
color_id = 1
img_rot = ndimage.rotate(img_red, (X_old[2, id]) * 180 / math.pi, reshape=False, cval=255)
newax.imshow(img_rot)
newax.axis('off')
# # Create an inset axes to plot the grass
# newax = ax.inset_axes([Upper_RoadBound_rectangle[0][0], Upper_RoadBound_rectangle[0][1] - 8.2, Upper_RoadBound_rectangle[2][0] - Upper_RoadBound_rectangle[0][0], 32],
# transform=ax.transData)
#
# im = newax.imshow(img_grass)
# patch = patches.Rectangle([Upper_RoadBound_rectangle[0][0], Upper_RoadBound_rectangle[0][1]], sum(abs(x_lim))*300, 1000, transform=newax.transData)
# im.set_clip_path(patch)
# # newax1.imshow(img_grass)
# newax.axis('off')
#
# # Create an inset axes to plot the grass
# newax = ax.inset_axes([x_lim[0], Lower_RoadBound_rectangle[1][1] - 8.5, sum(abs(x_lim)), 16],
# transform=ax.transData)
# im = newax.imshow(img_grass)
# patch = patches.Rectangle([x_lim[0], Lower_RoadBound_rectangle[1][1]], sum(abs(x_lim)) * 200, 370,
# transform=newax.transData)
# im.set_clip_path(patch)
# # newax2.imshow(img_grass)
# newax.axis('off')
# # Vehicle rectangle
# plt.plot(np.squeeze(rect[0:2, 0]), np.squeeze(rect[0:2, 1]), color=color[color_id], LineWidth=car_rect_lw,
# linestyle='-')
# plt.plot([rect[0, 0], rect[2, 0]], [rect[0, 1], rect[2, 1]], color=color[color_id], LineWidth=car_rect_lw,
# linestyle='-')
# plt.plot([rect[2, 0], rect[3, 0]], [rect[2, 1], rect[3, 1]], color=color[color_id], LineWidth=car_rect_lw,
# linestyle='-')
# plt.plot([rect[1, 0], rect[3, 0]], [rect[1, 1], rect[3, 1]], color=color[color_id], LineWidth=car_rect_lw,
# linestyle='-')
# plt.plot([rect[4, 0], rect[5, 0]], [rect[4, 1], rect[5, 1]], color=color[color_id], LineWidth=car_rect_lw,
# linestyle='-')
# # Coll rectangle
#
# plt.plot([coll_rect[0, 0], coll_rect[1, 0]], [coll_rect[0, 1], coll_rect[1, 1]], color=color[color_id], LineWidth=car_rect_lw+1,
# linestyle='-')
# plt.plot([coll_rect[1, 0], coll_rect[2, 0]], [coll_rect[1, 1], coll_rect[2, 1]], color=color[color_id], LineWidth=car_rect_lw+1,
# linestyle='-')
# plt.plot([coll_rect[2, 0], coll_rect[3, 0]], [coll_rect[2, 1], coll_rect[3, 1]], color=color[color_id], LineWidth=car_rect_lw+1,
# linestyle='-')
# plt.plot([coll_rect[0, 0], coll_rect[3, 0]], [coll_rect[0, 1], coll_rect[3, 1]], color=color[color_id], LineWidth=car_rect_lw+1,
# linestyle='-')
#
#
# # Safe rectangle
#
# plt.plot([safe_rect[0, 0], safe_rect[1, 0]], [safe_rect[0, 1], safe_rect[1, 1]], color=color[color_id], LineWidth=car_rect_lw+1,
# linestyle='-')
# plt.plot([safe_rect[1, 0], safe_rect[2, 0]], [safe_rect[1, 1], safe_rect[2, 1]], color=color[color_id], LineWidth=car_rect_lw+1,
# linestyle='-')
# plt.plot([safe_rect[2, 0], safe_rect[3, 0]], [safe_rect[2, 1], safe_rect[3, 1]], color=color[color_id], LineWidth=car_rect_lw+1,
# linestyle='-')
# plt.plot([safe_rect[0, 0], safe_rect[3, 0]], [safe_rect[0, 1], safe_rect[3, 1]], color=color[color_id], LineWidth=car_rect_lw+1,
# linestyle='-')
# plot the disturbance set from the perspective of the AV
dist_comb = params.dist_comb
w_ext = dist_comb[3] # choosing [1 1]
W_curr = w_ext * np.array([l_car / params.W_l_car_fac, w_car / params.W_w_car_fac])
# count = 0
# for car_id in range(0, params.num_cars):
# if id != car_id:
# for i in range(0, 2):
# X_old[i, car_id] = X_old[i, car_id] + W_curr[i] * Level_ratio[(id) * (params.num_cars - 1) + count][0]
# count += 1
ego_car_id = 1 # AV id
P0 = Level_ratio[ego_car_id * (params.num_cars-1) + count, 0]
if params.sim_case == 0:
W_curr *= 0
elif params.sim_case == 1:
W_curr *= P0
dist_rect = np.array(
[[X_old[0, id] - l_car_safe / 2 * math.cos(X_old[2, id]) + w_car_safe / 2 * math.sin(X_old[2, id]) - W_curr[0],
X_old[1, id] - l_car_safe / 2 * math.sin(X_old[2, id]) - w_car_safe / 2 * math.cos(X_old[2, id]) - W_curr[1]],
[X_old[0, id] - l_car_safe / 2 * math.cos(X_old[2, id]) - w_car_safe / 2 * math.sin(X_old[2, id]) - W_curr[0],
X_old[1, id] - l_car_safe / 2 * math.sin(X_old[2, id]) + w_car_safe / 2 * math.cos(X_old[2, id]) + W_curr[1]],
[X_old[0, id] + l_car_safe / 2 * math.cos(X_old[2, id]) - w_car_safe / 2 * math.sin(X_old[2, id]) + W_curr[0],
X_old[1, id] + l_car_safe / 2 * math.sin(X_old[2, id]) + w_car_safe / 2 * math.cos(X_old[2, id]) + W_curr[1]],
[X_old[0, id] + l_car_safe / 2 * math.cos(X_old[2, id]) + w_car_safe / 2 * math.sin(X_old[2, id]) + W_curr[0],
X_old[1, id] + l_car_safe / 2 * math.sin(X_old[2, id]) - w_car_safe / 2 * math.cos(X_old[2, id]) - W_curr[1]]])
if id != ego_car_id:
# Disturbance rectangle
plt.plot([dist_rect[0, 0], dist_rect[1, 0]], [dist_rect[0, 1], dist_rect[1, 1]], color=color[color_id], LineWidth=car_rect_lw+1,
linestyle='-.')
plt.plot([dist_rect[1, 0], dist_rect[2, 0]], [dist_rect[1, 1], dist_rect[2, 1]], color=color[color_id], LineWidth=car_rect_lw+1,
linestyle='-.')
plt.plot([dist_rect[2, 0], dist_rect[3, 0]], [dist_rect[2, 1], dist_rect[3, 1]], color=color[color_id], LineWidth=car_rect_lw+1,
linestyle='-.')
plt.plot([dist_rect[0, 0], dist_rect[3, 0]], [dist_rect[0, 1], dist_rect[3, 1]], color=color[color_id], LineWidth=car_rect_lw+1,
linestyle='-.')
count += 1
#fig = plt.figure()
# Setting axes limts
ax.set_xlim(x_lim)
ax.set_ylim(y_lim)
for axis in ['top', 'bottom', 'left', 'right']:
ax.spines[axis].set_linewidth(0)
# Display Car_id
for id in range(0, len(X_old[0,:])):
ax.annotate(str(id+1), xy=(X_old[0, id], X_old[1, id]-0.5))
#ax.annotate('v='+str(X_old[3,0])+'m/s', xy=(5, -10))
#ax.annotate('v='+str(X_old[3,1])+'m/s', xy=(5, 10))
plt.yticks([])
# plt.xlabel('x (m)')
# ax.axis('off')
plt.savefig(params.outdir+'/'+params.plot_fname+str(step)+plot_format, dpi=1200)
plt.show(block=False)
plt.pause(0.001)
plt.clf()