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, 11 Sep 2018 06:37:00 -0500tf::Quaternion getAngle() eqivalent for rospyhttps://answers.ros.org/question/302953/tfquaternion-getangle-eqivalent-for-rospy/ Is there an equivalent to the [getAngle()](http://docs.ros.org/jade/api/tf/html/c++/classtf_1_1Quaternion.html#a09c215f0fea82532418c8ae2fc516d4b) method of C++ interface of tf in Python version too? Mon, 10 Sep 2018 08:31:33 -0500https://answers.ros.org/question/302953/tfquaternion-getangle-eqivalent-for-rospy/Answer by PeteBlackerThe3rd for <p>Is there an equivalent to the <a href="http://docs.ros.org/jade/api/tf/html/c++/classtf_1_1Quaternion.html#a09c215f0fea82532418c8ae2fc516d4b">getAngle()</a> method of C++ interface of tf in Python version too? </p>
https://answers.ros.org/question/302953/tfquaternion-getangle-eqivalent-for-rospy/?answer=302957#post-id-302957I don't know if there's a built in function to do this. But the relationship between the w element of a quaternion and the angle it's rotating by is fairly simple.
w = cos(angle / 2)
therefore:
angle = 2 x arccos(w)
So you can calculate this yourself using this equation from the w element.
**Update:**
This is mathematically equivalent to the more complex form shown on the [Wikipedia page here](https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Recovering_the_axis-angle_representation). See their curves here:
[angle = 2 x arccos(w)](http://www.wolframalpha.com/input/?i=angle+%3D+2+*+acos(w))
[angle = 2*atan2(sqrt(1-w^2), w)](http://www.wolframalpha.com/input/?i=angle+%3D+2*atan2(sqrt(1-w%5E2),+w))
Note: I've replaced `sqrt(qi^2 + qj^2 + qk^2)` with `sqrt(1-qr^2)` since this expression can more easily be calculated using just the w value.Mon, 10 Sep 2018 09:09:12 -0500https://answers.ros.org/question/302953/tfquaternion-getangle-eqivalent-for-rospy/?answer=302957#post-id-302957Comment by PeteBlackerThe3rd for <p>I don't know if there's a built in function to do this. But the relationship between the w element of a quaternion and the angle it's rotating by is fairly simple.</p>
<pre><code>w = cos(angle / 2)
</code></pre>
<p>therefore:</p>
<pre><code>angle = 2 x arccos(w)
</code></pre>
<p>So you can calculate this yourself using this equation from the w element.</p>
<p><strong>Update:</strong></p>
<p>This is mathematically equivalent to the more complex form shown on the <a href="https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Recovering_the_axis-angle_representation">Wikipedia page here</a>. See their curves here:</p>
<p><a href="http://www.wolframalpha.com/input/?i=angle+%3D+2+*+acos(w">angle = 2 x arccos(w)</a>)</p>
<p><a href="http://www.wolframalpha.com/input/?i=angle+%3D+2*atan2(sqrt(1-w%5E2">angle = 2*atan2(sqrt(1-w^2), w)</a>,+w))</p>
<p>Note: I've replaced <code>sqrt(qi^2 + qj^2 + qk^2)</code> with <code>sqrt(1-qr^2)</code> since this expression can more easily be calculated using just the w value.</p>
https://answers.ros.org/question/302953/tfquaternion-getangle-eqivalent-for-rospy/?comment=303056#post-id-303056No worries, rotation quaternions are 4D unit vectors so they should always be normalized.Tue, 11 Sep 2018 06:37:00 -0500https://answers.ros.org/question/302953/tfquaternion-getangle-eqivalent-for-rospy/?comment=303056#post-id-303056Comment by smihael for <p>I don't know if there's a built in function to do this. But the relationship between the w element of a quaternion and the angle it's rotating by is fairly simple.</p>
<pre><code>w = cos(angle / 2)
</code></pre>
<p>therefore:</p>
<pre><code>angle = 2 x arccos(w)
</code></pre>
<p>So you can calculate this yourself using this equation from the w element.</p>
<p><strong>Update:</strong></p>
<p>This is mathematically equivalent to the more complex form shown on the <a href="https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Recovering_the_axis-angle_representation">Wikipedia page here</a>. See their curves here:</p>
<p><a href="http://www.wolframalpha.com/input/?i=angle+%3D+2+*+acos(w">angle = 2 x arccos(w)</a>)</p>
<p><a href="http://www.wolframalpha.com/input/?i=angle+%3D+2*atan2(sqrt(1-w%5E2">angle = 2*atan2(sqrt(1-w^2), w)</a>,+w))</p>
<p>Note: I've replaced <code>sqrt(qi^2 + qj^2 + qk^2)</code> with <code>sqrt(1-qr^2)</code> since this expression can more easily be calculated using just the w value.</p>
https://answers.ros.org/question/302953/tfquaternion-getangle-eqivalent-for-rospy/?comment=303054#post-id-303054Ok thanks, I thought that the Wikipedia was maybe better because essentially it enforces normalization on the vector part or something into this direction.Tue, 11 Sep 2018 06:33:13 -0500https://answers.ros.org/question/302953/tfquaternion-getangle-eqivalent-for-rospy/?comment=303054#post-id-303054Comment by PeteBlackerThe3rd for <p>I don't know if there's a built in function to do this. But the relationship between the w element of a quaternion and the angle it's rotating by is fairly simple.</p>
<pre><code>w = cos(angle / 2)
</code></pre>
<p>therefore:</p>
<pre><code>angle = 2 x arccos(w)
</code></pre>
<p>So you can calculate this yourself using this equation from the w element.</p>
<p><strong>Update:</strong></p>
<p>This is mathematically equivalent to the more complex form shown on the <a href="https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Recovering_the_axis-angle_representation">Wikipedia page here</a>. See their curves here:</p>
<p><a href="http://www.wolframalpha.com/input/?i=angle+%3D+2+*+acos(w">angle = 2 x arccos(w)</a>)</p>
<p><a href="http://www.wolframalpha.com/input/?i=angle+%3D+2*atan2(sqrt(1-w%5E2">angle = 2*atan2(sqrt(1-w^2), w)</a>,+w))</p>
<p>Note: I've replaced <code>sqrt(qi^2 + qj^2 + qk^2)</code> with <code>sqrt(1-qr^2)</code> since this expression can more easily be calculated using just the w value.</p>
https://answers.ros.org/question/302953/tfquaternion-getangle-eqivalent-for-rospy/?comment=303043#post-id-303043The two equations are mathematically equivalent, for some reason they've put the more complex form on Wikipedia! I've added the graphs of them both to my answer, as you can see they give the same results.Tue, 11 Sep 2018 05:00:46 -0500https://answers.ros.org/question/302953/tfquaternion-getangle-eqivalent-for-rospy/?comment=303043#post-id-303043Comment by smihael for <p>I don't know if there's a built in function to do this. But the relationship between the w element of a quaternion and the angle it's rotating by is fairly simple.</p>
<pre><code>w = cos(angle / 2)
</code></pre>
<p>therefore:</p>
<pre><code>angle = 2 x arccos(w)
</code></pre>
<p>So you can calculate this yourself using this equation from the w element.</p>
<p><strong>Update:</strong></p>
<p>This is mathematically equivalent to the more complex form shown on the <a href="https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Recovering_the_axis-angle_representation">Wikipedia page here</a>. See their curves here:</p>
<p><a href="http://www.wolframalpha.com/input/?i=angle+%3D+2+*+acos(w">angle = 2 x arccos(w)</a>)</p>
<p><a href="http://www.wolframalpha.com/input/?i=angle+%3D+2*atan2(sqrt(1-w%5E2">angle = 2*atan2(sqrt(1-w^2), w)</a>,+w))</p>
<p>Note: I've replaced <code>sqrt(qi^2 + qj^2 + qk^2)</code> with <code>sqrt(1-qr^2)</code> since this expression can more easily be calculated using just the w value.</p>
https://answers.ros.org/question/302953/tfquaternion-getangle-eqivalent-for-rospy/?comment=303035#post-id-303035Are there any preconditions to be met to use this formula? Wikipedia uses atan2 (https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Recovering_the_axis-angle_representation)Tue, 11 Sep 2018 03:14:10 -0500https://answers.ros.org/question/302953/tfquaternion-getangle-eqivalent-for-rospy/?comment=303035#post-id-303035Answer by Thomas D for <p>Is there an equivalent to the <a href="http://docs.ros.org/jade/api/tf/html/c++/classtf_1_1Quaternion.html#a09c215f0fea82532418c8ae2fc516d4b">getAngle()</a> method of C++ interface of tf in Python version too? </p>
https://answers.ros.org/question/302953/tfquaternion-getangle-eqivalent-for-rospy/?answer=302963#post-id-302963The [tf.transformations](http://docs.ros.org/api/tf/html/python/transformations.html) library has a lot of useful conversion methods for rotations.
You can get [Euler angles](http://docs.ros.org/api/tf/html/python/transformations.html#tf.transformations.euler_from_quaternion) from your quaternion. If you are trying to get the axis-angle representation you could get a [rotation matrix](http://docs.ros.org/api/tf/html/python/transformations.html#tf.transformations.quaternion_matrix) from your quaternion and convert that to [axis-angle](http://docs.ros.org/api/tf/html/python/transformations.html#tf.transformations.rotation_from_matrix).Mon, 10 Sep 2018 10:06:43 -0500https://answers.ros.org/question/302953/tfquaternion-getangle-eqivalent-for-rospy/?answer=302963#post-id-302963Comment by fvd for <p>The <a href="http://docs.ros.org/api/tf/html/python/transformations.html">tf.transformations</a> library has a lot of useful conversion methods for rotations.</p>
<p>You can get <a href="http://docs.ros.org/api/tf/html/python/transformations.html#tf.transformations.euler_from_quaternion">Euler angles</a> from your quaternion. If you are trying to get the axis-angle representation you could get a <a href="http://docs.ros.org/api/tf/html/python/transformations.html#tf.transformations.quaternion_matrix">rotation matrix</a> from your quaternion and convert that to <a href="http://docs.ros.org/api/tf/html/python/transformations.html#tf.transformations.rotation_from_matrix">axis-angle</a>.</p>
https://answers.ros.org/question/302953/tfquaternion-getangle-eqivalent-for-rospy/?comment=303011#post-id-303011I think an example would be appreciated by future searchersMon, 10 Sep 2018 19:50:30 -0500https://answers.ros.org/question/302953/tfquaternion-getangle-eqivalent-for-rospy/?comment=303011#post-id-303011