-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrobots.py
564 lines (441 loc) · 19.2 KB
/
robots.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# 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.
# Standard imports
import numpy as np
import os
import math
from typing import List, Type, Tuple, Union
# Isaac Sim Imports
from omni.isaac.core.prims.xform_prim import XFormPrim
from omni.isaac.core.robots.robot import Robot as _Robot
from omni.isaac.core.articulations import ArticulationView as _ArticulationView
from omni.isaac.wheeled_robots.robots import WheeledRobot as _WheeledRobot
from omni.isaac.wheeled_robots.controllers.differential_controller import DifferentialController
from omni.isaac.examples.humanoid.h1 import H1FlatTerrainPolicy
from omni.isaac.examples.quadruped.quadruped_example import SpotFlatTerrainPolicy
import omni.isaac.core.utils.numpy.rotations as rot_utils
# Extension imports
from omni.ext.mobility_gen.common import Buffer, Module
from omni.ext.mobility_gen.sensors import Sensor, HawkCamera
from omni.ext.mobility_gen.utils.global_utils import get_stage, get_world
from omni.ext.mobility_gen.utils.stage_utils import stage_get_prim, stage_add_camera, stage_add_usd_ref
from omni.ext.mobility_gen.utils.prim_utils import prim_rotate_x, prim_rotate_y, prim_rotate_z, prim_translate
from omni.ext.mobility_gen.types import Pose2d
from omni.ext.mobility_gen.utils.registry import Registry
#=========================================================
# BASE CLASSES
#=========================================================
class Robot(Module):
"""Abstract base class for robots
This class defines an abstract base class for robots.
Robot implementations must subclass this class, define the
required class parameters and abstract methods.
The two main abstract methods subclasses must define are the build() and write_action()
methods.
Parameters:
physics_dt (float): The physics time step the robot requires (in seconds).
This may need to be modified depending on the underlying controller's
z_offset (float): A z offset to use when spawning the robot to ensure it
drops at an appropriate height when initializing.
chase_camera_base_path (str): A path (in the USD stage) relative to the
base path to use as a parent when defining the "chase" camera. Typically,
this is the same as the path to the transform used for determining
the 2D pose of the robot.
chase_camera_x_offset (str): The x offset of the chase camera. Typically this is
negative, to locate the camera behind the robot.
chase_camera_z_offset (str): The z offset of the chase camera. Typically this is
positive, to locate the camera above the robot.
chase_camera_tile_angle (float): The tilt angle of the chase camera. Typically
this does not need to be modified.
front_camera_type (Type[Sensor]): The configurable sensor class to attach
at the front camera for the robot. This should be a final sensor class (like HawkCamera)
that can be built using the class method HawkCamera.build(prim_path).
front_camera_base_path (str): The relative path (in the USD stage) relative to the
robot prim path to use as the basis for creating the front camera XForm.
front_camera_rotation (Tuple[float, float, float]): The (x, y, z) rotation to apply
when building the XForm for the front camera.
front_camera_translation (Tuple[float, float, float]): The (x, y, z) rotation to apply
when building the XForm for the front camera.
"""
physics_dt: float
z_offset: float
chase_camera_base_path: str
chase_camera_x_offset: float
chase_camera_z_offset: float
chase_camera_tilt_angle: float
occupancy_map_radius: float
occupancy_map_z_min: float
occupancy_map_z_max: float
occupancy_map_cell_size: float
occupancy_map_collision_radius: float
front_camera_type: Type[Sensor]
front_camera_base_path: str
front_camera_rotation: Tuple[float, float, float]
front_camera_translation: Tuple[float, float, float]
keyboard_linear_velocity_gain: float
keyboard_angular_velocity_gain: float
gamepad_linear_velocity_gain: float
gamepad_angular_velocity_gain: float
random_action_linear_velocity_range: Tuple[float, float]
random_action_angular_velocity_range: Tuple[float, float]
random_action_linear_acceleration_std: float
random_action_angular_acceleration_std: float
random_action_grid_pose_sampler_grid_size: float
path_following_speed: float
path_following_angular_gain: float
path_following_stop_distance_threshold: float
path_following_forward_angle_threshold = math.pi
path_following_target_point_offset_meters: float
def __init__(self,
prim_path: str,
robot: _Robot,
articulation_view: _ArticulationView,
front_camera: Sensor
):
self.prim_path = prim_path
self.robot = robot
self.articulation_view = articulation_view
self.action = Buffer(np.zeros(2))
self.position = Buffer()
self.orientation = Buffer()
self.joint_positions = Buffer()
self.joint_velocities = Buffer()
self.front_camera = front_camera
@classmethod
def build_front_camera(cls, prim_path):
# Add camera
camera_path = os.path.join(prim_path, cls.front_camera_base_path)
front_camera_xform = XFormPrim(camera_path)
stage = get_stage()
front_camera_prim = stage_get_prim(stage, camera_path)
prim_rotate_x(front_camera_prim, cls.front_camera_rotation[0])
prim_rotate_y(front_camera_prim, cls.front_camera_rotation[1])
prim_rotate_z(front_camera_prim, cls.front_camera_rotation[2])
prim_translate(front_camera_prim, cls.front_camera_translation)
return cls.front_camera_type.build(prim_path=camera_path)
def build_chase_camera(self) -> str:
stage = get_stage()
camera_path = os.path.join(self.prim_path, self.chase_camera_base_path, "chase_camera")
stage_add_camera(stage,
camera_path,
focal_length=10, horizontal_aperature=30, vertical_aperature=30
)
camera_prim = stage_get_prim(stage, camera_path)
prim_rotate_x(camera_prim, self.chase_camera_tilt_angle)
prim_rotate_y(camera_prim, 0)
prim_rotate_z(camera_prim, -90)
prim_translate(camera_prim, (self.chase_camera_x_offset, 0., self.chase_camera_z_offset))
return camera_path
@classmethod
def build(cls, prim_path: str) -> "Robot":
raise NotImplementedError
def write_action(self, step_size: float):
raise NotImplementedError
def update_state(self):
pos, ori = self.robot.get_local_pose()
self.position.set_value(pos)
self.orientation.set_value(ori)
self.joint_positions.set_value(self.robot.get_joint_positions())
self.joint_velocities.set_value(self.robot.get_joint_velocities())
super().update_state()
def write_replay_data(self):
self.robot.set_local_pose(
self.position.get_value(),
self.orientation.get_value()
)
self.articulation_view.set_joint_positions(
self.joint_positions.get_value()
)
super().write_replay_data()
def set_pose_2d(self, pose: Pose2d):
self.articulation_view.initialize()
self.robot.set_world_velocity(np.array([0., 0., 0., 0., 0., 0.]))
self.robot.post_reset()
position, orientation = self.robot.get_local_pose()
position[0] = pose.x
position[1] = pose.y
position[2] = self.z_offset
orientation = rot_utils.euler_angles_to_quats(np.array([0., 0., pose.theta]))
self.robot.set_local_pose(
position, orientation
)
def get_pose_2d(self) -> Pose2d:
position, orientation = self.robot.get_local_pose()
theta = rot_utils.quats_to_euler_angles(orientation)[2]
return Pose2d(
x=position[0],
y=position[1],
theta=theta
)
class WheeledRobot(Robot):
# Wheeled robot parameters
wheel_dof_names: List[str]
usd_url: str
chassis_subpath: str
wheel_radius: float
wheel_base: float
def __init__(self,
prim_path: str,
robot: _WheeledRobot,
articulation_view: _ArticulationView,
controller: DifferentialController,
front_camera: Sensor | None = None
):
super().__init__(
prim_path=prim_path,
robot=robot,
articulation_view=articulation_view,
front_camera=front_camera
)
self.controller = controller
self.robot = robot
@classmethod
def build(cls, prim_path: str) -> "WheeledRobot":
world = get_world()
robot = world.scene.add(_WheeledRobot(
prim_path,
wheel_dof_names=cls.wheel_dof_names,
create_robot=True,
usd_path=cls.usd_url
))
view = _ArticulationView(
os.path.join(prim_path, cls.chassis_subpath)
)
world.scene.add(view)
controller = DifferentialController(
name="controller",
wheel_radius=cls.wheel_radius,
wheel_base=cls.wheel_base
)
camera = cls.build_front_camera(prim_path)
return cls(
prim_path=prim_path,
robot=robot,
articulation_view=view,
controller=controller,
front_camera=camera
)
def write_action(self, step_size: float):
self.robot.apply_wheel_actions(
self.controller.forward(
command=self.action.get_value()
)
)
class IsaacLabRobot(Robot):
usd_url: str
articulation_path: str
def __init__(self,
prim_path: str,
robot: _Robot,
articulation_view: _ArticulationView,
controller: Union[H1FlatTerrainPolicy, SpotFlatTerrainPolicy],
front_camera: Sensor | None = None
):
super().__init__(prim_path, robot, articulation_view, front_camera)
self.controller = controller
@classmethod
def build_policy(cls, prim_path: str):
raise NotImplementedError
@classmethod
def build(cls, prim_path: str):
stage = get_stage()
world = get_world()
stage_add_usd_ref(
stage=stage,
path=prim_path,
usd_path=cls.usd_url
)
robot = _Robot(prim_path=prim_path)
world.scene.add(robot)
# Articulation
view = _ArticulationView(
os.path.join(prim_path, cls.articulation_path)
)
world.scene.add(view)
# Controller
controller = cls.build_policy(prim_path)
prim = stage_get_prim(stage, prim_path)
prim_translate(prim, (0, 0, cls.z_offset))
camera = cls.build_front_camera(prim_path)
return cls(
prim_path=prim_path,
robot=robot,
articulation_view=view,
controller=controller,
front_camera=camera
)
def write_action(self, step_size):
action = self.action.get_value()
command = np.array([action[0], 0., action[1]])
self.controller.advance(step_size, command)
def set_pose_2d(self, pose):
super().set_pose_2d(pose)
self.controller.initialize()
#=========================================================
# FINAL CLASSES
#=========================================================
ROBOTS = Registry[Robot]()
@ROBOTS.register()
class JetbotRobot(WheeledRobot):
physics_dt: float = 0.005
z_offset: float = 0.1
chase_camera_base_path = "chassis"
chase_camera_x_offset: float = -0.5
chase_camera_z_offset: float = 0.5
chase_camera_tilt_angle: float = 60.
occupancy_map_radius: float = 0.25
occupancy_map_z_min: float = 0.05
occupancy_map_z_max: float = 0.5
occupancy_map_cell_size: float = 0.05
occupancy_map_collision_radius: float = 0.25
front_camera_base_path = "chassis/rgb_camera/front_hawk"
front_camera_rotation = (0., 0., 0.)
front_camera_translation = (0., 0., 0.)
front_camera_type = HawkCamera
keyboard_linear_velocity_gain: float = 1.0
keyboard_angular_velocity_gain: float = 1.0
gamepad_linear_velocity_gain: float = 1.0
gamepad_angular_velocity_gain: float = 1.0
random_action_linear_velocity_range: Tuple[float, float] = (-0.3, 1.0)
random_action_angular_velocity_range: Tuple[float, float] = (-0.75, 0.75)
random_action_linear_acceleration_std: float = 5.0
random_action_angular_acceleration_std: float = 5.0
random_action_grid_pose_sampler_grid_size: float = 5.0
path_following_speed: float = 1.0
path_following_angular_gain: float = 1.0
path_following_stop_distance_threshold: float = 0.5
path_following_forward_angle_threshold = math.pi / 4
path_following_target_point_offset_meters: float = 1.0
wheel_dof_names: List[str] = ["left_wheel_joint", "right_wheel_joint"]
usd_url: str = "http://omniverse-content-production.s3-us-west-2.amazonaws.com/Assets/Isaac/4.2/Isaac/Robots/Jetbot/jetbot.usd"
chassis_subpath: str = "chassis"
wheel_radius: float = 0.1125
wheel_base: float = 0.03
@ROBOTS.register()
class CarterRobot(WheeledRobot):
physics_dt: float = 0.005
z_offset: float = 0.25
chase_camera_base_path = "chassis_link"
chase_camera_x_offset: float = -1.5
chase_camera_z_offset: float = 0.8
chase_camera_tilt_angle: float = 60.
occupancy_map_radius: float = 1.0
occupancy_map_z_min: float = 0.1
occupancy_map_z_max: float = 0.62
occupancy_map_cell_size: float = 0.05
occupancy_map_collision_radius: float = 0.5
front_camera_base_path = "chassis_link/front_hawk/front_hawk"
front_camera_rotation = (0., 0., 0.)
front_camera_translation = (0., 0., 0.)
front_camera_type = HawkCamera
keyboard_linear_velocity_gain: float = 1.0
keyboard_angular_velocity_gain: float = 1.0
gamepad_linear_velocity_gain: float = 1.0
gamepad_angular_velocity_gain: float = 1.0
random_action_linear_velocity_range: Tuple[float, float] = (-0.3, 1.0)
random_action_angular_velocity_range: Tuple[float, float] = (-0.75, 0.75)
random_action_linear_acceleration_std: float = 5.0
random_action_angular_acceleration_std: float = 5.0
random_action_grid_pose_sampler_grid_size: float = 5.0
path_following_speed: float = 1.0
path_following_angular_gain: float = 1.0
path_following_stop_distance_threshold: float = 0.5
path_following_forward_angle_threshold = math.pi / 4
path_following_target_point_offset_meters: float = 1.0
wheel_dof_names: List[str] = ["joint_wheel_left", "joint_wheel_right"]
usd_url: str = "http://omniverse-content-production.s3-us-west-2.amazonaws.com/Assets/Isaac/4.2/Isaac/Robots/Carter/nova_carter_sensors.usd"
chassis_subpath: str = "chassis_link"
wheel_base = 0.413
wheel_radius = 0.14
@ROBOTS.register()
class H1Robot(IsaacLabRobot):
physics_dt: float = 0.005
z_offset: float = 1.05
chase_camera_base_path = "pelvis"
chase_camera_x_offset: float = -1.5
chase_camera_z_offset: float = 0.8
chase_camera_tilt_angle: float = 60.
occupancy_map_radius: float = 1.0
occupancy_map_z_min: float = 0.1
occupancy_map_z_max: float = 2.0
occupancy_map_cell_size: float = 0.05
occupancy_map_collision_radius: float = 0.5
front_camera_base_path = "d435_left_imager_link/front_camera/front"
front_camera_rotation = (0., 250., 90.)
front_camera_translation = (-0.06, 0., 0.)
front_camera_type = HawkCamera
keyboard_linear_velocity_gain: float = 1.0
keyboard_angular_velocity_gain: float = 1.0
gamepad_linear_velocity_gain: float = 1.0
gamepad_angular_velocity_gain: float = 1.0
random_action_linear_velocity_range: Tuple[float, float] = (-0.3, 1.0)
random_action_angular_velocity_range: Tuple[float, float] = (-0.75, 0.75)
random_action_linear_acceleration_std: float = 5.0
random_action_angular_acceleration_std: float = 5.0
random_action_grid_pose_sampler_grid_size: float = 5.0
path_following_speed: float = 1.0
path_following_angular_gain: float = 1.0
path_following_stop_distance_threshold: float = 0.5
path_following_forward_angle_threshold = math.pi / 4
path_following_target_point_offset_meters: float = 1.0
usd_url = "http://omniverse-content-production.s3-us-west-2.amazonaws.com/Assets/Isaac/4.2/Isaac/Robots/Unitree/H1/h1.usd"
articulation_path = "pelvis"
controller_z_offset: float = 1.05
@classmethod
def build_policy(cls, prim_path: str):
return H1FlatTerrainPolicy(
prim_path=prim_path,
position=np.array([0., 0., cls.controller_z_offset])
)
@ROBOTS.register()
class SpotRobot(IsaacLabRobot):
physics_dt: float = 0.005
z_offset: float = 0.7
chase_camera_base_path = "body"
chase_camera_x_offset: float = -1.5
chase_camera_z_offset: float = 0.8
chase_camera_tilt_angle: float = 60.
occupancy_map_radius: float = 1.0
occupancy_map_z_min: float = 0.1
occupancy_map_z_max: float = 0.62
occupancy_map_cell_size: float = 0.05
occupancy_map_collision_radius: float = 0.5
front_camera_base_path = "body/front_camera"
front_camera_rotation = (180, 180, 180)
front_camera_translation = (0.44, 0.075, 0.01)
front_camera_type = HawkCamera
keyboard_linear_velocity_gain: float = 1.0
keyboard_angular_velocity_gain: float = 1.0
gamepad_linear_velocity_gain: float = 1.0
gamepad_angular_velocity_gain: float = 1.0
random_action_linear_velocity_range: Tuple[float, float] = (-0.3, 1.0)
random_action_angular_velocity_range: Tuple[float, float] = (-0.75, 0.75)
random_action_linear_acceleration_std: float = 5.0
random_action_angular_acceleration_std: float = 5.0
random_action_grid_pose_sampler_grid_size: float = 5.0
path_following_speed: float = 1.0
path_following_angular_gain: float = 1.0
path_following_stop_distance_threshold: float = 0.5
path_following_forward_angle_threshold = math.pi / 4
path_following_target_point_offset_meters: float = 1.0
usd_url = "http://omniverse-content-production.s3-us-west-2.amazonaws.com/Assets/Isaac/4.2/Isaac/Robots/BostonDynamics/spot/spot.usd"
articulation_path = "/"
controller_z_offset: float = 0.7
@classmethod
def build_policy(cls, prim_path: str):
return SpotFlatTerrainPolicy(
prim_path=prim_path,
position=np.array([0., 0., cls.controller_z_offset])
)