forked from NVlabs/MobilityGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscenarios.py
250 lines (182 loc) · 8.53 KB
/
scenarios.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
from typing import Tuple
from mobility_gen_path_planner import generate_paths
from omni.ext.mobility_gen.utils.path_utils import PathHelper, vector_angle
from omni.ext.mobility_gen.utils.registry import Registry
from omni.ext.mobility_gen.common import Module, Buffer
from omni.ext.mobility_gen.robots import Robot
from omni.ext.mobility_gen.occupancy_map import OccupancyMap
import omni.ext.mobility_gen.pose_samplers as pose_samplers
import omni.ext.mobility_gen.inputs as inputs
class Scenario(Module):
def __init__(self,
robot: Robot,
occupancy_map: OccupancyMap
):
self.robot = robot
self.occupancy_map = occupancy_map
self.buffered_occupancy_map = occupancy_map.buffered_meters(self.robot.occupancy_map_radius)
@classmethod
def from_robot_occupancy_map(cls, robot: Robot, occupancy_map: OccupancyMap):
return cls(robot, occupancy_map)
def reset(self):
raise NotImplementedError
def step(self, step_size: float) -> bool:
raise NotImplementedError
SCENARIOS = Registry[Scenario]()
@SCENARIOS.register()
class KeyboardTeleoperationScenario(Scenario):
def __init__(self,
robot: Robot,
occupancy_map: OccupancyMap
):
super().__init__(robot, occupancy_map)
self.keyboard = inputs.Keyboard()
self.pose_sampler = pose_samplers.UniformPoseSampler()
def reset(self):
pose = self.pose_sampler.sample(self.buffered_occupancy_map)
self.robot.set_pose_2d(pose)
def step(self, step_size):
self.update_state()
buttons = self.keyboard.buttons.get_value()
w_val = float(buttons[0])
a_val = float(buttons[1])
s_val = float(buttons[2])
d_val = float(buttons[3])
linear_velocity = (w_val - s_val) * self.robot.keyboard_linear_velocity_gain
angular_velocity = (a_val - d_val) * self.robot.keyboard_angular_velocity_gain
self.robot.action.set_value(np.array([linear_velocity, angular_velocity]))
self.robot.write_action(step_size)
self.update_state()
return True
@SCENARIOS.register()
class GamepadTeleoperationScenario(Scenario):
def __init__(self,
robot: Robot,
occupancy_map: OccupancyMap
):
super().__init__(robot, occupancy_map)
self.gamepad = inputs.Gamepad()
self.pose_sampler = pose_samplers.UniformPoseSampler()
def reset(self):
pose = self.pose_sampler.sample(self.buffered_occupancy_map)
self.robot.set_pose_2d(pose)
def step(self, step_size: float):
self.gamepad.update_state()
axes = self.gamepad.axes.get_value()
linear_velocity = axes[0] * self.robot.gamepad_linear_velocity_gain
angular_velocity = axes[3] * self.robot.gamepad_angular_velocity_gain
self.robot.action.set_value(np.array([linear_velocity, angular_velocity]))
self.robot.write_action(step_size)
self.update_state()
return True
@SCENARIOS.register()
class RandomAccelerationScenario(Scenario):
def __init__(self,
robot: Robot,
occupancy_map: OccupancyMap
):
super().__init__(robot, occupancy_map)
self.pose_sampler = pose_samplers.GridPoseSampler(robot.random_action_grid_pose_sampler_grid_size)
self.is_alive = True
self.collision_occupancy_map = occupancy_map.buffered(robot.occupancy_map_collision_radius)
def reset(self):
self.robot.action.set_value(np.zeros(2))
pose = self.pose_sampler.sample(self.buffered_occupancy_map)
self.robot.set_pose_2d(pose)
self.is_alive = True
def step(self, step_size: float):
self.update_state()
current_action = self.robot.action.get_value()
linear_velocity = current_action[0] + step_size * np.random.randn(1) * self.robot.random_action_linear_acceleration_std
angular_velocity = current_action[1] + step_size * np.random.randn(1) * self.robot.random_action_angular_acceleration_std
linear_velocity = np.clip(linear_velocity, *self.robot.random_action_linear_velocity_range)[0]
angular_velocity = np.clip(angular_velocity, *self.robot.random_action_angular_velocity_range)[0]
self.robot.action.set_value(np.array([linear_velocity, angular_velocity]))
self.robot.write_action(step_size)
self.update_state()
# Check out of bounds or collision
pose = self.robot.get_pose_2d()
if not self.collision_occupancy_map.check_world_point_in_bounds(pose):
self.is_alive = False
elif not self.collision_occupancy_map.check_world_point_in_freespace(pose):
self.is_alive = False
return self.is_alive
@SCENARIOS.register()
class RandomPathFollowingScenario(Scenario):
def __init__(self,
robot: Robot,
occupancy_map: OccupancyMap
):
super().__init__(robot, occupancy_map)
self.pose_sampler = pose_samplers.UniformPoseSampler()
self.is_alive = True
self.target_path = Buffer()
self.collision_occupancy_map = occupancy_map.buffered(robot.occupancy_map_collision_radius)
def set_random_target_path(self):
current_pose = self.robot.get_pose_2d()
start_px = self.occupancy_map.world_to_pixel_numpy(np.array([
[current_pose.x, current_pose.y]
]))
freespace = self.buffered_occupancy_map.freespace_mask()
start = (start_px[0, 1], start_px[0, 0])
output = generate_paths(start, freespace)
end = output.sample_random_end_point()
path = output.unroll_path(end)
path = path[:, ::-1] # y,x -> x,y coordinates
path = self.occupancy_map.pixel_to_world_numpy(path)
self.target_path.set_value(path)
self.target_path_helper = PathHelper(path)
def reset(self):
self.robot.action.set_value(np.zeros(2))
pose = self.pose_sampler.sample(self.buffered_occupancy_map)
self.robot.set_pose_2d(pose)
self.set_random_target_path()
self.is_alive = True
def step(self, step_size: float):
self.update_state()
target_path = self.target_path.get_value()
current_pose = self.robot.get_pose_2d()
if not self.collision_occupancy_map.check_world_point_in_bounds(current_pose):
self.is_alive = False
return self.is_alive
elif not self.collision_occupancy_map.check_world_point_in_freespace(current_pose):
self.is_alive = False
return self.is_alive
pt_robot = np.array([current_pose.x, current_pose.y])
pt_path, pt_path_length, _, _ = self.target_path_helper.find_nearest(pt_robot)
pt_target = self.target_path_helper.get_point_by_distance(distance=
pt_path_length + self.robot.path_following_target_point_offset_meters
)
path_end = target_path[-1]
dist_to_target = np.sqrt(np.sum((pt_robot - path_end)**2))
if dist_to_target < self.robot.path_following_stop_distance_threshold:
self.set_random_target_path()
else:
vec_robot_unit = np.array([np.cos(current_pose.theta), np.sin(current_pose.theta)])
vec_target = (pt_target - pt_robot)
vec_target_unit = vec_target / np.sqrt(np.sum(vec_target**2))
d_theta = vector_angle(vec_robot_unit, vec_target_unit)
if abs(d_theta) > self.robot.path_following_forward_angle_threshold:
linear_velocity = 0.
else:
linear_velocity = self.robot.path_following_speed
angular_gain: float = self.robot.path_following_angular_gain
angular_velocity = - angular_gain * d_theta
self.robot.action.set_value(np.array([linear_velocity, angular_velocity]))
self.robot.write_action(step_size=step_size)
return self.is_alive