Android: Accelerometer false detection

前端 未结 2 1543
情歌与酒
情歌与酒 2021-02-01 11:47

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 idl

相关标签:
2条回答
  • 2021-02-01 11:50

    I personally, in my augmented reality library, use a rolling average for the updates:

    float kFilteringFactor = (float)0.05;    
    rollingZ = (float) ((rawZValue * kFilteringFactor) + (rollingZ * (1.0 - kFilteringFactor)));
    

    This tends to smooth out the data pretty well, and you can tweak the filtering factor to get the responsiveness you want.

    rawZValue is the raw value coming in from the accelerometer.

    0 讨论(0)
  • 2021-02-01 12:11

    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.

    0 讨论(0)
提交回复
热议问题