11

i'am trying to build a simple Augmented Reality App, so I start working with sensor Data.

According to this thread (Android compass example) and example (http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html), the calculation of the orientation using the Sensor.TYPE_ACCELEROMETER and Sensor.TYPE_MAGNETIC_FIELD doesn't really fit.

So I'm not able to get "good" values. The azimut values doesn't make any sense at all, so if I just move the Phone upside the value changes extremly. Even if I just rotate the phone, the values doesn't represent the phones orientation.

Has anybody an idea, who to improve the values quality according to the given example?

5 Answers 5

24

In what kind of orientation do you use this sample app? From what is written is this code, the only orientation supported is Portrait or flat on the table, it depends on devices. What do you mean by "good"?

It is normal that the value is not "good" when rotating the device, the device coordinate system is supposed to be working in Portrait, or flat i don't know (Y axis vertical along the screen pointing up, Z axis pointing out of the screen coming from the center of screen, X axis perpendicular to the Y axis going on the right along the screen). Having this, rotating the device will not rotate the device coordinate system, you'll have to remap it.

But if you want the heading of the device in Portrait orientation, here is a piece of code that works good for me:

@Override
public void onSensorChanged(SensorEvent event)
{
    // It is good practice to check that we received the proper sensor event
    if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR)
    {
        // Convert the rotation-vector to a 4x4 matrix.
        SensorManager.getRotationMatrixFromVector(mRotationMatrix,
                event.values);
        SensorManager
                .remapCoordinateSystem(mRotationMatrix,
                        SensorManager.AXIS_X, SensorManager.AXIS_Z,
                        mRotationMatrix);
        SensorManager.getOrientation(mRotationMatrix, orientationVals);

        // Optionally convert the result from radians to degrees
        orientationVals[0] = (float) Math.toDegrees(orientationVals[0]);
        orientationVals[1] = (float) Math.toDegrees(orientationVals[1]);
        orientationVals[2] = (float) Math.toDegrees(orientationVals[2]);

        tv.setText(" Yaw: " + orientationVals[0] + "\n Pitch: "
                + orientationVals[1] + "\n Roll (not used): "
                + orientationVals[2]);

    }
}

You'll get the heading (or azimuth) in:

orientationVals[0]
1
  • 1
    For the record, I tried this code with a 3x3 Matrix but it only worked with 4x4 (aka float[16]) Apr 22, 2015 at 21:06
15

Answer from Tíbó is good, but if you log roll value, you will expect irregular numbers. (roll is important for AR Browsers)

This is due to

SensorManager.remapCoordinateSystem(mRotationMatrix,
                    SensorManager.AXIS_X, SensorManager.AXIS_Z,
                    mRotationMatrix);

You have to use different matrix for in and out of remap. This following code works for me with a correct roll value:

@Override
public void onSensorChanged(SensorEvent event)
{
    // It is good practice to check that we received the proper sensor event
    if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR)
    {
        // Convert the rotation-vector to a 4x4 matrix.
        SensorManager.getRotationMatrixFromVector(mRotationMatrixFromVector, event.values);
        SensorManager.remapCoordinateSystem(mRotationMatrixFromVector,
                    SensorManager.AXIS_X, SensorManager.AXIS_Z,
                    mRotationMatrix);
        SensorManager.getOrientation(mRotationMatrix, orientationVals);

        // Optionally convert the result from radians to degrees
        orientationVals[0] = (float) Math.toDegrees(orientationVals[0]);
        orientationVals[1] = (float) Math.toDegrees(orientationVals[1]);
        orientationVals[2] = (float) Math.toDegrees(orientationVals[2]);

        tv.setText(" Yaw: " + orientationVals[0] + "\n Pitch: "
                + orientationVals[1] + "\n Roll (not used): "
                + orientationVals[2]);

    }
}
1
  • Yes, You can check in the source code: public static boolean remapCoordinateSystem(float[] inR, int X, int Y, float[] outR) * @param outR * the transformed rotation matrix. inR and outR should not be the same * array.
    – N Kaushik
    Jul 7, 2015 at 7:21
9

Probably late to the party. Anyway here is how I got the azimuth

private final int sensorType =  Sensor.TYPE_ROTATION_VECTOR;
float[] rotMat = new float[9];
float[] vals = new float[3];

@Override
public void onSensorChanged(SensorEvent event) {
    sensorHasChanged = false;
    if (event.sensor.getType() == sensorType){
        SensorManager.getRotationMatrixFromVector(rotMat,
                event.values);
        SensorManager
                .remapCoordinateSystem(rotMat,
                        SensorManager.AXIS_X, SensorManager.AXIS_Y,
                        rotMat);
        SensorManager.getOrientation(rotMat, vals);
        azimuth = deg(vals[0]); // in degrees [-180, +180]
        pitch = deg(vals[1]);
        roll = deg(vals[2]);
        sensorHasChanged = true;
    }
}

Hope it helps

1
  • 1
    The remapCoordinateSystem call is just the identity transformation, so it's redundant. Worse, it's using rotMat for both input and output, something the documentation explicitly says you should not do.
    – Thomas
    Oct 28, 2015 at 19:40
0

Have you tried the combined (sensor-fusion) type Sensor.TYPE_ROTATION_VECTOR. This may give better results: Go to https://developer.android.com/reference/android/hardware/SensorEvent.html and search for 'rotation_vector'.

2
  • 1
    Ok, so is there any example how to work with this sensor? I get values for xsin(θ/2), ysin(θ/2) and z*sin(θ/2). But how do I get the value, I need to build my compass. Should I use getRotationMatrix again? Thanks for your help.
    – Adrian Eb
    Feb 7, 2013 at 9:54
  • "X is defined as the vector product Y.Z (It is tangential to the ground at the device's current location and roughly points East). Y is tangential to the ground at the device's current location and points towards magnetic north. Z points towards the sky and is perpendicular to the ground." Dust off your geometry class books, or google it :), and you should be able to figure it out. Feb 7, 2013 at 15:08
0

Here's a Kotlin approach with all the necessary matrices included (for some reason the previous answers leave out the array sizes, which matter)

// This is determined from the deprecated Sensor.TYPE_ORIENTATION
var lastOrientation: FloatArray = FloatArray(3)
var lastHeading: Float = 0f
var currentHeading: Float = 0f

// This is from the non deprecated Sensor.TYPE_ROTATION_VECTOR
var lastVectorOrientation: FloatArray = FloatArray(5)
var lastVectorHeading: Float = 0f
var currentVectorHeading: Float = 0f

override fun onSensorChanged(event: SensorEvent) {
    when(event.sensor?.type) {
        null -> return
        Sensor.TYPE_ORIENTATION -> {
            lastOrientation = event.values
            lastHeading = currentHeading
            currentHeading = abs(event.values[0].roundToInt().toFloat())
        }
        Sensor.TYPE_ROTATION_VECTOR -> {
            lastVectorOrientation = event.values
            lastVectorHeading = currentVectorHeading

            val tempRotationMatrix = FloatArray(9)
            val tempOrientationMatrix = FloatArray(3)
            getRotationMatrixFromVector(tempRotationMatrix, event.values)
            remapCoordinateSystem(tempRotationMatrix, AXIS_X, AXIS_Z, tempRotationMatrix)
            getOrientation(tempRotationMatrix, tempOrientationMatrix)
            currentVectorHeading = Math.toDegrees(tempOrientationMatrix[0].toDouble()).toFloat()
            
            if(currentVectorHeading < 0) {
                currentVectorHeading += 360f//heading = 360 - abs(neg heading), which is really 360 + (-heading)
            }
        }
        else -> return
    }
}

I've also included the deprecated Sensor.TYPE_ORIENTATION for anybody wanting to see the difference between the two approaches. There is a several degree difference when using the deprecated method vs the updated approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.