I'm writing a package that, among other things, needs to get a transformation from the /base_laser
frame to the (world) /odom
frame. Both frames exist, and rosrun tf tf_echo /odom /base_laser
returns the correct transformation values. The problem appears when I try to get the transformation using a TransformListener
in my node; in this case, canTransform()
always returns false
.
The code is part of a test using the husky_simulator package to get the laser measurements and the environment.
This is the relevant part of the code:
tf2_ros::Buffer tfBuffer;
tf2_ros::TransformListener tfListener(tfBuffer);
geometry_msgs::TransformStamped transformStamped;
try
{
ros::Time now = ros::Time::now();
if (tfBuffer.canTransform("odom",
"base_laser",
now,
ros::Duration(0.01)))
{
transformStamped = tfBuffer.lookupTransform("odom",
"base_laser",
now);
ROS_INFO("canTransform: TRUE");
ROS_INFO("Transform: [%.2f, %.2f, %.2f]",
transformStamped.transform.translation.x,
transformStamped.transform.translation.y,
transformStamped.transform.translation.z);
}
else
{
ROS_INFO("canTransform: FALSE");
}
}
catch (tf2::TransformException &ex)
{
ROS_WARN("%s", ex.what());
ros::Duration(1);
}
When launching the node, the output shows the following:
[ INFO] [1553804232.701323614, 0.176000000]: canTransform: FALSE
[ INFO] [1553804232.736923460, 0.207000000]: canTransform: FALSE
[ INFO] [1553804232.813116964, 0.275000000]: canTransform: FALSE
[ INFO] [1553804232.856465945, 0.311000000]: canTransform: FALSE
[ INFO] [1553804232.923821528, 0.365000000]: canTransform: FALSE
[ INFO] [1553804233.977405692, 0.397000000]: canTransform: FALSE
and keeps this way, without returning a true
.
This code is inside a function that is called periodically from the node main function. Everything else works as expected.
Running ROS Melodic in Ubuntu 18.04
Any help is welcome.
I finally found what was going on: the husky_simulator
code uses tf
while I was trying to get the transforms using tf2
; once I switched to tf
, the transform was found.