Can anyone help on removing the g factor from accelerometer readings?
I am using SensorEventListener with onSensorChanged()
method for getting Sensor.TY
Use Sensor.TYPE_LINEAR_ACCELERATION instead of Sensor.TYPE_ACCELEROMETER
Differentiating with respect to time a function of time rids you of the constants.
So by taking the derivative of the accelerometer's signal you'll get the "Jerk", which you can then re-integrate in order to get the non-constant part of the acceleration you're looking for.
In Layman's terms, take a sample from the accelerometer every 1 second, and subtract it from the previous sample. If the answer is (very close to) zero, you're not accelerating relatively to earth. If the result is non-zero, integrate it (in this case, multiply by one second), you have your acceleration.
Two things, though : -Look out for noise in the signal, round off your input. -Don't expect hyper-accurate results from on-chip accelerometers. You can use them to detect shaking, changes in orientation, but not really for knowing how many G's you're experiencing while making sharp turns in your car.
You can use a low-pass filter.
Do this for each of your sensor values:
g = 0.9 * g + 0.1 * v
Where v
is your current sensor value and g
is a global variable initially set to zero. Mind that you'll need as many g variables as you have axes.
With v = v - g
you can eliminate the gravity factor from your sensor value.
One way (for devices only with accelerometer) is to remove gravity vector from accelerometer data by subtracting the values that would come in static case for same orientation. But as orientation is again calculated by taking acceleration readings and not independently, its not very accurate.
Gyroscope may help in this case. But few androids still have a true gyroscope. And using its raw readings is not so simple.
Take a look of the following link.
http://developer.android.com/reference/android/hardware/SensorEvent.html
you need to assume two coordinate systems: 1- fixed global system. 2- moving coordinate system in which the origin moves & rotates as sensor does. in global system, g is always parallel to z axis but in moving system it is not. so all you have to do is to compute 3*3 rotation matrix from orientation angles or yaw, pitch & roll. (you can find formulas everywhere). then multiply this rotation matrix by 3*1 acceleration vector measured by sensor. this will transform coordinates and declare the values in fixed global system. the only thing afterward is to simply subtract g from z value.