|
Mujoco KDL Wrapper
0.1.0
MuJoCo + KDL bridge for robot kinematics and dynamics
|
This tutorial builds a simulation application in layers: install the package, compile a robot scene, add KDL control, add reset hooks, add objects and cameras, use the Simulate UI, record video, and then extend the scene to more robots and more complex task assets.
The snippets assume the package is built with MuJoCo Menagerie available, because the examples use the Kinova GEN3 arm and Robotiq 2F-85 gripper.
The wrapper has four layers. Keep them separate and the API stays simple:
| Layer | Type | Responsibility |
|---|---|---|
| Scene description | SceneSpec, RobotSpec, AttachmentSpec, SceneObject, CameraSpec | Describe what should be compiled into MuJoCo |
| Runtime environment | Env | Own mjModel/mjData, registered robots, and reset hooks |
| Robot control handle | Robot | KDL chain, joint maps, measured ports, command ports |
| Visualization/recording | Viewer, VideoRecorder | Interactive Simulate UI and offscreen MP4 recording |
The important ownership rule:
Env owns mjModel and mjData.Robot borrows mjModel and mjData.Viewer borrows the same model/data through the step calls.VideoRecorder owns only its EGL/rendering/ffmpeg resources; it also borrows model/data when recording frames.Most examples follow this flow:
SceneSpec.init_env().Robot handles from env.model and env.data.env_add_robot().env.on_reset if the task has object/controller state.init_window_sim() or a headless loop.step(), update(), compute commands, write command ports.Install system dependencies:
Install MuJoCo 3.8.0:
Configure and build:
The package expects MuJoCo 3.8.0 by default at /opt/mujoco-3.8.0. Override it with -DMUJOCO_ROOT=/path/to/mujoco-3.8.0 if needed.
Useful CMake options:
| Option | Default | When to change it |
|---|---|---|
MUJOCO_ROOT | /opt/mujoco-3.8.0 | MuJoCo is installed somewhere else |
FETCH_MENAGERIE | OFF | You want the bundled examples to build robot assets automatically |
BUILD_RECORDER | ON | Turn off if EGL is unavailable and you do not need MP4 recording |
BUILD_TESTS | ON | Turn off for a smaller install-only build |
BUILD_DOCS | OFF | Turn on to generate Doxygen HTML docs |
Run a smoke test:
If you are developing inside a larger workspace, point CMake at this package directory explicitly:
When using the wrapper from another CMake project, link against mj_kdl_wrapper and include the public header:
If you are building directly inside this repository before install, the example targets in src/examples/CMakeLists.txt show the same pattern with the in-tree target:
Scenes are declared with SceneSpec. The first useful scene is just one robot MJCF, floor, skybox, default timestep, and default gravity:
Env owns env.model and env.data. Call mj_kdl::cleanup(&env) when done.
You can also build raw pointers directly:
Prefer Env for applications that need reset hooks or registered robots.
build_scene() creates one MuJoCo mjSpec, attaches robot MJCF trees into it, adds global scene options, adds objects and cameras, then compiles the model.
Important fields:
Use RobotSpec::prefix for repeated robots. The prefix is applied during MJCF attachment so names stay unique in the compiled model:
Use RobotSpec::pos and RobotSpec::euler to place the robot root in the world. Euler angles are extrinsic XYZ degrees.
Use a consistent cleanup order:
If you created raw mjModel* and mjData* with build_scene(), free them with:
Do not call both cleanup(&env) and destroy_scene(env.model, env.data) for the same model/data. Env owns those pointers once init_env() succeeds.
Robot is the runtime handle for one controllable articulation. It stores the borrowed mjModel/mjData pointers, KDL chain, joint-name maps, measured ports, and command ports.
Registering with env_add_robot() lets reset(&env) sync the robot command ports after MuJoCo data is reset.
For position mode, write jnt_pos_cmd; update() copies it to MuJoCo actuator controls.
init_window_sim() starts MuJoCo Simulate in a render thread. Your loop still owns stepping, updating control, and task logic.
KDL dynamics work directly from the generated chain:
This is the core pattern used by ex_gravity_comp.
Attachments are MJCF assets attached under a body in the accumulated robot spec. They are applied in order, so mount -> sensor -> gripper chains are natural.
Tell KDL about the attached tool when initializing the robot:
tool_body lumps the tool subtree inertia into the KDL chain. tcp_site adds a terminal frame from a MuJoCo site.
attachments is ordered. Each attachment is applied to the robot spec after all previous attachments, so later attach_to values may refer to bodies introduced by earlier attachments.
For contact stability, add contact exclusions only when two attached bodies are known to overlap structurally:
Do not use exclusions to hide unstable task contacts; fix geometry, friction, or controller gains instead.
SceneObject supports primitive objects and MJCF-backed assets. Tables are just assets, not special first-class fields.
MJCF-backed object names are prefixed with object.name + "_". If an asset has a site named table_top, get its compiled name like this:
This is useful for placing robot bases and objects relative to authored asset sites rather than hardcoding offsets.
Add fixed scene cameras through SceneSpec::cameras:
After compile, all cameras are visible to MuJoCo and include:
SceneSpec::cameras,List them:
Use one in the viewer or recorder:
The Simulate UI also has its own live camera selector in the Rendering panel.
reset(&env) resets MuJoCo, runs your hook, forwards dynamics, then syncs all registered robots so stale commands do not hit the first post-reset step.
Put robot, object, controller, randomization, and task-specific state in the hook. Do not hide reset work in the control loop.
reset(&env) performs the reset in this order:
ResetContext.env.on_reset, if provided.mj_forward().Robot:jnt_pos_cmd becomes the measured joint position,jnt_trq_cmd is cleared,That order is deliberate. User hooks restore task state after the low-level MuJoCo reset, and robot ports are synchronized after the hook so the first post-reset update does not apply stale commands.
Use ResetContext when your hook needs direct MuJoCo access:
For randomized starts, generate random values in the hook and write them to MuJoCo before reset() returns.
The full viewer path is:
Useful wrapper-specific controls:
| Control | Behavior |
|---|---|
, | slow the wrapper real-time factor |
. | speed the wrapper real-time factor |
Simulation -> RTF | current wrapper real-time factor |
| Simulation -> Recorder | path, camera, resolution, FPS, start/stop, status |
Recorder camera options include Current, Free, Tracking, and every fixed camera compiled into the model.
The recorder controls live in the left Simulation panel:
| Field | Meaning |
|---|---|
Path | Output MP4 path, relative to the process working directory unless absolute |
Camera | Current, Free, Tracking, or any compiled fixed camera |
Resolution | 360p, 480p, 720p, or 1080p |
FPS | Recording frame rate |
Start rec | Opens EGL recorder and starts feeding frames |
Stop rec | Closes ffmpeg and finalizes the MP4 |
Rec | idle, recording, or failed |
Current means the recorder follows the Simulate viewer camera. A fixed camera records from that camera even if the live viewer is moved elsewhere.
When recording stops successfully, the terminal prints:
If the status changes to failed, check that ffmpeg is installed and the path is writable.
Interactive recording is available in the Simulate UI:
Path, Camera, Resolution, and FPS.Start rec.Stop rec.The terminal prints:
Headless recording uses the same VideoRecorder API:
This section assembles the pieces into a complete pick-place task. The goal is to pick a cube from one table location, move it to another location, open the gripper, and retreat. The full implementation is src/examples/ex_table_pick_place.cpp; the code below shows the structure you should reproduce in your own application.
The application has five parts:
Start with the gripper attachment:
Add the table as an MJCF-backed SceneObject. The table asset origin is the tabletop surface center, so placing it at z = 0.7 makes the top surface 0.7 m.
Add a free cube. For a box, size is half-extents, so the center z coordinate is surface_z + half_height.
Assemble the scene:
The table asset defines a table_top site. Because MJCF-backed objects are prefixed when attached, get the compiled site name through the helper:
Now define task points from the table surface:
This makes the task robust if you move the table asset or swap it for another asset with the same site convention.
Initialize the robot with gripper inertia and TCP site:
Create KDL solvers:
q_min and q_max come from robot.joint_limits. Keep them in KDL::JntArray so IK respects the compiled model limits.
Use one orientation for the gripper and solve a sequence of poses:
Solve each waypoint seeded from the previous solution:
In production code, print the target names when IK fails. It saves time when a single pose is outside the workspace or has an impossible orientation.
Keep state configuration data-driven. Each row says where to move, how long to interpolate, how long to wait before forcing transition, and what the gripper should do.
On entry to a state, capture the measured joint position. Interpolate from that entry pose to the state target:
Transition when either the duration has elapsed and the joint error is small, or the timeout is reached:
Use KDL gravity plus joint PD:
The gripper actuator is model-specific. For the Robotiq Menagerie model, examples write the gripper command directly to its actuator control after update():
Reset must restore both the robot and task objects:
Because reset(&env) syncs registered robot command ports after this hook, the first control step after reset starts from the reset pose without stale torque or position commands.
Start the Simulate UI:
Use , and . to slow down or speed up the wrapper real-time factor. In the Simulation panel, the Recorder subsection can write an MP4 using Current, Free, Tracking, or any compiled fixed camera.
Before treating the example as working, check:
set_body_pose() can reset it.This is the architecture used by ex_table_pick_place: a small scene spec, a reset hook, precomputed IK waypoints, a data-driven state table, and one control law used consistently across states.
Use prefixes to disambiguate the second robot:
Then initialize two robot handles:
Each robot gets its own KDL chain and command ports, while both share the same MuJoCo model/data.
The included examples show how these pieces combine:
ex_table_scene: table asset, primitive objects, sites, cameras, reset hook.ex_pick: IK waypoints, state machine, torque impedance.ex_table_pick_place: tabletop pick/place using table asset sites.ex_table_pour: gripper-held bottle asset, free particles, receiver asset.ex_dual_arm: two prefixed robots in one scene.ex_record: headless MP4 recording.Read src/examples/README.md for behavior summaries and expected outputs.
For occasional changes, use the scene add/remove helpers. They rebuild the model, so this is for task setup and coarse changes, not per-frame object spawning.
Raw model/data form:
Env form:
After a rebuild, any cached MuJoCo IDs may be invalid. Recompute body IDs, joint IDs, site names, and KDL solvers that depend on the old model.
Use this checklist when a scene behaves incorrectly:
| Symptom | Check |
|---|---|
| Robot does not move in position mode | Model has actuators and kdl_to_mj_ctrl[i] >= 0 |
| First step after reset jumps | Robot is registered with env_add_robot() and reset uses reset(&env) |
| KDL gravity is wrong with a tool | ToolFrameSpec::tool_body points at the tool subtree root |
| TCP frame is wrong | ToolFrameSpec::tcp_site names an authored MuJoCo site |
| Object asset site not found | Use scene_object_site_name(object, "site") to account for prefixes |
| Recorder fails | BUILD_RECORDER=ON, EGL available, ffmpeg installed, output path writable |
| Camera missing in recorder list | Camera must exist in the compiled mjModel (get_camera_names(model)) |
Run the default build:
Run clang-tidy on wrapper code:
Run tests:
The vendored src/simulate_ui/simulate.cc is MuJoCo 3.8.0 sample UI code with small wrapper UI additions. It is intentionally excluded from clang-tidy style cleanup so local changes stay reviewable against upstream.