在没有重力的情况下获得加速度 [英] get the acceleration without gravity

查看:118
本文介绍了在没有重力的情况下获得加速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to android development. I want to get actual acceleration of the phone.I found a code to get acceleration.but it gives acceleration with gravity. Please any one help me to find a way to get actual acceleration without gravity. Here's the code i found,,Please help me with this code. thank you

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

    }


推荐答案

没有重力就不可能直接获得加速度。

It is not possible to get the acceleration directly without gravity.

您可以使用高通滤波器,例如 Android参考页,位于 Sensor.TYPE_ACCELEROMETER 部分:

You can use a high-pass filter, like on the Android Reference Page, in the Sensor.TYPE_ACCELEROMETER section:

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

这篇关于在没有重力的情况下获得加速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