CodeTheRobot

ROS 2 Lyrical Luth · Lesson 17 of 86

TF2 transforms

Frames, static and dynamic broadcasters, lookups and debugging the tree.

Estimated time · 60–90 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, sensors, map). TF2 keeps them as a tree of transforms (translation plus a quaternion rotation) that change over time. A broadcaster publishes 'child frame in parent frame'; a buffer and listener let any node ask for the transform between two frames.

Static transforms (fixed sensor mounts) use static_transform_publisher; moving ones are broadcast continuously. Lookups can fail if data is missing, so catch tf2::TransformException. Debug with ros2 run tf2_tools view_frames and ros2 run tf2_ros tf2_echo.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the tf2 transforms example and identify what each line does.

  3. 3.

    Build only the affected package, source the workspace overlay, and run the example.

  4. 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

geometry_msgs::msg::TransformStamped t;
// A transform with a timestamp.
t.header.stamp = now();
// The time this transform is valid for.
t.header.frame_id = "base_link";
// Parent frame.
t.child_frame_id = "lidar_link";
// Child frame.
t.transform.translation.z = 0.25;
// The lidar sits 25 cm above the base.
t.transform.rotation.w = 1.0;
// Identity rotation (no turn).
broadcaster_->sendTransform(t);
// Publish it on /tf.
auto tf = buffer_->lookupTransform("base_link", "lidar_link", tf2::TimePointZero);
// Ask for the latest base-to-lidar transform (throws if unavailable).

What the example is doing

  1. 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
  2. 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
  3. 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.

tip x110 y58ROBOT ARM

Demo for TF2 transforms. Expected result: Translation: [0.000, 0.000, 0.250]

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.

# Publish a fixed mount: lidar 25 cm above base_link
# TODO: run static_transform_publisher with --z 0.25 from base_link to lidar_link
# Check it from another terminal: ros2 run tf2_ros tf2_echo base_link lidar_link

Expected output

Translation: [0.000, 0.000, 0.250]
Stuck? Show the full solution (try it yourself first)
# Publish a fixed mount: lidar 25 cm above base_link
ros2 run tf2_ros static_transform_publisher --z 0.25 --frame-id base_link --child-frame-id lidar_link
# Check it from another terminal: ros2 run tf2_ros tf2_echo base_link lidar_link

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

Broadcast base_link→lidar_link statically from the CLI, then write a C++ node that broadcasts a moving base_link→camera_link at 20 Hz and a listener that prints the transform. Check the tree with view_frames.

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

ros2 cannot find a package or executable.

Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.

Nodes run but cannot discover each other.

Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.