views:

39

answers:

0

Hi, i made my own application in Android that use compass and accelerometer sensors to display the degrees of rotation and inclination of my device. I initialized all the listener and objects i needed (i followed some tutorials), and now i can catch the degrees as i wished. The problem is that the measures that sensors return aren't accurate. I mean even if i try to round the values of degrees i catch from the sensor, they oscillate between -/+ 7 (or 8) degrees every fraction of a second, even if i stay in a grass field away from any disturbing source. What i want to have is a accurate measure of the degrees, something like a method to round the values i recieve from the sensors.

    float[] mags = null;
    float[] accels = null;
    float[] R = new float[matrix_size];
    float[] outR = new float[matrix_size];
    float[] I = new float[matrix_size];
    float[] values = null;

    private void startSensor() {
    sensorMan.registerListener(this, sensorMan.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_UI);
    sensorMan.registerListener(this, sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);

}

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
        return;
    }

    switch (event.sensor.getType()) {
    case Sensor.TYPE_MAGNETIC_FIELD:
        mags = event.values.clone();
        break;
    case Sensor.TYPE_ACCELEROMETER:
        accels = event.values.clone();
        break;
    }

    if (mags != null && accels != null) {
        SensorManager.getRotationMatrix(R, I, accels, mags);
        // Correct if screen is in Landscape
        SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_X,
                SensorManager.AXIS_Z, outR);

        SensorManager.getOrientation(outR, values);
        azimuth = (float) Math.round((Math.toDegrees(values[0]))*7)/7;
        azimuth = ( azimuth + 360)%360; 
        //here is inclination. The problem is just the same with compass
        //inclination=-Math.round((float) (values[1]*(360/(2*Math.PI))));

        //other code to update my view
        //in azimuth i have the degree value. It changes continuously
        //even if i aim still the same direction
    }
}