Skip to content

physics: MuJoCo robots on libcvc terrain (CVC_WITH_MUJOCO), the Unitree Go2 first - #1266

Closed
transfix wants to merge 1 commit into
masterfrom
feat/mujoco-go2
Closed

transfix wants to merge 1 commit into
masterfrom
feat/mujoco-go2

Conversation

@transfix

Copy link
Copy Markdown
Owner

Adds MuJoCo to libcvc as an optional physics backend. The Unitree Go2 runs on the libcvc terrain. This is the base for the C-PHAST lab (Phase 6 of PH-COMPOSE-ROADMAP.md on #1227), where MuJoCo is the ground truth that C-PHAST's predicted futures and energy books are checked against.

What's in it

  • cvc::physics::mujoco_robot (inc/cvc/physics/mujoco_robot.h) owns one robot's mjModel and mjData.
    • It is built from an MJCF file (the Menagerie's unitree_go2/scene.xml), with the scene's floor replaced by a cvc::height_field. MuJoCo's hfield turned out to have the height field's layout and diagonal split, so the robot stands on field->height(x, y).
    • It steps at a fixed dt.
    • Commands are position targets or torques, per actuator. Position mode is MuJoCo's own servo, written into this robot's model; forces are clamped to the Go2's motor ranges.
    • Pushes are wrenches on a body (apply_wrench, apply_force_at).
    • Readouts: qpos, qvel, the base's pose and velocity, actuator forces, and contacts (with forces in world axes).
    • MuJoCo's own energies (mjENBL_ENERGY), plus books of the work each kind of force did: actuators, pushes, contacts, joint friction, limits, damping.
    • save / restore snapshots.
  • No process-wide state. No mju_user_* handlers and no plugin registration: everything lives on the object. Reading a robot never changes its trajectory. The writable-data scan finds nothing in the new objects.
  • CVC_WITH_MUJOCO, off by default.
    • MuJoCo 4 is looked for in the deps prefix only (cvcpkg's mujoco), never in system paths. If it isn't found, configure fails.
    • With tests on, configure also needs cvcpkg's mujoco-menagerie, so the tests never skip quietly.
    • The MuJoCo TU is its own private target, cvc_physics_mujoco, following CVC_WITH_JOLT's pattern. No public header names a MuJoCo type.
    • Licence: MuJoCo is Apache-2.0 and libcvc is LGPL-2.1-only, so MuJoCo stays a separate library. A shared libcvc has NEEDED libmujoco.so.4.0.0 and exports no MuJoCo symbol, and configure refuses a static MuJoCo there. Whether the two can ship together still needs checking, as with cvc::ph composition: compositional port-Hamiltonian systems (C-PHAST, Phase 1) #1227. This is noted in THIRD_PARTY_NOTICES.md and docs/MUJOCO.md.
  • run_mujoco_trial and cvc mujoco info|stand run a stand-and-push trial on a sloped plane and print JSON. The logic lives in the library.
  • CI: a new mujoco job ("MuJoCo robot (Go2, linux)") on ubuntu-24.04.
    • It builds a minimal libcvc with the option on, using only cvcpkg's boost, pegtl, mujoco and mujoco-menagerie.
    • It runs physics_mujoco_robot_test and fails if any test skips.
    • It runs cvc mujoco stand --slope 10 --push 0,60,0 and requires that the Go2 recovers.
    • It is path-gated like cgal-ab.
    • Every other lane builds the test with MuJoCo off: one test checks that the constructor refuses, and the rest skip.

Measured (Linux x86_64, GCC 13, Release, MuJoCo 4.0.0 from cvcpkg)

Test Result
Terrain = height field a ball placed over both triangles of bumpy cells: MuJoCo's contact distance matches the height field's triangle to 1e-5 m (18 points)
Stands, flat (3 s) base 0.258 m over the terrain, moves 1.5 mm after settling; only the feet touch, and they carry the weight to within 1%
Stands on 10° slopes (facing up, down, across) moves less than 1.2 mm after settling; the base keeps the slope's tilt
Sideways push on the base, 0.1 s 60 N: moves 1.7 cm, then recovers. 150 N: recovers. 200 N: falls. 300 N: rolls over. The test checks 60 N and 300 N
Energy ΔE vs the books' total: residual 3.0e-5 J at 2 ms and 1.5e-5 J at 1 ms, against 5.9 J of gross work. Stated tolerance: 2e-5 of the gross work, and the residual must halve with the step
Determinism (one binary) two runs, a run that reads the robot every step, and a restored snapshot all end on the same bits

Also built locally with MuJoCo off (clang 18): the stub path and the skips work, and cvc mujoco reports that MuJoCo is missing. The new files compile without warnings under clang 18 -Wall -Wextra -Wpedantic.

Not here, and why

  • The legged plant kind (legged:go2 in plant_host) is left for later. nav's plant takes command(steer, accel) for a bicycle. Turning that into leg motion needs a locomotion policy that maps (vx, yaw rate) to joint targets, and none exists yet. Without one, a legged:go2 agent would stand still while nav plans as if it were driving.
  • pycvc binding: next. The pycvc lanes have to carry MuJoCo first.
  • Jolt coupling: one-way kinematic proxies come first.
  • macOS, Windows, wasm: cvcpkg publishes mujoco for all of them, but only Linux x86_64 is built and tested here.

No merge without Joe's OK.

…ee Go2 first

cvc::physics::mujoco_robot owns one robot's mjModel and mjData, built from an MJCF file
(the Menagerie's unitree_go2/scene.xml) with its floor replaced by a cvc::height_field.
MuJoCo's hfield has the height field's layout and diagonal, so the robot stands on
field->height(x, y). It steps at a fixed dt and takes position targets (MuJoCo-side servos,
written into its own model) or torques per actuator. It applies wrenches for pushes, reads
qpos, qvel, actuator forces and contacts, and reads MuJoCo's own energies (mjENBL_ENERGY)
next to books of the work each kind of force did. It also saves and restores snapshots. No
process-wide state: no mju_user_* handlers, no plugin registration.

CVC_WITH_MUJOCO is off by default. MuJoCo 4 is found in the deps prefix only (cvcpkg's
mujoco), never the system's paths. As with Jolt, the MuJoCo TU is its own private target
(cvc_physics_mujoco); a shared libcvc links the shared libmujoco and exports none of it.

run_mujoco_trial and `cvc mujoco info|stand` run a standing and push trial. The new CI job
`mujoco` builds a minimal libcvc with the option on, from cvcpkg's mujoco and
mujoco-menagerie, and runs the tests:
- the Go2 stands on flat ground and on 10 degree slopes;
- a 60 N push perturbs it and it recovers, and 300 N knocks it over;
- MuJoCo's energy change matches the books to 2e-5 of the gross work, and the residual
  halves with the step;
- two runs, a run read every step, and a restored snapshot all end on the same bits.

The legged plant kind (legged:go2 in plant_host) is left for later: nav's command(steer,
accel) needs a locomotion policy to become leg motion (docs/MUJOCO.md).
@transfix
transfix enabled auto-merge (squash) November 15, 2026 08:48
transfix added a commit that referenced this pull request Nov 15, 2026
…d on #1266) (#1268)

* physics: MuJoCo robots on libcvc terrain (CVC_WITH_MUJOCO), the Unitree Go2 first

cvc::physics::mujoco_robot owns one robot's mjModel and mjData, built from an MJCF file
(the Menagerie's unitree_go2/scene.xml) with its floor replaced by a cvc::height_field.
MuJoCo's hfield has the height field's layout and diagonal, so the robot stands on
field->height(x, y). It steps at a fixed dt and takes position targets (MuJoCo-side servos,
written into its own model) or torques per actuator. It applies wrenches for pushes, reads
qpos, qvel, actuator forces and contacts, and reads MuJoCo's own energies (mjENBL_ENERGY)
next to books of the work each kind of force did. It also saves and restores snapshots. No
process-wide state: no mju_user_* handlers, no plugin registration.

CVC_WITH_MUJOCO is off by default. MuJoCo 4 is found in the deps prefix only (cvcpkg's
mujoco), never the system's paths. As with Jolt, the MuJoCo TU is its own private target
(cvc_physics_mujoco); a shared libcvc links the shared libmujoco and exports none of it.

run_mujoco_trial and `cvc mujoco info|stand` run a standing and push trial. The new CI job
`mujoco` builds a minimal libcvc with the option on, from cvcpkg's mujoco and
mujoco-menagerie, and runs the tests:
- the Go2 stands on flat ground and on 10 degree slopes;
- a 60 N push perturbs it and it recovers, and 300 N knocks it over;
- MuJoCo's energy change matches the books to 2e-5 of the gross work, and the residual
  halves with the step;
- two runs, a run read every step, and a restored snapshot all end on the same bits.

The legged plant kind (legged:go2 in plant_host) is left for later: nav's command(steer,
accel) needs a locomotion policy to become leg motion (docs/MUJOCO.md).

* pycvc: mujoco_robot from Python (the Go2 gym's reset and step)

pycvc_mujoco.i puts cvc::physics::mujoco_robot in every pycvc: pycvc.mujoco_robot(model,
terrain=height_field, ...) with the C++ options as keywords. It binds the commands
(position or torque, per actuator or all), pushes, step() (without the GIL), the readouts
(qpos, qvel, commands and actuator forces as read-only float64 numpy copies; base() and
contacts() as dicts), energy() and books(), save/restore, and run_mujoco_trial as a dict.
Without CVC_WITH_MUJOCO, mujoco_available() is False and the constructor raises.

mujoco_robot::reset() goes back to the robot as built (state, modes, gains and books), for
a gym's reset. ResetReplaysAFreshRobot checks it to the bit.

test_pycvc_mujoco.py has 112 checks. The push moves the Go2 1.66 cm, as in C++. The books
close to 2.9e-5 J of 5.9 J. reset() and a restored snapshot replay an episode bitwise, and
another thread runs while step() does. The CI job `mujoco` now also builds pycvc (Python
3.12, numpy and SWIG from cvcpkg) and runs it, failing if it skips.
@transfix
transfix disabled auto-merge November 15, 2026 14:04
@transfix

Copy link
Copy Markdown
Owner Author

Its commit (2837de0) is on master, inside #1268's squash commit 76b1988: #1268 was stacked on this PR and merged first, once its CI was green. That run included the MuJoCo job, which ran all 11 C++ tests, cvc mujoco stand and the 112 pycvc checks. The MuJoCo files on master are byte-identical to this branch plus #1268's commit, so nothing is left to merge here.

@transfix transfix closed this Nov 15, 2026
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