Search code examples
androidaugmented-realityandroid-sensors

Wrong x value for sensors when using for augmented reality


I use the following (assuming to be quite standard) code to retrieve the orientation of a tablet device for augmented reality purpose (so the back-camera should point a valid direction).

private Sensor mAccelerometer;
private Sensor mMagnetometer;
private float[] mLastAccelerometer = new float[3];
private boolean mLastAccelerometerSet = false;
private float[] mLastMagnetometer = new float[3];
private boolean mLastMagnetometerSet = false;

private Sensor mAccelerometer;
private Sensor mMagnetometer;
private float[] mLastAccelerometer = new float[3];
private boolean mLastAccelerometerSet = false;
private float[] mLastMagnetometer = new float[3];
private boolean mLastMagnetometerSet = false;
private float[] inR = new float[9]; 
private float[] mR = new float[9];
private float[] mOrientation = new float[3];
...
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
...     
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
...
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_FASTEST);
...
public void onSensorChanged(SensorEvent event) {
    synchronized (this) {
       if (event.sensor == mAccelerometer) {
            System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length);

            mLastAccelerometerSet = true;

        } else if (event.sensor == mMagnetometer) {
            System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length);

            mLastMagnetometerSet = true;
        }

        if (mLastAccelerometerSet && mLastMagnetometerSet && scene.camera() != null && mPlane1 != null) {
            SensorManager.getRotationMatrix(inR, null, mLastAccelerometer, mLastMagnetometer);
            SensorManager.remapCoordinateSystem(inR, SensorManager.AXIS_X, SensorManager.AXIS_Z, mR);
            SensorManager.getOrientation(mR, mOrientation);

            /* my code that handles mOrientation */
        }
    }
}

The problem is that this code works flawlessly on a Samsung Galaxy Tab 10.1 (P7500, first version). With the same code running on a Asus Transformer Pad Infinity (TF700), the first value mOrientation[0] only varies between +2.0 and +3.0 when rotating the tablet around myself (I would expect that it will go between 0 and 3.14).

Is there something wrong with the device or are there other variables that I overlooked?


Solution

  • There must have been some error with the hardware of the Asus TF700. It's either in this specific version that we had or in the design of the device. With other Android devices the application worked as expected.