Obtaining orientation using gyroscope and accelerometer

CL22 picture CL22 · Mar 19, 2013 · Viewed 18.4k times · Source
  • I want to monitor the orientation of a device on 2 axis: very simply: which way is down.
  • It must maintain accuracy over extended time period (12 hours)
  • It will be subject to rotations and accelerations about all axes

The device will be an Android phone with standard gyroscope / accelerometer. With those two sensor types, is it possible to satisfy the above requirements?

My thoughts so far: An accelerometer alone cannot achieve this, since given any set of values for an instant in time, it is not possible to separate the gravitational and spacial acceleration components. And I can't get my head round how a gyroscope could help resolve that. Are there any clever formulas / algorithms which would manage this?

Thanks

Answer

Pieter-Jan picture Pieter-Jan · May 18, 2013

An easy way to combine accelerometer and gyroscope data is by the use of a complementary filter. This way, you don't have problems with drift from the gyroscope and noise from the accelerometer. It is also much easier to understand and use than a Kalman filter.

You will calculate the angle from the gyroscope using an integral. And for the accelerometer you will use a tan2 function to determine the position of the gravity vector. The complementary filter would then combine these two angles as follows:

angle = 0.98*(angle + gyroData*dt) + 0.02*accAngle

Notice how you only take a fraction of the accelerometer data (just enough to compensate for the drift). You will thus use the gyroscope data for fast changes, but on the long run, you will keep following the mean value of the accelerometer angle calculation so that you don't drift.

I hope this helps. If you need more info and a C code example, I wrote an article about it here