Advanced Dynamics: Introduction to Mujoco Part 2
Watch on YouTube →
Overview
Christian Hubicki extends a MuJoCo and Python workflow for building articulated systems, recording simulation state, and plotting joint motion. He constructs a double pendulum and parallel branches in XML, explains key dynamics outputs, and implements a PD controller to drive a joint toward a 90-degree target.
Key takeaways
- MuJoCo capsule geometry can be positioned and oriented with six fromto coordinates in a body's frame, making link length and offset easy to edit in XML.
- Log qpos, qvel, and time at every simulation step, copying mutable joint arrays before appending them, then convert the logs to NumPy arrays for plotting.
- MuJoCo separates joint-space forces into bias, passive, actuator, applied, and constraint contributions, which helps relate simulation outputs to manipulator dynamics.
- Nested XML bodies define a kinematic tree; an explicit body position of z = -0.5 attaches the second pendulum at the end of the first 0.5-unit link.
- A PD controller can drive a MuJoCo joint toward a target by combining proportional position error and derivative velocity error; the example targets 90 degrees with gains 50 and 5.
- A 0.05-second simulation step produces visibly less accurate motion than smaller steps, while the example's 0.001-second setting provides finer temporal resolution.
Chapters
- Christian Hubicki sets out to cover model construction, simulation, data extraction, plotting, and animation in MuJoCo.
- The ongoing example is a cart with a pendulum, implemented in a Google Colab Python notebook.
- The longer-term goal is to apply MuJoCo to more complicated systems involving contact and constraints.
- The model contains a ground plane, gravity, a cart connected to the world by a slider, and a pendulum attached by a hinge.
- MuJoCo's XML describes bodies and their connecting joints as a kinematic tree.
- A cart force drives the example to the right while gravity points downward.
- A capsule is specified by a radius and a fromto attribute containing two 3D endpoints: x1, y1, z1 and x2, y2, z2.
- The endpoints are coordinates in the body frame, so changing the endpoint from z = -0.3 to z = -0.6 lengthens the pendulum downward.
- Changing the endpoint to a positive x or y value places the capsule along that axis, including a floating segment offset from the joint.
- MuJoCo computes rotational inertia from the body's mass and geometry when explicit inertia values are not supplied.
- The example assigns the pendulum a mass of 0.1, while more advanced models can provide a 3 by 3 inertia tensor.
- Christian flags quaternions and Euler angles as important topics for representing 3D rotations.
- Adding bodies means nesting them deeper in the XML hierarchy, such as world to cart to pole to another pole.
- The existing animation code renders each simulation frame and stacks the resulting images into a video-like sequence.
- For analysis, the same simulation loop can record numerical state instead of only rendered frames.
- The cart-pendulum has two generalized coordinates, cart position x and pendulum angle theta, stored in MuJoCo's qpos array.
- Joint velocities are stored in qvel, while simulation time is available in the data structure.
- Python lists are initialized for time, qpos, and qvel, then appended to at each simulation step.
- The code copies qpos and qvel at each step so later updates to MuJoCo's data do not overwrite previously logged values.
- The logged lists are converted with np.array before plotting them as ordinary numerical arrays.
- Rows represent successive simulation times, while columns represent different joints; Python uses zero-based indexing.
- The notebook imports matplotlib.pyplot as plt and plots simulation time against the cart's first qpos column.
- The cart position rises as the applied force moves it forward, then approaches a near-linear trajectory as damping limits its speed.
- A second subplot displays the pendulum angle in radians using the next qpos column.
- The timestep is set in the model's option block, alongside settings such as gravity.
- The example reduces the step to 0.001 seconds, then tests a much larger 0.05-second step.
- The larger step makes the motion visibly choppier and less accurate, illustrating the timestep tradeoff.
- Useful data fields include time, qpos for joint positions, qvel for joint velocities, and ctrl for actuator commands.
- qfrc_applied applies external forces at joints, while xfrc_applied applies forces to bodies.
- MuJoCo also exposes qacc for joint accelerations, Cartesian acceleration data, body orientations as quaternions, and actuator forces.
- MuJoCo's dynamics breakdown includes the inertia matrix times joint acceleration, bias forces, passive forces, actuator forces, applied forces, and constraint forces.
- The bias term includes Coriolis, centrifugal, and gravity contributions, while passive forces include elements such as springs and dampers.
- Constraint forces represent contact and other constraints; the inertia matrix can be retrieved from the data structure for inspection.
- The new body is nested under the first pendulum, extending the kinematic chain from world to cart to pole one to pole two.
- Pole two receives a hinge joint around the y-axis and a capsule geometry defined with fromto endpoints.
- The example reuses a mass of 0.1 and a capsule radius of 0.03, then relies on MuJoCo to include the new degrees of freedom.
- Live debugging catches malformed XML, missing equals signs, incorrect closing tags, and confusion over where nested bodies belong.
- A helper in the chat identifies that the second pendulum needs an explicit body position to attach at the end of the first link.
- Setting the new body's position to z = -0.5 places its joint at the first pendulum's endpoint and produces a working double pendulum.
- A second actuator is associated with pivot two; applying a torque of 1 newton-meter produces a large response because the links have small masses.
- The controller computes torque from proportional angle error and derivative velocity error, using qpos and qvel for the elbow joint.
- With gains Kp = 50 and Kd = 5, the controller targets pi/2 radians, or 90 degrees, and drives the elbow toward that angle.
- Christian copies the double-pendulum structure to create parallel branches, then resolves duplicate XML body and joint names.
- The resulting model resembles a rudimentary walking robot, with one branch actuated and the other unactuated.
- The branches contact one another in the simulation, demonstrating that MuJoCo handles body contact unless those interactions are disabled.
Transcript excerpt
0:02 Good morning everybody. Hope you're doing well. Hope you're doing great. Sorry, I'm still a little under the weather. So, I figured I would lecture from this my home studio where I have my robot mug. I didn't make this one. That's from a cafe called the broken robot cafe somewhere on the west coast. But anyway, since I am not with you today, I am here in my coding setup. though while I've been sick I've been managed I think to upgrade my uh in le inhal lecture setup for easier coding I think I think I got it working in the process of fixing this up I think I fixed that one up too so it should be easier to switch back and forth from uh from math to coding and back and forth because what I want to do next week is by the end of this week I
0:51 want you to know how the basics of using Majoko okay uh the basic like can you assemble a body? Can you put it in a simulation? Can you get the data out? Can you plot it? Can you animate it and play with it from there? Because there's so many options in Moko. You can go through their documentation just googling Mo and get it. But I want to show you the the the bones for how you build this thing. And hopefully today we will have those bones for it, right? Be able to get the things that you need. Okay. So, uh that's what I hope to do today. And next week I want to show you how you can do more advanced simulations in mat lab. Okay, in the ways that we've been setting up all of our lrangee mechanics, okay, and uh using things
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Christian Hubicki.