In the previous post we have seen, how we can simulate the rotations of the gravity vector (thus measuring the exact tilt) with the help of the gyroscope. During that measurement we moved the device only slowly to validate the claim that the gyroscope is able to track the gravity vector for a certain period of time. We were aware of the fact that measurement errors for this type of measurement will eventually accumulate and therefore we have to pick the correct gravity vector time to time.

In this post we go one step further. We will use the gyro-based simulated gravity vector only if the accelerometer does not provide us with reliable gravity vector measurement (because the device is subject to motion acceleration too). But how to figure out if the gravity measurement of the accelerometer is reliable or not? Let's see the picture below which is shows the absolute value (the length) of the accelerometer's output vector as a function of sample count when the device is subject to a "tennis-like" movement. This means that the device is held in one hand and I simulated as if it was a tennis racquet. The device rotates but is also subject to a significant motion acceleration.

We have problem when the absolute value of the acceleration is significantly larger (or smaller) than 1 g (about 10 m/s^2). This means that when the absolute value of the acceleration vector is close to 1g, we can be quite sure that there is no motion acceleration and we have reliable gravity vector measurement.

This detection method is not foolproof, however. We can construct gravity and motion acceleration vector arrangements so that the length of the resulting vector is 1 g, still it points to completely wrong direction compared to the gravity vector. The keyword is "construction". Even though these arrangements are possible, it is very unlikely that they persist for a long time during motion.

So the gyro compensation algorithm is the following:

Let's see now our racquet swing. The samples were taken with the Android application attached to the previous post, the analysis was done with this Sage script. The Sage script takes the samples from capture.csv file - you can easily change that in line 456. Blue line is the collection of 3D acceleration vectors from the accelerometer, red line is the gyro-compensated gravity vector (taken from the accelerometer if there is no motion, simulated with the help of the gyroscope if motion acceleration is detected).

As you can see, the blue line is everywhere. Actually it goes out of the coordinate system as the Nexus S has its accelerometer limited at 2g - that's why you see the acceleration curve turned into straight lines at the fastest points of the movement. The red line, however, follows the curve of the device's tilt nicely and eventually ends almost where the acceleration's blue line does. Then it gets compensated by the accelerometer's gravity vector measurement - no cumulative errors.

There's more to come, just stay tuned.

In this post we go one step further. We will use the gyro-based simulated gravity vector only if the accelerometer does not provide us with reliable gravity vector measurement (because the device is subject to motion acceleration too). But how to figure out if the gravity measurement of the accelerometer is reliable or not? Let's see the picture below which is shows the absolute value (the length) of the accelerometer's output vector as a function of sample count when the device is subject to a "tennis-like" movement. This means that the device is held in one hand and I simulated as if it was a tennis racquet. The device rotates but is also subject to a significant motion acceleration.

We have problem when the absolute value of the acceleration is significantly larger (or smaller) than 1 g (about 10 m/s^2). This means that when the absolute value of the acceleration vector is close to 1g, we can be quite sure that there is no motion acceleration and we have reliable gravity vector measurement.

This detection method is not foolproof, however. We can construct gravity and motion acceleration vector arrangements so that the length of the resulting vector is 1 g, still it points to completely wrong direction compared to the gravity vector. The keyword is "construction". Even though these arrangements are possible, it is very unlikely that they persist for a long time during motion.

So the gyro compensation algorithm is the following:

- If the absolute value of the acceleration vector is significantly larger or smaller than 1g, ignore it and use the gyro-simulated gravity vector.
- If the absolute value of the acceleration vector is close to 1g but differs significantly from the gyro-simulated vector then start counting these cases. If this case happens 5 times consecutively then update the gyro-simulated vector from the acceleration vector and use that.

Let's see now our racquet swing. The samples were taken with the Android application attached to the previous post, the analysis was done with this Sage script. The Sage script takes the samples from capture.csv file - you can easily change that in line 456. Blue line is the collection of 3D acceleration vectors from the accelerometer, red line is the gyro-compensated gravity vector (taken from the accelerometer if there is no motion, simulated with the help of the gyroscope if motion acceleration is detected).

As you can see, the blue line is everywhere. Actually it goes out of the coordinate system as the Nexus S has its accelerometer limited at 2g - that's why you see the acceleration curve turned into straight lines at the fastest points of the movement. The red line, however, follows the curve of the device's tilt nicely and eventually ends almost where the acceleration's blue line does. Then it gets compensated by the accelerometer's gravity vector measurement - no cumulative errors.

There's more to come, just stay tuned.