Ask Your Question

How to translate a pose in rospy?

asked 2021-03-16 05:04:53 -0500

Py gravatar image

updated 2021-03-16 11:36:20 -0500

How would I go about translating a pose along the X axis in rospy? Basically, I have a pose and want to to return a translated pose in my Python script. Is tf or tf2 useful for this? What would the line of code look like?

I suppose I'm hoping for a function that returns a translated pose, maybe like translated_pose = pose_translator(original_pose, transform).

EDIT: So far I've tried the following:

pose_transform = Transform()
pose_transform.translation.x = 0.15
pose_transform.translation.y = 0
pose_transform.translation.z = 0
transformed_pose = tf2_geometry_msgs.do_transform_pose(original_pose, pose_transform)

However, I get this error:

AttributeError: 'Transform' object has no attribute 'transform'

Any ideas what might be going wrong?

edit retag flag offensive close merge delete


Do you want to translate a pose in the same frame of reference or in a different one?

Roberto Z. gravatar image Roberto Z.  ( 2021-03-16 05:35:07 -0500 )edit

Same frame :)

Py gravatar image Py  ( 2021-03-16 05:51:34 -0500 )edit

In general you shouldn't use tf anymore as it is depricated and has been replaced by tf2 as described here. tf still works and probably would do the job in many cases, but to future-proof you software i'd recommend to allways go with tf2.

Tristan9497 gravatar image Tristan9497  ( 2021-03-19 12:08:04 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2021-03-16 12:21:47 -0500

Roberto Z. gravatar image

updated 2021-03-16 14:20:24 -0500

To translate a pose in the same frame of reference just add the translation offsets for each axis. For instance:

initial_pose = Pose()
initial_pose.position.x = 0
initial_pose.position.y = 0
initial_pose.position.z = 0

# translate +5 in x-axis, +2 in y-axis and -10 in z-axis
initial_pose.position.x += 5
initial_pose.position.y += 2
initial_pose.position.z += -10

tf2 is used to transform between multiple coordinate frames, which you said isn't what you want to do. So no need to use tf2 here.

edit flag offensive delete link more


I've tried the following, which adds offset = 0.4 to a marker position in the x-axis. This works to some extent because it does move the marker. However, I want to move the marker relation to the robot base_link. Does that mean I've misunderstood and I actually do need to transform between multiple frames? In RViz, it looks the offset is currently applied in the map frame as the marker does not stay in front of the robot.

def pub_marker(self, robot_pose):
    marker_publisher = rospy.Publisher('visualization_marker', Marker, queue_size=1, latch=True)
    marker = Marker()
    offset = 0.4
    marker.pose.position.x = robot_pose.pose.pose.position.x
    marker.pose.position.x += offset
    marker.pose.position.y = robot_pose.pose.pose.position.y
    marker.pose.position.z = 0
    marker.pose.orientation = robot_pose.pose.pose.orientation
Py gravatar image Py  ( 2021-03-18 09:55:19 -0500 )edit

The pose of an Rviz markers is defined relative to the coordinate frame set during the markers initialization.
For instance here we are creating a marker whose pose is relative to the map frame:

marker = Marker()
marker.header.frame_id = "/map"

Therefore if you offset this marker by, say 0.4 in the x-axis, it will appear as offset with respect to the map. If you want to offset with respect to the robot base, set your markers id_frame equal to base_link. In this case the marker will be published at a fixed pose with respect to the robot, and change its position with respect to the robot when you add an offset. Is this what you are trying to accomplish?

Roberto Z. gravatar image Roberto Z.  ( 2021-03-18 12:06:42 -0500 )edit

Excellent! This worked great. Thanks a lot :)

Py gravatar image Py  ( 2021-03-19 10:51:10 -0500 )edit

I am glad this works for you!

Roberto Z. gravatar image Roberto Z.  ( 2021-03-19 11:04:19 -0500 )edit

Hope its ok to revisit this question after I marked it as answered. I actually need to have marker.header.frame_id set as /map so my markers display where I want them to. Given this constraint, is tf2 needed to transform the marker pose so that it is offset in the x-axis relative to the base_link but the x, y and z values would be specified relative to the map frame? I've tried this so far (as posted in this question) but the transformed_pose just ends up with the same pose.position.x as transform.translation.x.

transform = TransformStamped()
transform.transform.translation.x = 0.2
transformed_pose = tf2_geometry_msgs.do_transform_pose(pose, transform)
Py gravatar image Py  ( 2021-04-24 03:11:26 -0500 )edit

answered 2021-05-10 10:08:11 -0500

Py gravatar image

After some playing around, the following code achieved me to translate my pose along the x-axis.

transform = self.tf_buffer.lookup_transform(frame, required_position, rospy.Time(0), rospy.Duration(1))
initial_pose = PoseStamped()
initial_pose.pose.position.x = 0.2
translated_pose = tf2_geometry_msgs.do_transform_pose(initial_pose, transform)
edit flag offensive delete link more


Thanks for your question & answer, I'm trying to solve the same problem. what is your 'required_position' ? a frame (string) is expected there. (I have a pose, which isn't a frame, so I'm stuck there).

crnewton gravatar image crnewton  ( 2021-05-11 08:44:44 -0500 )edit

My required_position is the robot base_link and the frame I want it in is map in my case. From my experience, I transformed the initial_pose using tf2_geometry_msgs.do_transform_pose(initial_pose, transform) as shown above. Maybe initial_pose could just be substituted with your pose?

Py gravatar image Py  ( 2021-05-11 09:01:18 -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



Asked: 2021-03-16 05:04:53 -0500

Seen: 86 times

Last updated: May 10