The azimuth found with Sensor.TYPE_MAGNETIC_FIELD is much more unstable than Sensor.TYPE_ORIENTATION

I am making an augmented reality application, and I need an azimuth to calculate some position of an object on the screen. I tried to get orientation data using Sensor.TYPE_ORIENTATION , and since it is deprecated, I tried Sensor.TYPE_MAGNETIC_FIELD . My problem is that the value obtained with Sensor.TYPE_MAGNETIC_FIELD and SensorManager.getOrientation is rather unstable compared to Sensor.TYPE_ORIENTATION , which causes my object to jump a little on the screen (if you do not move the value jump in the range, say, [azimuth azimuth -5] +5] until it 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)); } } 
+4
source share
1 answer

This only happens when using SensorManager.DELAY_FASTER. With SensorManager.SENSOR_DELAY_GAME, the values ​​are more stable ...

+2
source

All Articles