Ask Your Question
0

Transform coordinates x,y from base_link to odom

asked 2021-04-14 07:06:32 -0500

UndefinedDuck gravatar image

updated 2021-04-14 15:41:48 -0500

Hi guys, my algorithm calculates the position x, y (as double type) of an object scanned by the laser, but the problem is that these coordinates belongs to the base_link frame. Now I have to convert them to the odom reference triad, how can I do it? Do you have any TF code to implement?

Edit: this is what I'm trying to use

  tf::StampedTransform base_link_to_odom_tf;
  tf::TransformListener tfListener_;
  tfListener_.waitForTransform("/odom", "/base_link", ros::Time(0), ros::Duration(0.1));

  try
  {
      tfListener_.lookupTransform("/odom", "/base_link", ros::Time(0), base_link_to_odom_tf);
  } catch (tf::TransformException ex) {}

  tf::Vector3 point, new_pos;
  point.setZ(0.0);
  point.setX(pos_x);
  point.setY(pos_y);
  new_pos = base_link_to_odom_tf * point;

    obj_x.push_back(new_pos.x());
    obj_y.push_back(new_pos.y());
    ...
    obst_markers.markers[i].pose.position.x = obj_x[i];
    obst_markers.markers[i].pose.position.y = obj_y[i];

where obj_x and obj_y are vectors of doubles used to load the objects as marker arrays

Edit2: after some tests moving the object in Gazebo I have these coordinates in output

x: 3.55343  new_pos.x(): 3.17272e-309 
y: -0.375864  new_pos.y(): 2.97874e-309

x: 5.35993  new_pos.x(): 4.41872e-309
y: -0.338986  new_pos.y(): 4.01111e-309

x: 3.71522  new_pos.x(): 3.28127e-309 
y: 1.33082   new_pos.y(): 3.65853e-309
edit retag flag offensive close merge delete

Comments

The Using Stamped datatypes with tf2_ros::MessageFilter tutorial may help. There are many questions similar to this one and related tutorials. If you feel your question is unique, please provide links to resources you've found and explain what is different about your situation. If you've tried implementing a solution, please post your code (and any relevant output) along with links to reference material, so we can troubleshoot more easily.

tryan gravatar image tryan  ( 2021-04-14 10:56:42 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2021-04-14 08:47:41 -0500

TzviH gravatar image

updated 2021-04-14 08:49:28 -0500

Hi, I wrote an example in C++

Hope it can help you.

tf::StampedTransform base_link_to_odom_tf;
tfListener_.waitForTransform("/odom", "/base_link", ros::Time(0), ros::Duration(0.1));
try {
      tfListener_.lookupTransform("/odom", "/base_link", ros::Time(0), base_link_to_odom_tf);
} catch (tf::TransformException ex) {
    return false;
 }
tf::Vector3 point, new_pose_v;
point.setZ(0.0);
point.setX(x);
point.setY(y);
new_pose_v = base_link_to_odom_tf * point;
edit flag offensive delete link more

Comments

Uhm when I use RViz I see the object always in the same position now (the origin), even if I move it, so I don't think it is working

UndefinedDuck gravatar image UndefinedDuck  ( 2021-04-14 10:24:02 -0500 )edit

Can you please paste your code here? I want to see how you implement the transform.

TzviH gravatar image TzviH  ( 2021-04-14 12:09:09 -0500 )edit

I edited the post with the code

UndefinedDuck gravatar image UndefinedDuck  ( 2021-04-14 12:36:26 -0500 )edit

OK, Can you add some prints of point & new_pos and upload them?

TzviH gravatar image TzviH  ( 2021-04-14 15:07:10 -0500 )edit

Yes updated

UndefinedDuck gravatar image UndefinedDuck  ( 2021-04-14 15:42:21 -0500 )edit

Please run on terminl: rosrun tf tf_echo /odom /base_link? Can you upload the output?

TzviH gravatar image TzviH  ( 2021-04-14 16:19:01 -0500 )edit

Usage: tf_echo source_frame target_frame [echo_rate]

This will echo the transform from the coordinate frame of the source_frame to the coordinate frame of the target_frame. Note: This is the transform to get data from target_frame into the source_frame. Default echo rate is 1 if echo_rate is not given.

UndefinedDuck gravatar image UndefinedDuck  ( 2021-04-14 16:40:48 -0500 )edit

Please check if you have odom frame from the gazebo. You can check with rqt

TzviH gravatar image TzviH  ( 2021-04-15 02:23:14 -0500 )edit
0

answered 2021-04-14 07:57:37 -0500

miura gravatar image

Perhaps the following is the tutorial you are looking for

C++ ver: http://wiki.ros.org/tf/Tutorials/Writ...

Python ver: http://wiki.ros.org/tf/Tutorials/Writ...

You can change '/turtle2' and '/turtle1' to /base_link and /odom.

edit flag offensive delete link more

Comments

Please refer to the tf2 tutorials as tf is depricated.

tryan gravatar image tryan  ( 2021-04-14 10:39:43 -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: 2021-04-14 07:06:32 -0500

Seen: 219 times

Last updated: Apr 14 '21