无重力地获得加速度

时间:2012-03-06 08:08:24

标签: android gravity acceleration

我是Android开发的新手。我想获得手机的实际加速度。我找到了一个获得加速的代码。但它通过重力提供加速度。请任何人帮我找到一种在没有重力的情况下获得实际加速度的方法。 这是我发现的代码,请帮我这个代码。谢谢

    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);
    }

    }

4 个答案:

答案 0 :(得分:2)

没有重力就无法直接获得加速度。

您可以在Sensor.TYPE_ACCELEROMETER部分使用Android Reference Page上的高通过滤器:

public void onSensorChanged(SensorEvent event) {
      // alpha is calculated as t / (t + dT)
      // with t, the low-pass filter's 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];
 }

答案 1 :(得分:1)

重复问题:android remove gravity from accelerometer readings

答案是使用Sensor.TYPE_LINEAR_ACCELERATION(API> =约占所有Android设备的99%)。

答案 2 :(得分:0)

答案在这里: http://developer.android.com/reference/android/hardware/SensorEvent.html#values

引用:

特别是,重力总是影响测得的加速度: Ad = -g - ΣF/质量

出于这个原因,当设备坐在桌子上(显然没有加速)时,加速度计读数为g = 9.81 m / s ^ 2

类似地,当设备处于自由落体状态并因此以9.81 m / s ^ 2危险地加速到地面时,其加速度计读数为0 m / s ^ 2.

显然,为了测量装置的实际加速度,必须消除重力的贡献。这可以通过应用高通滤波器来实现。相反,可以使用低通滤波器来隔离重力。

public void onSensorChanged(SensorEvent event)
 {
      // alpha is calculated as t / (t + dT)
      // with t, the low-pass filter's 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];
 }

答案 3 :(得分:0)

为什么不使用Android传感器直接从观察到的加速度中减去重力值。我认为这是更简单的方法。如果使用高通滤波器有任何特殊原因,请通过支持文档或技术文章的链接告诉我。 谢谢 拉吉夫