Compass and Accelerometer precision

2019-03-22 07:12发布

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
    }
}

3条回答
甜甜的少女心
2楼-- · 2019-03-22 07:52

What you're seeing is the real thing- the orientation sensors on most phones are only good enough to give you a rough compass heading.

If you want to smooth the displayed value out so it gives you something that's doesn't appear to change randomly I recommend implementing a http://en.wikipedia.org/wiki/Moving_average or other smoothing filter in Java on that orientation result.

For the highest performance you could write the filter using the NDK and use the Boost Accumulators library: http://www.boost.org/doc/libs/1_46_1/doc/html/accumulators.html

查看更多
Rolldiameter
3楼-- · 2019-03-22 07:52

I did this using a Kalman filter from here: Greg Czerniak's Website

I'm sending data to a udp port and smoothing it on the PC using python. But I guess you can find a Kalman filter implementation for java/android out there.

查看更多
【Aperson】
4楼-- · 2019-03-22 07:57

See my answer here: Smoothing data from a sensor

I run this filter on both the accelerometer and magetometer event values before passing them to SensorManager.getRotationMatrix(). I think this algorithm has the advantage of not having to keep a large array of historic values, just the prior low-pass output array.

The algorithm was derived from this Wikipedia entry: http://en.wikipedia.org/wiki/Low-pass_filter#Algorithmic_implementation

查看更多
登录 后发表回答