0
$\begingroup$

Rosanswers logo

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?


Originally posted by TifferPelode on ROS Answers with karma: 96 on 2018-06-26

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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;
    

Originally posted by Tom Moore with karma: 13689 on 2018-06-26

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by TifferPelode on 2018-06-26:
Thank you for your detailed explanation, it really wake me up.

$\endgroup$

Your Answer

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