Reputation: 59
hello i want to ask which sensor is the best to find your current orientation? the orientation sensor or the combination of accelerometer and magnometer (compass). I have seen a lot of augmented reality versions and i wonder which one is the best! Some of these use the orientation to find the azimuth whereas other use the accelerometer and magnometer. As i know orientation sensor is deprecated.
Upvotes: 0
Views: 540
Reputation: 549
You can use gyroscope sensor to find the rotation vector. It can be used to get the azimuth.
SensorManager mSensorManager = (SensorManager)
getSystemService(Context.SENSOR_SERVICE);
rSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
And in onSensorChanged function,
SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
SensorManager.getOrientation(mRotationMatrix, mValues);
azimuth = Math.toDegrees(mValues[0]));
But in some devices this sensor may not be available. In that case you can use combination of Accelerometer and Magnetometer.
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
aSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
In onSensorChaged function,
switch (event.sensor.getType()) {
case Sensor.TYPE_MAGNETIC_FIELD:
magnetic = event.values.clone();
break;
case Sensor.TYPE_ACCELEROMETER:
accelerometer = event.values.clone();
break;
}
if (magnetic != null && accelerometer != null) {
Rot = new float[9];
I = new float[9];
SensorManager.getRotationMatrix(Rot, I, accelerometer, magnetic);
float[] outR = new float[9];
SensorManager.remapCoordinateSystem(Rot, SensorManager.AXIS_X,
SensorManager.AXIS_Z, outR);
SensorManager.getOrientation(outR, values);
azimuth = values[0];
magnetic = null;
accelerometer = null;
}
Gyroscope provides the best result out of the two options.
Upvotes: 2