Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android remove gravity from accelerometer readings

Tags:

I am developing an application for Android where I need to remove gravity from accelerometer readings. I have read multiple discussions on this problem, I have also found an algorithm here, but I didn't really understand it.

I want to filter gravity from each axis, not from the total acceleration.

Could you please help me out? My code should be something like:

public void onSensorChanged(SensorEvent sensorEvent) {
    float vals[] = sensorEvent.values;
    float accelerationX = filterGravity(vals[0]);
    float accelerationY = filterGravity(vals[1]);
    float accelerationZ = filterGravity(vals[2]);
}

What code should I place in the filterGravity() method?

like image 888
Gabriel Avatar asked Aug 02 '11 12:08

Gabriel


People also ask

How do you remove gravity component from accelerometer data?

To leave out the gravity vector from the accelerometer value, you need to rotate the accelerometer vector to the earth frame using a rotation matrix or quaternion which can be calculated from accelerometer, gyroscope, and magnetometer.

How do I remove gravity from IMU data?

remove the gravity. correct (rotate) the accelerometer readings so it's axes will be aligned with earth's frame of reference's axes. read the acceleration towards earth (now Z component of accelerometer)

Do accelerometers measure gravity?

For example, an accelerometer at rest on the surface of the Earth will measure an acceleration due to Earth's gravity, straight upwards (by definition) of g ≈ 9.81 m/s2.

How accurate is the Android accelerometer?

The accelerometers read around -0.8 ms^-2 on both X and Y axes when stationary, so I used this as my offset.


2 Answers

For a basic solution you would need a low pass filter other approaches like a Kalman filter are pretty tough regarding the maths behind. A simple example for Android is one click away from your link at http://developer.android.com/reference/android/hardware/SensorEvent.html#values.

Simply spoken a low pass filter builds a weighted average from all your history values. If you have for example a filtering factor of 0.1 it means that 10% of your current value is added to the previous mean value: newMeanValue = 10% of currentValue + 90% of oldMeanValue. That means even if there is an abrupt peak it will only push your mean value slowly because of the 10%.

like image 109
Kay Avatar answered Sep 19 '22 12:09

Kay


Linear acceleration is what you need. Check Sensor.TYPE_LINEAR_ACCELERATION here.

like image 24
Ali Avatar answered Sep 19 '22 12:09

Ali