Vibration makes the accelerometer incorrect

I have a simple application that reads accelerator values ​​(for the x, y, and z axis). But for a certain value, I vibrate my phone programmatically. Thus, whenever vibration occurs, the value of the accelerometer flickers high up and down in range. I want to avoid this. Please let me know how to prevent an accelerometer giving a variable result in vibration mode.

public void onSensorChanged(SensorEvent event) {

if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
return;          
         switch (mDisplay.getRotation()) {
                case Surface.ROTATION_0:
                    x = event.values[0];
                    y = event.values[1];
                    break;
                case Surface.ROTATION_90:
                    x = -event.values[1];
                    y = event.values[0];
                    Log.i("ROT: ", "90");
                    break;
                case Surface.ROTATION_180:
                    x = -event.values[0];
                    y = -event.values[1];
                    break;
                case Surface.ROTATION_270:
                    x = event.values[1];
                    y = -event.values[0];
                    break;
            }
}

Thanks.

+3
source share
2 answers

You can use a low pass filter so that you can filter high jitter. Something like that:

float lastAccel[] = new float[3];
float accelFilter[] = new float[3];
     accelFilter[0] = (float) (alpha * (accelFilter[0] + accelX - lastAccel[0]));
        accelFilter[1] = (float) (alpha * (accelFilter[1] + accelY - lastAccel[1]));
        accelFilter[2] = (float) (alpha * (accelFilter[2] + accelZ - lastAccel[2]));
+1
source

, .

0

Source: https://habr.com/ru/post/1525150/


All Articles