Commit 05cab760 authored by Mayank Mittal's avatar Mayank Mittal

Fixes divide by zero issue when setting gravity to zero (#399)

# Description

This PR fixes divide by zero issue when setting gravity to (0, 0, 0).

It was causing unintended effects - position and linear velocity
remained nan even when modifying root state via
`RigidObject.write_root_state_to_sim()`.

Fixes #398

## Type of change

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

## Checklist

- [x] I have run the [`pre-commit` checks](https://pre-commit.com/) with
`./orbit.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
- [x] I have run all the tests with `./orbit.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 e4d1cdd3
[package] [package]
# Note: Semantic Versioning is used: https://semver.org/ # Note: Semantic Versioning is used: https://semver.org/
version = "0.10.21" version = "0.10.22"
# Description # Description
title = "ORBIT framework for Robot Learning" title = "ORBIT framework for Robot Learning"
......
Changelog Changelog
--------- ---------
0.10.22 (2024-02-14)
~~~~~~~~~~~~~~~~~~~~
Fixed
^^^^^
* Fixed "divide by zero" bug in :class:`~omni.isaac.orbit.sim.SimulationContext` when setting gravity vector.
Now, it is correctly disabled when the gravity vector is set to zero.
0.10.21 (2024-02-12) 0.10.21 (2024-02-12)
~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~
......
...@@ -190,7 +190,10 @@ class SimulationCfg: ...@@ -190,7 +190,10 @@ class SimulationCfg:
"""The number of physics simulation steps per rendering step. Default is 1.""" """The number of physics simulation steps per rendering step. Default is 1."""
gravity: tuple[float, float, float] = (0.0, 0.0, -9.81) gravity: tuple[float, float, float] = (0.0, 0.0, -9.81)
"""The gravity vector (in m/s^2). Default is (0.0, 0.0, -9.81).""" """The gravity vector (in m/s^2). Default is (0.0, 0.0, -9.81).
If set to (0.0, 0.0, 0.0), gravity is disabled.
"""
enable_scene_query_support: bool = False enable_scene_query_support: bool = False
"""Enable/disable scene query support for collision shapes. Default is False. """Enable/disable scene query support for collision shapes. Default is False.
......
...@@ -492,7 +492,13 @@ class SimulationContext(_SimulationContext): ...@@ -492,7 +492,13 @@ class SimulationContext(_SimulationContext):
# need to convert the gravity vector to a direction and magnitude pair explicitly. # need to convert the gravity vector to a direction and magnitude pair explicitly.
gravity = np.asarray(self.cfg.gravity) gravity = np.asarray(self.cfg.gravity)
gravity_magnitude = np.linalg.norm(gravity) gravity_magnitude = np.linalg.norm(gravity)
gravity_direction = gravity / gravity_magnitude
# Avoid division by zero
if gravity_magnitude != 0.0:
gravity_direction = gravity / gravity_magnitude
else:
gravity_direction = gravity
physics_scene.CreateGravityDirectionAttr(Gf.Vec3f(*gravity_direction)) physics_scene.CreateGravityDirectionAttr(Gf.Vec3f(*gravity_direction))
physics_scene.CreateGravityMagnitudeAttr(gravity_magnitude) physics_scene.CreateGravityMagnitudeAttr(gravity_magnitude)
......
...@@ -125,6 +125,16 @@ class TestSimulationContext(unittest.TestCase): ...@@ -125,6 +125,16 @@ class TestSimulationContext(unittest.TestCase):
sim.clear_instance() sim.clear_instance()
self.assertEqual(ctypes.c_long.from_address(id(sim)).value, sim_ref_count - 1) self.assertEqual(ctypes.c_long.from_address(id(sim)).value, sim_ref_count - 1)
def test_zero_gravity(self):
"""Test that gravity can be properly disabled."""
cfg = SimulationCfg(gravity=(0.0, 0.0, 0.0))
sim = SimulationContext(cfg)
gravity_dir, gravity_mag = sim.get_physics_context().get_gravity()
gravity = np.array(gravity_dir) * gravity_mag
np.testing.assert_almost_equal(gravity, cfg.gravity)
if __name__ == "__main__": if __name__ == "__main__":
try: try:
......
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