Skip to content

Commit 8fb4b8a

Browse files
committed
optimize obstacle cost calculation
The obstacle calculation was too slow to work with more obstacles, it is vectorized to speed up the calculation.
1 parent b7f6c1c commit 8fb4b8a

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

PathPlanning/DynamicWindowApproach/dynamic_window_approach.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ def __init__(self):
4747
self.obstacle_cost_gain = 1.0
4848
self.robot_radius = 1.0 # [m] for collision check
4949

50-
51-
5250
def motion(x, u, dt):
5351
"""
5452
motion model
@@ -138,24 +136,14 @@ def calc_obstacle_cost(traj, ob, config):
138136
"""
139137
calc obstacle cost inf: collision
140138
"""
141-
142-
skip_n = 2 # for speed up
143-
minr = float("inf")
144-
145-
for ii in range(0, len(traj[:, 1]), skip_n):
146-
for i in range(len(ob[:, 0])):
147-
ox = ob[i, 0]
148-
oy = ob[i, 1]
149-
dx = traj[ii, 0] - ox
150-
dy = traj[ii, 1] - oy
151-
152-
r = math.sqrt(dx**2 + dy**2)
153-
if r <= config.robot_radius:
154-
return float("Inf") # collision
155-
156-
if minr >= r:
157-
minr = r
158-
139+
ox = ob[:, 0]
140+
oy = ob[:, 1]
141+
dx = traj[:, 0] - ox[:, None]
142+
dy = traj[:, 1] - oy[:, None]
143+
r = np.sqrt(np.square(dx) + np.square(dy))
144+
if (r <= config.robot_radius).any():
145+
return float("Inf")
146+
minr = np.min(r)
159147
return 1.0 / minr # OK
160148

161149

@@ -178,7 +166,7 @@ def plot_arrow(x, y, yaw, length=0.5, width=0.1): # pragma: no cover
178166
plt.plot(x, y)
179167

180168

181-
def main(gx=10, gy=10):
169+
def main(gx=10.0, gy=10.0):
182170
print(__file__ + " start!!")
183171
# initial state [x(m), y(m), yaw(rad), v(m/s), omega(rad/s)]
184172
x = np.array([0.0, 0.0, math.pi / 8.0, 0.0, 0.0])
@@ -194,7 +182,12 @@ def main(gx=10, gy=10):
194182
[5.0, 9.0],
195183
[8.0, 9.0],
196184
[7.0, 9.0],
197-
[12.0, 12.0]
185+
[8.0, 10.0],
186+
[9.0, 11.0],
187+
[12.0, 13.0],
188+
[12.0, 12.0],
189+
[15.0, 15.0],
190+
[13.0, 13.0]
198191
])
199192

200193
# input [forward speed, yawrate]
@@ -204,7 +197,6 @@ def main(gx=10, gy=10):
204197

205198
while True:
206199
u, ptraj = dwa_control(x, u, config, goal, ob)
207-
208200
x = motion(x, u, config.dt) # simulate robot
209201
traj = np.vstack((traj, x)) # store state history
210202

0 commit comments

Comments
 (0)