# Euler angles or quaternion?by Omid Sarbishei, Ph.D.

Posted on March 30, 2017 at 8:44 pm8:44 pm

Orientation of a moving object in 3D space can be represented by the three Euler angles: Yaw, Pitch and Roll, where Yaw is your direction angle, pitch is your elevation angle, and Roll is your tilt angle. More specifically, if we consider the reference Earth frame with x, y, and z axes representing North, West, and Up, we will have:

Yaw $$(\psi)$$: rotation around z axis Pitch $$(\theta)$$: rotation around y axis Roll $$(\phi)$$: rotation around x axis

The rotations should be done in a specific order to have matching results. In aerospace and many other applications, we perform rotation in yaw first, then pitch, and finally roll. This is how a plane takes off, where initially on the ground it changes its yaw (direction) to get into the take-off line, then it takes off the ground by changing its pitch (elevation), and finally moves left/right by doing a roll (tilting its wings). We will consider this sequence of rotations here as well.

Now the question is how can we compute orientation for a moving object? Shall we simply use the Euler angles? Well, Euler angles are very informative in the sense of representing an actual orientation. However, calculating Euler angles directly using physical sensing devices has major drawbacks. Namely, there are certain corner cases, and more technically singularity conditions, where a unique Euler angle representation cannot be extracted from a physical sensing device.

Let us explain this matter in a very simple way. Consider a 3-axis accelerometer, which captures the force vector. If the device is not involved with a major external force, it is acceptable to assume that the force vector is approximately equal to the gravity vector. The fact that gravity is pretty much constant on earth, i.e., 1g = 9.8 m/s2, gives accelerometers a mathematical edge to detect the device’s orientation in pitch and roll angles. This key ingredient has been used in almost all motion tracking devices! A simple phenomenon with numerous applications! Let us elaborate on this. The gravity vector in the reference earth frame is pointing downwards and it is equal to $$(0,0,1g)$$ on earth. Now, if the accelerometer sensor, which is mounted on our device, starts rotating around the reference x-axis (North vector) to generate roll, the y and z components of the accelerometer readings will start to change. Also, if the rotation is done around the reference y-axis (East vector) to generate pitch, the x and z components of the accelerometer readings will start to change. In summary, one can show that using the aerospace sequence of rotations, the accelerometer reading vector is as follows:

$$Acc = (a_x,a_y,a_z) = (-\sin\theta\,\cos\theta\sin\phi\,\cos\theta\cos\phi)$$

Now let us assume that the pitch angle $$\theta$$ is 90 degrees (elevation up). Under such conditions, accelerometer readings will be $$(-1g,0,0)$$, which makes them independent from the roll angle $$\phi$$. This is a singularity condition, where the Euler angle roll is lost within accelerometer readings at 90 degree elevation.

Quaternion is a different representation of a rotation (orientation), which does not suffer from this singularity condition. Generally, one can describe an arbitrary orientation by a 3-dimensional reference vector $$r = (r_x,r_y,r_z)$$ and a rotation angle $$\alpha\$$. Quaternion is a compact scheme, a 4-dimensional vector, which contains these 4 key elements. Conventionally, the reference vector r is defined to be unit-length and so is the quaternion, which is defined below:

$$Q = (q_1,q_2,q_3,q_4) = (w,x,y,z) = (\cos(\dfrac{\alpha}{2}),r_x\sin(\dfrac{\alpha}{2}),r_y\sin(\dfrac{\alpha}{2}),r_z\sin(\dfrac{\alpha}{2}))$$

If we negate $$\alpha\$$, the relative orientation will be reversed, which defines the conjugate quaternion as follows:

$$Q^* = (q_1,-q_2,-q_3,-q_4) = (w,-x,-y,-z)$$

Quaternion has very interesting properties. For instance, one can rotate a 3-dimensional vector $$p$$ using the quaternion $$q$$ to get a new 3-dimensional vector $$\hat{p}$$ using the following formula:

$$\hat{p} = q\otimes p \otimes q^*$$,

where the three dimensional vectors $$p$$ and $$\hat{p}$$ contain a 0 inserted as the first element to make them 4 element vectors, and the operator is the Hamilton product defined below:

$$c = (c_1,c_2,c_3,c_4) = (a_1,a_2,a_3,a_4) \otimes (b_1,b_2,b_3,b_4)$$,

where

$c_1 = a_1b_1 – a_2b_2 – a_3b_3 – a_4b_4$ $c_2 = a_1b_2 + a_2b_1 + a_3b_4 – a_4b_3$ $c_3 = a_1b_3 – a_2b_4 + a_3b_1 + a_4b_2$ $c_4 = a_1b_4 + a_2b_3 – a_3b_2 + a_4b_1$

The Hamilton product is not commutative, i.e., $$a\otimes b \neq\ b\otimes a$$

The rotation formula $$\hat{p} = q\otimes p \otimes q^*$$ does not involve trigonometric functions unlike the rotation matrix represented by Euler angles. Also, from the quaternion representation one can directly derive the Euler angles as follows:

$\phi = atan2(2(q_3q_4 + q_1q_2),1-2(q_2^2+q_3^2))$ $\theta = \arcsin(2(q_1q_3 – q_2q_4))$ $\psi = atan2(2(q_2q_3 + q_1q_4),1-2(q_3^2+q_4^2))$

The quaternion representation has been used as the standard format for orientation representation in almost all gaming applications and motion tracking device.

Motsai designs and builds innovative embedded systems, and is the leading Canadian developer of miniature low-power, wireless devices used in wearables and human motion analysis. Please feel free to send us an email at info@motsai.com or drop us a line at +1-888 -849-6956. Don’t forget to signup for blog updates below.