Android getOrientation()

 

I spend some time today trying to understand getOrientation()  and found a few notes when I googled, but most of the notes left things fuzzier than needed. The author’s appear to know what they were talking about – but failed to express it with a good clarity…

 

The documentation states:

 

getOrientation(float[] R, float[] values)

Computes the device's orientation based on the rotation matrix.

 

Not exactly a rich description! When I attempted to write code, the expected results did not occur….

 

The core of the code is:

SensorManager.getRotationMatrix(rotationMatrix, null, accelVals, magVals);            
SensorManager.getOrientation(rotationMatrix, orientationVals); textView1.setText(" 0:"+orientationVals[0]+ " 1:"+orientationVals[1]+" 2:"+orientationVals[2]);
Where the values are supplied by (I’ve omitted the code for stabilizing the values)
@Override
public void onSensorChanged(SensorEvent se) {
case Sensor.TYPE_MAGNETIC_FIELD:
     magVals = se.values.clone();
     break;
case Sensor.TYPE_ACCELEROMETER:
    accelVals = se.values.clone();
break;
}

So far, so good, but what do the orientationVals  mean?  The simplest way is to just run thru same sample data gathering.

 

Device Flat

Putting the device on a flat table and going to the 4 compass points.

image

The numbers were not what I was expecting…

Direction orientationVals[0] orientationVals[1] orientationVals[2]
N 2.8 -0.04 0.002
W 2.2 -0.04 0.002
S 1.5 -0.03 0.04
E 1.1 -0.04 0.04

Ugh … this is not what I was expecting…

Device Vertical:

image

Direction orientationVals[0] orientationVals[1] orientationVals[2]
N -1.6 -1.5 2.2
W 0.2 -1.53 2.2
S -1.4 -1.5 1.72
E -1 -1.5 2.3

Device Horizontal

image

Direction orientationVals[0] orientationVals[1] orientationVals[2]
N -0.7 -0.04 1.6
W -0.4 -0.04 1.6
S -2 -0.04 1.6
E -1.2 -0.02 1.6

 

OrientationVal[0] is the main item that changes with each experiment. The range from max-value to min-value seems is be about Pi/2 (3.14/2 = 1.57), so the values appear to be angle measures and not vector lengths (which I had assumed)…

 

In various forums, I finally found the text below – which forgot to mention that the values are angles in radians…

Computes the device's orientation based on the rotation matrix.
When it returns, the array values is filled with the result:
values[0]: azimuth, rotation around the Z axis.
values[1]: pitch, rotation around the X axis.
values[2]: roll, rotation around the Y axis.
Parameters
R        rotation matrix see getRotationMatrix(float[], float[], float[],
float[]).
values        an array of 3 floats to hold the result.
Returns
The array values passed as argument.

I now understand that values[0]: azimuth, rotation around the Z axis with 0.0 being magnetic north (another omission in the documentation).

Comments

Popular posts from this blog

Simple WP7 Mango App for Background Tasks, Toast, and Tiles: Code Explanation

Yet once more into the breech (of altered programming logic)

Error : /ScriptResource.axd : Invalid viewstate.