Thanks to mobile phones, a wide range of sensors have come down in price to the point, any one can purchase one and make their own devices. Simply by going to Amazon, you can buy gyros, accelerometers and magnetic field sensors for less than 500 yen.
I purchased a couple of MPU6050 six axis devices. These include a 3 axis gyro and a 3 axis accelerometer. Interfacing is achieved using the I2C bus.
I have my trusty Raspberry PI, which allows quick prototyping of many devices. Below I have connected the gyro and Raspberry PI using the I2C bus. Of course the same is possible using many other boards such as the arduinio.
Open source software is available for the Raspberry PI, so within 10 or 20 minutes you should be able to get useful data from the gyro and accelerometer.
My objective was to use the gyro data to track the angle/orientation of a devices. I achieved that simply by integrating the outputs of the gyro.
However an issue I stumbled across has been drift. I tested for drift with my iPhone using an application called Motion Logger. As you can see in the the graph below, the angle steadily drifts over time.
In some cases drift compensation is already included in the hardware or software provided by the manufacturer and it is simply just a case of enabling the calibration.
However it may be beneficial to use the raw data and carry out any drift compensation in software.
There are a number of causes of drift, which are dependent on the gyro device.
- Under sampling
- Low frequency drift
- Temperature based drift
- Hidden software And Hardware Algorithms
I was using a gyro in a tracking application and found it failed miserably at high speeds. The cause of the problem was simple, the sample rate was simply too low. So before you choose a sample rate, it is a good idea to have an understanding of the dynamics of the system of interest.
Low frequency drift
If you simply place the gyro on a flat surface, over a period of time, output will drift. Over a period of time there will be a DC component, which can be quite significant when calculating the angle from the gyro rate.
Hidden Software & Hardware Algorithms
A word of caution. Some devices may have automatic calibration settings. I observed this with a Braveridge device, which uses an Invense gyro. I noticed for the first 20 seconds, when the device was static, there was a large drift. After about 20 seconds the data stabilized. After looking at the sensor source code, I noted it was set by default, to calibration.
However this proved quite useful. The calibration only occurred when the device had be static for a period of time. To determine if the device was static it simply used the accelerometer data.
Drift Correction Strategies
There are a range of strategies including:
- Sensor Data Fusion
- Continuous recalibration
Sensor Data Fusion
Simply combine data from multiple sensors to produce a more accurate estimate. A popular technique is the Kalman filter.
Using a Kalman filter, data from multiple sensors with different noise characteristics can be combined to produce a more accurate estimate.
Typical combinations are:
- Magnetic direction and gyro data
- Acceleration data ( using orientation with respect to gravity) and gyro data
- All of the above
The basic Kalman filter is linear and assumes noise is Gaussian. There are also extended Kalman filters for non-linear systems and the possibility of noise shaping for non Gaussian noise sources.
Below are some Kalman filter resource links.
Another technique, which is popular because it is simple, it also seems to provide adequate estimates is the complimentary filter.
Below is a simple example which combines gyro and accelerometer data. The key is to find the correct ratio, by trial and error.
angle = 0.98 *(angle+gyro*dt) + 0.02*acc
Gyro Drift Calibration
In order to automatically calibrate the gyro, the use of other sensors such as accelerometers is necessary. One method is to monitor the accelerometer.
If no acceleration is detected over a period of time, it is assumed the sensor is not moving. During this period it is assumed the gyro reading is also zero. Any reading which is not zero is assumed to be the drift. The average value over this period is assumed to be the drift.
This works quite well, however significant periods of non movement are necessary. This can range from a few seconds to up to 20 seconds.
Implementation is relatively easy. Data from the accelerometer is relatively noisey, so some filtering is likely to be required.