Ask Your Question
0

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;
try
{
    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());
    ros::Duration(1.0).sleep();
    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;

try
{
    transformStamped = tfBuffer.lookupTransform("/map", "/base_link", ros::Time(0));
}
catch (tf2::TransformException &ex){
    ROS_WARN("%s", ex.what());
    ros::Duration(1.0).sleep();
}
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
2

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

Comments

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

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

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

Stats

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

Seen: 233 times

Last updated: Jun 26 '18