Blog

tf2 for Mobile Robots: The Transform Tree That Makes Sensor Fusion Work

Tom Cosgrove,

Reading Room: The Fictionlab Blog

TL;DR: A correct tf2 transform tree on a robot lets ROS 2 answer one question at any timestamp: “where is frame A relative to frame B?” Without it, LiDAR points, camera data, and wheel odometry sit in disconnected coordinate systems and cannot be fused. This guide shows how to design a tf tree for a mobile robot like Leo Rover, which transforms should be static and which dynamic, how to diagnose broken trees with tf2_tools, and how to avoid the two failure modes that break most integrations: missing static transforms and mismatched timestamps.

Why a correct tf2 transform tree matters for mobile robots

Every sensor on a mobile robot produces data in its own frame of reference. A LiDAR reports points relative to its sensor frame. A depth camera publishes images and depth data associated with its camera frames. Wheel encoders can be used to estimate motion in an odometry frame. If you want to project a LiDAR scan onto a camera image, or feed both into an EKF, you need a single mathematical structure that expresses how those frames relate to each other over time. That structure is the tf2 transform tree.

tf2 is the second-generation transform library in ROS 2. It maintains a tree of coordinate frames, buffers transforms with timestamps, and can interpolate between dynamic transforms on demand. If the tree is disconnected, or if a required transform is missing at the queried time, the lookup fails and downstream nodes may stop producing output. A Nav2 stack that cannot resolve the transforms between its global frame and robot base frame will not plan correctly. A point cloud assembler that cannot resolve the transform between base_link and velodyne will drop scans.

The tree is not optional bookkeeping. It is the shared geometric contract between every node in your system.

What the tf tree of a real rover looks like

Consider a Leo Rover configured for outdoor mapping with a 2D LiDAR, an IMU, a depth camera, and a GNSS receiver. The canonical tree looks like this:

  • map – the global frame, typically maintained by SLAM, localization, or GNSS fusion
  • odom – continuous but drifting, published by wheel odometry or an EKF
  • base_footprint – the robot’s projection on the ground plane
  • base_link – the rigid body frame of the chassis
  • imu_link, laser, camera_link, gps_link – sensor frames rigidly attached to base_link
  • camera_optical_frame – the ROS optical convention (Z forward, X right) child of camera_link

The parent-child chain reads: map -> odom -> base_footprint -> base_link -> {imu_link, laser, camera_link -> camera_optical_frame, gps_link}. Each child frame must have only one parent, and one authoritative source should publish each dynamic transform. If two nodes publish conflicting transforms for odom -> base_footprint, consumers can receive inconsistent pose data and the estimate may jitter.

Which transforms are static and which are dynamic

Static transforms describe rigid geometry that never changes at runtime: the position of the LiDAR mount, the IMU offset, the camera baseline. Publish them once with tf2_ros static_transform_publisher or, better, embed them in the URDF and let robot_state_publisher emit them. Dynamic transforms describe motion: odom -> base_footprint from wheel odometry, map -> odom from SLAM or AMCL.

A minimal static publisher for a LiDAR mounted 15 cm above base_link:

ros2 run tf2_ros static_transform_publisher --x 0.1 --y 0 --z 0.15 --yaw 0 --pitch 0 --roll 0 --frame-id base_link --child-frame-id laser

For anything more than one or two sensors, put the geometry in URDF. It is versioned, reviewable, and consumed by RViz and Gazebo as well.

How to diagnose a broken tf tree with tf2_tools

When sensor fusion silently fails, the tree is the first place to look. ROS 2 ships two tools that answer 90% of the questions you will have.

To generate a diagram of the available tree, run:

ros2 run tf2_tools view_frames

This subscribes to /tf and /tf_static for a short period, then normally writes frames.pdf, provided Graphviz is available. The diagram includes frames, parents, broadcasters, average rates, and buffer information. Check that all frames required by a given application are connected, and that dynamic edges publish at rates appropriate for that application.

To query a specific transform, use:

ros2 run tf2_ros tf2_echo map base_link

This prints the current translation and quaternion. If it repeatedly reports an invalid frame ID or extrapolation errors, you have found your bug.

The two failure modes you will actually hit

Most tf problems on a real robot reduce to one of two causes.

  • Missing static transform. A sensor driver publishes data in camera_optical_frame, but nothing connects that frame to base_link. The frames needed by the application are disconnected. view_frames will reveal the separate components. Fix by adding the URDF joint or a static publisher.
  • Timestamp mismatch. A driver stamps messages with a zero or incorrect timestamp, or with a clock that is not synchronized with the ROS clock. tf2 refuses to interpolate transforms that lie outside its buffer window, which is commonly 10 seconds by default. You may see errors such as “Lookup would require extrapolation into the past by 0.4s”. Fix the timestamp source and synchronize clocks; increase the listener buffer duration only when delayed data genuinely requires it.

