安卓:加速度计误检加速度计

2023-09-04 08:54:40 作者:慕斯

我有一个code段检测到加速度计动作。它的工作原理有时被正确检测到轻微移动,但有时它检测到动作时,我一直在我的设备闲置过多。有没有带内置加速度传感器检测在Android?

任何问题

我用的HTC G-1设备。我的code片段如下。我该如何解决这个问题,所以我可以检测小装置的动作,但没有检测到任何东西时,设备处于空闲状态?

 私有静态最终诠释SHAKE_THRESHOLD = 50;

公共无效onSensorChanged(INT传感器,浮动[]值){

    如果(传感器== SensorManager.SENSOR_ACCELEROMETER){
        长CURTIME = System.currentTimeMillis的();
        //只允许一个更新每100ms。
        如果((CURTIME  -  lastUpdate)→100){
            长diffTime =(CURTIME  -  lastUpdate);
            lastUpdate = CURTIME;

            X =值[SensorManager.DATA_X]
            Y =值[SensorManager.DATA_Y]
            Z =值[SensorManager.DATA_Z]

            浮动速度= Math.abs(X + Y + Z  -  last_x  -  last_y  -  last_z)/ diffTime * 10000;

            如果(速度> SHAKE_THRESHOLD){
                长CURTIME = System.currentTimeMillis的();
                长差异=(CURTIME  -  shakeTime);
                shakeTime = CURTIME;

                如果(myFlagIgnoreShakeDetection ==真)//引起不必要的加速计
                                                       //当设备处于空闲状态的通知循环
                   返回;

                // 在做一些事情...
            }
         last_x = X;
         last_y = Y;
         last_z = Z;
        }

    }

}
 

解决方案

下面是一些code不符......

有可能是关于 last_x last_y 的更新问题,而 last_z 。我相信他们应该包含的 的的如果内((CURTIME - lastUpdate)→100){语句。换句话说,它们被每个 onSensorChanged 被调用时,不是每100毫秒更新一次。你或许应该将这些三个变量的更新到大括号在他们之上。

如何通过加速度计内部功能是实现电池供电设备的姿态与运动检测

在那里你计算速度,公式与 ... / diffTime * 10000结尾的行; 您想乘刚 diffTime 10000,或整个结果?由于 / * 通常具有相同的运算符precedence 在大多数语言我知道(如的 Java的),你的公式将从左到右,先除以 diffTime 的然后的10000相乘的结果。

我猜你的意思是刚才乘 diffTime 10000,这样的分的最后由量的结果。这是10000分和10000乘以之间的差异,这意味着你可能得到的值速度,比你应该在10 ^ 8更大,因此绊倒你的门槛,甚至当设备处于空闲状态。你需要把周围的乘法,如 ... /括号(diffTime * 10000); ,以确保它执行的在的分工需要的地方。

此外,如果您正打算扩展 diffTime 从毫秒到几秒,你的规模因素应该是1000。

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?

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

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.

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.

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.

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