1
- # Copyright (c) 2022-2024 , The Isaac Lab Project Developers.
1
+ # Copyright (c) 2022-2025 , The Isaac Lab Project Developers.
2
2
# All rights reserved.
3
3
#
4
4
# SPDX-License-Identifier: BSD-3-Clause
24
24
from omni .isaac .lab .app import AppLauncher , run_tests
25
25
26
26
# Can set this to False to see the GUI for debugging
27
- HEADLESS = False
27
+ HEADLESS = True
28
28
29
29
# launch omniverse app
30
30
app_launcher = AppLauncher (headless = HEADLESS , enable_cameras = True )
35
35
import torch
36
36
import unittest
37
37
38
+ import omni .usd
39
+ from pxr import Sdf
40
+
38
41
import omni .isaac .lab .envs .mdp as mdp
39
42
import omni .isaac .lab .sim as sim_utils
40
43
from omni .isaac .lab .assets import AssetBaseCfg , RigidObject , RigidObjectCfg
@@ -159,9 +162,22 @@ class MySceneCfg(InteractiveSceneCfg):
159
162
# add terrain
160
163
terrain = TerrainImporterCfg (prim_path = "/World/ground" , terrain_type = "plane" , debug_vis = False )
161
164
162
- # add cube
163
- cube : RigidObjectCfg = RigidObjectCfg (
164
- prim_path = "{ENV_REGEX_NS}/cube" ,
165
+ # add cube for scale randomization
166
+ cube1 : RigidObjectCfg = RigidObjectCfg (
167
+ prim_path = "{ENV_REGEX_NS}/cube1" ,
168
+ spawn = sim_utils .CuboidCfg (
169
+ size = (0.2 , 0.2 , 0.2 ),
170
+ rigid_props = sim_utils .RigidBodyPropertiesCfg (max_depenetration_velocity = 1.0 , disable_gravity = True ),
171
+ mass_props = sim_utils .MassPropertiesCfg (mass = 1.0 ),
172
+ physics_material = sim_utils .RigidBodyMaterialCfg (),
173
+ visual_material = sim_utils .PreviewSurfaceCfg (diffuse_color = (0.0 , 0.0 , 0.0 )),
174
+ ),
175
+ init_state = RigidObjectCfg .InitialStateCfg (pos = (0.0 , 0.0 , 5 )),
176
+ )
177
+
178
+ # add cube for static scale values
179
+ cube2 : RigidObjectCfg = RigidObjectCfg (
180
+ prim_path = "{ENV_REGEX_NS}/cube2" ,
165
181
spawn = sim_utils .CuboidCfg (
166
182
size = (0.2 , 0.2 , 0.2 ),
167
183
rigid_props = sim_utils .RigidBodyPropertiesCfg (max_depenetration_velocity = 1.0 , disable_gravity = True ),
@@ -188,7 +204,7 @@ class MySceneCfg(InteractiveSceneCfg):
188
204
class ActionsCfg :
189
205
"""Action specifications for the MDP."""
190
206
191
- joint_pos = CubeActionTermCfg (asset_name = "cube " )
207
+ joint_pos = CubeActionTermCfg (asset_name = "cube1 " )
192
208
193
209
194
210
@configclass
@@ -200,7 +216,7 @@ class PolicyCfg(ObsGroup):
200
216
"""Observations for policy group."""
201
217
202
218
# cube velocity
203
- position = ObsTerm (func = base_position , params = {"asset_cfg" : SceneEntityCfg ("cube " )})
219
+ position = ObsTerm (func = base_position , params = {"asset_cfg" : SceneEntityCfg ("cube1 " )})
204
220
205
221
def __post_init__ (self ):
206
222
self .enable_corruption = True
@@ -224,16 +240,27 @@ class EventCfg:
224
240
"y" : (- 0.5 , 0.5 ),
225
241
"z" : (- 0.5 , 0.5 ),
226
242
},
227
- "asset_cfg" : SceneEntityCfg ("cube " ),
243
+ "asset_cfg" : SceneEntityCfg ("cube1 " ),
228
244
},
229
245
)
230
246
231
- randomize_scale = EventTerm (
247
+ # Scale randomization as intended
248
+ randomize_scale1 = EventTerm (
232
249
func = mdp .randomize_scale ,
233
250
mode = "scene" ,
234
251
params = {
235
252
"scale_range" : {"x" : (0.5 , 1.5 ), "y" : (0.5 , 1.5 ), "z" : (0.5 , 1.5 )},
236
- "asset_cfg" : SceneEntityCfg ("cube" ),
253
+ "asset_cfg" : SceneEntityCfg ("cube1" ),
254
+ },
255
+ )
256
+
257
+ # Static scale values
258
+ randomize_scale2 = EventTerm (
259
+ func = mdp .randomize_scale ,
260
+ mode = "scene" ,
261
+ params = {
262
+ "scale_range" : {"x" : (1.0 , 1.0 ), "y" : (1.0 , 1.0 ), "z" : (1.0 , 1.0 )},
263
+ "asset_cfg" : SceneEntityCfg ("cube2" ),
237
264
},
238
265
)
239
266
@@ -248,7 +275,7 @@ class CubeEnvCfg(ManagerBasedEnvCfg):
248
275
"""Configuration for the locomotion velocity-tracking environment."""
249
276
250
277
# Scene settings
251
- scene : MySceneCfg = MySceneCfg (num_envs = 64 , env_spacing = 2.5 , replicate_physics = False )
278
+ scene : MySceneCfg = MySceneCfg (num_envs = 10 , env_spacing = 2.5 , replicate_physics = False )
252
279
# Basic settings
253
280
observations : ObservationsCfg = ObservationsCfg ()
254
281
actions : ActionsCfg = ActionsCfg ()
@@ -281,29 +308,46 @@ def test_scale_randomization(self):
281
308
# offset all targets so that they move to the world origin
282
309
target_position -= env .scene .env_origins
283
310
311
+ stage = omni .usd .get_context ().get_stage ()
312
+
313
+ # Test to make sure randomized values are truly random
314
+ applied_scaling_randomization = set ()
315
+ prim_paths = sim_utils .find_matching_prim_paths ("/World/envs/env_.*/cube1" )
316
+ for i in range (3 ):
317
+ prim_spec = Sdf .CreatePrimInLayer (stage .GetRootLayer (), prim_paths [i ])
318
+ scale_spec = prim_spec .GetAttributeAtPath (prim_paths [i ] + ".xformOp:scale" )
319
+ if scale_spec .default in applied_scaling_randomization :
320
+ raise ValueError (
321
+ "Detected repeat in applied scale values - indication scaling randomizatio is not working."
322
+ )
323
+ applied_scaling_randomization .add (scale_spec .default )
324
+
325
+ # Test to make sure that fixed values are assigned correctly
326
+ prim_paths = sim_utils .find_matching_prim_paths ("/World/envs/env_.*/cube2" )
327
+ for i in range (3 ):
328
+ prim_spec = Sdf .CreatePrimInLayer (stage .GetRootLayer (), prim_paths [i ])
329
+ scale_spec = prim_spec .GetAttributeAtPath (prim_paths [i ] + ".xformOp:scale" )
330
+ torch .testing .assert_close (tuple (scale_spec .default ), (1.0 , 1.0 , 1.0 ))
331
+
284
332
# simulate physics
285
333
count = 0
286
- obs , _ = env .reset ()
287
334
while simulation_app .is_running ():
288
335
with torch .inference_mode ():
289
336
# reset
290
- if count % 300 == 0 :
291
- count = 0
292
- obs , _ = env .reset ()
293
- print ("-" * 80 )
294
- print ("[INFO]: Resetting environment..." )
295
- # step env
296
- obs , _ = env .step (target_position )
297
- # print mean squared position error between target and current position
298
- error = torch .norm (obs ["policy" ] - target_position ).mean ().item ()
299
- print (f"[Step: { count :04d} ]: Mean position error: { error :.4f} " )
337
+ if count % 100 == 0 :
338
+ env .reset ()
339
+
340
+ # step the environment
341
+ env .step (target_position )
342
+
300
343
# update counter
301
344
count += 1
302
345
346
+ # After 2 iterations finish the test and close env
347
+ if count >= 200 :
348
+ env .close ()
349
+ break
350
+
303
351
304
352
if __name__ == "__main__" :
305
353
run_tests ()
306
- # # run the main function
307
- # main()
308
- # # close sim app
309
- # simulation_app.close()
0 commit comments