Skip to content

Python: per-step MJCF actuator control + multibody state readback - #12

Closed
haixuanTao wants to merge 4 commits into
dimforge:mainfrom
haixuanTao:py-mjcf-actuation
Closed

Python: per-step MJCF actuator control + multibody state readback#12
haixuanTao wants to merge 4 commits into
dimforge:mainfrom
haixuanTao:py-mjcf-actuation

Conversation

@haixuanTao

Copy link
Copy Markdown
Contributor

Makes MJCF robots drivable per control step from Python, with full state observation — the two pieces an RL / sim-to-sim evaluation loop needs. Follow-up to #7/#8, standalone on main.

What

ControlNexusState.apply_actuator_controls(viewer, ctrl, env=0) applies one MJCF control vector (one entry per <actuator>, in actuator order — NexusState.actuator_names() reports the order) with full MJCF actuator semantics via rapier3d-mjcf's apply_controls_multibody (<position> servos with kp/kv, <motor> force/gear, force limits), then pushes the refreshed joint-motor state to the GPU in one buffer write. The PD servo itself runs inside the GPU solver, so there is no per-step solver round-trip beyond that single upload.

ObservationNexusViewer.read_multibody_links(state, env=0) reads every link's state back in one readback: (coords, positions, quats, linvels, angvels) numpy arrays (one row per link, GPU traversal order). coords are the generalized joint coordinates (a revolute joint's angle is coords[3], the first angular slot); poses/velocities are world-space.

How

  • GpuMultibodySet::sync_joint_data_from_rapier refreshes each link's GenericJoint data (motors, limits) from the rapier multibody set, mirroring from_rapier's traversal order, then uploads links_static once. Only joint data is touched — coordinates/velocities/mass properties are untouched, so this can't teleport links.
  • NexusState::control_multibody_motors wraps "mutate rapier joints, then sync" without marking the world dirty (motor updates are control, not topology — no GPU rebuild).
  • links_workspace gains COPY_SRC + public accessors for the readback.
  • The Python NexusState keeps the MjcfRobotHandles from insert_mjcf.

Validation

On the LeRobot bipedal platform MJCF (12 position actuators, 13 links): a passive run collapses to torso z ≈ −0.34 within 1 s; driving the servos each step holds it at z ≈ −0.02, and read_multibody_links tracks joint angles/limits throughout. cargo check clean on nexus3d, nexus_viewer3d, nexus_python3d.

🤖 Generated with Claude Code

- GpuMultibodySet::sync_joint_data_from_rapier: refresh every link's joint
  data (motor targets/gains, limits) from the rapier multibody set in one
  buffer write, mirroring from_rapier's traversal order.
