Post date: Jun 10, 2012 12:17:18 PM
The use of accelerometer and gyroscope as a 1-DOF tilt sensor is described here. The gyro gives a signal that is proportional to an angular velocity. It is sensitive to fast motion, however, we will need to integrate the signal to get an angular position. When we integrate a noisy signal, it will cause a signal drift in the long run. An accelerometer measures both static (including gravity vector), and dynamic acceleration. So in a stationary setup, we can use an accelerometer to determine the gravity vector and hence determine the tilting angle. Unlike the gyro, an accelerometer will give a drift free stable reading, however, the measurement will be disturbed by transient motion. Since both sensors have advantages and disadvantages, we need a wise way to combine the information from both sensors. Two techniques exist: complementary filter and Kalman filter. With a complementary filter, the accelerometer signal, which has good low frequency characteristics, will be low-pass filtered. The gyro signal, which has good high frequency characteristics, will be high-pass filtered. Then the filtered results from the accelerometer and gyro are combined. This method will improve the measurement, however, it cannot entirely eliminate drift. The Kalman filter is a better solution. We will need to create a sensor model. And put in noise characteristics for each sensor. The Kalman filter will magically combine the signals that minimizes the resulting measurement noise.
The iMEMS sensors from Analog Device are chosen:
Accelerometer ADXL203 ($29.95) is a dual axis acceleration. It measures acceleration in range ±1.7g with sensitivity 1 V/g, and gives analog output in range 0-5 V. ADXL203 datasheet
Gyroscope ADXRS401 ($50.00) measures one axis yaw rate up to 75 deg/s with sensitivity 15 mV/deg/s, and gives analog output in range 0-5 V. ADXRS401 datasheet
Sparkfun.com made an IMU combo board of ADXL203 and ADXR401 ($114.95).
I found the code written by Trammell Hudson from Rotomotion. Here is the sensor model used in Kalman filter.
Matlab Implementation
I translated this C code into Matlab m-file program (tilt.m) and also add the codes to generate simulation signal. After running the simulation signal through kalman filter, the results from kalman filter is compared with the original signal, and also compared with the results if the accelerometer or gyroscope is used alone. The gyroscope result shows the drift from integration error, while the accellerometer gives a noisy output. Kalman filter gives the most desirable result. The resulting angle is drift free and not as noisy as that from accelerometer. Click on the graph to see larger image.
PIC16F88 Implementation
I implement the Kalman filter algorithm on a PIC16F88 microcontroller. There are two versions of programs with different output types: PWM and analog outputs. Both programs were written in C for Hi-Tech PICC Compiler.
PWM Output Version
In this version, the angle measurement is showed on the LCD display. In addition, the microcontroller give PWM signal output. The PWM signal is a pulse width modulated signal with the positive width of the pulse representing the angle. The pulse width varies from 1mS (0° ) to 36.99mS (359.9° ) – in other words 100uS/° with a +1mS offset. This output specifcation is similar to the CMPS03 compass module. The source code of the PWM version for Hi-Tech PICC compiler is available here: Tilt_LCD_PWM.zip The picture on the left shows the pulse output on the oscilloscope. The duty cycle of the pulse changed promptly as I tilted the project board back and forth. Like the simulation result, PIC microcontroller gives drift free measurement. However, I am not sure if the result is accurate. The angle measurement seems not sensitive to fast motion. I have a question that is it only low-pass filtering of the acceleration signal.
Analog Output Version
In this version, the microcontroller gives analog output with sensitivity of the output is 10 mV/deg. The reading can be from 0-360 deg, yielding the range of voltage output from 0-3.6 V. The R-2R ladder is used as a digital to analog converter (DAC) with 10 bit accuracy. The output can be directly measured from the ladder as shown in the schematic diagram. However, an op-amp circuit (voltage follower) may be connected at the output if we want to have low impedance output. Note that the LCD display is not available in this analog version due to the limited number of I/O pins. The source code of the analog version for Hi-Tech PICC compiler is available here: Tilt_Analog.zip The picture on the left shows the analog output on the oscilloscope. The voltage changed up and down as I tilted the project board back and forth.
The next task is to test the sensor accuracy, I will need to find a good angle reference to compare with my measurement. I plan to use an encoder to measure angle.
Some good references:
"An Introduction to the Kalman Filter" by Greg Welch and Gary Bishop, A good introduction to Kalman Filter.
"Kalman Filtering" By Dan Simon, Demonstration of Kalman filter to estimate vehicle position/velocity.