获得俯仰和滚动值

时间:2014-05-11 08:26:36

标签: android sensor

我正在做一个通过使用加速度计检测移动方向的项目但是,当我们倾斜或旋转手机时,加速度计的3轴并不总是跟随世界xyz平面。我试图通过从手机获得音高和滚动并将手机的轴转换为世界xyz轴来修复它。但是现在我的推杆和滚动总是得到0.任何人都可以帮助我吗?还是有其他方法来解决我的问题?谢谢你的帮助!

package com.example.suntracking;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;

public class AxisCheck extends Activity implements SensorEventListener{

    Sensor accelerometer,magnetometer;
    float[] lastAccelerometer = new float[3];
    float[] lastMagnetometer = new float[3];
    boolean lastAccelerometerSet = false;
    boolean lastMagnetometerSet = false;

    SensorManager sm;
    float pval,rval;
    float[] mR = new float[9];
    float[] mhold= new float[3];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        initialize();
        accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        magnetometer = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        finish();
    }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        sm.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

    protected void onPause() {
        super.onPause();
        sm.unregisterListener(this);
    }
    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
        // TODO Auto-generated method stub
    }


    private void initialize() {
        // TODO Auto-generated method stub
        sm =(SensorManager)getSystemService(SENSOR_SERVICE);
    }


    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        if (event.sensor == accelerometer) {
            System.arraycopy(event.values, 0, lastAccelerometer, 0, event.values.length);
            lastAccelerometerSet = true;
        } else if (event.sensor == magnetometer) {
            System.arraycopy(event.values, 0, lastMagnetometer, 0, event.values.length);
            lastMagnetometerSet = true;
        }
        if (lastAccelerometerSet && lastMagnetometerSet) {
        SensorManager.getRotationMatrix(mR, null, lastAccelerometer, lastMagnetometer);
        SensorManager.getOrientation(mR,mhold);
        pval = Math.round(mhold[1]);
        rval = Math.round(mhold[2]);
        savePreferences();
        }
    }


    private void savePreferences() {
        // TODO Auto-generated method stub
        SharedPreferences savedata = getSharedPreferences("savealldata",0);
        Editor editor=savedata.edit();
        editor.putFloat("pval", pval);
        editor.putFloat("rval", rval);
        editor.commit();
    }


}

0 个答案:

没有答案