This repository has been archived by the owner on Dec 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adding Checkpoints.py
492 lines (384 loc) · 15.6 KB
/
Adding Checkpoints.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
"""
A* grid planning
author: Atsushi Sakai(@Atsushi_twi)
Nikos Kanargias (nkana@tee.gr)
See Wikipedia article (https://en.wikipedia.org/wiki/A*_search_algorithm)
This is the simple code for path planning class
"""
import math
import matplotlib.pyplot as plt
import numpy as np
from numpy.core.records import array
show_animation = True
Delta_F_A = 0.2
Delta_T_A = 0.2
C_T = 2
d_T = 5
C_F = 1
d_F = 1
cp_list_x = [-8.0, 9, 28, 38.0]
cp_list_y = [28.0, 26, -6, -3.0]
# cp_d_c = [0]
# fig = plt.figure()
class AStarPlanner:
def __init__(self, ox, oy, resolution, rr, fc_x, fc_y, tc_x, tc_y):
"""
Initialize grid map for a star planning
ox: x position list of Obstacles [m]
oy: y position list of Obstacles [m]
resolution: grid resolution [m]
rr: robot radius[m]
"""
self.resolution = resolution # get resolution of the grid
self.rr = rr # robot radis
self.min_x, self.min_y = 0, 0
self.max_x, self.max_y = 0, 0
self.obstacle_map = None
self.x_width, self.y_width = 0, 0
self.motion = self.get_motion_model() # motion model for grid search expansion
self.calc_obstacle_map(ox, oy)
self.fc_x = fc_x
self.fc_y = fc_y
self.tc_x = tc_x
self.tc_y = tc_y
#model
############you could modify the setup here for different aircraft models (based on the lecture slide) ##########################
self.C_F = C_F
self.Delta_F = d_F
self.C_T = C_T
self.Delta_T = d_T
self.C_C = 10
# self.Delta_F_A = 2 # additional fuel
# self.Delta_T_A = 5 # additional time
self.costPerGrid = self.C_F * self.Delta_F + self.C_T * self.Delta_T + self.C_C
# print("PolyU-A380 cost part1-> ", self.C_F * (self.Delta_F + self.Delta_F_A) )
# print("PolyU-A380 cost part2-> ", self.C_T * (self.Delta_T + self.Delta_T_A) )
# print("PolyU-A380 cost part3-> ", self.C_C )
class Node: # definition of a sinle node
def __init__(self, x, y, cost, parent_index):
self.x = x # index of grid
self.y = y # index of grid
self.cost = cost
self.parent_index = parent_index
def __str__(self):
return str(self.x) + "," + str(self.y) + "," + str(
self.cost) + "," + str(self.parent_index)
def planning(self, sx, sy, gx, gy):
"""
A star path search
input:
s_x: start x position [m]
s_y: start y position [m]
gx: goal x position [m]
gy: goal y position [m]
output:
rx: x position list of the final path
ry: y position list of the final path
"""
start_node = self.Node(self.calc_xy_index(sx, self.min_x), # calculate the index based on given position
self.calc_xy_index(sy, self.min_y), 0.0, -1) # set cost zero, set parent index -1
goal_node = self.Node(self.calc_xy_index(gx, self.min_x), # calculate the index based on given position
self.calc_xy_index(gy, self.min_y), 0.0, -1)
open_set, closed_set = dict(), dict() # open_set: node not been tranversed yet. closed_set: node have been tranversed already
open_set[self.calc_grid_index(start_node)] = start_node # node index is the grid index
while 1:
if len(open_set) == 0:
print("Open set is empty..")
break
c_id = min(
open_set,
key=lambda o: open_set[o].cost + self.calc_heuristic(self, goal_node,
open_set[o])) # g(n) and h(n): calculate the distance between the goal node and openset
current = open_set[c_id]
# show graph
if show_animation: # pragma: no cover
plt.plot(self.calc_grid_position(current.x, self.min_x),
self.calc_grid_position(current.y, self.min_y), "xc", alpha = 0.5)
# for stopping simulation with the esc key.
plt.gcf().canvas.mpl_connect('key_release_event',
lambda event: [exit(
0) if event.key == 'escape' else None])
if len(closed_set.keys()) % 10 == 0:
plt.pause(0.001)
# reaching goal
if current.x == goal_node.x and current.y == goal_node.y:
# print("Find goal with cost of -> ",current.cost )
goal_node.parent_index = current.parent_index
goal_node.cost = current.cost
break
# Remove the item from the open set
del open_set[c_id]
# Add it to the closed set
closed_set[c_id] = current
# print(len(closed_set))
# expand_grid search grid based on motion model
for i, _ in enumerate(self.motion): # tranverse the motion matrix
node = self.Node(current.x + self.motion[i][0],
current.y + self.motion[i][1],
current.cost + self.motion[i][2] * self.costPerGrid, c_id)
## add more cost in time-consuming area
if self.calc_grid_position(node.x, self.min_x) in self.tc_x:
if self.calc_grid_position(node.y, self.min_y) in self.tc_y:
# print("time consuming area!!")
node.cost = node.cost + Delta_T_A * self.motion[i][2]
# add more cost in fuel-consuming area
if self.calc_grid_position(node.x, self.min_x) in self.fc_x:
if self.calc_grid_position(node.y, self.min_y) in self.fc_y:
# print("fuel consuming area!!")
node.cost = node.cost + Delta_F_A * self.motion[i][2]
# print()
n_id = self.calc_grid_index(node)
# If the node is not safe, do nothing
if not self.verify_node(node):
continue
if n_id in closed_set:
continue
if n_id not in open_set:
open_set[n_id] = node # discovered a new node
else:
if open_set[n_id].cost > node.cost:
# This path is the best until now. record it
open_set[n_id] = node
rx, ry = self.calc_final_path(goal_node, closed_set)
# print(len(closed_set))
# print(len(open_set))
return rx, ry, current.cost
def calc_final_path(self, goal_node, closed_set):
# generate final course
rx, ry = [self.calc_grid_position(goal_node.x, self.min_x)], [
self.calc_grid_position(goal_node.y, self.min_y)] # save the goal node as the first point
parent_index = goal_node.parent_index
while parent_index != -1:
n = closed_set[parent_index]
rx.append(self.calc_grid_position(n.x, self.min_x))
ry.append(self.calc_grid_position(n.y, self.min_y))
parent_index = n.parent_index
return rx, ry
@staticmethod
def calc_heuristic(self, n1, n2):
w = 1.0 # weight of heuristic
d = w * math.hypot(n1.x - n2.x, n1.y - n2.y)
d = d * self.costPerGrid
return d
def calc_heuristic_maldis(n1, n2):
w = 1.0 # weight of heuristic
dx = w * math.abs(n1.x - n2.x)
dy = w *math.abs(n1.y - n2.y)
return dx + dy
def calc_grid_position(self, index, min_position):
"""
calc grid position
:param index:
:param min_position:
:return:
"""
pos = index * self.resolution + min_position
return pos
def calc_xy_index(self, position, min_pos):
return round((position - min_pos) / self.resolution)
def calc_grid_index(self, node):
return (node.y - self.min_y) * self.x_width + (node.x - self.min_x)
def verify_node(self, node):
px = self.calc_grid_position(node.x, self.min_x)
py = self.calc_grid_position(node.y, self.min_y)
if px < self.min_x:
return False
elif py < self.min_y:
return False
elif px >= self.max_x:
return False
elif py >= self.max_y:
return False
# collision check
if self.obstacle_map[node.x][node.y]:
return False
return True
def calc_obstacle_map(self, ox, oy):
self.min_x = round(min(ox))
self.min_y = round(min(oy))
self.max_x = round(max(ox))
self.max_y = round(max(oy))
# print("min_x:", self.min_x)
# print("min_y:", self.min_y)
# print("max_x:", self.max_x)
# print("max_y:", self.max_y)
self.x_width = round((self.max_x - self.min_x) / self.resolution)
self.y_width = round((self.max_y - self.min_y) / self.resolution)
# print("x_width:", self.x_width)
# print("y_width:", self.y_width)
# obstacle map generation
self.obstacle_map = [[False for _ in range(self.y_width)]
for _ in range(self.x_width)] # allocate memory
for ix in range(self.x_width):
x = self.calc_grid_position(ix, self.min_x) # grid position calculation (x,y)
for iy in range(self.y_width):
y = self.calc_grid_position(iy, self.min_y)
for iox, ioy in zip(ox, oy): # Python’s zip() function creates an iterator that will aggregate elements from two or more iterables.
d = math.hypot(iox - x, ioy - y) # The math. hypot() method finds the Euclidean norm
if d <= self.rr:
self.obstacle_map[ix][iy] = True # the griid is is occupied by the obstacle
break
#motion
@staticmethod
def get_motion_model(): # the cost of the surrounding 8 points
# dx, dy, cost
motion = [[1, 0, 1],
[0, 1, 1],
[-1, 0, 1],
[0, -1, 1],
[-1, -1, math.sqrt(2)],
[-1, 1, math.sqrt(2)],
[1, -1, math.sqrt(2)],
[1, 1, math.sqrt(2)]]
return motion
# def onclick(event):
# if(event.button == 1):
# global ix, iy
# ix, iy = event.xdata, event.ydata
# cp_list_x.append(ix)
# cp_list_y.append(iy)
# plt.plot(ix, iy, 'o', color = '#800080', alpha = 0.7)
# fig, ax = plt.subplots(subplot_kw={'xlim': [0,1],
# 'ylim': [0,1]})
# global cp_d_c
# cp_d_c[len(cp_list_x) - 1], = ax.plot(ix, iy, 'o', color = '#800080', alpha = 0.7) # creates a blue dot
# global coords
# coords = [ix, iy]
# return coords
# if(event.button == 3):
# cp_d_c[len(cp_list_x) - 1].remove()
# cp_list_x.pop(len(cp_list_x) - 1)
# cp_list_y.pop(len(cp_list_y) - 1)
# return coords
def check_point_planing(cp_x, cp_y, ox, oy, grid_size, robot_radius, fc_x, fc_y, tc_x, tc_y, sx, sy, gx, gy):
for i in range(0, len(cp_x)):
plt.plot(cp_x[i], cp_y[i], 'o', color = '#800080', alpha = 0.7)
cp_x = [sx] + cp_x + [gx]
cp_y = [sy] + cp_y + [gy]
rx, ry = [], []
cost = 0.0
while(len(cp_x) > 1):
a_star = AStarPlanner(ox, oy, grid_size, robot_radius, fc_x, fc_y, tc_x, tc_y)
_rx, _ry, _cost = a_star.planning(cp_x[0], cp_y[0], cp_x[1], cp_y[1])
if(show_animation):
plt.plot(_rx, _ry, "-r") # show the route
plt.pause(0.001) # pause 0.001 seconds
#plt.show() # show the plot
rx += reversed(_rx)
ry += reversed(_ry)
_rx = []
_ry = []
cost += _cost
cp_x.pop(0)
cp_y.pop(0)
return rx, ry, cost
#---------------------------------------------------------------------main---------------------------------------------------------------
def main():
print(__file__ + " start the A star algorithm demo !!") # print simple notes
# start and goal position
sx = 0.0 # [m]
sy = 50.0 # [m]
gx = 50.0 # [m]
gy = -5.0 # [m]
grid_size = 1 # [m]
robot_radius = 1.0 # [m]
# set obstacle positions for group 8
# ox, oy = [], []
# for i in range(-10, 60): # draw the button border
# ox.append(i)
# oy.append(-10.0)
# for i in range(-10, 60):
# ox.append(60.0)
# oy.append(i)
# for i in range(-10, 61):
# ox.append(i)
# oy.append(60.0)
# for i in range(-10, 61):
# ox.append(-10.0)
# oy.append(i)
# for i in range(-10, 40):
# ox.append(20.0)
# oy.append(i)
# for i in range(0, 40):
# ox.append(40.0)
# oy.append(60.0 - i)
# set fuel consuming area
fc_x, fc_y = [], []
for i in range(-10, 10):
for j in range(20, 30):
fc_x.append(i)
fc_y.append(j)
# set time consuming area
tc_x, tc_y = [], []
for i in range(25, 40):
for j in range(-10, 10):
tc_x.append(i)
tc_y.append(j)
# set obstacle positions for group 3
ox, oy = [], []
for i in range(-10, 61): # draw the button border
ox.append(i)
oy.append(-10.0)
for i in range(-10, 61): # draw the right border
ox.append(60.0)
oy.append(i)
for i in range(-10, 61): # draw the top border
ox.append(i)
oy.append(60.0)
for i in range(-10, 61): # draw the left border
ox.append(-10.0)
oy.append(i)
#Start_draw boarder
for i in range(0, 41): # draw the free border
ox.append(i)
oy.append(20.0 + i)
for i in range(-10, 31):
ox.append(25)
oy.append(i)
for i in range(0, 51):
ox.append(40)
oy.append(i)
#End_draw boarder
# for i in range(40, 45): # draw the button border
# ox.append(i)
# oy.append(30.0)
global C_T
global d_T
global C_F
global d_F
global Delta_F_A
global Delta_T_A
# global show_animation
cost = []
min_cost = 3.4E38
cost_temp = 0.0
# show_animation = False
if True: # pragma: no cover
plt.plot(ox, oy, ".k") # plot the obstacle
plt.plot(sx, sy, "og") # plot the start position
plt.plot(gx, gy, "xb") # plot the end position
plt.plot(fc_x, fc_y, "oy", alpha=0.5) # plot the fuel consuming area
plt.plot(tc_x, tc_y, "or", alpha=0.3) # plot the time consuming area
plt.grid(True) # plot the grid to the plot panel
plt.axis("equal") # set the same resolution for x and y axis
# plt.title("Left click to select the checkpoint\nRight click to revoke the lastest point")
# plt.legend(bbox_to_anchor=(0, 1))
# for i in range(0,1):
# cid = fig.canvas.mpl_connect('button_press_event', onclick)
# plt.show()
# x=range(1,10)
# y=[2*v for v in x]
# print(x, y)
# plt.plot(x, y)
# pos=plt.ginput(3)
# print(pos)
# plt.show()
rx, ry, cost = check_point_planing(cp_list_x, cp_list_y, ox, oy, grid_size, robot_radius, fc_x, fc_y, tc_x, tc_y, sx, sy, gx, gy)
if True: # pragma: no cover
plt.plot(rx, ry, "-r", label = "Cost: {0}".format(cost)) # show the route
plt.legend(loc='upper left', bbox_to_anchor=(0, 1))
plt.pause(0.001) # pause 0.001 seconds
plt.show() # show the plot
return 0
if __name__ == '__main__':
main()