C++ Advanced · Lesson 17 of 23
TF2 transforms
Coordinate frames, broadcasting transforms and looking them up in C++.
Estimated time · 90–120 min
Learning goals
What you will understand
- Explain tf2 transforms 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 robot has many coordinate frames: base_link, each joint, the camera, the map. TF2 keeps them as a tree of transforms (translation plus rotation) that change over time. A transform broadcaster publishes 'child frame relative to parent frame' and a buffer plus listener lets any node ask for the transform between two frames.
Frame names are strings, orientation is a quaternion, and lookups can fail if the transform is not available yet, so catch tf2::TransformException. Inspect the tree with ros2 run tf2_tools view_frames.
Guided lesson
Work through these steps
- 1.
Read the tf2 transforms example and identify what each line owns or changes.
- 2.
Create a small source file, compile with -std=c++17 and warnings enabled, and run it.
- 3.
Change one input, predict the result before running, then compare the output.
- 4.
Record one compiler or runtime error and explain the correction in your own words.
Worked example
Read it before you run it
#include "tf2_ros/transform_broadcaster.h"
// Publishes transforms.
#include "tf2_ros/transform_listener.h"
// Receives transforms into a buffer.
#include "tf2_ros/buffer.h"
// Stores recent transforms.
geometry_msgs::msg::TransformStamped t;
// One transform with a timestamp.
t.header.stamp = now();
// Time the transform is valid for.
t.header.frame_id = "base_link";
// The parent frame.
t.child_frame_id = "camera_link";
// The child frame.
t.transform.translation.x = 0.10;
// The camera sits 10 cm in front of the base.
t.transform.rotation.w = 1.0;
// Identity quaternion: no rotation.
broadcaster_->sendTransform(t);
// Publish it to /tf.
try { auto tf = buffer_->lookupTransform("base_link", "camera_link", tf2::TimePointZero); }
// Ask for the latest base-to-camera transform.
catch (const tf2::TransformException& e) { RCLCPP_WARN(get_logger(), "%s", e.what()); }
// Handle a missing transform without crashing.What the example is doing
- 1.Read declarations from right to left: identify each value's type, name, and initial value.
- 2.Follow the data through each expression before considering syntax shortcuts.
- 3.Compile with warnings enabled; a clean build is part of the result, not an optional polish step.
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 TF2 transforms. Expected result: ros2 run tf2_ros tf2_echo base_link camera_link -> Translation: [0.100, 0.000, 0.000]
Practice code
Now type it yourself
Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.
// Inside a node with a member std::unique_ptr<tf2_ros::TransformBroadcaster> broadcaster_;
#include "geometry_msgs/msg/transform_stamped.hpp"
#include "tf2_ros/transform_broadcaster.h"
void publish_camera_frame() {
geometry_msgs::msg::TransformStamped t;
// TODO: fill the transform: stamp = now(), parent base_link, child camera_link, x = 0.10, rotation w = 1.0
broadcaster_->sendTransform(t);
}Expected output
ros2 run tf2_ros tf2_echo base_link camera_link -> Translation: [0.100, 0.000, 0.000]Stuck? Show the full solution (try it yourself first)
// Inside a node with a member std::unique_ptr<tf2_ros::TransformBroadcaster> broadcaster_;
#include "geometry_msgs/msg/transform_stamped.hpp"
#include "tf2_ros/transform_broadcaster.h"
void publish_camera_frame() {
geometry_msgs::msg::TransformStamped t;
t.header.stamp = now();
t.header.frame_id = "base_link";
t.child_frame_id = "camera_link";
t.transform.translation.x = 0.10;
t.transform.rotation.w = 1.0;
broadcaster_->sendTransform(t);
}Expected result
How you know it worked
- After adding the shown fragment to the guided exercise with its listed headers and surrounding program, the finished program builds with -Wall -Wextra -Wpedantic.
- Changing the stated input produces the result you predicted.
Practical exercise
Apply the lesson
Broadcast base_link→camera_link at 10 Hz with a slowly rotating quaternion. Write a listener node that looks up the transform each second and prints x, y, z. Check the tree with view_frames.
Before you continue
- The program builds with -std=c++17 and no warnings
- You can explain the key rule without reading the example
- The normal and edge cases both have recorded results
If something goes wrong
Start at the first error. Check spelling, required #include lines, and whether the declaration appears before use.
Print the inputs and intermediate values, then check conversions, units, and collection order.