使用加速度计的精度问题 android

Accuracy problems using accelerometer android

我正在开发一个简单的计数器应用程序,当用户使用加速度计测量加速度时,它会在用户沿 x 轴(正或负)移动他们的 phone 大约 90 degrees.i 时计数通过移动 phone 并将其用于计数。 但是有一个问题,准确度不好,有时不计有时又算两次。 这是我的代码,我想知道是否有办法获得良好的准确性?

@Override
protected void onResume() {
    super.onResume();
    SnManager.registerListener(this,acc,SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event) {
    if(!stop) {
        if (event.values[0] > 14) {
            Count++;
            txt_count.setText("" + Count);
            values.add("OK");
        }
        values.add(""+event.values[0]);
        lst.invalidate();
    }
}

您可以检查事件时间戳以确定它是否是一个已经测量的值,但我建议您实施一种平滑处理,例如对最后 3-5 个值进行滚动平均。这会让你的价值观更顺畅,更容易处理。 这是一个带有双精度值的示例,您可以更改为任何您想要的值: ´

public class Rolling {
    private int size;
    private double total = 0d;
    private int index = 0;
    private double samples[];
    public Rolling(int size) {
        this.size = size;
        samples = new double[size];
        for (int i = 0; i < size; i++) samples[i] = 0d;
    }

    public void add(double x) {
        total -= samples[index];
        samples[index] = x;
        total += x;
        if (++index == size) index = 0; 
    }

    public double getAverage() {
        return total / size;
    }   
}

´

您需要进一步的帮助吗?