Skip to content

Conversation

@Creampelt
Copy link
Contributor

@Creampelt Creampelt commented Jul 15, 2025

Description

Fixes the IndexError caused by simultaneously indexing env_ids and joint_ids in reset_joints_by_scale and reset_joints_by_offset.

Fixes # 2948

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • I have run the pre-commit checks with ./isaaclab.sh --format
  • I have made corresponding changes to the documentation
  • 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
  • I have added my name to the CONTRIBUTORS.md or my name already exists there

Copy link
Collaborator

@ooctipus ooctipus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Creampelt Sorry I was my miss!

Thank you for the fix, it looks very good XD

@jhs3330395
Copy link

jhs3330395 commented Jul 24, 2025

This content has not yet been merged into the main branch.. (2025.07.24)

Signed-off-by: Kelly Guo <kellyg@nvidia.com>
@kellyguo11 kellyguo11 changed the title Fix IndexError in reset_joints_by_scale and reset_joints_by_offset Fixes IndexError in reset_joints_by_scale and reset_joints_by_offset Jul 24, 2025
Creampelt and others added 3 commits July 26, 2025 13:56
Signed-off-by: Emily Sturman <emily@sturman.org>
Add dimension to env_ids (rather than indexing twice)

Co-authored-by: Mayank Mittal <12863862+Mayankm96@users.noreply.github.com>
Signed-off-by: Emily Sturman <emily@sturman.org>
@jhs3330395
Copy link

jhs3330395 commented Jul 28, 2025

Am I using git incorrectly? Still after downloading to git clone, the main branch does not have that issue updated, please check.
now main branch`s commit is this
083ca3c (HEAD -> main, origin/main, origin/HEAD) Shifts order of docker deployment documentation (#2984)

i can`t find this commit or branch in gitlab original code

@Creampelt
Copy link
Contributor Author

@jhs3330395 this PR hasn't been merged yet -- I don't have push access, so it needs to be approved & merged by someone who does. I don't think you'll be able to see the commit in gitlab, since it is on a branch on my own fork of the repo.

@Mayankm96 let me know if there are any other edits you'd like to see; otherwise, feel free to merge whenever :)

@Creampelt Creampelt requested a review from Mayankm96 July 28, 2025 16:08
@kellyguo11
Copy link
Contributor

I can't seem to get CI to run properly :( but locally most tests are passing. @Creampelt @ooctipus have either of you tried running the unit tests locally just to be sure?

@ooctipus
Copy link
Collaborator

let me do that overnight :))

@Mayankm96
Copy link
Contributor

I went over my own implementation for the fix. Sharing it here in case we find this implementation better:

def reset_joints_by_scale(
    env: ManagerBasedEnv,
    env_ids: torch.Tensor,
    position_range: tuple[float, float],
    velocity_range: tuple[float, float],
    asset_cfg: SceneEntityCfg = SceneEntityCfg("robot"),
):
    """Reset the robot joints by scaling the default position and velocity by the given ranges.

    This function samples random values from the given ranges and scales the default joint positions and velocities
    by these values. The scaled values are then set into the physics simulation.
    """
    # extract the used quantities (to enable type-hinting)
    asset: Articulation = env.scene[asset_cfg.name]

    # cast env_ids to allow broadcasting
    if asset_cfg.joint_ids != slice(None):
        iter_env_ids = env_ids[:, None]
    else:
        iter_env_ids = env_ids

    # get default joint state
    joint_pos = asset.data.default_joint_pos[iter_env_ids, asset_cfg.joint_ids].clone()
    joint_vel = asset.data.default_joint_vel[iter_env_ids, asset_cfg.joint_ids].clone()

    # scale these values randomly
    joint_pos *= math_utils.sample_uniform(*position_range, joint_pos.shape, joint_pos.device)
    joint_vel *= math_utils.sample_uniform(*velocity_range, joint_vel.shape, joint_vel.device)

    # clamp joint pos to limits
    joint_pos_limits = asset.data.soft_joint_pos_limits[iter_env_ids, asset_cfg.joint_ids]
    joint_pos = joint_pos.clamp_(joint_pos_limits[..., 0], joint_pos_limits[..., 1])
    # clamp joint vel to limits
    joint_vel_limits = asset.data.soft_joint_vel_limits[iter_env_ids, asset_cfg.joint_ids]
    joint_vel = joint_vel.clamp_(-joint_vel_limits, joint_vel_limits)

    # set into the physics simulation
    asset.write_joint_state_to_sim(joint_pos, joint_vel, joint_ids=asset_cfg.joint_ids, env_ids=env_ids)

