ROS 2 Lyrical Luth · Lesson 53 of 86
Odometry estimation
Dead reckoning for a differential drive, drift and nav_msgs/Odometry.
Estimated time · 60–90 min
Learning goals
What you will understand
- Explain odometry estimation 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
Wheel odometry integrates velocities: x += v cos θ dt, y += v sin θ dt, θ += ω dt. With encoders, v = (v_right + v_left)/2 and ω = (v_right − v_left)/wheel_separation. It is smooth but drifts with wheel slip and small measurement errors.
Publish the result as nav_msgs/msg/Odometry (pose plus twist and covariances) on /odom and broadcast the transform odom → base_link. Fuse with an IMU or lidar localization to reduce drift. The diff_drive_controller already does this for a real or simulated robot.
Guided lesson
Work through these steps
- 1.
Source the ROS 2 Lyrical environment in a fresh terminal.
- 2.
Read the odometry estimation 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 v = 0.5, w = 0.0, dt = 0.1;
// Linear and angular velocity for this step.
x += v * std::cos(theta) * dt;
// Advance along the current heading (X).
y += v * std::sin(theta) * dt;
// Advance along the current heading (Y).
theta += w * dt;
// Turn by the angular velocity.
nav_msgs::msg::Odometry odom;
// The message to publish.
odom.pose.pose.position.x = x;
// Write the position into the message.
odom.pose.pose.orientation.z = std::sin(theta / 2); odom.pose.pose.orientation.w = std::cos(theta / 2);
// Convert yaw to a quaternion.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 · Mobile robot
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 Odometry estimation. Expected result: x=1.00 y=1.00 theta=1.57
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 <cmath>
#include <cstdio>
int main() {
double x = 0.0, y = 0.0, theta = 0.0;
const double dt = 0.1;
auto step = [&](double v, double w) {
// TODO: Euler update: x += v*cos(theta)*dt, y += v*sin(theta)*dt, theta += w*dt
};
for (int i = 0; i < 20; ++i) step(0.5, 0.0); // drive 1 m
for (int i = 0; i < 10; ++i) step(0.0, M_PI / 2); // turn 90 degrees in 1 s
for (int i = 0; i < 20; ++i) step(0.5, 0.0); // drive 1 m
std::printf("x=%.2f y=%.2f theta=%.2f\n", x + 0.0, y, theta);
return 0;
}Expected output
x=1.00 y=1.00 theta=1.57Stuck? Show the full solution (try it yourself first)
#include <cmath>
#include <cstdio>
int main() {
double x = 0.0, y = 0.0, theta = 0.0;
const double dt = 0.1;
auto step = [&](double v, double w) {
x += v * std::cos(theta) * dt;
y += v * std::sin(theta) * dt;
theta += w * dt;
};
for (int i = 0; i < 20; ++i) step(0.5, 0.0); // drive 1 m
for (int i = 0; i < 10; ++i) step(0.0, M_PI / 2); // turn 90 degrees in 1 s
for (int i = 0; i < 20; ++i) step(0.5, 0.0); // drive 1 m
std::printf("x=%.2f y=%.2f theta=%.2f\n", x + 0.0, y, theta);
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
Integrate odometry for a robot that drives 1 m, turns 90 degrees left, then drives 1 m. Compare your result with /odom from the simulated diff drive and explain any difference.
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.