Skip to content

Commit 72d1728

Browse files
committed
Fixed motion calculation.
To goal cost calculation is now based on heading error rather than the distance to the goal as suggested by the paper.
1 parent ad867d7 commit 72d1728

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

PathPlanning/DynamicWindowApproach/dynamic_window_approach.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ def __init__(self):
3535
def motion(x, u, dt):
3636
# motion model
3737

38+
x[2] += u[1] * dt
3839
x[0] += u[0] * math.cos(x[2]) * dt
3940
x[1] += u[0] * math.sin(x[2]) * dt
40-
x[2] += u[1] * dt
4141
x[3] = u[0]
4242
x[4] = u[1]
4343

@@ -60,7 +60,8 @@ def calc_dynamic_window(x, config):
6060
# [vmin,vmax, yawrate min, yawrate max]
6161
dw = [max(Vs[0], Vd[0]), min(Vs[1], Vd[1]),
6262
max(Vs[2], Vd[2]), min(Vs[3], Vd[3])]
63-
# print(dw)
63+
64+
#print(dw)
6465

6566
return dw
6667

@@ -97,10 +98,12 @@ def calc_final_input(x, u, dw, config, goal, ob):
9798
speed_cost = config.speed_cost_gain * \
9899
(config.max_speed - traj[-1, 3])
99100
ob_cost = calc_obstacle_cost(traj, ob, config)
100-
# print(ob_cost)
101+
#print(ob_cost)
101102

102103
final_cost = to_goal_cost + speed_cost + ob_cost
103104

105+
#print (final_cost)
106+
104107
# search minimum trajectory
105108
if min_cost >= final_cost:
106109
min_cost = final_cost
@@ -128,7 +131,7 @@ def calc_obstacle_cost(traj, ob, config):
128131

129132
r = math.sqrt(dx**2 + dy**2)
130133
if r <= config.robot_radius:
131-
return float("Inf") # collisiton
134+
return float("Inf") # collision
132135

133136
if minr >= r:
134137
minr = r
@@ -139,10 +142,12 @@ def calc_obstacle_cost(traj, ob, config):
139142
def calc_to_goal_cost(traj, goal, config):
140143
# calc to goal cost. It is 2D norm.
141144

142-
dy = goal[0] - traj[-1, 0]
143-
dx = goal[1] - traj[-1, 1]
144-
goal_dis = math.sqrt(dx**2 + dy**2)
145-
cost = config.to_goal_cost_gain * goal_dis
145+
goal_magnitude = math.sqrt(goal[0]**2 + goal[1]**2)
146+
traj_magnitude = math.sqrt(traj[-1, 0]**2 + traj[-1, 1]**2)
147+
dot_product = (goal[0]*traj[-1, 0]) + (goal[1]*traj[-1, 1])
148+
error = dot_product / (goal_magnitude*traj_magnitude)
149+
error_angle = math.acos(error)
150+
cost = config.to_goal_cost_gain * error_angle
146151

147152
return cost
148153

@@ -192,6 +197,8 @@ def main():
192197
x = motion(x, u, config.dt)
193198
traj = np.vstack((traj, x)) # store state history
194199

200+
#print(traj)
201+
195202
if show_animation:
196203
plt.cla()
197204
plt.plot(ltraj[:, 0], ltraj[:, 1], "-g")

0 commit comments

Comments
 (0)