@@ -300,54 +300,53 @@ def randomize_actuator_gains(
300300 .. tip::
301301 For implicit actuators, this function uses CPU tensors to assign the actuator gains into the simulation.
302302 In such cases, it is recommended to use this function only during the initialization of the environment.
303-
304- Raises:
305- NotImplementedError: If the joint indices are in explicit motor mode. This operation is currently
306- not supported for explicit actuator models.
307303 """
308- # extract the used quantities (to enable type-hinting)
304+ # Extract the used quantities (to enable type-hinting)
309305 asset : Articulation = env .scene [asset_cfg .name ]
310306
311- # resolve environment ids
307+ # Resolve environment ids
312308 if env_ids is None :
313309 env_ids = torch .arange (env .scene .num_envs , device = asset .device )
314310
315- # resolve joint indices
316- if asset_cfg .joint_ids == slice (None ):
317- joint_ids_list = range (asset .num_joints )
318- joint_ids = slice (None ) # for optimization purposes
319- else :
320- joint_ids_list = asset_cfg .joint_ids
321- joint_ids = torch .tensor (asset_cfg .joint_ids , dtype = torch .int , device = asset .device )
311+ joint_ids_list = list (range (asset .num_joints )) if isinstance (asset_cfg .joint_ids , slice ) else asset_cfg .joint_ids
322312
323- # check if none of the joint indices are in explicit motor mode
324- for joint_index in joint_ids_list :
325- for act_name , actuator in asset .actuators .items ():
326- # if joint indices are a slice (i.e., all joints are captured) or the joint index is in the actuator
327- if actuator .joint_indices == slice (None ) or joint_index in actuator .joint_indices :
328- if not isinstance (actuator , ImplicitActuator ):
329- raise NotImplementedError (
330- "Event term 'randomize_actuator_stiffness_and_damping' is performed on asset"
331- f" '{ asset_cfg .name } ' on the joint '{ asset .joint_names [joint_index ]} ' ('{ joint_index } ') which"
332- f" uses an explicit actuator model '{ act_name } <{ actuator .__class__ .__name__ } >'. This operation"
333- " is currently not supported for explicit actuator models."
334- )
313+ def randomize (data : torch .Tensor , params : tuple [float , float ]) -> torch .Tensor :
314+ return _randomize_prop_by_op (
315+ data , params , env_ids , target_joint_indices , operation = operation , distribution = distribution
316+ )
335317
336- # sample joint properties from the given ranges and set into the physics simulation
337- # -- stiffness
338- if stiffness_distribution_params is not None :
339- stiffness = asset .data .default_joint_stiffness .to (asset .device ).clone ()
340- stiffness = _randomize_prop_by_op (
341- stiffness , stiffness_distribution_params , env_ids , joint_ids , operation = operation , distribution = distribution
342- )[env_ids ][:, joint_ids ]
343- asset .write_joint_stiffness_to_sim (stiffness , joint_ids = joint_ids , env_ids = env_ids )
344- # -- damping
345- if damping_distribution_params is not None :
346- damping = asset .data .default_joint_damping .to (asset .device ).clone ()
347- damping = _randomize_prop_by_op (
348- damping , damping_distribution_params , env_ids , joint_ids , operation = operation , distribution = distribution
349- )[env_ids ][:, joint_ids ]
350- asset .write_joint_damping_to_sim (damping , joint_ids = joint_ids , env_ids = env_ids )
318+ # Loop through actuators and randomize gains
319+ for actuator in asset .actuators .values ():
320+ # Resolve joint indices for each actuator
321+ # Only target the joints specified by the asset_cfg.joint_ids (joint_ids_list)
322+ if isinstance (actuator .joint_indices , slice ): # All joints of the articulation are this actuator type
323+ target_joint_indices = torch .tensor (joint_ids_list , dtype = torch .int , device = asset .device )
324+ else :
325+ target_joint_indices = torch .tensor (
326+ list (set (actuator .joint_indices ).intersection (joint_ids_list )), dtype = torch .int , device = asset .device
327+ )
328+ if not target_joint_indices .nbytes : # Skip when there are no joints
329+ continue
330+
331+ all_envs = torch .arange (env .scene .num_envs , device = asset .device )
332+ # Randomize stiffness
333+ if stiffness_distribution_params is not None :
334+ stiffness = asset .data .default_joint_stiffness .to (asset .device ).clone ()
335+ randomize (stiffness , stiffness_distribution_params )
336+ actuator .stiffness = stiffness [all_envs [:, None ], actuator .joint_indices ]
337+ if isinstance (actuator , ImplicitActuator ):
338+ asset .write_joint_stiffness_to_sim (
339+ stiffness [env_ids ][:, target_joint_indices ], joint_ids = target_joint_indices , env_ids = env_ids
340+ )
341+ # Randomize damping
342+ if damping_distribution_params is not None :
343+ damping = asset .data .default_joint_damping .to (asset .device ).clone ()
344+ randomize (damping , damping_distribution_params )
345+ actuator .damping = damping [all_envs [:, None ], actuator .joint_indices ]
346+ if isinstance (actuator , ImplicitActuator ):
347+ asset .write_joint_damping_to_sim (
348+ damping [env_ids ][:, target_joint_indices ], joint_ids = target_joint_indices , env_ids = env_ids
349+ )
351350
352351
353352def randomize_joint_parameters (
@@ -383,7 +382,7 @@ def randomize_joint_parameters(
383382
384383 # resolve joint indices
385384 if asset_cfg .joint_ids == slice (None ):
386- joint_ids = slice ( None ) # for optimization purposes
385+ joint_ids = torch . arange ( asset . num_joints , dtype = torch . int , device = asset . device )
387386 else :
388387 joint_ids = torch .tensor (asset_cfg .joint_ids , dtype = torch .int , device = asset .device )
389388
0 commit comments