Gyros and Accelerometers: The Basics
1. Introduction
Once upon a time, gyros and accelerometers where mythical devices that started at the cost of a small car and ranged to the price of a villa in Monaco. Fortunately, the cost of these sensors dropped substantially in recent years where they are now well in the price range of a student of hobbyist.
Whilst gyros and accelerometers are cool, they are full of traps for the unwary. This tutorial starts covering the basics of gyros and accelerometers and goes through some of the common pitfalls. In later tutorials, we'll go into more advanced topics like how to do your own Pitch and Roll calculations.
Before getting too far into it, it's worth looking at http://en.wikipedia.org/wiki/Yaw... to see my definitions for "pitch, roll and yaw"
-----------------------------------------------------------
2. What's a Gyro?
Put simply, a gyroscope (or gyro for short) is a device that measures angular rate about a particular axis - that is, the rate of change of angle with time.
When I talk about "rotation about a particular axis", there is a convention associated with it. It's the same as for a magnetic field - stick your right-hand thumb in the direction of the axis and curl your fingers around. The direction of your curled fingers is the positive direction of rotation.
Not all devices will measure rate about the same axis. For example, so called "yaw-rate" gyros will measure about the axis perpendicular to the circuit board on which it's mounted. Other devices measure about the "x-axis" which is usually in the lengthways direction of the chip.
The reason why "yaw-rate" is in inverted commas is because it's only truly "yaw-rate" when the circuit board is precisely level with the ground. Imagine standing the board directly on it's side with the axis pointing to the right - it will now measure the rotation rate of doing a backflip, not a flat spin as would be case when it's level with the ground.
As a result, a gyro will always read the rotation in the body-fixed co-ordinate frame. That is, unless it's perfectly aligned with an axis, it will read a combination of pitch-rate, roll-rate, and yaw-rate. Change the axis a little and it reads a different proportion of the various rates.
Of course, this is an ideal case - Like all sensors, they have errors and are not as easy to deal with as what might be imagined. I'll go through these a little later since many of the errors are very similar to accelerometer errors.
-----------------------------------------------------------
3. What's an accelerometer?
An accelerometer reads linear acceleration. Unlike gyros which (with notable exceptions) read only about a single axis, most commercial low-cost accelerometers have three accelerometers built into one package - that is, they can sense accelerations in the x-, y- and z-directions.
Unless you're in orbit or in perfect free-fall, accelerometers will always read gravity as well. Whilst this seems like a curse, in reality it's quite useful - it means we have a vertical reference.
Like gyros, accelerometers will always output accelerations in the body-fixed coordinate frame - a small tilt will mean that x- and y- will also read some portion of gravity along with the z-axis. Hence, by measuring the proportion of gravity in the x-direction (or y-direction) compared to the z-direction, we can work out our pitch (or roll) angle to within the accuracy of the sensor! Unfortunantly, if you're not perfectly still, your estimate of your angle will be affected by small changes in motion.
In addition to sensing angle and motion, accelerometers are sometimes used to measure and analyse vibration - it can be useful for determining when equipment needs to be serviced.
-----------------------------------------------------------
4. Interfacing to Gyros and Accelerometers
The interface to a gyro or accelerometer is entirely dependent on the device itself. Traditionally, they've been analogue out (and are still oftne cheaper in many instances), but more modern devices are I2C, SPI or PWM.
If you're using an analogue to digital converter, be aware on how they can introduce errors into your system - temperature, bias and scale factor are key to getting a good inertial sensor system.
Any mega AVR is more than capable of taking measurements from multiple sensors (including if there is an ADC involved) and performing some basic processing. Some are even capable of doing a full attitude solution calculation on the AVR itself at something like 50-100Hz.
For anything more complicated, it's worth using an AVR for sampling the sensors then sending the data off to a more powerful processor for sophisticated algorithms (there's whole textbooks written on algorithms alone).
-----------------------------------------------------------
5. What errors exist on Gyros and Accelerometers?
The first thing you'll notice when you plug in your first accelerometer is that they're really noisy. Seriously noisy! Unfortunately, unless you're willing to spend thousands of dollars, you'll have to do a lot of filtering to make these sensors clean.
So what do I mean by clean?
My original training is in aerospace, but I'm firmly ground-based these days and I dabble in these types of sensors as part of my job from time to time. As such, I tend to view these sensors through three main classes of applications:
* Motion detection (e.g. hard disk falling, pedometer)
* Attitude measurement (e.g. flight control, chassis stability, robotic arms)
* Inertial Navigation (e.g. airliners, military jets)
Each one requires a full order of magnitude (or more) better performance out of the sensor with a multiple order of magnitude cost difference.
Motion detection tends to be the simplest: A high pass filter, integrator and threshold on a cheap accelerometer tends to be sufficient - many chips even have such algorithms built in. Noisy devices in this class are very common, because all they're really doing is looking for deviations from a steady-state value of the sensor.
Attitude measurement relies on the accelerometer detecting the gravity vector - simple trigonometry can resolve the measurements into pitch and roll angles. Any noise or bias on the measurements directly corresponds to noisy attitude estimates, with a rule of thumb of about 1 milliradian of angular error per milli-g of error (noise/bias).
Inertial Navigation is by far the most demanding on these sensors - the purpose is that you're trying to integrate acceleration **twice** to get an estimate of position. Therefore, any bias will quadratically increase error with time, and any noise will turn into a doubly integrated random walk. Given a flight can easily last 8 hours or more, you can see why they demand precision!
So can't I just filter it for a long time to clean up the signal?
Well, Yes and No, depending on your application. If you're a non-static application, then quite clearly you can't average for seconds at a time!
Even if you've got a static application, it's not just a matter of averaging/low pass filtering forever. The reason being that there's not just white noise acting on the device.
In addition to white noise, there are several other types of noises not always seen or cared about in the electronics world. I'll limit myself to a couple of these, which tend to dominate low-cost devices:
* Turn-on bias
* Temperature bias
* Bias Instability ("Flicker Noise", "Correlated Noise", "Gyro drift")
* Bias Random Walk
* Scale Factor error.
In general, you'll only need to start worrying about these errors when doing something needing precision, like attitude measurement.
Turn-on bias is a constant offset that changes every time that the device is turned on - not too much different from having to zero a pair of digital scales.
Temperature bias is a change in the bias of these devices as a function of temperature. After all, MEMS sensors are just little machines in silicon and temperature will expand/contract the structures inside the device. This is particularly dominant - especially if your device is exposed outdoors.
Bias instability, sometimes called gyro drift (it has a specific meaning, but I'll talk generally) refers to random changes in the bias of these devices just because they feel like it, or because they're having a bad day. It happens very slowly and like white noise, you can't predict it (although you can statistically characterise it)
Random Walk Bias also refers to random fluctuations to the bias, but one that follows a "random walk process". See http://en.wikipedia.org/wiki/Ran...
The result is, after averaging for a period of time, the standard deviation of your average actually increases because of the slow fluctuations in bias listed. We generally use a tool known as an Allan Variance chart to analyse the bias performance of these sensors.
I've used the word "bias" a lot. It's pretty much as you'd expect - a constant (or slowly moving) offset from the true measurement. Because it moves very slowly, it's actually quite difficult to deal with - you just can't average it away like you can with white noise!
Unfortunately, because none of the low-cost datasheet publishes any of the standard errors (there's an IEEE standard), there's generally a lot of playing around before working out if a device is suitable for your application.
-----------------------------------------------------------
6. FAQs
a) So what are "Degrees of Freedom" (DoF)?
This refers to the number of independent dimensions that an Inertial Measurement Unit (IMU) is capable of measuring. There are three linear dimensions (e.g. acceleration) and three angular dimensions (e.g. angular rate - gyros). Hence, 6DoF.
For a "fully observable" IMU, you need to be able to measure all 6 degrees of freedom. Otherwise, you'll need to "guess" the other motion parameters.
Some units of "9DoF" or "8-axis". Usually, this refers to an IMU that also contains a 2-axis or 3-axis magnetometer. It's marketing talk, not true "degrees of freedom" as such, but the compass *is* useful (so long as you know there's no metal nearby!)
---
b) I can use an IMU to measure position, right?
In theory, you can integrate the accelerometers twice to get position (adjusting for the attitude by integrating the gyros) to make what's called an Inertial Navigation System (INS).
Unfortunately, as soon as you start integrating, you start accumulating errors - twice for the accelerometers and three times for the gyros, as the attitude couples into position! Therefore, for the grade of sensors you can get from SparkFun, inertial navigation so useful only for a few seconds at best.
In general, you need a good quality IMU to perform Inertial Navigation. For example, the Novatel SPAN is about $40K, and will give (at best) 7m after one minute, though in my experience, even this is optimistic.
There are certain trick you can use to make it better, but without reference to an external source (e.g. GPS), the position and attitude will drift forever.
-----------------------------------------------------------
7. Legal Stuff
(c) 2010, damien_d, https://www.avrfreaks.net
You may share this document under the Creative Commons Attribution-Share Alike 3.0 Unported Licence. You can read the full text of the licence at http://creativecommons.org/licen...
To the maximum possible extent under law, there is NO WARRANTY associated with this document. You should seek independent technical and legal advice before using this document as the basis of any design.
-----------------------------------------------------------
8. Further Reading:
This was originally a post in the following thread:
* https://www.avrfreaks.net/index.p...
I'm intending to do a tutorial series on accelerometers and gyros. As I write them, I'll add the links here.
-----------------------------------------------------------
9. Improving this Tutorial
This tutorial is intended to be a living beast. If you can spot mistakes, provide constructive criticism or otherwise suggest improvements, just leave a post below.
-----------------------------------------------------------
10. Edit History
1.0 - 2nd Feb 2010, Initial version
1.1 - 4th Feb 2010, Small amendment for gyro drift
1.2 - 24th Sep 2010, Added FAQ
1.3 - 8th Feb 2011, Fixed typo
-- Damien