Skip to content

Commit 60aae7f

Browse files
committed
keep implementing potentialfieldplanning
1 parent d109460 commit 60aae7f

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

PathPlanning/PotentialFieldPlanning/potential_field_planning.py

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,97 @@
77
88
"""
99

10-
# import numpy as np
10+
import numpy as np
1111
import matplotlib.pyplot as plt
1212

1313

14+
def calc_potential_field(gx, gy, reso):
15+
minx = -10.0
16+
miny = -10.0
17+
maxx = 50.0
18+
maxy = 50.0
19+
xw = round(maxx - minx)
20+
yw = round(maxy - miny)
21+
22+
pmap = [[0.0 for i in range(xw)] for i in range(yw)]
23+
24+
for ix in range(xw):
25+
x = ix * reso + minx
26+
for iy in range(yw):
27+
y = iy * reso + miny
28+
29+
ug = np.hypot(x - gx, y - gy)
30+
uo = 0.0
31+
uf = ug + uo
32+
33+
pmap[ix][iy] = uf
34+
35+
# print(pmap)
36+
37+
return pmap, minx, miny
38+
39+
40+
def get_motion_model():
41+
# dx, dy
42+
motion = [[1, 0],
43+
[0, 1],
44+
[-1, 0],
45+
[0, -1],
46+
[-1, -1],
47+
[-1, 1],
48+
[1, -1],
49+
[1, 1]]
50+
51+
return motion
52+
53+
54+
def potential_field_planning(sx, sy, gx, gy, reso):
55+
56+
# calc potential field
57+
pmap, minx, miny = calc_potential_field(gx, gy, reso)
58+
59+
# search path
60+
d = np.hypot(sx - gx, sy - gy)
61+
ix = round((sx - minx) / reso)
62+
iy = round((sy - miny) / reso)
63+
rx, ry = [sx], [sy]
64+
motion = get_motion_model()
65+
while d >= reso:
66+
# print(ix, iy)
67+
# input()
68+
minp = float("inf")
69+
minix, miniy = -1, -1
70+
for i in range(len(motion)):
71+
inx = ix + motion[i][0]
72+
iny = iy + motion[i][1]
73+
p = pmap[inx][iny]
74+
if minp > p:
75+
minp = p
76+
minix = inx
77+
miniy = iny
78+
ix = minix
79+
iy = miniy
80+
xp = ix * reso + minx
81+
yp = iy * reso + miny
82+
d = np.hypot(gx - xp, gy - yp)
83+
# print(d, xp, yp)
84+
rx.append(xp)
85+
ry.append(yp)
86+
87+
return rx, ry
88+
89+
1490
def main():
1591

1692
sx = 0.0
1793
sy = 10.0
1894
gx = 30.0
1995
gy = 30.0
96+
grid_size = 2.0 # [m]
97+
98+
rx, ry = potential_field_planning(sx, sy, gx, gy, grid_size)
99+
100+
plt.plot(rx, ry, "-r")
20101

21102
plt.plot(sx, sy, "*r")
22103
plt.plot(gx, gy, "*r")

0 commit comments

Comments
 (0)