Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - SensorManager strange behaviour of getOrientation

I need to retrieve the orientation of my phone. At the moment i wrote this :

public void onSensorChanged(SensorEvent event) {
  switch (event.sensor.getType()){
  case Sensor.TYPE_MAGNETIC_FIELD:
    this.mag_vals = event.values.clone();
    this.sensorReady = true;
    break;
  case Sensor.TYPE_ACCELEROMETER:
    this.acc_vals = event.values.clone();
    break;
  }
  if (this.mag_vals != null && this.acc_vals != null && this.sensorReady) {
    this.sensorReady = false;
    float[] R = new float[ProjectConstants.SIZE_MATRIX];
    float[] I = new float[ProjectConstants.SIZE_MATRIX];
    SensorManager.getRotationMatrix(R, I, this.acc_vals, this.mag_vals);

    SensorManager.getOrientation(R, this.actual_orientation);
    ...  

This code allows me to get the orientation of the phone if i leave the phone on a flat surface and i rotate it over the surface.

What i did not understand is why if i move the phone upwards the value of this.actual_orientation[0] ,which is the rotation on the zed axis as described [here][1], the value increases although there was no rotation.

Did someone know what happens?

EDIT

Another strange thing..

I tried my application in the office at work and it had the strange behaviour i described before.. I tried in the same office(same situation) a compass app that i took from the market and it has the same behaviour of mine..when i moved the phone upwards the value changed consistently.. I tried in the same office(same situation) the i-phone compass and it didn't have that strange behaviour!

Then when i arrived at home i tried both, my application and the compass app of my android phone, and they worked!!even if i move upwards the phone the value are stable...

Thanks a lot.

[1]: http://developer.android.com/reference/android/hardware/SensorManager.html#getOrientation(float[], float[])

like image 594
hara Avatar asked Jul 07 '10 14:07

hara


2 Answers

The orientation estimate obtained by the device depends in part upon the local magnetic field direction. Within buildings, near iron structures and near electrical appliances the magnetic field sensed may vary significantly over distances measured in feet. This effect can be easily seen with a hand held magnetometer or Gauss-meter.

I suspect that these distortions are responsible for your applications strange behavior.

like image 59
t-ril Avatar answered Sep 22 '22 13:09

t-ril


When you lift it up you introduce a rotation angle and this affect the return values in getOrientation. The rotation angle measure the amount of rotation of your device from normal position, i.e. for most device where normal position means Portrait then if the phone is in landscape orientation, the rotation angle would be 90 or -90. So if you want the correct reading you have to call remapCoordinateSystem before the call to getOrientation.

like image 22
Hoan Nguyen Avatar answered Sep 22 '22 13:09

Hoan Nguyen