如何在Android中使方位角和度数不那么摇晃

时间:2015-01-23 23:13:54

标签: android accelerometer azimuth

首先我使用了TYPE_ORIENTATION,但这是折旧的。因此,我将代码更改为更新版本,并按this post中的建议使用TYPE_ACCELEROMETERTYPE_MAGNETIC_FIELD

// Initialize android device sensor capabilities
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

然后在onResume()中,我使用了这个:

// For the system's orientation sensor registered listeners
mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);

以及我用过的地方:

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // not in use
}

float[] mGravity;
float[] mGeomagnetic;

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
    mGravity = event.values;
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
    mGeomagnetic = event.values;
    if (mGravity != null && mGeomagnetic != null) {
        float R[] = new float[9];
        float I[] = new float[9];
        boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
        if (success) {
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);
            float azimuthInRadians = orientation[0];
            float azimuthInDegrees = (float) Math.toDegrees(azimuthInRadians);
            if (azimuthInDegrees < 0.0f) {
                azimuthInDegrees += 360f;
            }
            float degree = Math.round(azimuthInDegrees);
            directionHeading.setText("Heading: " + degree + " degrees");

            // Create rotation animation
            RotateAnimation ra = new RotateAnimation(currentDegree, -degree,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);

            // How long the animation will take place
            ra.setDuration(210);

            // Set the animation after the end of the reservation status
            ra.setFillAfter(true);

            // Start the animation
            image.startAnimation(ra);
            currentDegree = -degree;
        }
    }
}

旧的方式非常精确,根本没有动摇。但现在,以非贬值的方式,它会大幅震动。为什么会这样,我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

解决方案是应用(LPF)低通滤波器。它现在很流畅,工作得很好。 我在这里找到了解决方案:Low Pass Filter