Skip to content

feat(sim): optional gravity compensation for MuJoCo arms - #4977

Closed
ruthwikdasyam wants to merge 5 commits into
mainfrom
ruthwik/feat/sim-gravity-compensation
Closed

ruthwikdasyam wants to merge 5 commits into
mainfrom
ruthwik/feat/sim-gravity-compensation

Conversation

@ruthwikdasyam

@ruthwikdasyam ruthwikdasyam commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Problem

MuJoCo position actuators sag under the arm's weight (about 1-2 cm at the xArm7 TCP). Real arm firmware cancels gravity, so sim and hardware disagree.

Solution

  • MujocoSimModuleConfig.gravity_compensation, passed to MujocoEngine. When on, the engine recompiles the MJCF with gravcomp=1 on every body under the controlled joints and actgravcomp on the actuated joints.
  • Each actuator's force range is copied to its joint (actuatorfrcrange), since MuJoCo clamps actuator gravity compensation only at joint level. The arm's weight and any load share one torque budget; an overloaded arm still drops.
  • Default False, so legged robots (G1, Go2) are unchanged.
  • Enabled for fixed-base arms via make_xarm7_sim_module_kwargs and mujoco_if_sim (xArm6/7, Piper).

Note: xArm sim runs no longer sag, so older eval scores and tuned thresholds may shift.

Tests

  • A one-link arm sags without compensation and holds its pose with it.
  • A one-link arm whose actuator is too weak still drops with compensation on.
  • self_hosted: on the xArm7 model, exactly the bodies from link1 down are compensated, and each arm joint carries its actuator's force range.
  • Holding the home pose for 3000 steps, max joint error: xArm7 0.0059 → 0.0000 rad, xArm6 0.0062 → 0.0000 rad. Piper already had gravcomp in its MJCF (0 → 0).

MujocoSimModule gains gravity_compensation (off by default). When on, the
engine recompiles the MJCF with gravcomp on the robot's bodies and
actgravcomp on its joints, so position actuators hold their targets as
real arm firmware does instead of sagging 1-2 cm at the xArm7's TCP.

@github-actions github-actions Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

go2 replay realtime (arm64)

Details
Benchmark suite Current: ec69b6b Previous: 813bad1 Ratio
peak memory 1728.398 MB 1757.906 MB 0.98
peak threads 368 threads 370 threads 0.99
network (transport) 2725.785 MB 2731.988 MB 1.00
disk write 1.492 MB 1.504 MB 0.99
instructions 136.422 G 136.78 G 1.00

This comment was automatically generated by workflow using github-action-benchmark.

xArm and Piper sim blueprints enable it through make_xarm7_sim_module_kwargs
and mujoco_if_sim. Legged robots keep it off.
@greptile-apps

greptile-apps Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issues remain.

Summary

Adds optional gravity compensation for MuJoCo arms and enables it for fixed-base arm helpers.

  • Fixed-base arm simulations can compensate for gravity while keeping force limits.

Reviews (3) · Last reviewed commit: "fix(sim): keep a joint's tighter force c..." · Reviewed by Greptile

MuJoCo clamps actuator gravity compensation only at joint level, so copy
each actuator's force range onto its joint. Skip unnamed bodies, which the
spec cannot address.
@ruthwikdasyam

Copy link
Copy Markdown
Contributor Author

@greptile review

@codecov

codecov Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.15663% with 9 lines in your changes missing coverage. Please review.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
dimos/simulation/engines/mujoco_engine.py 77.77% 3 Missing and 5 partials ⚠️
dimos/robot/manipulators/common/sim.py 0.00% 1 Missing ⚠️
@@            Coverage Diff             @@
##             main    #4977      +/-   ##
==========================================
+ Coverage   83.00%   83.12%   +0.11%     
==========================================
  Files        1752     1752              
  Lines      169316   169398      +82     
  Branches    13955    13972      +17     
==========================================
+ Hits       140540   140810     +270     
+ Misses      25018    24844     -174     
+ Partials     3758     3744      -14     
Components Coverage Δ
Tests 96.41% <100.00%> (+<0.01%) ⬆️
Flag Coverage Δ
OS-macos-latest 77.47% <67.46%> (+0.01%) ⬆️
OS-ubuntu-24.04-arm 77.27% <67.46%> (+0.01%) ⬆️
OS-ubuntu-latest 77.64% <67.46%> (+0.01%) ⬆️
Py-3.10 77.64% <67.46%> (+0.01%) ⬆️
Py-3.11 77.62% <67.46%> (+0.01%) ⬆️
Py-3.12 77.64% <67.46%> (+0.01%) ⬆️
SelfHosted-Linux 40.16% <60.24%> (+0.08%) ⬆️
SelfHosted-macOS 39.88% <60.24%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
dimos/robot/manipulators/xarm/config.py 97.56% <ø> (+2.43%) ⬆️
dimos/simulation/engines/mujoco_sim_module.py 57.33% <100.00%> (+8.18%) ⬆️
dimos/simulation/engines/test_mujoco_sim_module.py 98.97% <100.00%> (+0.99%) ⬆️
dimos/robot/manipulators/common/sim.py 66.66% <0.00%> (ø)
dimos/simulation/engines/mujoco_engine.py 53.80% <77.77%> (+17.27%) ⬆️

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ruthwikdasyam

Copy link
Copy Markdown
Contributor Author

@greptile check

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.

1 participant