Using ROS 2? Check out the ROS 2 tf2 tutorials.

(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Migrating a TransformBroadcaster from tf to tf2

Description: This is a guide for converting a tf TransformBroadcaster class to a tf2 Broadcaster class

Tutorial Level: INTERMEDIATE

Primary difference

The TransformBroadcaster uses the public API only and populates an independently allocated tf2 Buffer.

C++

Old Style

Depend on tf package.

   1 #include "tf/transform_broadcaster.h"
   2 #include "tf/transform_datatypes.h"
   3 #include "geometry_msgs/TransformStamped.h"
   4 
   5 tf::TransformBroadcaster tfb;
   6 
   7 geometry_msgs::TransformStamped transformGeom = ...;
   8 tfb.sendTransform(transformGeom);
   9 
  10 tf::StampedTransform transformTf = ...;
  11 tfb.sendTransform(transformTf);

Equivalent tf2 Replacement

Depend on tf2_ros package and possibly also tf2_geometry_msgs.

   1 #include "tf2_ros/transform_broadcaster.h"
   2 #include "tf2_geometry_msgs/tf2_geometry_msgs.h"
   3 #include "geometry_msgs/TransformStamped.h"
   4 
   5 tf2_ros::TransformBroadcaster tfb;
   6 
   7 geometry_msgs::TransformStamped transformGeom = ...;
   8 tfb.sendTransform(transformGeom);  # this stays the same
   9 
  10 # there is no tf2::StampedTransform... 
  11 # only tf2::Stamped<Transform> which misses child_frame_id
  12 tf2::Stamped<Transform> transformTf = ...;
  13 geometry_msgs::TransformStamped transformTfGeom = tf2::toMsg(transformTf);
  14 transformTfGeom.child_frame_id = ...;
  15 tfb.sendTransform(transformTfGeom);

Python

Old Style

Depend on tf package.

   1 import tf
   2 tfb = tf.TransformBroadcaster()

Equivalent tf2 Replacement

Depend on tf2_ros pacakage.

   1 import tf2_ros
   2 tfb = tf2_ros.TransformBroadcaster()

Wiki: tf2/Tutorials/Migration/TransformBroadcaster (last edited 2022-10-06 16:52:15 by ShaneLoretz)