A subtler variant: your IMU driver stamps at hardware-receive time, but your camera stamps at publish time. The resulting delta can degrade tight fusion. Use hardware timestamping where possible, or apply a validated fixed time offset in the relevant driver or fusion node.

How tf2 enables sensor fusion in practice

Once the tree is intact, fusion becomes mechanical. An EKF node such as robot_localization can fuse wheel odometry and IMU data using the frames declared in those messages. Raw GNSS fixes are typically converted into an odometry or pose measurement first, for example with navsat_transform_node. tf2 supplies the transforms required to express measurements consistently with the filter configuration. If the LiDAR frame is off by 5 cm because someone measured the mount wrong, every scan match in SLAM inherits that error and your map quality suffers.

The same tree drives Nav2. A costmap layer transforms incoming point clouds from a sensor frame into the costmap’s configured global frame through the available tf chain. A mobile-robot mapping stack commonly publishes multiple frames, and tf lookups must be available at the timestamps of incoming sensor data. If transforms arrive too late or sensor data exceeds the costmap’s configured tolerance, Nav2 can discard stale observations.

For research platforms used in field robotics experiments, the discipline is the same: describe the geometry once in URDF, verify with view_frames before every deployment, and treat the tree as production configuration, not scratch work. Custom integrations built by Fictionlab’s team follow the same pattern regardless of payload complexity.

A reproducible checklist before you fuse anything

Before you enable an EKF or launch Nav2, walk through this sequence:

  1. Run ros2 run tf2_tools view_frames and confirm that all frames required by your application are connected.
  2. For every sensor topic, run ros2 topic echo <topic> --once and check the frame_id matches a frame that exists in the tree.
  3. Run ros2 run tf2_ros tf2_echo <source_frame> <target_frame> for each fusion input. It must return a valid transform without extrapolation errors.
  4. Check publish rates in view_frames. Dynamic-transform rates must be sufficient for the robot speed, sensor rates, and consumers using them.
  5. Verify clock consistency: ros2 topic echo /tf --field transforms[0].header.stamp and compare against ros2 topic echo /clock if using simulation, or against the host’s ROS time otherwise.

If all five pass, your tf2 transform tree is ready to support fusion. If any fail, fix the tree first. Debugging a Kalman filter on top of a broken tree is a waste of an afternoon.

Frequently asked questions

Q: What is the difference between /tf and /tf_static?

A: /tf_static uses transient-local QoS and carries transforms that never change. /tf carries time-varying transforms. Publish static geometry on /tf_static so late-joining subscribers can receive it.

Q: Can I have two nodes publishing the same tf edge?

A: You should not. tf2 does not provide a safe way to merge conflicting publishers for the same child frame. If both AMCL and an EKF try to publish map -> odom, consumers can receive inconsistent transforms. Pick one localization source.

Q: Why does tf2 complain about extrapolation into the future?

A: A node asked for a transform at a timestamp newer than any transform in the buffer. Usually the sensor clock runs ahead of the ROS clock or the transform publisher is delayed. Synchronize clocks and reduce publication latency. Use tf2::TimePointZero only when using the latest available transform is acceptable, because it does not preserve measurement-time alignment.

Q: How large should the tf buffer be?

A: The commonly used default is 10 seconds. Increase it when you must process delayed messages or request older transforms, and size it according to the expected delay and transform rate. Larger buffers use more memory.

Q: Do I need base_footprint if my robot is not humanoid?

A: Not strictly. Nav2 can be configured to use another robot base frame, but base_footprint is a useful convention for a frame projected onto the ground plane. Keeping it as the parent of base_link can improve compatibility with common mobile-robot configurations.

Q: Can I publish transforms from Python at high rate?

A: Yes. Whether Python is sufficient depends on the transform rate, message volume, and workload of the process. Profile the system, and use C++ or publish directly from the driver if Python cannot meet timing requirements.

Q: How do I visualize the tree while the robot is moving?

A: Open RViz, add the TF display, and enable the frames you care about. RViz updates in real time and can show axis triads at frame origins.

Build your tf tree once, verify it always

A correct transform tree is the difference between a robot that fuses sensors and a robot that produces error messages. Design it in URDF, publish static edges through robot_state_publisher, keep dynamic edges to one authoritative publisher each, and run view_frames before every field session. For step-by-step configuration of the Leo Rover tf tree, ROS 2 launch files, and sensor integration examples, consult the technical documentation at docs.fictionlab.pl.


Read more