# How to subtract gravity from raw acceleration of an MPU9250 - 9DOF IMU

I'm working with an MPU9250 and using the Madgwick Filter. For what I know I need to subtract the GRAVITY from my X, Y, Z Acceleration values, basically, some kind of gravity compensation to get linear acceleration values. I was using the following code (q is my quaternion generated by the Madgwick filter):

 g = 2 * (q * q - q * q)
g = 2 * (q * q + q * q)
g = q * q - q * q - q * q + q * q


Then, to compensate the gravity, I subtract gx, gy, gz from my ax, ay, az values respectively. I'm not sure if this is the correct approach to solve this problem and I would like a bit of help. After stripping the gravity values, I need to get velocity. I don't need position so I'm not going to double integrate the acceleration values. By the way, I have to mention that this will be used to measure speed and acceleration/deceleration of football players while running, so I don't need this to be extremely precise.

PS: I read about using a Low-Pass filter to subtract gravity, but I'm not sure if I should be doing that.

• Have you read this post yet? stackoverflow.com/questions/12617200/… – Rekamanon Nov 6 '19 at 13:21
• Yes I did, but that guy used a Kalman Filter, the Madwick algorithm gets me a different quaternion and thats my question. Im not sure if I should be rotating the quaternion. The guy on that question was using a 6DOF IMU and I have a 9DOF IMU, so thats why I am able to use the Madgwick Filter with my Magnetometer inputs. Also what I really want to understand are these three lines of code In my post and why they work (well, that is what this guy said) – LittnerDiAzure Nov 6 '19 at 13:37
• I'd try a steep low pass filter, with a cutoff of less than 0.2Hz. – Scott Seidman Nov 6 '19 at 14:02
• Can the low pass filter remove the gravity from the accelerometer values? Should I apply it to the accelerometer raw data? – LittnerDiAzure Nov 6 '19 at 14:10
• Yes. Apply it to the raw signal. – Scott Seidman Nov 6 '19 at 20:47