有没有稳定的方法来检测震动事件?

发布于 2024-11-14 21:24:36 字数 1914 浏览 5 评论 0原文

我尝试使用加速度计检测震动事件。 我发现当我连续摇晃手机时,价值差异 的加速度相当稳定。 但是当我旋转手机时,该值总是会有很大的变化(该值通常比“摇动而不旋转”大)。 我想关注摇动事件,而不是旋转事件。有办法解决吗 问题?

这是我的抖动检测代码

public void onSensorChanged(SensorEvent event)
    {
        if (event.sensor.getType() == SensorManager.SENSOR_ACCELEROMETER)
        {
            nowTime = System.currentTimeMillis();

            float x = event.values[SensorManager.DATA_X];
            float y = event.values[SensorManager.DATA_Y];
            float z = event.values[SensorManager.DATA_Z];
            nowAcc = Math.sqrt(x*x+y*y+z*z);
            accDiff = Math.abs(nowAcc - preAcc);
            timeDiff = (nowTime - preTime);
                        //  try to get the sum of 10 samplings of accDiff
            tempAccDiff10 = tempAccDiff9;
            tempAccDiff9 = tempAccDiff8;
            tempAccDiff8 = tempAccDiff7;
            tempAccDiff7 = tempAccDiff6;
            tempAccDiff6 = tempAccDiff5;
            tempAccDiff5 = tempAccDiff4;
            tempAccDiff4 = tempAccDiff3;
            tempAccDiff3 = tempAccDiff2;
            tempAccDiff2 = tempAccDiff1;
            tempAccDiff1 = accDiff;
            sumAcc = tempAccDiff10+tempAccDiff9+tempAccDiff8+tempAccDiff7+tempAccDiff6+
                     tempAccDiff5+tempAccDiff4+tempAccDiff3+tempAccDiff2+tempAccDiff1;
            Log.i("SSSS",String.valueOf(sumAcc));
                        //when I give the phone a big & continuous "shake", it returns
                        //a value about 30~40, but when I give the phone a small
                        //"rotation", it returns a value of 80~120
            preAcc = nowAcc;
            preTime = nowTime;
            if (sumAcc>100)
            {
                SM.unregisterListener(sensorListener, sensor);
            }

            //123
        }


    }//end of onSensorChanged();

是否可以使用加速度计忽略旋转事件? 或者我应该尝试获得方向变化并进行一些计算 在 sumAcc 上?

I've tried to detect shaking event using accelerometer.
I've found that when I give a contiuous shake to the phone, the value difference
of the acceleration is quite stable.
But when I rotate the phone, there's always a big change on the value( the value is usually bigger than "shaking without rotation").
I want to focus on the shaking event, not the rotation event. Is there a way to solve
the problem?

here's my code for shaing detection

public void onSensorChanged(SensorEvent event)
    {
        if (event.sensor.getType() == SensorManager.SENSOR_ACCELEROMETER)
        {
            nowTime = System.currentTimeMillis();

            float x = event.values[SensorManager.DATA_X];
            float y = event.values[SensorManager.DATA_Y];
            float z = event.values[SensorManager.DATA_Z];
            nowAcc = Math.sqrt(x*x+y*y+z*z);
            accDiff = Math.abs(nowAcc - preAcc);
            timeDiff = (nowTime - preTime);
                        //  try to get the sum of 10 samplings of accDiff
            tempAccDiff10 = tempAccDiff9;
            tempAccDiff9 = tempAccDiff8;
            tempAccDiff8 = tempAccDiff7;
            tempAccDiff7 = tempAccDiff6;
            tempAccDiff6 = tempAccDiff5;
            tempAccDiff5 = tempAccDiff4;
            tempAccDiff4 = tempAccDiff3;
            tempAccDiff3 = tempAccDiff2;
            tempAccDiff2 = tempAccDiff1;
            tempAccDiff1 = accDiff;
            sumAcc = tempAccDiff10+tempAccDiff9+tempAccDiff8+tempAccDiff7+tempAccDiff6+
                     tempAccDiff5+tempAccDiff4+tempAccDiff3+tempAccDiff2+tempAccDiff1;
            Log.i("SSSS",String.valueOf(sumAcc));
                        //when I give the phone a big & continuous "shake", it returns
                        //a value about 30~40, but when I give the phone a small
                        //"rotation", it returns a value of 80~120
            preAcc = nowAcc;
            preTime = nowTime;
            if (sumAcc>100)
            {
                SM.unregisterListener(sensorListener, sensor);
            }

            //123
        }


    }//end of onSensorChanged();

