Presumably, you are using C++. If that is the case, you'd likely want to look to the doTransform
methods of tf2. For example, there is an overload of doTransform specifically for geometry_msgs/Point
types. So if you had your point expressed in the B frame P_b
, you could lookup the transform from A to B, and then apply that to B to get the point in the A frame.
EDIT
Adding a small Python example that assumes the tf
tree already has a frame_a
and a frame_b
available.
#!/usr/bin/env python
import rospy
import tf
import tf2_ros
import tf2_geometry_msgs
rospy.init_node("demo_lookup")
tfbuffer = tf2_ros.Buffer()
listener = tf2_ros.TransformListener(tfbuffer)
tf1_listener = tf.TransformListener()
# give listeners time to receive required transforms
rospy.sleep(1.0)
# make Pose in the 'b' frame
p_b = tf2_geometry_msgs.tf2_geometry_msgs.PoseStamped()
p_b.header.frame_id = 'frame_b'
p_b.pose.position.y = 0.5
p_b.pose.orientation.w = 1.0
# Use tf2 to transform pose to the 'a' frame and print results. Best practice
# would wrap this in a try-except:
t_a_b = tfbuffer.lookup_transform('frame_a', 'frame_b', rospy.Time.now(), rospy.Duration(1.0))
p_a = tf2_geometry_msgs.do_transform_pose(p_b, t_a_b)
rospy.loginfo("Pose expressed in 'frame_a' using tf2: \n%s\n", str(p_a))
# Use tf to transform pose to the 'a' frame and print results. Best practice
# would wrap this in a try-except:
p_a_tf1 = tf1_listener.transformPose('frame_a', p_b)
rospy.loginfo("Pose expressed in 'frame_a' using tf: \n%s\n", str(p_a_tf1))
Originally posted by jarvisschultz with karma: 9031 on 2019-10-17
This answer was ACCEPTED on the original site
Post score: 1
Original comments
Comment by RobotsAreCool on 2019-10-21:
Hi @jarvischultz and thank you for you comment. I have to use python.
I think that transformPose does exactly what I need and I don't even need to call lookupTransform. However I cannot find any confirmation, which makes me unsure.
Comment by jarvisschultz on 2019-10-21:
transformPose
should do exactly what you want. I will point out that that uses the old tf
API instead of the newer tf2
API. While the old API doesn't seem to be going away any time soon, I generally prefer using the new API.
Comment by jarvisschultz on 2019-10-21:
Sorry.. I didn't read your comment closely enough. I still thought you were in C++. I'll add a little code snippet to my answer illustrating how to do this in both APIs in Python.