# Integrating Gyroscopes – Calculating Orientation pt2

By | February 20, 2016

### Integrating Gyroscopes

Gyroscopes measure angular velocity. As with regular velocity, by multiplying by time you get change in position (a distance). By adding these distances up as you go, you can estimate position. Naturally, every little error will also get added up and eventually you’ll end up with large errors. This is called drift and is the reason gyros can’t be used alone in an orientation sensing system.

To integrate angular rates from gyroscopes, you must calculate angular displacement and add this displacement to an accumulator to estimate orientation. This quaternion accumulates angular displacement from the gyros. I’ve always called it ‘q’. q represents the estimated orientation of the body.

etk::Quaternion has a function called fromAngularVelocity(). It takes angular velocity in radians per second, as well as time ( dt ) in seconds where time is the gyroscope sample rate in seconds. If you’re reading the gyros 200 times per second, dt would be 1/200=0.005s.

You’ll probably know from previous reading that to ‘add’ some orientation using quaternions, you must multiply.

The fromAngularVelocity function uses sin and cos to calculate angular displacement. If the angular rates are assumed to be small, there is a faster approximation that can be used.

This works sufficiently well in most cases and is substantially faster than the fromAngularVelocity function.

### Correcting Gyros For Drift

Using accelerometers and magnetometers are terribly noisy sensors. Gyroscopes are comparatively noise-free, but they only measure angular rates. Every small error is also integrated and eventually q will become very inaccurate. These errors can be due to mechanical vibration, temperature effecting gyro bias, lack of floating point precision and gyros measuring the rotation of the earth (15 deg per hour). This process of accumulating errors is called gyro drift. Accelerometers and magnetometers can be used to create a quaternion (accel_q) that represents the actually orientation of the body referenced to Earth, and this can be used to correct q for gyro drift.

Spherical Linear Interpolation (SLERP) can be used to calculate a point in between two quaternions. The idea is to use SLERP to find a point somewhere between q and accel_q, and then move q to that point.

ORIENTATION_FILTER_GAIN is a value between 0 and 1 which determines how much slerp should move q. If it’s 0, q won’t change. If it’s 1, q will become equal to accel_q every time. The ideal value will depend on how quickly q drifts and how noisy accel_q is. A reasonable starting point is 0.05.

Continue on to part 3 . . .