Ask Your Question

[ROS2]Are there any examples of tf2 broadcast or listener

asked 2019-11-25 08:16:21 -0600

Sol gravatar image

Hello comrades, I`m just trying to get odometry from t265 from realsense2_lib examples. After compiling and running this code below, I get that target frame does not exist.

Thanks in advance.

#include <math.h>
#include <iostream>
#include <chrono>
#include "std_msgs/msg/string.hpp"
#include "rclcpp/rclcpp.hpp"
#include <rclcpp/time_source.hpp>
#include "geometry_msgs/msg/pose_stamped.h"
#include <geometry_msgs/msg/pose_stamped.hpp>
#include <tf2_ros/buffer_interface.h>
#include <tf2_ros/visibility_control.h>
#include <tf2/buffer_core.h>
#include <tf2/time.h>
#include <tf2_ros/buffer.h>
#include <tf2_ros/transform_listener.h>
#include <rclcpp/rclcpp.hpp>
#include <geometry_msgs/msg/transform_stamped.h>
/* We do not recommend this style anymore, because composition of multiple
 * nodes in the same executable is not possible. Please see one of the subclass
 * examples for the "new" recommended styles. This example is only included
 * for completeness because it is similar to "classic" standalone ROS nodes. */

int main(int argc, char * argv[])
  rclcpp::init(argc, argv);
  auto node = rclcpp::Node::make_shared("tf_pub");
  auto publisher = node->create_publisher<geometry_msgs::msg::PoseStamped>("tf_pub", 10);

  //printf("TimePoint %d",tp);
//  std::shared_ptr<rclcpp::Clock> clock_ptr;
  rclcpp::Clock::SharedPtr clock = std::make_shared<rclcpp::Clock>(RCL_ROS_TIME);
  rclcpp::TimeSource timesource;

//  tf2_ros::Buffer tfBuffer(clock_ptr);
  std::unique_ptr<tf2_ros::Buffer> buffer = nullptr;

  tf2::Transform transform_;
  buffer = std::make_unique<tf2_ros::Buffer>(clock);

  geometry_msgs::msg::PoseStamped message;
  rclcpp::WallRate loop_rate(500ms);

  while (rclcpp::ok()) {

    try {

        geometry_msgs::msg::TransformStamped transformStamped = buffer->lookupTransform("odom_frame", "camera_link", tf2::TimePointZero);
        message.pose.position.x = transformStamped.transform.translation.x;
        message.pose.position.y = transformStamped.transform.translation.y;
        RCLCPP_INFO(node->get_logger(), "Publishing: '%s'", message.pose);
    catch (tf2::TransformException & ex) {
          RCLCPP_ERROR(node->get_logger(), "StaticLayer: %s", ex.what());

  return 0;
edit retag flag offensive close merge delete


This probably doesn't help, but we did push a big set of changes to the documentation and how to's for Eloquent. Check them out here.

kscottz gravatar imagekscottz ( 2019-11-25 12:38:36 -0600 )edit

The code looks okay to me. Are you sure that the target frame ("odom_frame") does indeed exist? You can confirm by running ros2 run tf2_ros tf2_monitor odom_frame camera_link.

jacobperron gravatar imagejacobperron ( 2019-11-27 15:36:55 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2019-11-27 15:55:17 -0600

jacobperron gravatar image

Unfortunately, we don't have any nice, succinct examples, but I think it would be good if we port the tf2 tutorials to ROS 2.

I know RViz makes use of tf2_ros::TransformListener as part of the TFWrapper class.

The AmclNode class also uses tf2_ros::TransformListener as well as tf2_ros::TransformBroadcaster. I'm sure there's other instances of them being used in the wild that others might be able to point you to.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-11-25 08:16:21 -0600

Seen: 99 times

Last updated: Nov 27 '19