-
Notifications
You must be signed in to change notification settings - Fork 2
/
potential_field_planning.py
202 lines (160 loc) · 5.02 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
"""
@Author: Malintha Fernando
Multi robot path planning based on Artificial Potential Fields
Using Gaussian Potentials
"""
import numpy as np
import math
from numpy import linalg as la
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter
import os
import glob
OBSLEN = 5
AREA_WIDTH = 30.0 #
show_animation = True
def set_obstacle_potential(xw, yw, ox, oy, res):
pmap = [[0.0 for i in range(yw)] for i in range(xw)]
leni = int(round(OBSLEN / res))
sidelen = int(round(leni / 2))
for o in range(len(ox)):
xval = int(ox[o] / res)
yval = int(oy[o] / res)
for l in range(-sidelen, sidelen):
for w in range(-sidelen, sidelen):
pmap[w + yval][l + xval] = 10
return gaussian_filter(pmap, sigma=1)
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 calc_goal_potential(pos, goal):
return 3 * math.exp(la.norm(np.array(pos) - np.array(goal)) / 30)
def calc_static_potential_field(ox, oy, gx, gy, res):
minx = 0
miny = 0
maxx = AREA_WIDTH
maxy = AREA_WIDTH
xw = int(round((maxx - minx) / res))
yw = int(round((maxy - miny) / res))
# calc each potential
pmap = [[0.0 for i in range(yw)] for i in range(xw)]
for ix in range(xw):
x = ix * res + minx
for iy in range(yw):
y = iy * res + miny
pmap[iy][ix] = calc_goal_potential([x, y], [gx, gy])
pmap = np.array(pmap)
op = set_obstacle_potential(xw, yw, ox, oy, res)
return pmap + op, minx, miny, maxx, maxy
def get_interaction_potential(posA, posB):
a = 0.2
b = 2
ca = 8
cr = 1
dis = la.norm(np.array(posA) - np.array(posB))
att_pot = -a * math.exp(-dis / ca)
rep_pot = b * math.exp(-dis / cr)
return att_pot + rep_pot
def cal_dynamic_potential_field(curr_pos, id, xw, yw, res):
pmap = [[0.0 for i in range(yw)] for i in range(xw)]
for i in range(yw):
for j in range(xw):
for r in range(len(curr_pos)):
if r == id:
continue
pmap[j][i] += 3 * get_interaction_potential([i, j], (np.array(curr_pos[r]) / res)) + 2
return pmap
def calc_next_position(id, curr_pos, res, pmap, xvals, yvals, it):
minx = xvals[0]
maxx = xvals[1]
miny = yvals[0]
maxy = yvals[1]
xw = int(round((maxx - minx) / res))
yw = int(round((maxy - miny) / res))
pdmap = cal_dynamic_potential_field(curr_pos, id, xw, yw, res)
synthesized_map = np.array(pmap) + np.array(pdmap)
ix = round((curr_pos[id][0] - minx) / res)
iy = round((curr_pos[id][1] - miny) / res)
if show_animation:
if id == 0:
colstr = "*r"
elif id == 1:
colstr = "*g"
elif id == 3:
colstr = "*b"
elif id == 4:
colstr = "*y"
else:
colstr = "*m"
plt.plot(ix, iy, colstr)
motion = get_motion_model()
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]):
p = float("inf") # outside area
else:
p = synthesized_map[iny][inx]
if minp > p:
minp = p
minix = inx
miniy = iny
ix = minix
iy = miniy
xp = ix * res + minx
yp = iy * res + miny
curr_pos[id] = [xp, yp]
if show_animation:
plt.pause(0.0001)
if id == len(curr_pos)-1:
plt.savefig('data/im_'+str(it))
return curr_pos
def draw_heatmap(data):
data = np.array(data)
plt.pcolor(data, vmax=20.0, cmap=plt.cm.Blues)
def main():
print("potential_field_planning start")
curr_pos = [[5, 8],
[3, 5],
[8, 6],
[3, 2],
[2, 4]]
gx = 25.0 # goal x position [m]
gy = 25.0 # goal y position [m]
grid_size = 0.5 # potential grid size [m]
ox = [15] # obstacle x position list [m]
oy = [15] # obstacle y position list [m]
if show_animation:
plt.grid(True)
plt.axis("equal")
pmap, minx, miny, maxx, maxy = calc_static_potential_field(ox, oy, gx, gy, grid_size)
gix = round((gx - minx) / grid_size)
giy = round((gy - miny) / grid_size)
if show_animation:
draw_heatmap(pmap)
plt.plot(gix, giy, "-ok")
plt.gcf().canvas.mpl_connect('key_release_event',
lambda event: [exit(0) if event.key == 'escape' else None])
xvals = [minx, maxx]
yvals = [miny, maxy]
# num of iterations
for it in range(60):
for r in range(len(curr_pos)):
curr_pos = calc_next_position(r, curr_pos, grid_size, pmap, xvals, yvals, it)
if __name__ == '__main__':
files = glob.glob('./data/*')
for f in files:
os.remove(f)
print(__file__ + " start!!")
main()
print(__file__ + " Done!!")