Ask Your Question
8

Transform a pose to another frame with tf2 in Python

asked 2015-12-10 07:10:08 -0600

fivef gravatar image

updated 2021-11-18 12:03:38 -0600

lucasw gravatar image

I have a pose stamped given in camera frame. Now I want to transform the pose to get its coordinates in the base_link frame.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
14

answered 2015-12-10 07:33:15 -0600

fivef gravatar image

updated 2021-11-18 12:02:41 -0600

lucasw gravatar image

Import the needed modules:

import tf2_ros
import tf2_geometry_msgs

Initialize the tf buffer and listener somewhere in your init function:

tf_buffer = tf2_ros.Buffer(rospy.Duration(100.0))  # tf buffer length
tf_listener = tf2_ros.TransformListener(self.tf_buffer)

Do the actual transformation:

transform = tf_buffer.lookup_transform(target_frame,
                                       # source frame:
                                       pose_stamped.header.frame_id,
                                       # get the tf at the time the pose was valid
                                       pose_stamped.header.stamp,
                                       # wait for at most 1 second for transform, otherwise throw
                                       rospy.Duration(1.0))

pose_transformed = tf2_geometry_msgs.do_transform_pose(pose_stamped, transform)
edit flag offensive delete link more

Comments

8

The lookup should better be wrapped inside a try-except block in case the transform is not found: http://wiki.ros.org/tf2/Tutorials/Wri...

bit-pirate gravatar image bit-pirate  ( 2016-03-22 03:03:46 -0600 )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

4 followers

Stats

Asked: 2015-12-10 07:10:08 -0600

Seen: 11,136 times

Last updated: Nov 18