使用加速度计android的精度问题 [英] Accuracy problems using accelerometer android
问题描述
我正在开发一个简单的计数器应用程序,该应用程序可以计算用户何时沿x轴(正向或负向)移动手机约90度。我正在使用加速度计来测量通过移动手机和使用手机产生的加速度用于计数。
但存在问题,准确性不高,有时不计算,有时甚至计算两次。
这是我的代码,我想知道是否有办法获得良好的准确性?
I'm developing a simple counter app which counts when the user move their phone along the x axis(positive or negative) about 90 degrees.i'm using the accelerometer for measuring the acceleration cause by moving the phone and using this for counting. but there is a problem,the accuracy is not good,sometimes it doesn't count and sometimes it count twice. this is my code,i want to know if there is a way to get good accuracy?
@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个值进行滚动平均。这使您的价值观变得更加顺畅和易于处理。
这是一个带有双精度值的示例,您可以更改为任意值:
´
You can check the event timestamp to determine if it's an already measured value, but I would suggest that you implement a kind of smoothing for example a rolling average over the last 3-5 values. That makes your values a lot smoother and better to handle. Here is an example with double values, you can change to whatever your want: ´
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;
}
}
´
您需要进一步的帮助吗?
Do you need further help?
这篇关于使用加速度计android的精度问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!