77"""
88
99import math
10- import numpy as np
11- import matplotlib .pyplot as plt
1210
11+ import matplotlib .pyplot as plt
12+ import numpy as np
1313
1414show_animation = True
1515
1616
17- def dwa_control (x , u , config , goal , ob ):
17+ def dwa_control (x , config , goal , ob ):
1818 """
1919 Dynamic Window Approach control
2020 """
2121
2222 dw = calc_dynamic_window (x , config )
2323
24- u , traj = calc_final_input (x , u , dw , config , goal , ob )
24+ u , trajectory = calc_final_input (x , dw , config , goal , ob )
2525
26- return u , traj
26+ return u , trajectory
2727
2828
29- class Config () :
29+ class Config :
3030 """
3131 simulation parameter class
3232 """
@@ -47,6 +47,7 @@ def __init__(self):
4747 self .obstacle_cost_gain = 1.0
4848 self .robot_radius = 1.0 # [m] for collision check
4949
50+
5051def motion (x , u , dt ):
5152 """
5253 motion model
@@ -99,63 +100,62 @@ def predict_trajectory(x_init, v, y, config):
99100 return traj
100101
101102
102- def calc_final_input (x , u , dw , config , goal , ob ):
103+ def calc_final_input (x , dw , config , goal , ob ):
103104 """
104- calculation final input with dinamic window
105+ calculation final input with dynamic window
105106 """
106107
107108 x_init = x [:]
108109 min_cost = float ("inf" )
109110 best_u = [0.0 , 0.0 ]
110- best_traj = np .array ([x ])
111+ best_trajectory = np .array ([x ])
111112
112- # evalucate all trajectory with sampled input in dynamic window
113+ # evaluate all trajectory with sampled input in dynamic window
113114 for v in np .arange (dw [0 ], dw [1 ], config .v_reso ):
114115 for y in np .arange (dw [2 ], dw [3 ], config .yawrate_reso ):
115116
116- traj = predict_trajectory (x_init , v , y , config )
117+ trajectory = predict_trajectory (x_init , v , y , config )
117118
118119 # calc cost
119- to_goal_cost = config .to_goal_cost_gain * calc_to_goal_cost (traj , goal , config )
120- speed_cost = config .speed_cost_gain * \
121- (config .max_speed - traj [- 1 , 3 ])
122- ob_cost = config .obstacle_cost_gain * calc_obstacle_cost (traj , ob , config )
120+ to_goal_cost = config .to_goal_cost_gain * calc_to_goal_cost (trajectory , goal )
121+ speed_cost = config .speed_cost_gain * (config .max_speed - trajectory [- 1 , 3 ])
122+ ob_cost = config .obstacle_cost_gain * calc_obstacle_cost (trajectory , ob , config )
123123
124124 final_cost = to_goal_cost + speed_cost + ob_cost
125125
126126 # search minimum trajectory
127127 if min_cost >= final_cost :
128128 min_cost = final_cost
129129 best_u = [v , y ]
130- best_traj = traj
130+ best_trajectory = trajectory
131131
132- return best_u , best_traj
132+ return best_u , best_trajectory
133133
134134
135- def calc_obstacle_cost (traj , ob , config ):
135+ def calc_obstacle_cost (trajectory , ob , config ):
136136 """
137137 calc obstacle cost inf: collision
138138 """
139139 ox = ob [:, 0 ]
140140 oy = ob [:, 1 ]
141- dx = traj [:, 0 ] - ox [:, None ]
142- dy = traj [:, 1 ] - oy [:, None ]
141+ dx = trajectory [:, 0 ] - ox [:, None ]
142+ dy = trajectory [:, 1 ] - oy [:, None ]
143143 r = np .sqrt (np .square (dx ) + np .square (dy ))
144144 if (r <= config .robot_radius ).any ():
145145 return float ("Inf" )
146- minr = np .min (r )
147- return 1.0 / minr # OK
146+ min_r = np .min (r )
147+ return 1.0 / min_r # OK
148148
149149
150- def calc_to_goal_cost (traj , goal , config ):
150+ def calc_to_goal_cost (trajectory , goal ):
151151 """
152152 calc to goal cost with angle difference
153153 """
154154
155- dx = goal [0 ] - traj [- 1 , 0 ]
156- dy = goal [1 ] - traj [- 1 , 1 ]
155+ dx = goal [0 ] - trajectory [- 1 , 0 ]
156+ dy = goal [1 ] - trajectory [- 1 , 1 ]
157157 error_angle = math .atan2 (dy , dx )
158- cost_angle = error_angle - traj [- 1 , 2 ]
158+ cost_angle = error_angle - trajectory [- 1 , 2 ]
159159 cost = abs (math .atan2 (math .sin (cost_angle ), math .cos (cost_angle )))
160160
161161 return cost
@@ -194,16 +194,16 @@ def main(gx=10.0, gy=10.0):
194194 # input [forward speed, yawrate]
195195 u = np .array ([0.0 , 0.0 ])
196196 config = Config ()
197- traj = np .array (x )
197+ trajectory = np .array (x )
198198
199199 while True :
200- u , ptraj = dwa_control (x , u , config , goal , ob )
201- x = motion (x , u , config .dt ) # simulate robot
202- traj = np .vstack ((traj , x )) # store state history
200+ u , predicted_trajectory = dwa_control (x , config , goal , ob )
201+ x = motion (x , u , config .dt ) # simulate robot
202+ trajectory = np .vstack ((trajectory , x )) # store state history
203203
204204 if show_animation :
205205 plt .cla ()
206- plt .plot (ptraj [:, 0 ], ptraj [:, 1 ], "-g" )
206+ plt .plot (predicted_trajectory [:, 0 ], predicted_trajectory [:, 1 ], "-g" )
207207 plt .plot (x [0 ], x [1 ], "xr" )
208208 plt .plot (goal [0 ], goal [1 ], "xb" )
209209 plt .plot (ob [:, 0 ], ob [:, 1 ], "ok" )
@@ -213,14 +213,14 @@ def main(gx=10.0, gy=10.0):
213213 plt .pause (0.0001 )
214214
215215 # check reaching goal
216- dist_to_goal = math .sqrt ((x [0 ] - goal [0 ])** 2 + (x [1 ] - goal [1 ])** 2 )
216+ dist_to_goal = math .sqrt ((x [0 ] - goal [0 ]) ** 2 + (x [1 ] - goal [1 ]) ** 2 )
217217 if dist_to_goal <= config .robot_radius :
218218 print ("Goal!!" )
219219 break
220220
221221 print ("Done" )
222222 if show_animation :
223- plt .plot (traj [:, 0 ], traj [:, 1 ], "-r" )
223+ plt .plot (trajectory [:, 0 ], trajectory [:, 1 ], "-r" )
224224 plt .pause (0.0001 )
225225
226226 plt .show ()
0 commit comments