Unverified Commit 104805f2 authored by DJ's avatar DJ Committed by GitHub

Adds CoM randomization term to manager-based events (#1714)

# Description

Introduced CoM randomization in events.py for manager based envs.
Individual range can be selected for individual axis and bodies.

Thanks to #641 and #693  for motivation of the code.  

## Type of change

- New feature (non-breaking change which adds functionality)
- This change requires a documentation update

## Checklist

- [x] I have run the [`pre-commit` checks](https://pre-commit.com/) with
`./isaaclab.sh --format`
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have updated the changelog and the corresponding version in the
extension's `config/extension.toml` file
- [x] I have added my name to the `CONTRIBUTORS.md` or my name already
exists there
parent 6183e153
......@@ -51,6 +51,7 @@ Guidelines for modifications:
* Connor Smith
* CY (Chien-Ying) Chen
* David Yang
* Dhananjay Shendre
* Dorsa Rohani
* Felipe Mohr
* Felix Yu
......
......@@ -351,6 +351,49 @@ def randomize_rigid_body_mass(
asset.root_physx_view.set_inertias(inertias, env_ids)
def randomize_rigid_body_com(
env: ManagerBasedEnv,
env_ids: torch.Tensor | None,
com_range: dict[str, tuple[float, float]],
asset_cfg: SceneEntityCfg,
):
"""Randomize the center of mass (CoM) of rigid bodies by adding a random value sampled from the given ranges.
.. note::
This function uses CPU tensors to assign the CoM. It is recommended to use this function
only during the initialization of the environment.
"""
# extract the used quantities (to enable type-hinting)
asset: Articulation = env.scene[asset_cfg.name]
# resolve environment ids
if env_ids is None:
env_ids = torch.arange(env.scene.num_envs, device="cpu")
else:
env_ids = env_ids.cpu()
# resolve body indices
if asset_cfg.body_ids == slice(None):
body_ids = torch.arange(asset.num_bodies, dtype=torch.int, device="cpu")
else:
body_ids = torch.tensor(asset_cfg.body_ids, dtype=torch.int, device="cpu")
# sample random CoM values
range_list = [com_range.get(key, (0.0, 0.0)) for key in ["x", "y", "z"]]
ranges = torch.tensor(range_list, device="cpu")
rand_samples = math_utils.sample_uniform(
ranges[:, 0], ranges[:, 1], (len(env_ids), len(body_ids), 3), device="cpu"
).unsqueeze(1)
# get the current com of the bodies (num_assets, num_bodies)
coms = asset.root_physx_view.get_coms().clone()
# Randomize the com in range
coms[:, body_ids, :3] += rand_samples
# Set the new coms
asset.root_physx_view.set_coms(coms, env_ids)
def randomize_rigid_body_collider_offsets(
env: ManagerBasedEnv,
env_ids: torch.Tensor | None,
......
......@@ -173,6 +173,15 @@ class EventCfg:
},
)
base_com = EventTerm(
func=mdp.randomize_rigid_body_com,
mode="startup",
params={
"asset_cfg": SceneEntityCfg("robot", body_names="base"),
"com_range": {"x": (-0.05, 0.05), "y": (-0.05, 0.05), "z": (-0.01, 0.01)},
},
)
# reset
base_external_force_torque = EventTerm(
func=mdp.apply_external_force_torque,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment