ROS 2 Lyrical Luth · Lesson 52 of 86
IMU fusion
Gyro and accelerometer errors, complementary filter and robot_localization.
Estimated time · 60–90 min
Learning goals
What you will understand
- Explain imu fusion 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
A gyroscope gives smooth angular velocity but its integral drifts; an accelerometer gives an absolute gravity direction but is noisy and disturbed by motion. A complementary filter combines them: angle = α (angle + gyro·dt) + (1 − α) accel_angle, with α near 0.98.
For full robots use robot_localization's EKF or the Madgwick/Mahony filters (imu_filter_madgwick). They fuse IMU, wheel odometry and other sources into one estimate. Always check the IMU frame and axis directions against REP 103 (x forward, y left, z up).
Guided lesson
Work through these steps
- 1.
Source the ROS 2 Lyrical environment in a fresh terminal.
- 2.
Read the imu fusion 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
double alpha = 0.98;
// Trust the gyro 98% of the time step.
double accel_angle = std::atan2(ax, az);
// Absolute tilt from the gravity direction.
angle = alpha * (angle + gyro_y * dt) + (1.0 - alpha) * accel_angle;
// Blend the integrated gyro with the accelerometer angle.
// Repeat every IMU sample; the gyro gives fast response, the accelerometer removes drift.
// How the two sensors cover each other's weakness.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 · Sensor view
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 IMU fusion. Expected result: 0.0108
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 <iostream>
int main() {
const double alpha = 0.98, dt = 0.1;
double angle = 0.0;
const double gyro = 0.1, accel_angle = 0.05;
// TODO: blend the gyro and the accelerometer: angle = alpha*(angle + gyro*dt) + (1-alpha)*accel_angle
std::cout << angle << '\n';
return 0;
}Expected output
0.0108Stuck? Show the full solution (try it yourself first)
#include <iostream>
int main() {
const double alpha = 0.98, dt = 0.1;
double angle = 0.0;
const double gyro = 0.1, accel_angle = 0.05;
angle = alpha * (angle + gyro * dt) + (1.0 - alpha) * accel_angle;
std::cout << angle << '\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
Record an IMU bag of a tilting robot, run the complementary filter offline, and compare it with the gyro integral alone. Then run imu_filter_madgwick and compare the orientation.
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.