- NexusState::control_multibody_motors: runtime actuation entry point that
  mutates the rapier joints (e.g. rapier3d-mjcf's apply_controls_multibody)
  and pushes the refreshed joint data to the GPU, without marking the world
  dirty (no rebuild).
- Viewer::read_multibody_links + links_workspace COPY_SRC: one-readback
  joint coordinates, link world poses and world-space velocities per env.
- Python: NexusState keeps the MjcfRobotHandles from insert_mjcf and exposes
  actuator_names() / apply_actuator_controls(viewer, ctrl, env) with full
  MJCF actuator semantics; NexusViewer.read_multibody_links(state, env)
  returns (coords, positions, quats, linvels, angvels) numpy arrays.

Together these make MJCF robots drivable per control step from Python
(position-servo PD runs inside the solver) with full state observation --
the two pieces sim-to-sim eval loops need.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@sebcrozet
sebcrozet force-pushed the py-mjcf-actuation branch from 43c5c98 to a7064e3 Compare July 16, 2026 12:14
sebcrozet and others added 3 commits July 16, 2026 14:14
…port

The rebase onto upstream main left control_multibody_motors and
read_multibody_links ungated while RbdState::multibodies(_mut) is
dim3-only, breaking the 2D crates. Also fixes the missing numpy
PyArray2 import and an unused_assignments warning in the MJCF loader.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Stock naga 29's MSL writer re-evaluates a loop's break_if condition after
the continuing block has advanced the loop phis, dropping the final body
iteration on Metal. In the multibody solve kernels the per-lane J.v loops
run exactly one iteration per lane, so they executed zero times: zero
impulses from the contact and joint/PD sweeps while gravity kept
integrating -> robots free-fell through the floor on macOS. Pin the fixed
fork (see zealot/docs/metal-contact-bug-proposal.md).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@sebcrozet

Copy link
Copy Markdown
Member

This will be merged as part of #36

sebcrozet added a commit that referenced this pull request Aug 29, 2026
* fix(rbd): apply the per-batch stride to collider_parent reads in the narrow phase

Replaces #21

Co-Authored-By: Haixuan Xavier Tao <tao.xavier@outlook.com>

* feat: per-environment collision-pair capacity override

Replaces #24

Co-Authored-By: Haixuan Xavier Tao <tao.xavier@outlook.com>

* feat(rbd): make the narrow-phase contact prediction distance configurable

Replaces #28

Co-Authored-By: Haixuan Xavier Tao <tao.xavier@outlook.com>

* fix(rbd): thread the configurable prediction distance through the brute-force broad phase

Completes #28

* feat(python): per-environment MJCF insertion

Replaces #16

Co-Authored-By: Haixuan Xavier Tao <tao.xavier@outlook.com>

* fix(python): drop the duplicated collisions-capacity setter and pass the RbdCoupling to insert_rigid_body_in

Completes #16

* feat(python): per-step MJCF actuator control + multibody state readback

Replaces #12

Co-Authored-By: Haixuan Xavier Tao <tao.xavier@1ms.ai>

* fix(python): gate multibody control/readback on dim3, add the missing PyArray2 import

Replaces #12

Co-Authored-By: Haixuan Xavier Tao <tao.xavier@outlook.com>

* fix(rbd): decode the SoA link workspace for the multibody readback and drop the stale set_gravity copy

Completes #12

* perf(rbd): dedupe shared TriMesh uploads in from_rapier

Replaces #19

Co-Authored-By: Haixuan Xavier Tao <tao.xavier@outlook.com>

* perf(rbd): optional GPU contact reduction, merging per-pair manifolds to <=4 points

Replaces #17

Co-Authored-By: Haixuan Xavier Tao <tao.xavier@outlook.com>

* fix(rbd): pass the prediction distance to manifold_reduction in the contact-reduction kernel

Completes #17

* perf(rbd): flat 1-D narrow-phase dispatch, packing warps across batches

Replaces #21

Co-Authored-By: Haixuan Xavier Tao <tao.xavier@outlook.com>

* fix(rbd): restore the contacts capacity binding and import atomic_load_u32 for the flat dispatch

Completes #21

* fix(rbd): drop the stale 2mm PREDICTION constant reintroduced by the flat-dispatch port

Completes #21

* fix mpm feature-gating

* feat(rbd): expose dof_state_mut, links_static, joint_constraints and link_of_body

* feat(rbd): env-reset primitives, GPU motor scatter, contact sensors, actuator delay, encoded step, substep-refresh cadence and per-DoF armature/frictionloss

* fix(rbd): guard against implicit-coriolis drifting from the batch_indices uniform

* feat(rbd): cluster contact manifolds by normal, matching rapier, with a tunable threshold

* feat(rbd): model multibody joint frictionloss as a constraint instead of a force

* feat(rbd): seed per-DoF joint friction from rapier's Multibody::frictions

* refactor(rbd): read the contact prediction distance from RbdSimParams instead of a dedicated uniform

* chore: cargo fmt

* refactor(rbd): move the contact merge cosine into RbdSimParams

* refactor: move read_multibody_links onto NexusState and drive every env from control_multibody_motors

* test(rbd): add a headless many-small-environments step-timing harness

* revert(rbd): drop the flat 1-D narrow-phase dispatch

Measured 7-21% slower on Metal.

* chore: cleanup comments

* fix: gate control_multibody_motors on dim3 so the 2D build still compiles

* fix instability in joint-ball3 demo

* chore: remove debug test files

* chore: cleanups

* fix(rbd): build the bench harness without the metal feature and only in 3D

* fix(rbd): silence the clippy needless-borrow and unnecessary-mut lints

* fix(rbd): split the joint-constraint back-solve into its own dispatch to fit 8 storage buffers

* test(rbd): keep the bench harness under wgpu's default buffer-size limit

* fix(rbd): split the batched env reset into pose and DoF passes to fit 8 storage buffers

* chore: switch to the published rapier version

* fix: make all envs share the same RbdSimParams

* chore: clippy fixes

---------

Co-authored-by: Haixuan Xavier Tao <tao.xavier@outlook.com>
Co-authored-by: Haixuan Xavier Tao <tao.xavier@1ms.ai>
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.

2 participants