开发者

Azimuth found with Sensor.TYPE_MAGNETIC_FIELD much more unstable than Sensor.TYPE_ORIENTATION

I'm doing some a开发者_如何学运维ugmented reality app and I need the azimuth to compute some object position on the screen. I tried to get orientation data using Sensor.TYPE_ORIENTATION and since it's deprecated I tried Sensor.TYPE_MAGNETIC_FIELD. My problem is that the value obtained with Sensor.TYPE_MAGNETIC_FIELD and SensorManager.getOrientation is quite unstable compared to Sensor.TYPE_ORIENTATION, which makes my object jump a bit on the screen (when not moving the value jump in a range say [azimuth-5 azimuth+5] while it hardly moves with Sensor.TYPE_ORIENTATION).

Sensor.TYPE_ORIENTATION

public void onSensorChanged(SensorEvent event)
{
    if(event.sensor.getType() == Sensor.TYPE_ORIENTATION)
    {
        newAzimuth = (event.values[0] + 360)%360;            
        azimuth = (float) ((newAzimuth * kFilteringFactor) + (azimuth * (1.0 - kFilteringFactor)));       
    }
}

Sensor.TYPE_MAGNETIC_FIELD

public void onSensorChanged(SensorEvent event) {
    switch (event.sensor.getType()) {
        case Sensor.TYPE_MAGNETIC_FIELD:
            mags = event.values.clone();
            break;
        case Sensor.TYPE_ACCELEROMETER:
            accels = event.values.clone();
            break;
    }
    if (mags != null && accels != null) {
        SensorManager.getRotationMatrix(R, I, accels, mags);
        SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_X,
                SensorManager.AXIS_Z, remappedR);
        SensorManager.getOrientation(remappedR, orientation);
        newAzimuth = (float) Math.round(Math.toDegrees(orientation[0]));
        newAzimuth = (newAzimuth + 360)%360;
        azimuth = (float) (newAzimuth * kFilteringFactor) + (azimuth * (1.0f - kFilteringFactor));
    }
}


It happens only when using SensorManager.DELAY_FASTER. With SensorManager.SENSOR_DELAY_GAME the values are more stable...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