ROS 2 Lyrical Luth · Lesson 43 of 86
Inertial modeling
Mass, centre of mass and the inertia tensor of boxes, cylinders and spheres.
Estimated time · 45–75 min
Learning goals
What you will understand
- Explain inertial modeling in your own words
- Use it in a small robotics example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
Physics engines need every link's mass and inertia. The <inertial> tag gives the mass (kg), the centre-of-mass origin and a 3×3 symmetric inertia matrix. For a solid box of mass m and size w (x), d (y), h (z): Ixx = m/12 (d² + h²), Iyy = m/12 (w² + h²), Izz = m/12 (w² + d²).
Wrong inertias make simulated robots jitter or explode. Never leave them at zero or copy the same value everywhere; compute from the geometry or export from CAD. A cylinder of radius r, height h: Ixx = Iyy = m/12 (3r² + h²), Izz = m r² / 2.
Guided lesson
Work through these steps
- 1.
Source the ROS 2 Lyrical environment in a fresh terminal.
- 2.
Read the inertial modeling example and identify what each line does.
- 3.
Build only the affected package, source the workspace overlay, and run the example.
- 4.
Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.
Worked example
Read it before you run it
<inertial>
<!-- Physical properties of the link. -->
<mass value="2.0"/>
<!-- Two kilograms. -->
<origin xyz="0 0 0"/>
<!-- Centre of mass at the link origin. -->
<inertia ixx="0.00208" iyy="0.00708" izz="0.00833" ixy="0" ixz="0" iyz="0"/>
<!-- Inertia of a 0.2 × 0.1 × 0.05 m box of 2 kg (products of inertia are zero). -->
</inertial>
<!-- End of the properties. -->What the example is doing
- 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
- 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
- 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.
Watch it run · Robot arm
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for Inertial modeling. Expected result: ixx=0.00208333 iyy=0.00708333 izz=0.00833333
Practice code
Now type it yourself
Copy the starter into practice.cpp and replace every TODO with your own code. Build and run it: g++ -std=c++17 -Wall -Wextra -Wpedantic practice.cpp -o practice -pthread && ./practice
#include <iomanip>
#include <iostream>
int main() {
const double m = 2.0, w = 0.2, d = 0.1, h = 0.05; // mass and box size (x, y, z)
// TODO: compute the box inertia: ixx = m/12*(d*d+h*h), iyy = m/12*(w*w+h*h), izz = m/12*(w*w+d*d)
std::cout << std::setprecision(6) << "ixx=" << ixx << " iyy=" << iyy << " izz=" << izz << '\n';
return 0;
}Expected output
ixx=0.00208333 iyy=0.00708333 izz=0.00833333Stuck? Show the full solution (try it yourself first)
#include <iomanip>
#include <iostream>
int main() {
const double m = 2.0, w = 0.2, d = 0.1, h = 0.05; // mass and box size (x, y, z)
const double ixx = m / 12.0 * (d * d + h * h);
const double iyy = m / 12.0 * (w * w + h * h);
const double izz = m / 12.0 * (w * w + d * d);
std::cout << std::setprecision(6) << "ixx=" << ixx << " iyy=" << iyy << " izz=" << izz << '\n';
return 0;
}Expected result
How you know it worked
- ROS_DISTRO is lyrical in every terminal used for the exercise.
- The complete package from the exercise builds and the expected graph entity appears in a second terminal.
Practical exercise
Apply the lesson
Compute the inertia of a 2 kg box (0.2 × 0.1 × 0.05 m) and a 0.5 kg wheel (r = 0.05, h = 0.03), put them in a URDF, then simulate with a deliberately wrong (zero) inertia and describe what happens.
Before you continue
- ROS_DISTRO reports lyrical in every terminal you used
- The example builds and runs without errors
- You checked the result with a ros2 command, RViz2 or a test, not just by reading the code
If something goes wrong
Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.
Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.