is it possible to neglect the rotation event using accelerometer?
Or should I try to get the orientation change and do some computations
on the sumAcc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哥,最终变帅啦 2024-11-21 21:24:36

您应该使用过滤器来消除重力的影响,有某种 有关执行此操作的文档教程。我做了类似的事情,就我而言,我还试图检测其他动作,所以你的任务看起来更简单一些。我可以在下周一发布代码(如果我记得的话)。

祝你好运!


我正在编辑答案以添加您可以使用传感器TYPE_LINEAR_ACCELERATION 如果您使用的是 Android 2.3 或更高版本。这是一个普通的加速度传感器,它忽略了
重力,您绝对不需要检测震动事件。

问候


编辑 13/06 ->我答应发布我的代码来检测晃动,但相反,我认为发布 此链接,它提供了各种响应中的示例代码片段。我的代码与那里发布的代码非常相似。

祝你好运!

You should use a filter to eliminate the influence of the gravity, there is some sort of tutorial on the docs to do so. I did something similar, in my case I was also trying to detect other movements, so your task seems a bit simpler. I can post the code next monday (If I remember).

Good luck!


I'm editing the answer to add you can use a Sensor TYPE_LINEAR_ACCELERATION if you're on Android 2.3 or higher. That is a normal acceleration sensor which discards the effect of
gravity, which you definetely don't need to detect a shake event.

regards


Edit 13/06 -> I promised to post my code to detect shaking, but instead, I think it is a better idea to post this link, which gives sample pieces of code in a variety of responses. My code is very similar to one of those posted there.

Good luck!

我不吻晚风 2024-11-21 21:24:36

从代码的角度来看,您需要实现 SensorListener:

public class ShakeActivity extends Activity implements SensorListener

您需要获取 SensorManager:

sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);

并使用所需的标志注册该传感器:

ensorMgr.registerListener(this,
SensorManager.SENSOR_ACCELEROMETER,
SensorManager.SENSOR_DELAY_GAME);

在 onSensorChange() 方法中,您确定是否是摇晃:

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) {
        Log.d("sensor", "shake detected w/ speed: " + speed);
        Toast.makeText(this, "shake detected w/ speed: " + speed, Toast.LENGTH_SHORT).show();
      }
      last_x = x;
      last_y = y;
      last_z = z;
    }
  }
}

摇晃阈值是定义为:

private static final int SHAKE_THRESHOLD = 800;

还有一些其他方法来检测震动运动。看看这个 链接。(如果该链接不起作用或链接已失效,查看此网络存档。)。

谢谢。

From the code point of view, you need to implement the SensorListener:

public class ShakeActivity extends Activity implements SensorListener

You will need to acquire a SensorManager:

sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);

And register this sensor with desired flags:

ensorMgr.registerListener(this,
SensorManager.SENSOR_ACCELEROMETER,
SensorManager.SENSOR_DELAY_GAME);

In your onSensorChange() method, you determine whether it’s a shake or not:

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) {
        Log.d("sensor", "shake detected w/ speed: " + speed);
        Toast.makeText(this, "shake detected w/ speed: " + speed, Toast.LENGTH_SHORT).show();
      }
      last_x = x;
      last_y = y;
      last_z = z;
    }
  }
}

The shake threshold is defined as:

private static final int SHAKE_THRESHOLD = 800;

There are some other methods too, to detect shake motion. look at this link.(If that link does not work or link is dead, look at this web archive.).

Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文