views:

627

answers:

1

Hi,

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[])

+2  A: 

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.

t-ril