Skip to content

Commit bcdf088

Browse files
committed
Change robot_type comparison to enum class named RobotType
- Instead of using string based comparison for robot_type, use Enum class. - Append authors
1 parent d986041 commit bcdf088

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

PathPlanning/DynamicWindowApproach/dynamic_window_approach.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
33
Mobile robot motion planning sample with Dynamic Window Approach
44
5-
author: Atsushi Sakai (@Atsushi_twi)
5+
author: Atsushi Sakai (@Atsushi_twi), Goktug Karakasli
66
77
"""
88

9+
from enum import Enum
910
import math
1011

1112
import matplotlib.pyplot as plt
@@ -25,6 +26,9 @@ def dwa_control(x, config, goal, ob):
2526

2627
return u, trajectory
2728

29+
class RobotType(Enum):
30+
circle = 0
31+
rectangle = 1
2832

2933
class Config:
3034
"""
@@ -45,13 +49,13 @@ def __init__(self):
4549
self.to_goal_cost_gain = 0.15
4650
self.speed_cost_gain = 1.0
4751
self.obstacle_cost_gain = 1.0
48-
self.robot_type = 'circle' # circle or rectangle
52+
self.robot_type = RobotType.circle
4953

50-
# if robot_type == circle
54+
# if robot_type == RobotType.circle
5155
# Also used to check if goal is reached in both types
5256
self.robot_radius = 1.0 # [m] for collision check
5357

54-
# if robot_type == rectangle
58+
# if robot_type == RobotType.rectangle
5559
self.robot_width = 0.5 # [m] for collision check
5660
self.robot_length = 1.2 # [m] for collision check
5761

@@ -150,7 +154,7 @@ def calc_obstacle_cost(trajectory, ob, config):
150154
dy = trajectory[:, 1] - oy[:, None]
151155
r = np.sqrt(np.square(dx) + np.square(dy))
152156

153-
if config.robot_type == 'rectangle':
157+
if config.robot_type == RobotType.rectangle:
154158
yaw = trajectory[:, 2]
155159
rot = np.array([[np.cos(yaw), -np.sin(yaw)], [np.sin(yaw), np.cos(yaw)]])
156160
local_ob = ob[:, None] - trajectory[:, 0:2]
@@ -164,7 +168,7 @@ def calc_obstacle_cost(trajectory, ob, config):
164168
if (np.logical_and(np.logical_and(upper_check, right_check),
165169
np.logical_and(bottom_check, left_check))).any():
166170
return float("Inf")
167-
elif config.robot_type == 'circle':
171+
elif config.robot_type == RobotType.circle:
168172
if (r <= config.robot_radius).any():
169173
return float("Inf")
170174

@@ -192,7 +196,7 @@ def plot_arrow(x, y, yaw, length=0.5, width=0.1): # pragma: no cover
192196

193197

194198
def plot_robot(x, y, yaw, config): # pragma: no cover
195-
if config.robot_type == 'rectangle':
199+
if config.robot_type == RobotType.rectangle:
196200
outline = np.array([[-config.robot_length/2, config.robot_length/2,
197201
(config.robot_length/2), -config.robot_length/2,
198202
-config.robot_length/2],
@@ -206,15 +210,15 @@ def plot_robot(x, y, yaw, config): # pragma: no cover
206210
outline[1, :] += y
207211
plt.plot(np.array(outline[0, :]).flatten(),
208212
np.array(outline[1, :]).flatten(), "-k")
209-
elif config.robot_type == 'circle':
213+
elif config.robot_type == RobotType.circle:
210214
circle = plt.Circle((x, y), config.robot_radius, color="b")
211215
plt.gcf().gca().add_artist(circle)
212216
out_x, out_y = (np.array([x, y]) +
213217
np.array([np.cos(yaw), np.sin(yaw)]) * config.robot_radius)
214218
plt.plot([x, out_x], [y, out_y], "-k")
215219

216220

217-
def main(gx=10.0, gy=10.0, robot_type='circle'):
221+
def main(gx=10.0, gy=10.0, robot_type=RobotType.circle):
218222
print(__file__ + " start!!")
219223
# initial state [x(m), y(m), yaw(rad), v(m/s), omega(rad/s)]
220224
x = np.array([0.0, 0.0, math.pi / 8.0, 0.0, 0.0])

0 commit comments

Comments
 (0)