How to compensate for gravity in accelerometer readings

I need to know how to eliminate gravity from my accelerometer readings, so when it is left stationary or is moving at the constant speed, the reading on all three axis should be zero.

I'm using an evaluation board from ST called STEVAL-MKI108V2 which comprises of 3D magnetometer, accelerometer and gyroscope, so basically 9 degrees of freedom.

I checked out a website: varesano.net - and it showed a pretty simple way to do that, but i have two questions:

  1. How is the quaternion representing the orientation of the entire sensor obtained?
  2. What is the math behind his algorithm?

Also, if there are better ways i can obtain accelerometer readings withou gravity, pls let me know.

Thanks!

The CHRobotics site has an excellent collection of well written literature describing motion sensors, coordinate systems and the math used for Attitude and Heading Reference System (AHRS) modules: https://www.chrobotics.com/library

If your ultimate goal is to use the residual acceleration (after the gravity vector is substracted) for other purposes such as dead reckoning, then you will find that the hobby-type sensors are too noisy and inaccurate to be of much use.

Fundamentally, you cannot distinguish "gravity" from other accelerations.