其他分享
首页 > 其他分享> > 如何以稳定和准确的方式阅读Android加速度计值?

如何以稳定和准确的方式阅读Android加速度计值?

作者:互联网

在我的项目中,我试图使用通过Android设备的加速度计获得的数据来控制汽车. (左,右,前,后).即使我已经设法从加速度计读取值,即使设备处于稳定位置,读数也会经常变化.有人能为我提供更好的方法吗?

以下是我使用过的代码

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class AccelerometerService {
    private static SensorManager sensorManager;
    private static SensorEventListener sensorEventListener;
    private static boolean started = false;

    private static float[] accelerometer = new float[3];
    private static float[] magneticField = new float[3];

    private static float[] rotationMatrix = new float[9];
    private static float[] inclinationMatrix = new float[9];
    private static float[] attitude = new float[3];

    private final static double RAD2DEG = 180/Math.PI;

    private static int initialAzimuth = 0;
    private static int initialPitch = 0;
    private static int initialRoll = 0;

    private static int[] attitudeInDegrees = new int[3];

    public static void start(final Context applicationContext) {
        if(started) {
            return;
        }

        sensorManager = (SensorManager) applicationContext
                .getSystemService(Context.SENSOR_SERVICE);

        sensorEventListener = new SensorEventListener() {

            @Override
            public void onSensorChanged(SensorEvent event) {

                int type = event.sensor.getType();
                if(type == Sensor.TYPE_MAGNETIC_FIELD) {
                    magneticField = event.values.clone();
                } 
                if(type == Sensor.TYPE_ACCELEROMETER) {
                    accelerometer = event.values.clone();
                }

                SensorManager.getRotationMatrix(rotationMatrix, inclinationMatrix, accelerometer, magneticField);
                SensorManager.getOrientation(rotationMatrix, attitude);

                attitudeInDegrees[0] =  (int) Math.round(attitude[0] * RAD2DEG);    //azimuth
                attitudeInDegrees[1] = (int) Math.round(attitude[1] * RAD2DEG);     //pitch
                attitudeInDegrees[2] = (int) Math.round(attitude[2] * RAD2DEG);     //roll
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
        };
        sensorManager.registerListener(sensorEventListener,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_UI);
        sensorManager.registerListener(sensorEventListener,
                sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_UI);

        started = true;
    }

    public static boolean getStarted() {
        return started;
    }

    public static void stop() {
        if(started) {
        sensorManager.unregisterListener(sensorEventListener);
        started = false;
        }
    }
}

解决方法:

老问题,但我在这里回答是为了找到这个问题的其他人.

首先,关注@ andrew-morton链接正是我所需要的,但是在伪代码中,所以这里是Java for Android!

第二:如果可以(你的最低API Lvl是9或更高),只需使用Sensor.TYPE_GRAVITY.因为它已经平滑了.如果你需要支持旧的API Lvls(就像我不得不这样),这个代码应该可以解决!

最后:这个代码片段与我使用的代码片段稍有不同,如果你想自己使用它,你需要为Vector3引力创建一个getter.

笔记:
我发现滚动平均值为5(MAX_SAMPLE_SIZE)非常平滑. 10甚至更​​平滑,但你开始注意滞后.

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;  
import java.util.ArrayList;
import java.util.List;

/**
 * Created by andy on 6/14/14.
 */
public class AndroidGravityUpdate implements SensorEventListener {
    private SensorManager sensorManager;
    Vector3 gravity;
    List<Float>[] rollingAverage = new List[3];

    private static final int MAX_SAMPLE_SIZE = 5;

    AndroidGravityUpdate( SensorManager sensorManager ) {
        this.gravity = new Vector3();
        this.sensorManager = sensorManager;

        if(sensorManager.getSensorList(Sensor.TYPE_GRAVITY).size() > 0){
            sensorManager.registerListener(
                    this,
                    sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY),
                    SensorManager.SENSOR_DELAY_GAME
            );
        } else if( sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() > 0) {
            rollingAverage[0] = new ArrayList<Float>();
            rollingAverage[1] = new ArrayList<Float>();
            rollingAverage[2] = new ArrayList<Float>();

            sensorManager.registerListener(
                    this,
                    sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                    SensorManager.SENSOR_DELAY_GAME
                );
        }

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType()==Sensor.TYPE_GRAVITY){
            gravity.z = event.values[0];
            gravity.x = event.values[1];
            gravity.y = - event.values[2];
        }
        else if ( event.sensor.getType()==Sensor.TYPE_ACCELEROMETER) {
            //For whatever reason, my Samsung only has "Accelerometer"
            // But it is incredibly rough, so attempting to smooth
            // it out with rolling averages.
            rollingAverage[0] = roll(rollingAverage[0], event.values[0]);
            rollingAverage[1] = roll(rollingAverage[1], event.values[1]);
            rollingAverage[2] = roll(rollingAverage[2], -event.values[2]);

            gravity.z = averageList(rollingAverage[0]);
            gravity.x = averageList(rollingAverage[1]);
            gravity.y = averageList(rollingAverage[2]);
        }
    }

    public List<Float> roll(List<Float> list, float newMember){
        if(list.size() == MAX_SAMPLE_SIZE){
            list.remove(0);
        }
        list.add(newMember);
        return list;
    }

    public float averageList(List<Float> tallyUp){

        float total=0;
        for(float item : tallyUp ){
            total+=item;
        }
        total = total/tallyUp.size();

        return total;
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}

class Vector3 {
    float x;
    float y;
    float z;
}

标签:android-sensors,android,accelerometer,tilt-sensor
来源: https://codeday.me/bug/20191001/1838217.html