Skip to content

Commit b9bc3aa

Browse files
authored
Merge pull request AtsushiSakai#239 from goktug97/master
Optimize DWA obstacle cost calculation
2 parents a520d83 + 07f0781 commit b9bc3aa

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

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

181169

182-
def main(gx=10, gy=10):
170+
def main(gx=10.0, gy=10.0):
183171
print(__file__ + " start!!")
184172
# initial state [x(m), y(m), yaw(rad), v(m/s), omega(rad/s)]
185173
x = np.array([0.0, 0.0, math.pi / 8.0, 0.0, 0.0])
@@ -195,7 +183,12 @@ def main(gx=10, gy=10):
195183
[5.0, 9.0],
196184
[8.0, 9.0],
197185
[7.0, 9.0],
198-
[12.0, 12.0]
186+
[8.0, 10.0],
187+
[9.0, 11.0],
188+
[12.0, 13.0],
189+
[12.0, 12.0],
190+
[15.0, 15.0],
191+
[13.0, 13.0]
199192
])
200193

201194
# input [forward speed, yawrate]
@@ -205,7 +198,6 @@ def main(gx=10, gy=10):
205198

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

0 commit comments

Comments
 (0)