Skip to content

Commit b46e60c

Browse files
committed
add sim documentation
1 parent 0ba5711 commit b46e60c

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

robot_nav/sim.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,41 @@
77

88

99
class SIM_ENV:
10+
"""
11+
A simulation environment interface for robot navigation using IRSim.
12+
13+
This class wraps around the IRSim environment and provides methods for stepping,
14+
resetting, and interacting with a mobile robot, including reward computation.
15+
16+
Attributes:
17+
env (object): The simulation environment instance from IRSim.
18+
robot_goal (np.ndarray): The goal position of the robot.
19+
"""
20+
1021
def __init__(self, world_file="robot_world.yaml", disable_plotting=False):
22+
"""
23+
Initialize the simulation environment.
24+
25+
Args:
26+
world_file (str): Path to the world configuration YAML file.
27+
disable_plotting (bool): If True, disables rendering and plotting.
28+
"""
1129
self.env = irsim.make(world_file, disable_all_plot=disable_plotting)
1230
robot_info = self.env.get_robot_info(0)
1331
self.robot_goal = robot_info.goal
1432

1533
def step(self, lin_velocity=0.0, ang_velocity=0.1):
34+
"""
35+
Perform one step in the simulation using the given control commands.
36+
37+
Args:
38+
lin_velocity (float): Linear velocity to apply to the robot.
39+
ang_velocity (float): Angular velocity to apply to the robot.
40+
41+
Returns:
42+
tuple: Contains the latest LIDAR scan, distance to goal, cosine and sine of angle to goal,
43+
collision flag, goal reached flag, applied action, and computed reward.
44+
"""
1645
self.env.step(action_id=0, action=np.array([[lin_velocity], [ang_velocity]]))
1746
self.env.render()
1847

@@ -41,6 +70,19 @@ def reset(
4170
random_obstacles=True,
4271
random_obstacle_ids=None,
4372
):
73+
"""
74+
Reset the simulation environment, optionally setting robot and obstacle states.
75+
76+
Args:
77+
robot_state (list or None): Initial state of the robot as a list of [x, y, theta, speed].
78+
robot_goal (list or None): Goal state for the robot.
79+
random_obstacles (bool): Whether to randomly reposition obstacles.
80+
random_obstacle_ids (list or None): Specific obstacle IDs to randomize.
81+
82+
Returns:
83+
tuple: Initial observation after reset, including LIDAR scan, distance, cos/sin,
84+
and reward-related flags and values.
85+
"""
4486
if robot_state is None:
4587
robot_state = [[random.uniform(1, 9)], [random.uniform(1, 9)], [0], [0]]
4688

@@ -85,6 +127,16 @@ def reset(
85127

86128
@staticmethod
87129
def cossin(vec1, vec2):
130+
"""
131+
Compute the cosine and sine of the angle between two 2D vectors.
132+
133+
Args:
134+
vec1 (list): First 2D vector.
135+
vec2 (list): Second 2D vector.
136+
137+
Returns:
138+
tuple: (cosine, sine) of the angle between the vectors.
139+
"""
88140
vec1 = vec1 / np.linalg.norm(vec1)
89141
vec2 = vec2 / np.linalg.norm(vec2)
90142
cos = np.dot(vec1, vec2)
@@ -93,6 +145,18 @@ def cossin(vec1, vec2):
93145

94146
@staticmethod
95147
def get_reward(goal, collision, action, laser_scan):
148+
"""
149+
Calculate the reward for the current step.
150+
151+
Args:
152+
goal (bool): Whether the goal has been reached.
153+
collision (bool): Whether a collision occurred.
154+
action (list): The action taken [linear velocity, angular velocity].
155+
laser_scan (list): The LIDAR scan readings.
156+
157+
Returns:
158+
float: Computed reward for the current state.
159+
"""
96160
if goal:
97161
return 100.0
98162
elif collision:

0 commit comments

Comments
 (0)