-
Notifications
You must be signed in to change notification settings - Fork 11
/
potential_field_planning.py
234 lines (178 loc) · 6.23 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
#!/usr/bin/env python
"""
Potential Field based path planner
author: Atsushi Sakai (@Atsushi_twi)
Ref:
https://www.cs.cmu.edu/~motionplanning/lecture/Chap4-Potential-Field_howie.pdf
"""
import time
import numpy as np
# import matplotlib.pyplot as plt
from cyber_py import cyber
from modules.planning.proto.planning_pb2 import PlanningInfo
from modules.planning.proto.planning_pb2 import Trajectory
from modules.planning.proto.planning_pb2 import Point
# Parameters
KP = 5.0 # attractive potential gain
ETA = 100.0 # repulsive potential gain
AREA_WIDTH = 2.0 # potential area width [m]
show_animation = False
def calc_potential_field(gx, gy, ox, oy, reso, rr):
minx = min(ox) - AREA_WIDTH / 2.0
miny = min(oy) - AREA_WIDTH / 2.0
maxx = max(ox) + AREA_WIDTH / 2.0
maxy = max(oy) + 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 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)
# 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)
# plt.plot(ix, iy, "*k")
# plt.plot(gix, giy, "*m")
rx, ry = [sx], [sy]
motion = get_motion_model()
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]):
p = float("inf") # outside area
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)
print("d is {}, ox{},oy{}, gx{}, gy{},xp{}, yp{}".format(d, ox, oy, gx, gy, xp, yp))
rx.append(xp)
ry.append(yp)
# 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=5.0, cmap=plt.cm.Blues)
class planning(object):
def __init__(self, node):
node.create_reader("/planning/target", PlanningInfo, self.callback)
self.writer = node.create_writer("/planning/trajectory", Trajectory)
self.plan()
def callback(self, data):
#print(data)
self.data = data
def plan(self):
print(hasattr(self, 'data'))
while not cyber.is_shutdown():
print("in plan ********************************************")
if not hasattr(self, 'data'):
time.sleep(0.2)
print("no data sleep firstly")
continue
sx = self.data.start_point.x # start x position [m]
sy = self.data.start_point.y # start y positon [m]
gx = self.data.end_point.x # goal x position [m]
gy = self.data.end_point.y # goal y position [m]
grid_size = 0.051 # potential grid size [m]
robot_radius = 0.0725 # robot radius [m]
print('start point,{},{} goal point,{}, {}'.format(sx, sy, gx, gy))
ox, oy = [], []
for obstacle in self.data.obs_points:
ox.append(obstacle.x)
oy.append(obstacle.y)
print('obstacle information, ox:{}, oy:{} '.format(ox, oy))
# ox = [-0.03464780002832413] # obstacle x position list [m]
# oy = [0.6250196099281311] # obstacle y position list [m]
# if show_animation:
# plt.grid(True)
# plt.axis("equal")
# path generation
rx, ry = [], []
start = time.time()
rx, ry = potential_field_planning(
sx, sy, gx, gy, ox, oy, grid_size, robot_radius)
end = time.time()
print('time cost: {}'.format(end - start))
print('rx,{}, ry.{}'.format(rx, ry))
self.planning_path = Trajectory()
if not rx:
print("Failed to find a path")
else:
point = Point()
for i, _ in enumerate(rx):
point.x = rx[i]
point.y = ry[i]
self.planning_path.point.append(point)
print('trajectory,{}'.format(self.planning_path))
self.write_to_channel()
def write_to_channel(self):
self.writer.write(self.planning_path)
def main():
print("potential_field_planning start")
cyber.init()
planning_node = cyber.Node("planning_potential")
_ = planning(planning_node)
planning_node.spin()
cyber.shutdown()
if __name__ == '__main__':
print(__file__ + " start!!")
main()
print(__file__ + " Done!!")