Saturday, June 23, 2012

[android-developers] Android Compass help

Hi Guys,
             I need your help on developing an android compass application.Here is the source code...I want to know if i am using correct algorithm and sensors for developing the app and secondly I am unable to understand why my compass jitters so much

package com.android.compass.example

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;

public class CompassExample4Activity extends Activity implements SensorEventListener{
private SensorManager mSensorManager;
CompassView mCompassView;
Sensor accelerometer;
Sensor magnetometer;
float azimut;
float[] mGravity;
float[] mGeomagnetic;
static final float ALPHA = 0.2f;
double SmoothFactorCompass = 0.5;
double SmoothThresholdCompass = 30.0;
double oldCompass = 0.0;
int oldCompass2 = 0;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCompassView = new CompassView(this);
        //this.setRequestedOrientation(0);
        setContentView(mCompassView);
        
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
   accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
   magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    }
    
    @Override
    public void onResume() {
   super.onResume();
   mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
   mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);
}
    @Override
public void onPause() {
   super.onPause();
   mSensorManager.unregisterListener(this);
}
 
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}

@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
     mGravity = lowPass(event.values, mGravity);
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
     mGeomagnetic = lowPass(event.values, mGeomagnetic);
   
if (mGravity != null && mGeomagnetic != null) {
     float R[] = new float[9];
     float I[] = new float[9];
     boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
     // Correct if screen is in Landscape
     SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_X,
               SensorManager.AXIS_Z, R);
           
     if (success) {
       float orientation[] = new float[3];
       SensorManager.getOrientation(R, orientation);
       float azimut = orientation[0]; // orientation contains: azimut, pitch and roll
       //float azimuth = (float) Math.round((Math.toDegrees(orientation[0]))*7)/7;
       //azimuth = ( azimuth + 360)%360; 
       
       mCompassView.updateDirection((float)smoothnessFunction(azimut*360/(2*3.14159f)));
       //mCompassView.updateDirection(azimuth);
     }
}
}
protected float[] lowPass( float[] input, float[] output ) {
   if ( output == null ) return input;

   for ( int i=0; i<input.length; i++ ) {
       output[i] = output[i] + ALPHA * (input[i] - output[i]);
   }
   return output;
}
public double smoothnessFunction( double newCompass) {
if (Math.abs(newCompass - oldCompass) < 180) {
   if (Math.abs(newCompass - oldCompass) > SmoothThresholdCompass) {
       oldCompass = newCompass;
   }
   else {
       oldCompass = oldCompass + SmoothFactorCompass * (newCompass - oldCompass);
   }
} else {
   if (360.0 - Math.abs(newCompass - oldCompass) > SmoothThresholdCompass) {
       oldCompass = newCompass;
   } else {
       if (oldCompass > newCompass) {
           oldCompass = (oldCompass + SmoothFactorCompass * ((360 + newCompass - oldCompass) % 360) + 360) % 360;
       } else {
           oldCompass = (oldCompass - SmoothFactorCompass * ((360 - newCompass + oldCompass) % 360) + 360) % 360;
       }
   }
}
return oldCompass;
}
public int smoothnessFunction2(double newCompass){
if ((int)newCompass % 360 != oldCompass2) {
oldCompass2 = (int) newCompass;
}
return oldCompass2;
}
}

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate