ROS Answers: Open Source Q&A Forum - RSS feedhttps://answers.ros.org/questions/Open source question and answer forum written in Python and DjangoenROS Answers is licensed under Creative Commons Attribution 3.0Tue, 27 Mar 2018 07:19:47 -0500what is the right way to inverse a transform in pythonhttps://answers.ros.org/question/229329/what-is-the-right-way-to-inverse-a-transform-in-python/I am wondering if you know a better way (less boiler code) that will allow me to inverse a transform msg, in Python. For now I use the following code:
from geometry_msgs.msg import PoseStamped, TransformStamped
from tf import TransformerROS
res = TransformStamped() # creating new transform msg
res.header.stamp = input_transform.header.stamp # same timestamp
res.header.frame_id = input_transform.child_frame_id # inverting frame_id and child_frame_id manualy
res.child_frame_id = input_transform.header.frame_id
transformer = TransformerROS() # using a transformer in order to compute inverse transform
transformer.setTransform(input_transform) # using the input_transform
translation, rotation = transformer.lookupTransform(res.header.frame_id, res.child_frame_id, rospy.Time(0)) # using lookup in order to get the inverse of input transform (here I use input_transform.child_frame_id to input_transform_frame_id
for attr_name, val in zip(['x', 'y', 'z'], translation):
res.transform.translation.__setattr__(attr_name, val)
for attr_name, val in zip(['x', 'y', 'z', 'w'], rotation):
res.transform.rotation.__setattr__(attr_name, val)
what do you think ?Thu, 17 Mar 2016 05:53:53 -0500https://answers.ros.org/question/229329/what-is-the-right-way-to-inverse-a-transform-in-python/Comment by eric-wieser for <p>I am wondering if you know a better way (less boiler code) that will allow me to inverse a transform msg, in Python. For now I use the following code:</p>
<pre><code>from geometry_msgs.msg import PoseStamped, TransformStamped
from tf import TransformerROS
res = TransformStamped() # creating new transform msg
res.header.stamp = input_transform.header.stamp # same timestamp
res.header.frame_id = input_transform.child_frame_id # inverting frame_id and child_frame_id manualy
res.child_frame_id = input_transform.header.frame_id
transformer = TransformerROS() # using a transformer in order to compute inverse transform
transformer.setTransform(input_transform) # using the input_transform
translation, rotation = transformer.lookupTransform(res.header.frame_id, res.child_frame_id, rospy.Time(0)) # using lookup in order to get the inverse of input transform (here I use input_transform.child_frame_id to input_transform_frame_id
for attr_name, val in zip(['x', 'y', 'z'], translation):
res.transform.translation.__setattr__(attr_name, val)
for attr_name, val in zip(['x', 'y', 'z', 'w'], rotation):
res.transform.rotation.__setattr__(attr_name, val)
</code></pre>
<p>what do you think ?</p>
https://answers.ros.org/question/229329/what-is-the-right-way-to-inverse-a-transform-in-python/?comment=229352#post-id-229352`setattr(x, y)` should always be preferred over `x.__setattr__(y)`Thu, 17 Mar 2016 10:16:57 -0500https://answers.ros.org/question/229329/what-is-the-right-way-to-inverse-a-transform-in-python/?comment=229352#post-id-229352Answer by Airuno2L for <p>I am wondering if you know a better way (less boiler code) that will allow me to inverse a transform msg, in Python. For now I use the following code:</p>
<pre><code>from geometry_msgs.msg import PoseStamped, TransformStamped
from tf import TransformerROS
res = TransformStamped() # creating new transform msg
res.header.stamp = input_transform.header.stamp # same timestamp
res.header.frame_id = input_transform.child_frame_id # inverting frame_id and child_frame_id manualy
res.child_frame_id = input_transform.header.frame_id
transformer = TransformerROS() # using a transformer in order to compute inverse transform
transformer.setTransform(input_transform) # using the input_transform
translation, rotation = transformer.lookupTransform(res.header.frame_id, res.child_frame_id, rospy.Time(0)) # using lookup in order to get the inverse of input transform (here I use input_transform.child_frame_id to input_transform_frame_id
for attr_name, val in zip(['x', 'y', 'z'], translation):
res.transform.translation.__setattr__(attr_name, val)
for attr_name, val in zip(['x', 'y', 'z', 'w'], rotation):
res.transform.rotation.__setattr__(attr_name, val)
</code></pre>
<p>what do you think ?</p>
https://answers.ros.org/question/229329/what-is-the-right-way-to-inverse-a-transform-in-python/?answer=229333#post-id-229333 There are a million ways to do this and none of them are really right or wrong. But personally, I like to use numpy (actually the tf.transformations library which is a nice wrapper for numpy) for everything inside my code.
I only use the transform messages to receive and send transforms to other processes. So as soon as any of my software takes in a transform, pose, point, or whatever, and I know I need to manipulate it, the first thing I do is convert it to a numpy type. I think always working with the same types and library makes things cleaner and easier in the long run.
So if you're grabbing a transform and want to invert it, that would look like this:
from tf import transformations as t
(trans, rot) = transformer.lookupTransform(frame1, frame2, rospy.Time(0))
transform = t.concatenate_matrices(t.translation_matrix(trans), t.quaternion_matrix(rot))
inversed_transform = t.inverse_matrix(transform)
Is that what you're looking for?
Thu, 17 Mar 2016 07:36:54 -0500https://answers.ros.org/question/229329/what-is-the-right-way-to-inverse-a-transform-in-python/?answer=229333#post-id-229333Comment by Airuno2L for <p>There are a million ways to do this and none of them are really right or wrong. But personally, I like to use numpy (actually the tf.transformations library which is a nice wrapper for numpy) for everything inside my code. </p>
<p>I only use the transform messages to receive and send transforms to other processes. So as soon as any of my software takes in a transform, pose, point, or whatever, and I know I need to manipulate it, the first thing I do is convert it to a numpy type. I think always working with the same types and library makes things cleaner and easier in the long run. </p>
<p>So if you're grabbing a transform and want to invert it, that would look like this: </p>
<pre><code>from tf import transformations as t
(trans, rot) = transformer.lookupTransform(frame1, frame2, rospy.Time(0))
transform = t.concatenate_matrices(t.translation_matrix(trans), t.quaternion_matrix(rot))
inversed_transform = t.inverse_matrix(transform)
</code></pre>
<p>Is that what you're looking for?</p>
https://answers.ros.org/question/229329/what-is-the-right-way-to-inverse-a-transform-in-python/?comment=255619#post-id-255619I think it's something like this:
translation = t.translation_from_matrix(inversed_transform)
quaternion = t.quaternion_from_matrix(inversed_transform)
The [transformations.py](http://www.lfd.uci.edu/~gohlke/code/transformations.py.html) is documented well for future reference.Sat, 25 Feb 2017 14:52:38 -0600https://answers.ros.org/question/229329/what-is-the-right-way-to-inverse-a-transform-in-python/?comment=255619#post-id-255619Comment by Will Chamberlain for <p>There are a million ways to do this and none of them are really right or wrong. But personally, I like to use numpy (actually the tf.transformations library which is a nice wrapper for numpy) for everything inside my code. </p>
<p>I only use the transform messages to receive and send transforms to other processes. So as soon as any of my software takes in a transform, pose, point, or whatever, and I know I need to manipulate it, the first thing I do is convert it to a numpy type. I think always working with the same types and library makes things cleaner and easier in the long run. </p>
<p>So if you're grabbing a transform and want to invert it, that would look like this: </p>
<pre><code>from tf import transformations as t
(trans, rot) = transformer.lookupTransform(frame1, frame2, rospy.Time(0))
transform = t.concatenate_matrices(t.translation_matrix(trans), t.quaternion_matrix(rot))
inversed_transform = t.inverse_matrix(transform)
</code></pre>
<p>Is that what you're looking for?</p>
https://answers.ros.org/question/229329/what-is-the-right-way-to-inverse-a-transform-in-python/?comment=286680#post-id-286680Note:
https://github.com/ros/geometry/blob/indigo-devel/tf/src/tf/transformations.py and http://www.lfd.uci.edu/~gohlke/code/transformations.py.html are differentTue, 27 Mar 2018 07:19:47 -0500https://answers.ros.org/question/229329/what-is-the-right-way-to-inverse-a-transform-in-python/?comment=286680#post-id-286680Comment by lucasw for <p>There are a million ways to do this and none of them are really right or wrong. But personally, I like to use numpy (actually the tf.transformations library which is a nice wrapper for numpy) for everything inside my code. </p>
<p>I only use the transform messages to receive and send transforms to other processes. So as soon as any of my software takes in a transform, pose, point, or whatever, and I know I need to manipulate it, the first thing I do is convert it to a numpy type. I think always working with the same types and library makes things cleaner and easier in the long run. </p>
<p>So if you're grabbing a transform and want to invert it, that would look like this: </p>
<pre><code>from tf import transformations as t
(trans, rot) = transformer.lookupTransform(frame1, frame2, rospy.Time(0))
transform = t.concatenate_matrices(t.translation_matrix(trans), t.quaternion_matrix(rot))
inversed_transform = t.inverse_matrix(transform)
</code></pre>
<p>Is that what you're looking for?</p>
https://answers.ros.org/question/229329/what-is-the-right-way-to-inverse-a-transform-in-python/?comment=255338#post-id-255338Can you add making the inversed_transform back into a translation vector and quaternion?Wed, 22 Feb 2017 16:06:01 -0600https://answers.ros.org/question/229329/what-is-the-right-way-to-inverse-a-transform-in-python/?comment=255338#post-id-255338