def reset_joints_by_offset(
    env: ManagerBasedEnv,
    env_ids: torch.Tensor,
    position_range: tuple[float, float],
    velocity_range: tuple[float, float],
    asset_cfg: SceneEntityCfg = SceneEntityCfg("robot"),
):
    """Reset the robot joints with offsets around the default position and velocity by the given ranges.

    This function samples random values from the given ranges and biases the default joint positions and velocities
    by these values. The biased values are then set into the physics simulation.
    """
    # extract the used quantities (to enable type-hinting)
    asset: Articulation = env.scene[asset_cfg.name]

    # cast env_ids to allow broadcasting
    if asset_cfg.joint_ids != slice(None):
        iter_env_ids = env_ids[:, None]
    else:
        iter_env_ids = env_ids

    # get default joint state
    joint_pos = asset.data.default_joint_pos[iter_env_ids, asset_cfg.joint_ids].clone()
    joint_vel = asset.data.default_joint_vel[iter_env_ids, asset_cfg.joint_ids].clone()

    # bias these values randomly
    joint_pos += math_utils.sample_uniform(*position_range, joint_pos.shape, joint_pos.device)
    joint_vel += math_utils.sample_uniform(*velocity_range, joint_vel.shape, joint_vel.device)

    # clamp joint pos to limits
    joint_pos_limits = asset.data.soft_joint_pos_limits[iter_env_ids, asset_cfg.joint_ids]
    joint_pos = joint_pos.clamp_(joint_pos_limits[..., 0], joint_pos_limits[..., 1])
    # clamp joint vel to limits
    joint_vel_limits = asset.data.soft_joint_vel_limits[iter_env_ids, asset_cfg.joint_ids]
    joint_vel = joint_vel.clamp_(-joint_vel_limits, joint_vel_limits)

    # set into the physics simulation
    asset.write_joint_state_to_sim(joint_pos, joint_vel, joint_ids=asset_cfg.joint_ids, env_ids=env_ids)

@ooctipus
Copy link
Collaborator

ooctipus commented Aug 4, 2025

Yeah I like it, thanks @Mayankm96
this could squeeze more performance if asset_cfg.joint_ids happens to be slice()
@Creampelt can you verify if that works

@Mayankm96
Copy link
Contributor

@ooctipus @Creampelt Could you please take a look at above so we can merge the fix soon? :)

@Creampelt
Copy link
Contributor Author

@Mayankm96 pushed and tested the edits, everything is good to go!

Signed-off-by: Emily Sturman <emily@sturman.org>
@ooctipus
Copy link
Collaborator

ooctipus commented Aug 12, 2025

taking look at this right now! one ci passed I will make sure merge it soon

Signed-off-by: ooctipus <zhengyuz@nvidia.com>
Signed-off-by: ooctipus <zhengyuz@nvidia.com>
@ooctipus ooctipus merged commit 089015f into isaac-sim:main Aug 14, 2025
StephenWelch pushed a commit to StephenWelch/IsaacLab that referenced this pull request Aug 17, 2025
…saac-sim#2949)

# Description

Fixes the IndexError caused by simultaneously indexing env_ids and
joint_ids in `reset_joints_by_scale` and `reset_joints_by_offset`.

Fixes # [2948](isaac-sim#2948)

## Type of change

<!-- As you go through the list, delete the ones that are not
applicable. -->

- Bug fix (non-breaking change which fixes an issue)

## 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
- [x] 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

---------

Signed-off-by: Kelly Guo <kellyg@nvidia.com>
Signed-off-by: Emily Sturman <emily@sturman.org>
Signed-off-by: ooctipus <zhengyuz@nvidia.com>
Co-authored-by: Kelly Guo <kellyg@nvidia.com>
Co-authored-by: Mayank Mittal <12863862+Mayankm96@users.noreply.github.com>
Co-authored-by: ooctipus <zhengyuz@nvidia.com>
george-nehma pushed a commit to george-nehma/DreamLander-IsaacLab that referenced this pull request Oct 24, 2025
…saac-sim#2949)

# Description

Fixes the IndexError caused by simultaneously indexing env_ids and
joint_ids in `reset_joints_by_scale` and `reset_joints_by_offset`.

Fixes # [2948](isaac-sim#2948)

## Type of change

<!-- As you go through the list, delete the ones that are not
applicable. -->

- Bug fix (non-breaking change which fixes an issue)

## 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
- [x] 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

---------

Signed-off-by: Kelly Guo <kellyg@nvidia.com>
Signed-off-by: Emily Sturman <emily@sturman.org>
Signed-off-by: ooctipus <zhengyuz@nvidia.com>
Co-authored-by: Kelly Guo <kellyg@nvidia.com>
Co-authored-by: Mayank Mittal <12863862+Mayankm96@users.noreply.github.com>
Co-authored-by: ooctipus <zhengyuz@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants