Thursday, 28 June 2018

Android getOrientation description does not match output values

Android getOrientation() function says that:

  • values at index 0: Azimuth is between pi and -pi
  • values at index 1: Pitch is between pi and -pi
  • values at index 2: Roll is between -pi/2 and pi/2

However I get different value ranges when testing with an emulator (Nexus 5 API 25)/device(Samsung API 21). I used Logcat to print the values. The pitch values I get are from -pi/2 to pi/2. With the screen pointing to the sky, the value ranges from 0 to -pi/2, with the screen pointing to the ground, it ranges from 0 to pi/2. And the roll goes above pi/2.

Why am I getting values different from the documentation?

My code is as following:

public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        System.arraycopy(event.values, 0, mAccelerometerReading,
                0, mAccelerometerReading.length);
    }
    else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        System.arraycopy(event.values, 0, mMagnetometerReading,
                0, mMagnetometerReading.length);
    }

    if (mAccelerometerReading != null && mMagnetometerReading != null) {
        // Update rotation matrix, which is needed to update orientation angles.
        mSensorManager.getRotationMatrix(mRotationMatrix, null,
                mAccelerometerReading, mMagnetometerReading);


        mSensorManager.getOrientation(mRotationMatrix, mOrientationAngles);

        Log.d("sensor", Float.toString(mOrientationAngles[1]));
    }



from Android getOrientation description does not match output values

No comments:

Post a Comment