0

我尝试了以下方法,但它不能完美地工作。如果设备保持正常,那么它也会触发抖动事件。

@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub

    long curTime = System.currentTimeMillis();
    // only allow one update every 200Ms.
    if ((curTime - lastUpdate) > 200) {

        lastUpdate = curTime;

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

        Vibrator vibrate = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

        if (Round(y, 4) > 22) {
            vibrate.vibrate(200);
            Log.d("sensor", "==== Up Detected===");

        } else if (Round(y, 4) < -20) {
            vibrate.vibrate(200);
            Log.d("sensor", "==== Down Detected=== ");

        }

        mLastX = x;
        mLastY = y;
        mLastZ = z;
    }

}

public static float Round(float Rval, int Rpl) {
    float p = (float) Math.pow(10, Rpl);

    Rval = Rval * p;
    float tmp = Math.round(Rval);
    return (float) tmp / p;
}

请帮忙。

任何帮助将不胜感激..

谢谢

4

2 回答 2

0

应用低通滤波器作为您的 y 轴值。

基本过滤器将如文档中所述。

public void onSensorChanged(SensorEvent event){
  // In this example, alpha is calculated as t / (t + dT),
 // where t is the low-pass filter's time-constant and
 // dT is the event delivery rate.

 final float alpha = 0.8;

 // Isolate the force of gravity with the low-pass filter.
 gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
 gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
 gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];

 // Remove the gravity contribution with the high-pass filter.
 linear_acceleration[0] = event.values[0] - gravity[0];
 linear_acceleration[1] = event.values[1] - gravity[1];
 linear_acceleration[2] = event.values[2] - gravity[2];
  }
于 2013-01-14T14:39:50.557 回答
0

传感器值非常原始且嘈杂。您需要在它们之上添加一层数字信号处理才能获得良好的效果。仅使用原始值会导致结果出现很多抖动。您正在尝试使用时间延迟做一些基本的事情,但您需要进行更多过滤。

此外,您会因震动而振动。这种振动会导致加速度计看到运动,从而导致更多误报。

于 2013-01-11T19:31:59.493 回答