# Converting MPU-6050's quaternion output to YPR angles

I am using the I2Cdev library which configures the MPU-6050 to use it's DMP and generate a quaternion output on it's FIFO. The problem is that when that quaternion number is converted to yaw, pitch and roll angles, the pitch and roll angles are limited to +-90 degrees (this constraint comes from the inverse trigonometric functions used in the equations).

What equations should be used to convert the quaternion number in euler's angles that give full information on the sensor's orientation.

• Look up "tangent unwrapping" for a start. The general approach is to find out what quadrant you're in on the Re/Im plane, and adjust accordingly. May 25, 2017 at 21:14
• When I rotate the sensor from 0 to 180 degrees around the pitch axis, the quaternion's q1 goes from 0 to 1 and q3 goes from 1 to 0, should I divide the quadrant? There has to be a standard way of solving this problem. May 26, 2017 at 9:32
• You don't lose sensor information by restricting pitch to range from -90 to 90 when roll and yaw are allowed to range from -180 to 180. The transformation from quaternion to yaw, pitch, and roll depends on the conventions used to define the quaternion and the yaw, pitch, and roll. For a given convention there are many "almost correct" transformations that will work for the majority of angles but only one truly correct transformation that will work for all angles including south and north poles where the "almost correct" transformations produce gimbal locks (spurious flips and rotations). Jan 17, 2022 at 5:30

Before I answer, it should be noted that there are some benefits to using quaternions:

• They avoid gimbal lock (important for devices that may be turned upside down)
• Quaternion transformations and multiplications generally require fewer mathematical operations than Euler angles

That being said, I would consult the Representing Attitude article from Stanford University.

To summarize, equation 452 states that:

$$\begin{bmatrix} \psi \\ \theta \\ \phi \end{bmatrix} = \begin{bmatrix} Y \\ P \\ R \end{bmatrix} = \begin{bmatrix} \mathrm{atan2}\left( -2q_1q_2 + 2q_0q_3,\; q_0^2 + q_1^2 - q_3^2 - q_2^2 \right) \\ \mathrm{asin}\left( 2q_1q_3 + 2q_0q_2 \right) \\ \mathrm{atan2}\left( -2q_2q_3 + 2q_0q_1,\; q_3^2 - q_2^2 - q_1^2 + q_0^2 \right) \end{bmatrix}$$

Wikipedia has a good explanation of atan2, if you need it.

• Thank you, but these equations return the angle constrained in +-90 degrees. May 26, 2017 at 9:03
• @VasilKalchev That's the point of using atan2 instead of atan. By taking a second argument (that defines the quadrant), atan2 returns a result between ±180°. May 26, 2017 at 18:08
• Yes, but the pitch angle uses asin :(. May 26, 2017 at 19:24
• The pitch doesn't need to go beyond ±90° because yaw and roll are defined over ±180°. This graphic may help visualize it. May 27, 2017 at 6:22
• How would someone go on about the fact that somehow pitch and roll are swapped after this conversion? Oct 7, 2021 at 0:01