0

I have an app where I want to access the magnotometer. I get results out of my application, but compared with orientation results from the compass app of g1 I get completely different results with my app than the compass app. e.g.: my app sais 250° and the compass app 90°! That just can't be, here is my code. Is there any difference to become better results?

public class MySensorListener implements SensorListener{
  int orientation;
  public MySensorListener(){
     orientation = 0;
  }
    public void onAccuracyChanged(int sensor, int accuracy) {
  // TODO Auto-generated method stub

 }

    public void onSensorChanged(int sensor, float[] values) {
        orientation = (int)values[0];
    }
}

I acess the Sensormanager like this:

private MySensorListener doCompass(){
 MySensorListener cl;
 SensorManager cm;
 compassListener = new MySensorListener();

  cm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

  cm.registerListener(
   cl, 
   SensorManager.SENSOR_ORIENTATION, 
   SensorManager.SENSOR_DELAY_UI); //updateRate
  return cl;
}

Can anyone help, pleeease?! :)

Thank you

Jens
  • 11
  • 3

3 Answers3

3

you must use Math.toDegrees. because value[0] return radian.

edit i see on other post, some people says that value[0] return radian. but, after i try to code it, i don't know value[0] return radian or grade/gon. because when i convert into degree.. the value is bigger than 360. so i try to convert it into grade. and it look good and don't have problem. the code is like this

Double azimuth = value[0];
azimuth = azimuth * 360 / 400;

sorry for bad english :)

qornanali
  • 66
  • 5
1

I am not sure whether Kalman filters is valid for Orientation Sensor values, but that is what you might need. More about Kalman filters for android is mentioned here

If you don't want to go through the headache of studying and implementing Kalman filter, use what I did below.It worked reasonably well for me. :

private boolean isMagneticFieldFine(float[] fieldValues){
    double value = Math.sqrt(fieldValues[0]*fieldValues[0] + fieldValues[1]*fieldValues[1] + fieldValues[2]*fieldValues[2]);        
    return value < 65.0 && value > 25.0;
}

Another thing, SensorListener is deprecated, try using SensorEventListener instead !

Community
  • 1
  • 1
Abhinav Manchanda
  • 6,546
  • 3
  • 39
  • 46
0

There are many ways to calibrate directions, such as using other sensors data to calibrate (e.g. gyroscope and accelerometer), or use statistical models (e.g. particle filter and Kalman Filter). This presentation talks about some latest technologies used in industry to calibrate movement sensors: https://www.youtube.com/watch?v=C7JQ7Rpwn2k

Kaifei
  • 1,568
  • 2
  • 13
  • 16