Lab 01-01: Setting Up Your MuJoCo Environment
Objectives​
- Install MuJoCo and verify the installation
- Load and visualize a simple robot model
- Understand the MuJoCo simulation loop
- Run your first physics simulation
Materials​
| Type | Name | Version | Tier |
|---|---|---|---|
| software | Python | 3.10+ | required |
| software | MuJoCo | 3.0+ | required |
| software | NumPy | 1.24+ | required |
| data | humanoid.xml | included | required |
Prerequisites​
- Basic Python programming knowledge
- Familiarity with command line operations
Instructions​
Step 1: Install MuJoCo​
MuJoCo is now free and open-source. Install it using pip:
# In your terminal or Jupyter notebook
!pip install mujoco
Verification: Run the following to verify installation:
import mujoco
print(f"MuJoCo version: {mujoco.__version__}")
Expected: Version 3.0.0 or higher displayed without errors.
Step 2: Load Your First Model​
MuJoCo uses XML files to define robot models. Let's load the built-in humanoid:
import mujoco
import numpy as np
# Load the humanoid model (built into MuJoCo)
model = mujoco.MjModel.from_xml_path(
mujoco.util.get_resource_path("humanoid/humanoid.xml")
)
data = mujoco.MjData(model)
# Examine model properties
print(f"Number of bodies: {model.nbody}")
print(f"Number of joints: {model.njnt}")
print(f"Number of actuators: {model.nu}")
print(f"Degrees of freedom: {model.nv}")
Verification: You should see output similar to:
Number of bodies: 14
Number of joints: 18
Number of actuators: 21
Degrees of freedom: 27
Troubleshooting: If you get a file not found error, ensure MuJoCo is properly installed.
Step 3: Understand the Simulation Loop​
MuJoCo simulations advance through discrete timesteps:
# Get simulation parameters
print(f"Timestep: {model.opt.timestep} seconds")
print(f"Simulation will run at {1/model.opt.timestep:.0f} Hz")
# Reset simulation to initial state
mujoco.mj_resetData(model, data)
# Run simulation for 1 second
simulation_time = 1.0
steps = int(simulation_time / model.opt.timestep)
for i in range(steps):
# Step the simulation forward
mujoco.mj_step(model, data)
```python
```python
if i % 100 == 0:
print(f"Time: {data.time:.3f}s, Height: {data.qpos[2]:.3f}m")
Verification: The humanoid should fall under gravity. Height should decrease from ~1.3m toward 0. Expected: You see the height decreasing as simulation time increases.
Step 4: Visualize the Simulation​
Launch the interactive viewer to see your robot:
import mujoco.viewer
# Launch passive viewer (non-blocking)
with mujoco.viewer.launch_passive(model, data) as viewer:
# Run simulation with visualization
while viewer.is_running():
mujoco.mj_step(model, data)
viewer.sync()
Verification: A window should open showing the humanoid model. Expected: You can rotate the view with mouse, see the robot fall, and close the window.
Step 5: Apply Control Inputs​
Let's prevent the humanoid from falling by applying joint torques:
# Reset simulation
mujoco.mj_resetData(model, data)
# Simple standing controller - apply torques to maintain position
def simple_controller(model, data):
"""Apply proportional control to maintain initial pose."""
kp = 100 # Proportional gain
# Target is initial joint positions (all zeros for this model)
target = np.zeros(model.nu)
# Current joint positions (skip free joint - first 7 elements)
current = data.qpos[7:7+model.nu]
# Apply proportional control
data.ctrl[:] = kp * (target - current)
# Run with controller
with mujoco.viewer.launch_passive(model, data) as viewer:
```python
```python
while viewer.is_running() and data.time < 5.0:
simple_controller(model, data)
mujoco.mj_step(model, data)
viewer.sync()
Verification: The humanoid should attempt to maintain its pose rather than immediately collapsing. Expected: While not perfect, the robot should show some resistance to falling.
Expected Outcomes​
By completing this lab, you should:
- Have a working MuJoCo installation
- Understand how to load and inspect robot models
- Know the basic simulation loop structure
- Be able to visualize simulations
- Understand how control inputs affect robot behavior
Rubric​
| Criterion | Points | Excellent | Satisfactory | Needs Improvement |
|---|---|---|---|---|
| Installation | 20 | MuJoCo installed, version verified | Installed with minor issues | Unable to install |
| Model Loading | 20 | All properties correctly displayed | Model loads with warnings | Cannot load model |
| Simulation Loop | 25 | Loop runs correctly, understands timestep | Loop runs but unclear on timing | Loop fails |
| Visualization | 20 | Viewer works, can interact with scene | Viewer opens but limited interaction | Viewer fails |
| Controller | 15 | Controller implemented and explained | Controller works but not understood | Controller fails |
Total Points: 100
Extensions​
For additional challenge:
- Modify the humanoid: Edit the XML to change mass or joint limits
- Data logging: Record joint positions over time and plot them
- Different models: Load other MuJoCo models (ant, cheetah, etc.)
Common Errors​
| Error | Cause | Fix |
|---|---|---|
ModuleNotFoundError: mujoco | MuJoCo not installed | Run pip install mujoco |
GLFWError | Display issues | Set MUJOCO_GL=egl environment variable |
FileNotFoundError | Model path wrong | Use mujoco.util.get_resource_path() |