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

Tf to tf2 upgrade adaptation problem

asked 2018-06-26 04:09:54 -0500

TifferPelode gravatar image

Hello everyone,

I wanna update tf to tf2 in my code, but get some question about it.

There are tf part :

tf::TransformListener listener;
tf::StampedTransform transform;
    listener.waitForTransform("/map", "/base_link", ros::Time::now(), ros::Duration(2));
    listener.lookupTransform("/map", "/base_link", ros::Time(0), transform);
catch(tf::TransformException ex)
    ROS_ERROR("%s", ex.what());
    QMessageBox::information(this, "Error", QString::fromStdString(ex.what()));
    return ;
geometry_msgs::Transform tt;
tf::transformTFToMsg(transform, tt);
pose.position.x = tt.translation.x;
pose.position.y = tt.translation.y;
pose.position.z = tt.translation.z;
pose.orientation.x = tt.rotation.x;
pose.orientation.y = tt.rotation.y;
pose.orientation.z = tt.rotation.z;
pose.orientation.w = tt.rotation.w;

pose is a parameter, via geometry_msgs::Pose &pose

When I follow the doc to update to tf2, I get some wrong.

There are tf2 part:

tf2_ros::Buffer tfBuffer;
tf2_ros::TransformListener tfListener(tfBuffer);
geometry_msgs::TransformStamped transformStamped;

    transformStamped = tfBuffer.lookupTransform("/map", "/base_link", ros::Time(0));
catch (tf2::TransformException &ex){
    ROS_WARN("%s", ex.what());
geometry_msgs::Transform tt;
tf::transformTFToMsg(transformStamped, tt);

I Don't know how to transfrom data under tf2, and I didn‘t find the relevant function in doc.

I want to know how to modify my code to achieve the same result?

Is there a function just like transformTFToMsg I can replace?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2018-06-26 05:14:17 -0500

Tom Moore gravatar image

updated 2018-06-26 05:14:37 -0500

In your first example, you make a tf::StampedTransform to hold the transform, then use transformTFToMsg to put it into a message object.

In the second example, tf2 is already returning a geometry_msgs::TransformStamped message type (instead of a StampedTransform object). So you're trying to convert a message to a message. You're effectively doing this:

geometry_msgs::TransformStamped transformStamped;
geometry_msgs::Transform tt;
tf::transformTFToMsg(transformStamped, tt);

But those are both message types, so you don't need to do that at all. You have two options:

  1. Use transformStamped instead of tt in the rest of your code, since they are both messages.
  2. Just do this:

    geometry_msgs::TransformStamped transformStamped;
    <your transform try/catch block>
    geometry_msgs::Transform tt = transformStamped.transform;
edit flag offensive delete link more


Thank you for your detailed explanation, it really wake me up.

TifferPelode gravatar image TifferPelode  ( 2018-06-26 09:52:59 -0500 )edit

Question Tools

1 follower


Asked: 2018-06-26 04:09:54 -0500

Seen: 349 times

Last updated: Jun 26 '18