[Math] Quaternion to Euler angles conversion

coordinate systemsquaternionsrotationstrigonometry

I have written the following MATLAB code for transforming Quaternion to Euler angles based on the mathematical formula from wikipedia:

function [phi theta psii]=quat(q0,q1,q2,q3)

q_length=sqrt(q0*q0+q1*q1+q2*q2+q3*q3);

q0=q0/q_length;
q1=q1/q_length;
q2=q2/q_length;
q3=q3/q_length;

phi=atan2(2*(q0*q1+q2*q3),1-2*(q1*q1+q2*q2));
theta=asin(2*(q0*q2-q3*q1));
psii=atan2(2*(q0*q3+q1*q2),1-2*(q2*q2+q3*q3));

end

I tested the code:

[phi theta psi]=quat(0.99619,0,0,0.08716)
psi*180/pi
ans =

   -115.0435

The result is different compared to this website which suggests:

$\theta=0,~~~\phi=0,~~~\psi=-90$

Best Answer

Be careful when you convert between quaternions and euler angles. One of the main source of confusions are the conventions adopted to represent angles. The same quaternion can represent a rotation or it's inverse based on the adopted convention.

Your equations seems to be correct at first glance. As a comparison write some code to compute the conversion to the rotation vector. I do not know what the website does and I believe that when it comes to quaternions it is safe not to use software from different sources because you do not know the conventions that are adopted.

By the way, quaternions are provided by matlab in the aerosym toolbox (included in the complete version), but they follow the JPL convention and not the Hamiltonian.