-
Notifications
You must be signed in to change notification settings - Fork 0
/
potential_field_planning.py
507 lines (438 loc) · 11.6 KB
/
potential_field_planning.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
"""
Potential Field based path planner
author: Atsushi Sakai (@Atsushi_twi)
Ref:
https://www.cs.cmu.edu/~motionplanning/lecture/Chap4-Potential-Field_howie.pdf
"""
from collections import deque
import numpy as np
import matplotlib.pyplot as plt
# Parameters
KP = 3.0 # attractive potential gain
ETA = 100.0 # repulsive potential gain
AREA_WIDTH = 10.0 # potential area width [m]
# the number of previous positions used to check oscillations
OSCILLATIONS_DETECTION_LENGTH = 3
show_animation = True
def calc_potential_field(gx, gy, ox, oy, reso, rr, sx, sy):
minx = min(min(ox), sx, gx) - AREA_WIDTH / 2.0
miny = min(min(oy), sy, gy) - AREA_WIDTH / 2.0
maxx = max(max(ox), sx, gx) + AREA_WIDTH / 2.0
maxy = max(max(oy), sy, gy) + AREA_WIDTH / 2.0
xw = int(round((maxx - minx) / reso))
yw = int(round((maxy - miny) / reso))
# calc each potential
pmap = [[0.0 for i in range(yw)] for i in range(xw)]
for ix in range(xw):
x = ix * reso + minx
for iy in range(yw):
y = iy * reso + miny
ug = calc_attractive_potential(x, y, gx, gy)
uo = calc_repulsive_potential(x, y, ox, oy, rr)
uf = ug + uo
pmap[ix][iy] = uf
return pmap, minx, miny
def calc_attractive_potential(x, y, gx, gy):
return 0.5 * KP * np.hypot(x - gx, y - gy)
def calc_repulsive_potential(x, y, ox, oy, rr):
# search nearest obstacle
minid = -1
dmin = float("inf")
for i, _ in enumerate(ox):
d = np.hypot(x - ox[i], y - oy[i])
if dmin >= d:
dmin = d
minid = i
# calc repulsive potential
dq = np.hypot(x - ox[minid], y - oy[minid])
if dq <= rr:
if dq <= 0.1:
dq = 0.1
return 0.5 * ETA * (1.0 / dq - 1.0 / rr) ** 2
else:
return 0.0
def get_motion_model():
# dx, dy
motion = [[1, 0],
[0, 1],
[-1, 0],
[0, -1],
[-1, -1],
[-1, 1],
[1, -1],
[1, 1]]
return motion
def oscillations_detection(previous_ids, ix, iy):
previous_ids.append((ix, iy))
if (len(previous_ids) > OSCILLATIONS_DETECTION_LENGTH):
previous_ids.popleft()
# check if contains any duplicates by copying into a set
previous_ids_set = set()
for index in previous_ids:
if index in previous_ids_set:
return True
else:
previous_ids_set.add(index)
return False
def potential_field_planning(sx, sy, gx, gy, ox, oy, reso, rr):
# calc potential field
pmap, minx, miny = calc_potential_field(gx, gy, ox, oy, reso, rr, sx, sy)
# search path
d = np.hypot(sx - gx, sy - gy)
ix = round((sx - minx) / reso)
iy = round((sy - miny) / reso)
gix = round((gx - minx) / reso)
giy = round((gy - miny) / reso)
if show_animation:
draw_heatmap(pmap)
# 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])
plt.plot(ix, iy, "*k")
plt.plot(gix, giy, "*m")
rx, ry = [sx], [sy]
motion = get_motion_model()
previous_ids = deque()
while d >= reso:
minp = float("inf")
minix, miniy = -1, -1
for i, _ in enumerate(motion):
inx = int(ix + motion[i][0])
iny = int(iy + motion[i][1])
if inx >= len(pmap) or iny >= len(pmap[0]) or inx < 0 or iny < 0:
p = float("inf") # outside area
print("outside potential!")
else:
p = pmap[inx][iny]
if minp > p:
minp = p
minix = inx
miniy = iny
ix = minix
iy = miniy
xp = ix * reso + minx
yp = iy * reso + miny
d = np.hypot(gx - xp, gy - yp)
rx.append(xp)
ry.append(yp)
if (oscillations_detection(previous_ids, ix, iy)):
print("Oscillation detected at ({},{})!".format(ix, iy))
break
if show_animation:
plt.plot(ix, iy, ".r")
plt.pause(0.01)
print("Goal!!")
return rx, ry
def draw_heatmap(data):
data = np.array(data).T
plt.pcolor(data, vmax=100.0, cmap=plt.cm.Blues)
def main():
print("potential_field_planning start")
sx = -40 # start x position [m]
sy = -40 # start y positon [m]
gx = 40 # goal x position [m]
gy = 40 # goal y position [m]
grid_size = 0.5 # potential grid size [m]
robot_radius = 1.0 # robot radius [m]
ox, oy = [], []
for i in range(-40, 20):
ox.append(70)
oy.append(i)
for i in range(0, 70):
ox.append(i)
oy.append(-40)
for i in range(40, 50):
ox.append(0.0)
oy.append(0 - i)
for i in range(-50, 0):
ox.append(i)
oy.append(-50)
for i in range(-50, 20):
ox.append(-50)
oy.append(i)
for i in range(-70, -50):
ox.append(i)
oy.append(20)
for i in range(20, 50):
ox.append(-70)
oy.append(i)
for i in range(-70, 60):
ox.append(i)
oy.append(50)
for i in range(20, 50):
ox.append(60)
oy.append(i)
for i in range(60, 70):
ox.append(i)
oy.append(20)
'''
# Experiment 01 Start
# people 1
for i in range(-26, -24):
ox.append(i)
oy.append(16)
for i in range(14, 16):
ox.append(-24)
oy.append(i)
for i in range(-26, -24):
ox.append(i)
oy.append(14)
for i in range(14, 16):
ox.append(-26)
oy.append(i)
# people 2
for i in range(14, 16):
ox.append(i)
oy.append(16)
for i in range(14, 16):
ox.append(16)
oy.append(i)
for i in range(14, 16):
ox.append(i)
oy.append(14)
for i in range(14, 16):
ox.append(14)
oy.append(i)
# people 3
for i in range(-36, -34):
ox.append(i)
oy.append(-29)
for i in range(-31, -29):
ox.append(-34)
oy.append(i)
for i in range(-36, -34):
ox.append(i)
oy.append(-31)
for i in range(-31, -29):
ox.append(-36)
oy.append(i)
# people 4
for i in range(19, 21):
ox.append(i)
oy.append(-29)
for i in range(-31, -29):
ox.append(21)
oy.append(i)
for i in range(19, 21):
ox.append(i)
oy.append(-31)
for i in range(-31, -29):
ox.append(19)
oy.append(i)
# setting 20x20 obstacle
for i in range(-10, 10):
ox.append(10)
oy.append(i)
for i in range(-10, 10):
ox.append(i)
oy.append(-10)
for i in range(-10, 10):
ox.append(-10)
oy.append(i)
for i in range(-10, 10):
ox.append(i)
oy.append(10)
# Experiment 01 End
'''
# Experiment 02 Start
for i in range(-10, 10):
ox.append(10)
oy.append(i)
for i in range(-10, 10):
ox.append(i)
oy.append(-10)
for i in range(-10, 10):
ox.append(-10)
oy.append(i)
for i in range(-10, 10):
ox.append(i)
oy.append(10)
for i in range(15,35):
ox.append(45)
oy.append(i)
for i in range(25,45):
ox.append(i)
oy.append(15)
for i in range(15,35):
ox.append(25)
oy.append(i)
for i in range(25,45):
ox.append(i)
oy.append(35)
for i in range(-35,-15):
ox.append(45)
oy.append(i)
for i in range(25,45):
ox.append(i)
oy.append(-35)
for i in range(-35,-15):
ox.append(25)
oy.append(i)
for i in range(25,45):
ox.append(i)
oy.append(-15)
for i in range(-35,-15):
ox.append(-25)
oy.append(i)
for i in range(-45,-25):
ox.append(i)
oy.append(-35)
for i in range(-35,-15):
ox.append(-45)
oy.append(i)
for i in range(-45,-25):
ox.append(i)
oy.append(-15)
for i in range(15,35):
ox.append(-25)
oy.append(i)
for i in range(-45,-25):
ox.append(i)
oy.append(15)
for i in range(15,35):
ox.append(-45)
oy.append(i)
for i in range(-45,-25):
ox.append(i)
oy.append(35)
# Experiment 02 End
'''
# Experiment 03 Start
for i in range(-10, 10):
ox.append(10)
oy.append(i)
for i in range(-10, 10):
ox.append(i)
oy.append(-10)
for i in range(-10, 10):
ox.append(-10)
oy.append(i)
for i in range(-10, 10):
ox.append(i)
oy.append(10)
for i in range(15,35):
ox.append(45)
oy.append(i)
for i in range(25,45):
ox.append(i)
oy.append(15)
for i in range(15,35):
ox.append(25)
oy.append(i)
for i in range(25,45):
ox.append(i)
oy.append(35)
for i in range(-35,-15):
ox.append(45)
oy.append(i)
for i in range(25,45):
ox.append(i)
oy.append(-35)
for i in range(-35,-15):
ox.append(25)
oy.append(i)
for i in range(25,45):
ox.append(i)
oy.append(-15)
for i in range(-35,-15):
ox.append(-25)
oy.append(i)
for i in range(-45,-25):
ox.append(i)
oy.append(-35)
for i in range(-35,-15):
ox.append(-45)
oy.append(i)
for i in range(-45,-25):
ox.append(i)
oy.append(-15)
for i in range(15,35):
ox.append(-25)
oy.append(i)
for i in range(-45,-25):
ox.append(i)
oy.append(15)
for i in range(15,35):
ox.append(-45)
oy.append(i)
for i in range(-45,-25):
ox.append(i)
oy.append(35)
for i in range(-10,10):
ox.append(45)
oy.append(i)
for i in range(25,45):
ox.append(i)
oy.append(-10)
for i in range(-10,10):
ox.append(25)
oy.append(i)
for i in range(25,45):
ox.append(i)
oy.append(10)
for i in range(15,35):
ox.append(10)
oy.append(i)
for i in range(-10,10):
ox.append(i)
oy.append(15)
for i in range(15,35):
ox.append(-10)
oy.append(i)
for i in range(-10,10):
ox.append(i)
oy.append(35)
for i in range(-35,-15):
ox.append(10)
oy.append(i)
for i in range(-10,10):
ox.append(i)
oy.append(-35)
for i in range(-35,-15):
ox.append(-10)
oy.append(i)
for i in range(-10,10):
ox.append(i)
oy.append(-15)
for i in range(-10,10):
ox.append(-25)
oy.append(i)
for i in range(-45,-25):
ox.append(i)
oy.append(-10)
for i in range(-10,10):
ox.append(-45)
oy.append(i)
for i in range(-45,-25):
ox.append(i)
oy.append(10)
# Experiment 03 End
# Experiment 04/05 Start
d = 4
l = 30
for i in range(10 - l, 10):
ox.append(70 - d)
oy.append(i)
for i in range(-50 + d, 70 - d):
ox.append(i)
oy.append(10 - l)
for i in range(10 - l, 10):
ox.append(-50 + d)
oy.append(i)
for i in range(-50 + d, 70 - d):
ox.append(i)
oy.append(10)
# Experiment 04/05 End
'''
if show_animation:
plt.grid(True)
plt.axis("equal")
# path generation
_, _ = potential_field_planning(
sx, sy, gx, gy, ox, oy, grid_size, robot_radius)
if show_animation:
plt.show()
if __name__ == '__main__':
print(__file__ + " start!!")
main()
print(__file__ + " Done!!")