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.
-
1\$\begingroup\$ 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. \$\endgroup\$Scott Seidman– Scott Seidman2017年05月25日 21:14:45 +00:00Commented May 25, 2017 at 21:14
-
\$\begingroup\$ 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. \$\endgroup\$Vasil Kalchev– Vasil Kalchev2017年05月26日 09:32:07 +00:00Commented May 26, 2017 at 9:32
-
\$\begingroup\$ 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). \$\endgroup\$Dimples– Dimples2022年01月17日 05:30:34 +00:00Commented Jan 17, 2022 at 5:30
1 Answer 1
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.
-
\$\begingroup\$ Thank you, but these equations return the angle constrained in +-90 degrees. \$\endgroup\$Vasil Kalchev– Vasil Kalchev2017年05月26日 09:03:46 +00:00Commented May 26, 2017 at 9:03
-
\$\begingroup\$ @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°. \$\endgroup\$Caleb Reister– Caleb Reister2017年05月26日 18:08:19 +00:00Commented May 26, 2017 at 18:08
-
\$\begingroup\$ Yes, but the pitch angle uses asin :(. \$\endgroup\$Vasil Kalchev– Vasil Kalchev2017年05月26日 19:24:22 +00:00Commented May 26, 2017 at 19:24
-
1\$\begingroup\$ The pitch doesn't need to go beyond ±90° because yaw and roll are defined over ±180°. This graphic may help visualize it. \$\endgroup\$Caleb Reister– Caleb Reister2017年05月27日 06:22:58 +00:00Commented May 27, 2017 at 6:22
-
\$\begingroup\$ How would someone go on about the fact that somehow pitch and roll are swapped after this conversion? \$\endgroup\$filip– filip2021年10月07日 00:01:55 +00:00Commented Oct 7, 2021 at 0:01