Maths - Conversion Quaternion to Euler - Forum Discussion

By: fik - fik_
file Error in Quaternion to Euler equations?  
2006-08-18 17:03

Hi i believe i have found a error in these equations, you seem to have the equations sent to the atan2 functions the wrong way round in both.  
I say this as when i use your eular to quaternion equations, then convert back using above i do not get the right values. For example using heading 10 attitude 20 bank 30. I get back heading 80 attitude 20 bank 60 which as you can see is not the same or eqivalent rotation. When these are reversed i get back 10 20 30 as expected. 
So i believe should read  
heading = atan2(1 - 2*qy2 - 2*qz2 , 2*qy*qw-2*qx*qz) 
attitude = asin(2*qx*qy + 2*qz*qw) 
bank = atan2(1 - 2*qx2 - 2*qz2 ,2*qx*qw-2*qy*qz ) 
 
not  
 
heading = atan2(2*qy*qw-2*qx*qz , 1 - 2*qy2 - 2*qz2) 
attitude = asin(2*qx*qy + 2*qz*qw)  
bank = atan2(2*qx*qw-2*qy*qz , 1 - 2*qx2 - 2*qz2) 
 
Hope this makes sense  

By: Martin Baker - martinbakerProject Admin
file RE: Error in Quaternion to Euler equations?  
2006-08-19 02:40

I tried converting heading =10 deg, attitude=20 deg, bank=30 deg 
to quaternion and then converting back to euler angle using the formula on the website, this gave the result: 
heading 8 deg, attitude=15 deg, bank=24 deg 
 
This could indicate an error but it might just be due to rounding errors, because I just did the calculations by hand using my old 4 figure tables, I would expect big errors due to taking difference of small numbers, then taking trig functions. 
 
Here are my results, which does not agree with your results, what is the place they differ from yours? 
 
Martin 
------------------------------------- 
c1 = cos(5 deg) = 0.9962  
c2 = cos(10 deg) = 0.9848  
c3 = cos(15 deg) = 0.9659  
s1 = sin(5 deg) = 0.0872  
s2 = sin(10 deg) = 0.1736  
s3 = sin(15 deg) = 0.2588  
 
qw = 0.9437  
qx = -0.2393  
qy = 0.0382  
qz = 0.1448  
 
tan(heading y part) = 0.1414  
tan(heading x part) = 0.9551  
tan(heading) = 0.1480 = 8 degrees 
 
sin(attitude) = 0.2551 = 15 degrees 
 
tan(bank y part) = -0.4627  
tan(bank x part) = -1.1565  
tan(bank) = 0.4001 = 24 degrees 

By: fik - fik_
file RE: Error in Quaternion to Euler equations?  
2006-08-19 16:59

I am using the first method on your euler to quaternion page and i do not get the same values as you for qx qy unless i change the signs at center all to - . 

By: Martin Baker - martinbakerProject Admin
file RE: Error in Quaternion to Euler equations?  
2006-08-20 08:42

As you pointed out, I made a mistake in the calculations on my last post, I've now corrected the sign and the round trip calculation (euler -> quaternion -> euler) now gives the correct result to the nearest degree. See calculations below. 
 
I got quaternion numbers all positive (although making them all negative does not affect the result, they represent the same rotation). 
 
I used the formulae as on the web pages, which gives me some confidence that the formulae are correct on the web pages? 
 
I used atan(y/x) instead of atan2(y,x) as I was looking up the values in 4 figure tables instead of using a computer program. However, because the angles are all in the first quadrant, I believe these are equivalent since in fist quadrant: 
tan(a) = sin(a)/cos(a) = opposite/adjacent = y/x 
and 
atan2(y,x) = atan2(opposite,adjacent) 
 
In the class libraries I have seen, for example java, atan2(y,x) is defined in this way. Its possible that some class libraries may be defined differently? How is the atan2 function you are using defined? 
 
Martin 
-------------------------------------

An OpenOffice.org with the following is here


c1 = cos(5 deg) = 0.9962  
c2 = cos(10 deg) = 0.9848  
c3 = cos(15 deg) = 0.9659  
s1 = sin(5 deg) = 0.0872  
s2 = sin(10 deg) = 0.1736  
s3 = sin(15 deg) = 0.2588  
 
qw = 0.9437  
qx = 0.2685  
qy = 0.1277  
qz = 0.1448  
 
tan(heading y part) = 0.1633  
tan(heading x part) = 0.9254  
tan(heading) = 0.1764 (10 degrees)  
 
sin(attitude) = 0.3419 (20 degrees) 
 
tan(bank y part) = 0.4698  
tan(bank x part) = 0.8138  
tan(bank) = 0.5773 (30 degrees)  

By: fik - fik_
file RE: Error in Quaternion to Euler equations?  
2006-08-20 15:33

Hi thanks for that explaination. Yes the problem was mine, i was using atan2 in Excel to which defines atan2 as: 
 
Returns the arctangent of the specified x- and y- coordinates. The arctangent is the angle from the x-axis to a line containing the origin (0, 0) and a point with coordinates (x_num, y_num). The angle is given in radians between -p and p, excluding -p. 
 
Syntax 
 
ATAN2(x_num,y_num) 
 
X_num is the x-coordinate of the point. 
 
Y_num is the y-coordinate of the point. 
 
Which is opposite to all other definitions i have found ,stupid microsoft they define it correctly in their programing languages i have checked (but can't use c/c++ well yet and it's not in VB) So wonder why the heck it is different in excel ? 
 
 

By: fik - fik_
file RE: Error in Quaternion to Euler equations?  
2006-08-20 17:22

Well i just had to post an apology to microsoft i just googled 'atan2 definition' and it seems no absolute answer which should be. What a mess for a math function thought they would all be standadised by now. May i suggest a little note added somewhere on page about this potential confusion might save some body else the haed ache this gave me. Many thanks for your help.Anyway glad your pages where correct keep them going as i have found them very helpful in understanding a lot of things

By: Martin Baker - martinbakerProject Admin
file RE: Error in Quaternion to Euler equations?  
2006-08-21 06:51

> May i suggest a little note added somewhere on page about this potential 
> confusion might save some body else the head ache this gave me. 
 
Thanks, this has helped me improve the website a lot: 
I have updated the information about atan2 on this page: 
https://www.euclideanspace.com/maths/geometry/trig/inverse/index.htm#atan2 
 
then I have put an additional warning on the pages that use it, with a link, such as this: 
https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/ 
 
I have also added the 10, 20, 30 degree example here: 
https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/examples/ 
 
and I have included this thread here: 
https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/fik.htm 
 
Cheers, 
Martin

 


metadata block
see also:

 

Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

cover If you are interested in 3D games, this looks like a good book to have on the shelf. If, like me, you want to have know the theory and how it is derived then there is a lot for you here. Including - Graphics pipeline, scenegraph, picking, collision detection, bezier curves, surfaces, key frame animation, level of detail, terrain, quadtrees & octtrees, special effects, numerical methods. Includes CDROM with code.

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2023 Martin John Baker - All rights reserved - privacy policy.