7
7
8
8
9
9
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
+
10
21
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
+ """
11
29
self .env = irsim .make (world_file , disable_all_plot = disable_plotting )
12
30
robot_info = self .env .get_robot_info (0 )
13
31
self .robot_goal = robot_info .goal
14
32
15
33
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
+ """
16
45
self .env .step (action_id = 0 , action = np .array ([[lin_velocity ], [ang_velocity ]]))
17
46
self .env .render ()
18
47
@@ -41,6 +70,19 @@ def reset(
41
70
random_obstacles = True ,
42
71
random_obstacle_ids = None ,
43
72
):
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
+ """
44
86
if robot_state is None :
45
87
robot_state = [[random .uniform (1 , 9 )], [random .uniform (1 , 9 )], [0 ], [0 ]]
46
88
@@ -85,6 +127,16 @@ def reset(
85
127
86
128
@staticmethod
87
129
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
+ """
88
140
vec1 = vec1 / np .linalg .norm (vec1 )
89
141
vec2 = vec2 / np .linalg .norm (vec2 )
90
142
cos = np .dot (vec1 , vec2 )
@@ -93,6 +145,18 @@ def cossin(vec1, vec2):
93
145
94
146
@staticmethod
95
147
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
+ """
96
160
if goal :
97
161
return 100.0
98
162
elif collision :
0 commit comments