Single-joint arm laboratory
How a MuJoCo-style model (one body, one hinge joint, one actuator, jointpos/jointvel sensors) turns a control value in data.ctrl into torque, motion and sensor readings, and how motor, position and velocity actuators interpret the same ctrl number differently.
What it demonstrates
How a MuJoCo-style model (one body, one hinge joint, one actuator, jointpos/jointvel sensors) turns a control value in data.ctrl into torque, motion and sensor readings, and how motor, position and velocity actuators interpret the same ctrl number differently.
How it works
The model mirrors the MJCF shown beside it: a capsule link of mass m and length L hangs from a hinge whose axis is (0, −1, 0), so q = 0 is horizontal and positive q raises the arm. The browser treats the link as a thin uniform rod (I = mL²/3); MuJoCo would compute the full capsule inertia, including its rounded ends.
Each tick is one mj_step. The actuator force is computed with MuJoCo's affine actuator model, force = gain·ctrl + bias₁·(gear·q) + bias₂·(gear·q̇), clamped to forcerange and multiplied by the gear to give joint torque. Forward dynamics gives q̈ = (τ_act + τ_gravity − d·q̇) / I. The Euler option is semi-implicit, like MuJoCo's: velocity is updated first (with joint damping treated implicitly), then position. RK4 is the classic four-stage Runge–Kutta method.
As in MuJoCo, qacc, actuator_force and sensordata are filled in from the state at the start of the step, while qpos, qvel and time hold the integrated result.
Adjustable parameters
- Actuator type: motor, position, velocity
- ctrl (torque −10…10 N·m, target angle −π…π rad, or target velocity −8…8 rad/s)
- Link mass (0.1–3 kg) and length (0.2–0.6 m)
- Joint damping (0–2 N·m·s/rad)
- Gear (0.5–10)
- kp (0–300 N·m/rad) and kv (0–10 N·m·s/rad)
- forcerange ±5 on/off, gravity on/off
- Timestep (0.5–40 ms) and integrator (Euler, RK4)
Known limitations
- A browser re-implementation of the equations for one hinge, not MuJoCo. No contacts, constraints, joint limits or armature.
- Thin-rod inertia instead of MuJoCo's capsule inertia, so the numbers differ slightly from a real MuJoCo run of the same XML.
- Euler here is implicit only in joint damping, like MuJoCo's Euler. MuJoCo's implicit and implicitfast integrators (which also treat actuator damping such as kv implicitly) are not implemented.
- The XML viewer is a faithful description of the model, but the page does not parse it. Edits go from the sliders to the XML, not the other way round.
Equations
Core update rule
// One mj_step of the arm (lib/sim/ch03-arm.ts)
const u = clamp(ctrl, ctrlrange);
const force = clip(gain * u + b1 * gear * q + b2 * gear * qd, forcerange);
const tauAct = gear * force;
const tauG = -m * g * (L / 2) * Math.cos(q);
qacc = (tauAct + tauG - damping * qd) / I; // forward dynamics
sensordata = [q, qd]; // jointpos, jointvel
// semi-implicit Euler with implicit joint damping
qd += h * (tauAct + tauG - damping * qd) / (I + h * damping);
q += h * qd;
time += h;Explained in Chapter 3: MuJoCo Internals