Skip to content

Commit ff3ad5b

Browse files
authored
* dwa pr * dwa_pr * dwa_pr * dwa_pr * dwa_pr * make changes rerun CI * rerun CI...again.. * rerun CI..... * rerun CI..... * rerun CI final time! * modified const to class variable * put back missing comment * add test for dwa stuck case * add test dwa stuck case * add test dwa stuck case * add test dwa stuck case * add stuck test in original test file
1 parent 3b92ae8 commit ff3ad5b

File tree

2 files changed

+62
-23
lines changed

2 files changed

+62
-23
lines changed

PathPlanning/DynamicWindowApproach/dynamic_window_approach.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def dwa_control(x, config, goal, ob):
1919
"""
2020
Dynamic Window Approach control
2121
"""
22-
2322
dw = calc_dynamic_window(x, config)
2423

2524
u, trajectory = calc_control_and_trajectory(x, dw, config, goal, ob)
@@ -51,6 +50,7 @@ def __init__(self):
5150
self.to_goal_cost_gain = 0.15
5251
self.speed_cost_gain = 1.0
5352
self.obstacle_cost_gain = 1.0
53+
self.robot_stuck_flag_cons = 0.001 # constant to prevent robot stucked
5454
self.robot_type = RobotType.circle
5555

5656
# if robot_type == RobotType.circle
@@ -60,6 +60,23 @@ def __init__(self):
6060
# if robot_type == RobotType.rectangle
6161
self.robot_width = 0.5 # [m] for collision check
6262
self.robot_length = 1.2 # [m] for collision check
63+
# obstacles [x(m) y(m), ....]
64+
self.ob = np.array([[-1, -1],
65+
[0, 2],
66+
[4.0, 2.0],
67+
[5.0, 4.0],
68+
[5.0, 5.0],
69+
[5.0, 6.0],
70+
[5.0, 9.0],
71+
[8.0, 9.0],
72+
[7.0, 9.0],
73+
[8.0, 10.0],
74+
[9.0, 11.0],
75+
[12.0, 13.0],
76+
[12.0, 12.0],
77+
[15.0, 15.0],
78+
[13.0, 13.0]
79+
])
6380

6481
@property
6582
def robot_type(self):
@@ -72,6 +89,9 @@ def robot_type(self, value):
7289
self._robot_type = value
7390

7491

92+
config = Config()
93+
94+
7595
def motion(x, u, dt):
7696
"""
7797
motion model
@@ -139,7 +159,6 @@ def calc_control_and_trajectory(x, dw, config, goal, ob):
139159
for y in np.arange(dw[2], dw[3], config.yaw_rate_resolution):
140160

141161
trajectory = predict_trajectory(x_init, v, y, config)
142-
143162
# calc cost
144163
to_goal_cost = config.to_goal_cost_gain * calc_to_goal_cost(trajectory, goal)
145164
speed_cost = config.speed_cost_gain * (config.max_speed - trajectory[-1, 3])
@@ -152,13 +171,19 @@ def calc_control_and_trajectory(x, dw, config, goal, ob):
152171
min_cost = final_cost
153172
best_u = [v, y]
154173
best_trajectory = trajectory
155-
174+
if abs(best_u[0]) < config.robot_stuck_flag_cons \
175+
and abs(x[3]) < config.robot_stuck_flag_cons:
176+
# to ensure the robot do not get stuck in
177+
# best v=0 m/s (in front of an obstacle) and
178+
# best omega=0 rad/s (heading to the goal with
179+
# angle difference of 0)
180+
best_u[1] = -config.max_delta_yaw_rate
156181
return best_u, best_trajectory
157182

158183

159184
def calc_obstacle_cost(trajectory, ob, config):
160185
"""
161-
calc obstacle cost inf: collision
186+
calc obstacle cost inf: collision
162187
"""
163188
ox = ob[:, 0]
164189
oy = ob[:, 1]
@@ -238,29 +263,12 @@ def main(gx=10.0, gy=10.0, robot_type=RobotType.circle):
238263
x = np.array([0.0, 0.0, math.pi / 8.0, 0.0, 0.0])
239264
# goal position [x(m), y(m)]
240265
goal = np.array([gx, gy])
241-
# obstacles [x(m) y(m), ....]
242-
ob = np.array([[-1, -1],
243-
[0, 2],
244-
[4.0, 2.0],
245-
[5.0, 4.0],
246-
[5.0, 5.0],
247-
[5.0, 6.0],
248-
[5.0, 9.0],
249-
[8.0, 9.0],
250-
[7.0, 9.0],
251-
[8.0, 10.0],
252-
[9.0, 11.0],
253-
[12.0, 13.0],
254-
[12.0, 12.0],
255-
[15.0, 15.0],
256-
[13.0, 13.0]
257-
])
258266

259267
# input [forward speed, yaw_rate]
260-
config = Config()
268+
261269
config.robot_type = robot_type
262270
trajectory = np.array(x)
263-
271+
ob = config.ob
264272
while True:
265273
u, predicted_trajectory = dwa_control(x, config, goal, ob)
266274
x = motion(x, u, config.dt) # simulate robot

tests/test_dynamic_window_approach.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import sys
3+
import numpy as np
34
from unittest import TestCase
45

56
sys.path.append(os.path.dirname(__file__) + "/../")
@@ -20,8 +21,38 @@ def test_main2(self):
2021
m.show_animation = False
2122
m.main(gx=1.0, gy=1.0, robot_type=m.RobotType.rectangle)
2223

24+
def test_stuck_main(self):
25+
m.show_animation = False
26+
# adjust cost
27+
m.config.to_goal_cost_gain = 0.2
28+
m.config.obstacle_cost_gain = 2.0
29+
# obstacles and goals for stuck condition
30+
m.config.ob = -1 * np.array([[-1.0, -1.0],
31+
[0.0, 2.0],
32+
[2.0, 6.0],
33+
[2.0, 8.0],
34+
[3.0, 9.27],
35+
[3.79, 9.39],
36+
[7.25, 8.97],
37+
[7.0, 2.0],
38+
[3.0, 4.0],
39+
[6.0, 5.0],
40+
[3.5, 5.8],
41+
[6.0, 9.0],
42+
[8.8, 9.0],
43+
[5.0, 9.0],
44+
[7.5, 3.0],
45+
[9.0, 8.0],
46+
[5.8, 4.4],
47+
[12.0, 12.0],
48+
[3.0, 2.0],
49+
[13.0, 13.0]
50+
])
51+
m.main(gx=-5.0, gy=-7.0)
52+
2353

2454
if __name__ == '__main__': # pragma: no cover
2555
test = TestDynamicWindowApproach()
2656
test.test_main1()
2757
test.test_main2()
58+
test.test_stuck_main()

0 commit comments

Comments
 (0)