BMA180 Pitch and Roll

Accelerometers can be used for measuring both dynamic and static acceleration. Pitch and Roll are static measurements where gravity is the acceleration being measured. The earth’s gravity is a constant acceleration where the force is always pointing down to the centre of the Earth. When the accelerometer is parallel with the gravity, the measured acceleration will be 1G, when the accelerometer is perpendicular with the gravity, it will measure 0G. So when rotating the accelerometer around the Z axis gravity will not have any change in the results. This means that a horizontally placed accelerometer can not be used for measuring the yaw component and we must use another device called a magnetometer. All accelerometers will experience acceleration in the range of +1g to -1g as the device is tilted from -90 degrees to +90 degrees due to gravity. In addition will be other forces such as an increace of speed which will impact the accelerometer readings.

In order to define the angles of the accelerometer in three dimensions the pitch, roll and theta are sensed using all three outputs of the accelerometer. Pitch (rho ρ ) is defined as the angle of the X-axis relative to ground.
Roll (phi φ) is defined as the angle of the Y-axis relative to the ground. Theta θ is the angle of the Z axis relative to gravity.
In order to determine the angle of tilt, θ, the acceleration is compared to a known position with known g offset. This can be for instance a horizontal position where the z axis experiences 1g and the other two axis zero g. It is then possible to determine if the g forces acting on the accelerometer are positive or negative, e.g. if value is greater than the offset then the acceleration is seeing a positive acceleration, so the offset is subtracted from the value and the resulting value is passed to a tilt algorithm. If the acceleration is negative, then the value is subtracted from the offset.

Unfortunately, this theory can only be applied when the accelerometer is completely static. If the accelerometer is accelerating, there will be other components of acceleration acting on it causing the calculated tilt angle to be inaccurate. One solution for this problem is low-pass filter the data from the accelerometer. Typically, the acceleration components caused by dynamic movement only happen in a short period of time, while the gravity acceleration is permanent. There is another effect which is present in accelerometer which is often reffered to as jitter. This describes the non static nature of the results. They tend to be eratic even when the accelerometer is completely stationary. By low-pass filtering the data from the accelerometer, we can filter out some of the the unwanted high frequency acceleration components and we are left with only the gravity acceleration which can be used to calculate the tilt angle. This can even be done using the built in filters in the chip itself. In the example below a 10Hz filter is used. However, the low-pass filter will increase the latency and slow down the response time.

There are other more sophisticated possibilities too such as Paul Badger’s digital smooth. A digital smoothing filter for smoothing sensor jitter. This filter accepts one new piece of data each time through a loop, which the filter inputs into a rolling array, replacing the oldest data with the latest reading. The array is then transferred to another array, and that array is sorted from low to high. Then the highest and lowest 15% of samples are thrown out. The remaining data is averaged and the result is returned. Every sensor used with the digitalSmooth function needs to have its own array to hold the raw sensor values. This array is then passed to the function, for it’s use. This is done with the name of the array associated with the particular sensor.

Many people use gyro’s to complement the accelerometer data. Gyro’s have drift which mean that they over time lose accuracy but in the short term they are very accurate.
What typically happens is that the short term of the gyro and long term of the accelerometer is taken as the dominant factors in a fused set up.
Many different types of filters exist such as Kalman, DCM or complementary.
For now i will show the results of only using the accelerometer data.

You can calculate the two angles for pitch and roll using the formula below.

I use the library from fabio Varesano which you can find here
But there are plenty of others around. Just look at the list of links at the bottom of the page and you will find many more.

Below is a short program which demonstrates some of the functionality of this library. Here it is used to calculate pitch and roll angles in degrees.
The listing below gives a short screendump of the output of the program. The last two colums are pitch and roll and the other three are the accelerometer values expressed in g.
You can download it from here.