Android:加速度计误检 [英] Android: Accelerometer false detection

查看:24
本文介绍了Android:加速度计误检的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码片段来检测加速度计的运动.它有时会通过正确检测轻微移动来工作,但有时当我让设备闲置时它也会检测到移动.Android 上的内置加速度计检测是否有任何问题?

I have a code snippet to detect accelerometer movements. It works some times by properly detecting slight movements, but sometimes it detects movements when I kept my device idle too. Are there any problems with built-in accelerometer detection on Android?

我使用 HTC G-1 设备.我的代码片段如下.如何解决此问题,以便在设备空闲时检测到小的设备移动但检测不到任何内容?

I use an HTC G-1 device. My code snippet is below. How do I resolve it so I can detect small device movements but not detect anything when the device is idle?

private static final int SHAKE_THRESHOLD = 50;

public void onSensorChanged(int sensor, float[] values) {

    if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
        long curTime = System.currentTimeMillis();
        // only allow one update every 100ms.
        if ((curTime - lastUpdate) > 100) {
            long diffTime = (curTime - lastUpdate);
            lastUpdate = curTime;

            x = values[SensorManager.DATA_X];
            y = values[SensorManager.DATA_Y];
            z = values[SensorManager.DATA_Z];

            float speed = Math.abs(x+y+z - last_x - last_y - last_z) / diffTime * 10000;

            if (speed > SHAKE_THRESHOLD) { 
                long curTime = System.currentTimeMillis();
                long diff = (curTime - shakeTime);              
                shakeTime = curTime;

                if (myFlagIgnoreShakeDetection==true)  //Caused unneccessary accelerometer   
                                                       //notification looping when device is idle
                   return;

                // Doing something...
            }
         last_x = x;
         last_y = y;
         last_z = z;
        }

    }

}

推荐答案

这里有一些代码差异...

Here are a few code discrepancies...

  • last_xlast_ylast_z 的更新可能有问题.我相信它们应该被包含在 inside if ((curTime - lastUpdate) > 100) { 语句中.换句话说,每次调用 onSensorChanged 时都会更新它们,而不是每 100 毫秒更新一次.您可能应该将这三个变量的更新移动到它们上方的花括号中.

  • There may be a problem regarding the updating of last_x, last_y, and last_z. I believe they should be included inside the if ((curTime - lastUpdate) > 100) { statement. In other words, they are being updated every time onSensorChanged is called, not every 100 milliseconds. You should probably move the updating of those three variables into the curly brace above them.

在计算 speed 的那一行,公式以 .../diffTime * 10000; 是否只想乘以 diffTime 乘以 10000,还是整个结果?由于 /* 通常具有相同的运算符优先级 在我知道的大多数语言中(例如 Java),您的方程将从左到右计算,首先除以 diffTime 然后 将该结果乘以 10000.

On the line where you compute the speed, the formula ends with ... / diffTime * 10000; Are you wanting to multiply just diffTime by 10000, or the entire result? Since / and * typically have the same operator precedence in most languages I know of (such as Java), your equation will be evaluated from left to right, dividing first by diffTime then multiplying that result by 10000.

我猜你的意思是将 diffTime 乘以 10000,从而除以最终结果除以那个数量.这是除以 10000 或乘以 10000 之间的差异,这意味着您获得的 speed 值可能比应有的值大 10^8,因此即使在设备空闲时也会触发阈值.您需要在乘法周围加上括号,例如 .../(diffTime * 10000);,以确保它在除法发生之前 执行.

I'm guessing you mean to multiply just diffTime by 10000, thus dividing the final result by that amount. This is the difference between dividing by 10000 or multiplying by 10000, which means you are probably getting values for speed that are 10^8 greater than you should, thus tripping your threshold even when the device is idle. You need to put parentheses around the multiplication, like ... / (diffTime * 10000);, to make sure it's performed before the division takes place.

此外,如果您打算将 diffTime 从毫秒缩放到秒,则缩放因子应为 1000.

Additionally, if you are intending to scale diffTime from milliseconds to seconds, your scale factor should be 1000.

这篇关于Android:加速度计误检的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