Acceleromter gravity compensation

Following is the algorithm Accelerometer measures combined gravity Rg and device acceleration Ra:

Rag = Rg + Ra

We’re seeking to find acceleration Ra, we know Rag (as measured by accelerometer), but we don’t know Rg or Ra.

We can calculate Rg for example by using a magnetometer and using the fact that Rg (gravity vector) can be obtained by rotating Mn (magnetometer North vector) by 90 degrees about Y axis.

So

Rg = Tn * Mn , where Tn is the DCM rotation matrix see http://en.wikipedia.org/wiki/Rotation_matrix, determined by calibration

So :

Ra = Rag – Rg = Rag – Tn * Mn

Can anyone provide sample code or maths of this calculation?Can it can be done gyro instead of magnometer?

Does your accelerometer measure 3 axes or 1? Do you know the vector angle of Ra?

GoForSmoke: Does your accelerometer measure 3 axes or 1? Do you know the vector angle of Ra?

3 axis dont know about the angle

With both the angle and acceleration unknown, I don't know how you can separate gravity out of the total, which is all the accelerometer gives you. It's like what vector plus 9.81 m/s/s in an unknown direction equals the result? If you know which way the thing is moving then you could get that.

With just an accelerometer, it is impossible to separate the acceleration due to gravity from that due to other forces. However with a "9 axis" sensor (includes gyro and magnetometer) you can do a reasonable job. You can buy an integrated device, called an IMU, which incorporates the CPU to do the calculations. The diydrones.com website has good introductory material on the theory, offers complete solutions and has an active forum. SparkFun, Pololu and CHRobotics also offer IMUs and they are becoming pretty cheap.