Unverified Commit 915ebdd6 authored by Mayank Mittal's avatar Mayank Mittal Committed by GitHub

Reads gravity direction from simulation inside `RigidObjectData` (#582)

# Description

Previously, the gravity direction inside the `RigidObjectData` class was
hard-coded as (0, 0, -1). However, this quantity may have a different
direction in some simulation situations. This MR removes this hard
coding and directly reads the value from the simulation view.

## Type of change

- Bug fix (non-breaking change which fixes an issue)
- New feature (non-breaking change which adds functionality)

## Checklist

- [x] I have run the [`pre-commit` checks](https://pre-commit.com/) with
`./isaaclab.sh --format`
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have run all the tests with `./isaaclab.sh --test` and they pass
- [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
parent 1ba9db69
[package]
# Note: Semantic Versioning is used: https://semver.org/
version = "0.18.4"
version = "0.18.5"
# Description
title = "Isaac Lab framework for Robot Learning"
......
Changelog
---------
0.18.5 (2024-06-26)
~~~~~~~~~~~~~~~~~~~
Fixed
^^^^^
* Fixed the gravity vector direction used inside the :class:`omni.isaac.lab.assets.RigidObjectData`class.
Earlier, the gravity direction was hard-coded as (0, 0, -1) which may be different from the actual
gravity direction in the simulation. Now, the gravity direction is obtained from the simulation context
and used to compute the projection of the gravity vector on the object.
0.18.4 (2024-06-26)
~~~~~~~~~~~~~~~~~~~
......
......@@ -45,8 +45,16 @@ class RigidObjectData:
# Set initial time stamp
self._sim_timestamp = 0.0
# Obtain global physics sim view
physics_sim_view = physx.create_simulation_view("torch")
physics_sim_view.set_subspace_roots("/")
gravity = physics_sim_view.get_gravity()
# Convert to direction vector
gravity_dir = torch.tensor((gravity[0], gravity[1], gravity[2]), device=self.device)
gravity_dir = math_utils.normalize(gravity_dir.unsqueeze(0)).squeeze(0)
# Initialize constants
self.GRAVITY_VEC_W = torch.tensor((0.0, 0.0, -1.0), device=self.device).repeat(self._root_physx_view.count, 1)
self.GRAVITY_VEC_W = gravity_dir.repeat(self._root_physx_view.count, 1)
self.FORWARD_VEC_B = torch.tensor((1.0, 0.0, 0.0), device=self.device).repeat(self._root_physx_view.count, 1)
# Initialize buffers for finite differencing
......@@ -121,12 +129,7 @@ class RigidObjectData:
@property
def projected_gravity_b(self):
"""Projection of the gravity direction on base frame. Shape is (num_instances, 3).
Note:
This quantity is computed by assuming that the gravity direction is along the z-direction,
i.e. :math:`(0, 0, -1)`.
"""
"""Projection of the gravity direction on base frame. Shape is (num_instances, 3)."""
return math_utils.quat_rotate_inverse(self.root_quat_w, self.GRAVITY_VEC_W)
@property
......
......@@ -19,6 +19,7 @@ import warnings
from typing import TYPE_CHECKING, Literal
import carb
import omni.physics.tensors.impl.api as physx
import omni.isaac.lab.sim as sim_utils
import omni.isaac.lab.utils.math as math_utils
......@@ -206,7 +207,12 @@ def randomize_physics_scene_gravity(
"""Randomize gravity by adding, scaling, or setting random values.
This function allows randomizing gravity of the physics scene. The function samples random values from the
given distribution parameters and adds, scales, or sets the values into the physics simulation based on the operation.
given distribution parameters and adds, scales, or sets the values into the physics simulation based on the
operation.
The distribution parameters are lists of two elements each, representing the lower and upper bounds of the
distribution for the x, y, and z components of the gravity vector. The function samples random values for each
component independently.
.. attention::
This function applied the same gravity for all the environments.
......@@ -225,9 +231,13 @@ def randomize_physics_scene_gravity(
slice(None),
operation=operation,
distribution=distribution,
)[0]
)
# unbatch the gravity tensor into a list
gravity = gravity[0].tolist()
# set the gravity into the physics simulation
sim_utils.SimulationContext.instance().physics_sim_view.set_gravity(carb.Float3(gravity[0], gravity[1], gravity[2]))
physics_sim_view: physx.SimulationView = sim_utils.SimulationContext.instance().physics_sim_view
physics_sim_view.set_gravity(carb.Float3(*gravity))
def randomize_actuator_gains(
......@@ -841,7 +851,7 @@ Internal helper functions.
def _randomize_prop_by_op(
data: torch.Tensor,
distribution_parameters: tuple[float, float],
distribution_parameters: tuple[float | torch.Tensor, float | torch.Tensor],
dim_0_ids: torch.Tensor | None,
dim_1_ids: torch.Tensor | slice,
operation: Literal["add", "scale", "abs"],
......
......@@ -149,7 +149,7 @@ class TestRigidObject(unittest.TestCase):
# check that the object is kinematic
default_root_state = cube_object.data.default_root_state.clone()
default_root_state[:, :3] += origins
torch.testing.assert_allclose(cube_object.data.root_state_w, default_root_state)
torch.testing.assert_close(cube_object.data.root_state_w, default_root_state)
def test_initialization_with_no_rigid_body(self):
"""Test that initialization fails when no rigid body is found at the provided prim path."""
......@@ -627,6 +627,36 @@ class TestRigidObject(unittest.TestCase):
# Check if mass is set correctly
torch.testing.assert_close(masses, masses_to_check)
def test_gravity_vec_w(self):
"""Test that gravity vector direction is set correctly for the rigid object."""
for num_cubes in (1, 2):
for device in ("cuda:0", "cpu"):
for gravity_enabled in [True, False]:
with self.subTest(num_cubes=num_cubes, device=device, gravity_enabled=gravity_enabled):
with build_simulation_context(device=device, gravity_enabled=gravity_enabled) as sim:
cube_object, _ = generate_cubes_scene(num_cubes=num_cubes, device=device)
# Obtain gravity direction
if gravity_enabled:
gravity_dir = (0.0, 0.0, -1.0)
else:
gravity_dir = (0.0, 0.0, 0.0)
# Play sim
sim.reset()
# Check that gravity is set correctly
self.assertEqual(cube_object.data.GRAVITY_VEC_W[0, 0], gravity_dir[0])
self.assertEqual(cube_object.data.GRAVITY_VEC_W[0, 1], gravity_dir[1])
self.assertEqual(cube_object.data.GRAVITY_VEC_W[0, 2], gravity_dir[2])
# Simulate physics
for _ in range(2):
# perform rendering
sim.step()
# update object
cube_object.update(sim.cfg.dt)
if __name__ == "__main__":
run_tests()
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