66
77"""
88
9- from enum import Enum
109import math
10+ from enum import Enum
1111
1212import matplotlib .pyplot as plt
1313import numpy as np
1717
1818def dwa_control (x , config , goal , ob ):
1919 """
20- Dynamic Window Approach control
20+ Dynamic Window Approach control
2121 """
2222
2323 dw = calc_dynamic_window (x , config )
@@ -26,10 +26,12 @@ def dwa_control(x, config, goal, ob):
2626
2727 return u , trajectory
2828
29+
2930class RobotType (Enum ):
3031 circle = 0
3132 rectangle = 1
3233
34+
3335class Config :
3436 """
3537 simulation parameter class
@@ -56,8 +58,8 @@ def __init__(self):
5658 self .robot_radius = 1.0 # [m] for collision check
5759
5860 # if robot_type == RobotType.rectangle
59- self .robot_width = 0.5 # [m] for collision check
60- self .robot_length = 1.2 # [m] for collision check
61+ self .robot_width = 0.5 # [m] for collision check
62+ self .robot_length = 1.2 # [m] for collision check
6163
6264 @property
6365 def robot_type (self ):
@@ -69,6 +71,7 @@ def robot_type(self, value):
6971 raise TypeError ("robot_type must be an instance of RobotType" )
7072 self ._robot_type = value
7173
74+
7275def motion (x , u , dt ):
7376 """
7477 motion model
@@ -98,7 +101,7 @@ def calc_dynamic_window(x, config):
98101 x [4 ] - config .max_dyawrate * config .dt ,
99102 x [4 ] + config .max_dyawrate * config .dt ]
100103
101- # [vmin,vmax, yawrate min, yawrate max]
104+ # [vmin,vmax, yaw_rate min, yaw_rate max]
102105 dw = [max (Vs [0 ], Vd [0 ]), min (Vs [1 ], Vd [1 ]),
103106 max (Vs [2 ], Vd [2 ]), min (Vs [3 ], Vd [3 ])]
104107
@@ -171,10 +174,10 @@ def calc_obstacle_cost(trajectory, ob, config):
171174 local_ob = local_ob .reshape (- 1 , local_ob .shape [- 1 ])
172175 local_ob = np .array ([local_ob @ - x for x in rot ])
173176 local_ob = local_ob .reshape (- 1 , local_ob .shape [- 1 ])
174- upper_check = local_ob [:, 0 ] <= config .robot_length / 2
175- right_check = local_ob [:, 1 ] <= config .robot_width / 2
176- bottom_check = local_ob [:, 0 ] >= - config .robot_length / 2
177- left_check = local_ob [:, 1 ] >= - config .robot_width / 2
177+ upper_check = local_ob [:, 0 ] <= config .robot_length / 2
178+ right_check = local_ob [:, 1 ] <= config .robot_width / 2
179+ bottom_check = local_ob [:, 0 ] >= - config .robot_length / 2
180+ left_check = local_ob [:, 1 ] >= - config .robot_width / 2
178181 if (np .logical_and (np .logical_and (upper_check , right_check ),
179182 np .logical_and (bottom_check , left_check ))).any ():
180183 return float ("Inf" )
@@ -185,6 +188,7 @@ def calc_obstacle_cost(trajectory, ob, config):
185188 min_r = np .min (r )
186189 return 1.0 / min_r # OK
187190
191+
188192def calc_to_goal_cost (trajectory , goal ):
189193 """
190194 calc to goal cost with angle difference
@@ -207,9 +211,9 @@ def plot_arrow(x, y, yaw, length=0.5, width=0.1): # pragma: no cover
207211
208212def plot_robot (x , y , yaw , config ): # pragma: no cover
209213 if config .robot_type == RobotType .rectangle :
210- outline = np .array ([[- config .robot_length / 2 , config .robot_length / 2 ,
211- (config .robot_length / 2 ), - config .robot_length / 2 ,
212- - config .robot_length / 2 ],
214+ outline = np .array ([[- config .robot_length / 2 , config .robot_length / 2 ,
215+ (config .robot_length / 2 ), - config .robot_length / 2 ,
216+ - config .robot_length / 2 ],
213217 [config .robot_width / 2 , config .robot_width / 2 ,
214218 - config .robot_width / 2 , - config .robot_width / 2 ,
215219 config .robot_width / 2 ]])
@@ -252,8 +256,7 @@ def main(gx=10.0, gy=10.0, robot_type=RobotType.circle):
252256 [13.0 , 13.0 ]
253257 ])
254258
255- # input [forward speed, yawrate]
256- u = np .array ([0.0 , 0.0 ])
259+ # input [forward speed, yaw_rate]
257260 config = Config ()
258261 config .robot_type = robot_type
259262 trajectory = np .array (x )
@@ -290,4 +293,4 @@ def main(gx=10.0, gy=10.0, robot_type=RobotType.circle):
290293
291294
292295if __name__ == '__main__' :
293- main ()
296+ main (robot_type = RobotType . rectangle )
0 commit comments