@@ -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+
7595def 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
159184def 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
0 commit comments