Skip to content

pycvc: mujoco_robot from Python, the Go2 gym's reset and step (stacked on #1266) - #1268

Merged
transfix merged 2 commits into
masterfrom
feat/mujoco-go2-pycvc
Nov 15, 2026
Merged

transfix merged 2 commits into
masterfrom
feat/mujoco-go2-pycvc

Conversation

@transfix

Copy link
Copy Markdown
Owner

Stacked on #1266. It includes #1266's commit (2837de0); the new work is the top commit, eca31f9. The base is master because ci.yml runs pull requests only into master, develop, main and perf/**, and changing a PR's base later doesn't re-run CI. Once #1266 merges, the diff here shrinks to this commit. Merge after #1266, and only on Joe's OK.

This puts cvc::physics::mujoco_robot in pycvc as the base for the C-PHAST lab's gym (Phase 6 of PH-COMPOSE-ROADMAP.md on #1227): a reset / step loop with MuJoCo's Go2 as the plant.

What's in it

  • bindings/pycvc/pycvc_mujoco.i is %included by pycvc.i after pycvc_physics.i, so every pycvc has it. Without CVC_WITH_MUJOCO, pycvc.mujoco_available() is False and the constructor raises.
    • pycvc.mujoco_robot(model, terrain=height_field, ...) takes the C++ options as keywords. The terrain is copied in.
    • Commands: position or torque mode, per actuator or all at once, and the servo gains. Pushes: apply_wrench and apply_force_at.
    • step(n) releases the GIL.
    • Readouts: qpos, qvel, commands and actuator_forces are read-only float64 numpy copies (pycvc_world.i's copy_of). base(), contacts(), energy() and books() return dicts.
    • save / restore / reset, and pycvc.run_mujoco_trial(), which returns a dict.
  • mujoco_robot::reset() (C++) goes back to the robot as built: its state, modes, gains and books. This is the gym's reset. The new C++ test ResetReplaysAFreshRobot checks it bitwise.
  • test_pycvc_mujoco.py (ctest pycvc_mujoco, 112 checks):
    • the model, and readouts that come back as read-only numpy arrays;
    • standing, with only the feet touching and carrying the weight;
    • a 60 N push moves it 1.66 cm (the same as C++) and it recovers;
    • the energy books close to 2.9e-5 J of 5.9 J;
    • reset(), a restored snapshot and a second robot all replay an episode bitwise;
    • torque mode saturates at the motor ranges;
    • the 10° trial;
    • another Python thread runs while step() does;
    • bad input raises.
  • CI: the mujoco job now also installs Python 3.12, numpy and SWIG from cvcpkg, builds pycvc with MuJoCo on, and runs pycvc_mujoco with -V. It fails if the test skips or doesn't report its checks. The other pycvc lanes run the same test with MuJoCo off, which checks the refusal.

Checked locally (Linux x86_64, Release)

  • GCC 13 with MuJoCo on: physics_mujoco_robot_test 11/11, and pycvc_mujoco 112 checks passed.
  • clang 18 with MuJoCo off: the pycvc build succeeds and pycvc_mujoco passes on the refusal path (2 checks).
  • The writable-data scan of pycvc's objects is clean, and clang-format is clean on the changed lines.

Not here

The gym itself (a scenario registry starting with push recovery, observation and reward conventions, and C-PHAST as the planning baseline) comes with the lab. The legged plant kind still waits for a locomotion policy (see #1266).

…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).
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 enabled auto-merge (squash) November 15, 2026 08:48
@transfix
transfix merged commit 76b1988 into master Nov 15, 2026
29 checks passed
@transfix
transfix deleted the feat/mujoco-go2-pycvc branch November 15, 2026 13:59
transfix added a commit that referenced this pull request Nov 16, 2026
…#1253, #1255, #1256, #1264, #1268, #1271, #1273, #1277)

- ci.yml: the sanitizer list keeps this branch's tests (stream_test, net_ws_client_test,
  ariadne_host_test) and master's segmentation_intrinsics_test.
- STATE_EXEC_THREADED.md: the room-call verbs (room-run is #1273's) and #1252's segment.seeded
  provider in the provider table, both registered without a kind. room-call's async functions
  stay on the app's host: a reply settles on the relay client's reader thread through
  host.finish, so moving them to *ictx.host needs a finisher first.
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