0
$\begingroup$

Rosanswers logo

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;
  timesource.attachClock(clock);

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




  tf2::Transform transform_;
  transform_.setIdentity();
  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);
        publisher->publish(message);
    }
    catch (tf2::TransformException & ex) {
          RCLCPP_ERROR(node->get_logger(), "StaticLayer: %s", ex.what());
    }


    rclcpp::spin_some(node);
    loop_rate.sleep();
  }
  rclcpp::shutdown();
  return 0;
}

Originally posted by Sol on ROS Answers with karma: 3 on 2019-11-25

Post score: 0


Original comments

Comment by kscottz on 2019-11-25:
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.

Comment by jacobperron on 2019-11-27:
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.

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

Hi! I had same trouble with lookup_transform. I'm found solution in tf_echo source code. You need add tf2_ros::TransformListener and init it with tf2_ros::Buffer:

std::unique_ptr<tf2_ros::Buffer> buffer = std::make_unique<tf2_ros::Buffer>(node->get_clock());
std::shared_ptr<tf2_ros::TransformListener> transform_listener_ = std::make_shared<tf2_ros::TransformListener>(*tf_buffer_);

Now when I call lookupTransform() it works as expected. I hope this helps you.


Originally posted by georgiy with karma: 76 on 2020-06-20

This answer was ACCEPTED on the original site

Post score: 6

$\endgroup$
0
$\begingroup$

Rosanswers logo

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.


Originally posted by jacobperron with karma: 1870 on 2019-11-27

This answer was NOT ACCEPTED on the original site

Post score: 3


Original comments

Comment by Almost on 2020-07-15:
From the description of tf2_ros I understand that this package is for ROS and roscpp, not ROS2. So why is it being used with ROS2? Isn't that some abomination?

Comment by jacobperron on 2020-07-15:
I think "port" is a better word than "abomination", haha.

tf2_ros is a set of ROS bindings for the tf2 library. Since the ROS 1 implementation proved very useful to robot makers, there exists a ROS 2 port. Since the client libraries for ROS 1 and ROS 2 are significantly different (roscpp vs rclcpp), we can expect the respective tf2_ros bindings to look different. This is why we currently maintain a separate ROS 2 version of tf2_ros, as the implementation diverges from the original.

Comment by Almost on 2020-07-15:
I understand why you would separate some interface to make the code maintenance easier. But I still don't see why it is not all in tf2:: namespace, or always tf2:: for ros2 and tf2_ros for ros. Now I even see ros2 code that uses both. I am sorry, I don't know the architecture and it just doesn't make sense to me.

Comment by jacobperron on 2020-07-15:
The differences in namespaces are coming from different packages in the geometry2 repository. There is a package named tf2, which is ROS-agnostic. ROS-specific bindings/features are provided by a different package tf2_ros (which depends on tf2). The different namespaces, tf2:: and tf2_ros::, are named after the package where the API is coming from. To clarify, this is the same package/namespace structure used in both ROS 1 and ROS 2.

Comment by jacobperron on 2020-07-15:
So, in theory, a single version of tf2 could be maintained and used by multiple versions of tf2_ros (but I think for historical reasons and convenience, we have separate versions of all geometry2 packages for ROS 1 and ROS 2).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.