# Use data from gyroscope to calculate orientation

From a gyroscope I'm getting angular velocities [dRoll, dPitch and dYaw] as rad/s, sampled at intervals dt = 10ms.

How do I calculate the short term global orientation (drift ignored) of the gyroscope?

Pseudo code would be helpful.

• I have same your question , Can I know how I get the angles from angular velocity ?? – user19008 Dec 8 '17 at 16:05

## 3 Answers

gyroscopes do not measure [dRoll ,...] they measure body rates. These are not the same things. There is a transformation matrix ( that I do not have on hand) that relates body rates to euler rates. The euler rates are then integrated to get the short term change in orientation.

-- relation --

this is the relation between the measured body rates from the imu and the euler rates.

\begin{equation} \label{eq:euler_rates} \begin{bmatrix} \dot \phi \\ \dot \theta \\ \dot \psi \end{bmatrix} = \begin{bmatrix} 1 & \sin\phi \tan\theta & \cos\phi \tan\theta \\ 0 & \cos\phi & -\sin\phi \\ 0 & \frac{\sin\phi}{\cos\theta} & \frac{\cos\phi}{\cos\theta} \end{bmatrix} \begin{bmatrix} p \\ q \\ r \end{bmatrix} \end{equation}

where $\left[ p, q, r \right] ^T$ are the body rates measured from the imu.

• Yes, but is it not correct that angular rate integrated over time is an angle - in this case a rotation about the principal axes "roll, pitch and yaw"? – Lars Mar 29 '16 at 9:52
• mostly no. i have edited my question with the correct integration. it is easy enough that there is no excuse not to use the correct equations. – holmeski Mar 29 '16 at 12:48
• yes, integrating the body rates is the same as integrating the euler rates when you start at zero attitude but this is only true when you are integrating a single axis, as soon as there is a rotation about a second axis the solution becomes incorrect. Even if one does not expect large rotations about two axes this approach should not be used because the correct approach is so simple. – holmeski Mar 29 '16 at 12:59
• I noticed that $\psi$ does not appear explicitly in the matrix. Assuming $\dot{\psi}$ is not zero, when $\psi$ changes wouldn't this have to be included in the matrix? – uhoh Oct 11 '17 at 10:29
• nope! $\psi$ is heading. The left hand side is orientation rate. The right hand side is turn rate. The heading of the vehicle will not change how the orientation is changed by the turn rate. – holmeski Oct 11 '17 at 21:21

Short answer: A gyroscope by itself cannot determine its global reference frame. You either need to start the device in a known initial global orientation and measure how much the orientation changes, or you need other devices to determine the "initial" or periodic global reference frame.

Long answer:

A gyroscope only gives you angular velocities, which then can be integrated into relative yaw, pitch, and roll from a starting point. This is a 3 degrees of freedom (3dof) system.

This can be combined with an accelerometer which measures the acceleration. If your device starts in a stationary position, the accelerometer can then be used to measure the direction of gravity and establish the initial "down" direction towards gravity. You now know the initial direction of down (but not north/south/east/west). This is a 6dof system.

Next, you can combine the above two with a magnetometer. This detects magnetic fields including magnetic north. Your device in its stationary starting position can determine which direction is north. You now know the initial down position and the initial north direction in the global reference frame. This is a 9dof system.

To get the orientation from each of the axes, you need to integrate the angular velocities with respect to time.

Below is the code implementation of the said integration:

//What we have
dPitch = 0; angular rate from Pitch
dRoll = 0; angular rate from Roll
dYaw = 0; angular rate from Yaw

//What we want
aPitch = 0; angle from Pitch
aRoll = 0; angle from Roll
aYaw = 0; angle from Yaw

//Calculating the angles:
aPitch += dPitch*dt; // or aPitch = aPitch + dPitch*dt;
aRoll += dRoll*dt; // or aRoll = aRoll + dRoll*dt;
aYaw += dYaw*dt; // or aYaw = aYaw + dYaw*dt;

• I'm aware that I need to integrate angular velocities to get angles, but the way you suggested only gives the accumulated rotation of the gyroscope's axes and not the orientation. What I want are the angles in a global reference system. – Lars Mar 27 '16 at 13:09