Skip to content

[BUG FIX] Add angular damping to drone to prevent numerical instability#1348

Merged
YilingQiao merged 2 commits intoGenesis-Embodied-AI:mainfrom
yun-long:yunlong/fix_nan
Jul 8, 2025
Merged

[BUG FIX] Add angular damping to drone to prevent numerical instability#1348
YilingQiao merged 2 commits intoGenesis-Embodied-AI:mainfrom
yun-long:yunlong/fix_nan

Conversation

@yun-long
Copy link
Contributor

@yun-long yun-long commented Jul 3, 2025

Description

This PR fixes a bug where the drone simulation could become numerically unstable due to unbounded angular velocities.

🔧 What changed:

  • Added small damping to the angular DOFs using self.drone.set_dofs_damping(torch.tensor([0.0, 0.0, 0.0, 1e-4, 1e-4, 1e-4]))
    • The first three values correspond to linear damping (kept at 0).
    • The last three apply small damping to the angular components (roll, pitch, yaw).
  • This helps suppress high-frequency oscillations and prevents angular velocity from exploding under extreme control inputs.

No other functional or API changes were made.

Related Issue

Related to #909

Motivation and Context

How Has This Been / Can This Be Tested?

Yes. The training still works as before

cd examples/drone/
python hover_train.py

# after training
python hover_eval.py

Also tested with the following minimum example

import torch

import genesis as gs

from hover_env import HoverEnv


gs.init(backend=gs.cpu, precision="32", logging_level="error", seed=0)

env = HoverEnv(
    env_cfg={
        "num_actions": 4,
        # termination
        "termination_if_roll_greater_than": 180,  # degree
        "termination_if_pitch_greater_than": 180,
        "termination_if_close_to_ground": 0.1,
        "termination_if_x_greater_than": 3.0,
        "termination_if_y_greater_than": 3.0,
        "termination_if_z_greater_than": 2.0,
        # base pose
        "base_init_pos": [0.0, 0.0, 1.0],
        "base_init_quat": [1.0, 0.0, 0.0, 0.0],
        "episode_length_s": 15.0,
        "at_target_threshold": 0.1,
        "resampling_time_s": 3.0,
        "simulate_action_latency": True,
        "clip_actions": 1.0,
        # visualization
        "visualize_target": False,
        "visualize_camera": False,
        "max_visualize_FPS": 60,
    },
    obs_cfg={
        "num_obs": 17,
        "obs_scales": {
            "rel_pos": 1 / 3.0,
            "lin_vel": 1 / 3.0,
            "ang_vel": 1 / 3.14159,
        },
    },
    reward_cfg={
        "yaw_lambda": -10.0,
        "reward_scales": {
            "target": 10.0,
            "smooth": -1e-4,
            "yaw": 0.01,
            "angular": -2e-4,
            "crash": -10.0,
        },
    },
    command_cfg={
        "num_commands": 3,
        "pos_x_range": [-1.0, 1.0],
        "pos_y_range": [-1.0, 1.0],
        "pos_z_range": [1.0, 1.0],
    },
    show_viewer=False,
    num_envs=1,
)

action = torch.tensor([[-0.5, 0.77, -1.0, 0.99]], dtype=gs.tc_float, device=gs.device)

i = 0
iter_exploding = -1
env.reset()
while True:
    env.step(action)
    num_constraints = env.scene.rigid_solver.constraint_solver.n_constraints.to_torch().item()
    acc = env.scene.rigid_solver.dofs_state.acc.to_torch()[:, 0]
    print(f"[{i}] num constraints: {num_constraints}")
    print(f"[{i}] acc: {acc}")
    i += 1

    if iter_exploding != -1:
        if torch.isnan(acc).any():
            break
    else:
        if (torch.abs(acc) > 1e5).any():
            iter_exploding = i

Screenshots (if appropriate):

Checklist:

  • [ x] I read the CONTRIBUTING document.
  • [ x] I followed the Submitting Code Changes section of CONTRIBUTING document.
  • [ x] I tagged the title correctly (including BUG FIX/FEATURE/MISC/BREAKING)
  • [ x] I updated the documentation accordingly or no change is needed.
  • [ x] I tested my changes and added instructions on how to test it for reviewers.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@duburcqa
Copy link
Collaborator

duburcqa commented Jul 3, 2025

What about making this the default for Drone in general. Not just this example. Because I think this issue is more general. I can do it if you don’t want to spend time on it.

@duburcqa
Copy link
Collaborator

duburcqa commented Jul 3, 2025

By the way, I changed “Resolve” for “Related”. Because I don’t think it is addressing the root cause of the issue, but it mitigates it successfully.

@YilingQiao
Copy link
Collaborator

YilingQiao commented Jul 8, 2025

I think this issue is more general.

Do you mean damping parameter for rotational DoFS?

@YilingQiao YilingQiao merged commit 0782728 into Genesis-Embodied-AI:main Jul 8, 2025
13 checks passed
@yun-long yun-long deleted the yunlong/fix_nan branch July 18, 2025 22:59
LeonLiu4 pushed a commit to LeonLiu4/Genesis that referenced this pull request Aug 7, 2025
YilingQiao pushed a commit to YilingQiao/Genesis that referenced this pull request Aug 22, 2025
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.

3 participants