Get acceleration without gravity
I am new to android development. I want to get actual phone acceleration. I found some code to speed up. But it gives acceleration with gravity. Please help me find a way to get the actual acceleration without gravity. Here is the code I found. Please help me with this code. thank
package com.SensorTest;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class SensorTestActivity extends Activity implements SensorEventListener {
SensorManager sensorManager = null;
//for accelerometer values
TextView outputX;
TextView outputY;
TextView outputZ;
//for orientation values
TextView outputX2;
TextView outputY2;
TextView outputZ2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.main);
//just some textviews, for data output
outputX = (TextView) findViewById(R.id.TextView01);
outputY = (TextView) findViewById(R.id.TextView02);
outputZ = (TextView) findViewById(R.id.TextView03);
outputX2 = (TextView) findViewById(R.id.TextView04);
outputY2 = (TextView) findViewById(R.id.TextView05);
outputZ2 = (TextView) findViewById(R.id.TextView06);
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
synchronized (this) {
switch (event.sensor.getType()){
case Sensor.TYPE_ACCELEROMETER:
outputX.setText("acclaration x:"+Float.toString(event.values[0]));
outputY.setText("acclaration y:"+Float.toString(event.values[1]));
outputZ.setText("acclaration z:"+Float.toString(event.values[2]));
break;
case Sensor.TYPE_ORIENTATION:
outputX2.setText("orientation x:"+Float.toString(event.values[0]));
outputY2.setText("orientation y:"+Float.toString(event.values[1]));
outputZ2.setText("orientation z:"+Float.toString(event.values[2]));
break;
}
}
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), sensorManager.SENSOR_DELAY_GAME);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), sensorManager.SENSOR_DELAY_GAME);
}
}
source to share
It is impossible to get acceleration directly without gravity.
You can use a high-pass filter, for example, in the Android man page under Sensor.TYPE_ACCELEROMETER
:
public void onSensorChanged(SensorEvent event) {
// alpha is calculated as t / (t + dT)
// with t, the low-pass filter time-constant
// and dT, the event delivery rate
final float alpha = 0.8;
gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
linear_acceleration[0] = event.values[0] - gravity[0];
linear_acceleration[1] = event.values[1] - gravity[1];
linear_acceleration[2] = event.values[2] - gravity[2];
}
source to share
The answer is here: http://developer.android.com/reference/android/hardware/SensorEvent.html#values
Quote:
In particular, gravity always affects the measured acceleration: Ad = -g - ΣF / mass
For this reason, when the device is sitting on a table (and obviously not accelerating), the accelerometer reads g = 9.81 m / s ^ 2
Likewise, when a device is in free fall and is therefore dangerously accelerating towards the ground to 9.81 m / s ^ 2, its accelerometer reads 0 m / s ^ 2.
Obviously, to measure the real acceleration of the device, it is necessary to eliminate the contribution of gravity. This can be achieved by using a high-pass filter. Conversely, a low-pass filter can be used to isolate gravity.
public void onSensorChanged(SensorEvent event)
{
// alpha is calculated as t / (t + dT)
// with t, the low-pass filter time-constant
// and dT, the event delivery rate
final float alpha = 0.8;
gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
linear_acceleration[0] = event.values[0] - gravity[0];
linear_acceleration[1] = event.values[1] - gravity[1];
linear_acceleration[2] = event.values[2] - gravity[2];
}
source to share
Duplicate question: android remove gravity from accelerometer readings
The answer lies in using Sensor.TYPE_LINEAR_ACCERERATION (API> = 9 about 99% of all Android devices).
source to share
Why not subtract the gravity values directly from the observed acceleration using an Android sensor. I think this is easier. If any specific reason is related to the use of a high pass filter, please let me know using some link for a supporting document or technical documentation. thanks to Rajiv
source to share