Submitted by fabio on Wed, 2011-07-06 15:12.
An accelerometer is subject to dynamic (or external) and static (gravity) accelerations (see my thesis, chapter 5.1). This means that whenever you are interested in measuring dynamic accelerations rather than gravity you have to compensate for gravity.
I implemented a very simple approach for doing so, assuming that you have a 9 degrees of measurement inertial measurement unit as my FreeIMU, from which you are able to compute its orientation quaternion representing the orientation of the Earth frame with respect to the Sensor frame and that you have the readings coming from the accelerometer.
This is the code, implemented in Python:
# compensate the accelerometer readings from gravity.
# @param q the quaternion representing the orientation of a 9DOM MARG sensor array
# @param acc the readings coming from an accelerometer expressed in g
# @return a 3d vector representing dinamic acceleration expressed in g
def gravity_compensate(q, acc):
g = [0.0, 0.0, 0.0]
# get expected direction of gravity
g = 2 * (q * q - q * q)
g = 2 * (q * q + q * q)
g = q * q - q * q - q * q + q * q
# compensate accelerometer readings with the expected direction of gravity
return [acc - g, acc - g, acc - g]
The idea behind this is pretty simple. With the quaternion we can compute the expected direction of gravity and then subtract that from the accelerometer readings. The result is the dynamic acceleration.
I'm sure that there could be a better solution but this is just the result of a 10 minutes coding and seems working pretty good.