Is there a sustainable way to detect shaking?

I tried to detect a shaking event with an accelerometer. I found that when I give a concussion in the phone, the difference in the cost of acceleration is quite stable. But when I turn the phone, there is always a big change in value (the value is usually greater than “shaking without rotation”). I want to focus on a shaking event, not a spin event. Is there any way to solve the problem?

here is my code for defining shaing

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 with the help of an accelerometer? Or should I try to change the orientation and do some calculations on sumAcc?

+5
source share
2

, , . - , , . ( ).

!


, , Sensor TYPE_LINEAR_ACCELERATION, Android 2.3 . , , .


13/06 → , , , post , . .

!

+3

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;

. . ( , -.).

.

0

All Articles