ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

waitForTransform fails in Callback

asked 2019-03-16 03:58:50 -0500

leon J. gravatar image

I want to get the transform from "/world" to "/velodyne" so that I can obtain the point cloud in the world frame. The code is as follows:

#include <ros/ros.h>
#include <sensor_msgs/PointCloud2.h>
#include <tf_conversions/tf_eigen.h>
#include <tf/transform_broadcaster.h>
#include <tf/transform_listener.h>
#include <tf/transform_datatypes.h>

void scanCallback(const sensor_msgs::PointCloud2& cloud_msg_in){
    tf::TransformListener tf_listener_;
    if(tf_listener_.waitForTransform("world", "velodyne", cloud_msg_in.header.stamp, ros::Duration(10.0))){
        std::cout<<"[tf is obtained]"<<std::endl;
        tf::StampedTransform tf_transform;
        tf_listener_.lookupTransform("world", "velodyne", cloud_msg_in.header.stamp, tf_transform);
    }else{
        std::cout<<"[wait for tf failed]"<<std::endl;;
    }
}

int main(int argc, char** argv){
    ros::init(argc, argv, "tf_test_node");
    ros::NodeHandle nh;
    ros::Subscriber scan_sub = nh.subscribe("/velodyne_points", 1u, &scanCallback);
    ros::spin();

    return 0;
}

Then, I played a rosbag, but the output of this node was always "[wait for tf failed]". I tried to change "world" to "/world" and "velodyne" to "/velodyne", but it didnot work. I also run rosrun tf tf_echo \world \velodyne, and the output is correct. And the tf_tree of the rosbag is also correct. Moreover, the output of rostopic echo \tf was correct. I have no idea why the code cannot get the \tf, any advises will be appreciated.(The robag can be download from link text)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-03-17 14:59:48 -0500

gvdhoorn gravatar image

TransformListener needs some time to fill its buffer. You don't give it any time, as you construct it in the callback itself.

Create it in main(..) and make sure scanCallback(..) can access it to call lookupTransform(..) on it.

edit flag offensive delete link more

Comments

Thank you for answering, it really works.

leon J. gravatar image leon J.  ( 2019-03-18 04:14:48 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2019-03-16 03:58:50 -0500

Seen: 153 times

Last updated: Mar 17 '19