2

I've found this code, Its function is to do something when the device is shaken strong enough, but I haven't fully understood it . Anyone please help me

public class ShakeActivity extends Activity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
        mAccel = 0.00f;
        mAccelCurrent = SensorManager.GRAVITY_EARTH;
        mAccelLast = SensorManager.GRAVITY_EARTH;
    }
      private SensorManager mSensorManager;
      private float mAccel; // acceleration apart from gravity
      private float mAccelCurrent; // current acceleration including gravity
      private float mAccelLast; // last acceleration including gravity

      private final SensorEventListener mSensorListener = new SensorEventListener() {

        public void onSensorChanged(SensorEvent se) {
          float x = se.values[0];
          float y = se.values[1];
          float z = se.values[2];
          mAccelLast = mAccelCurrent;
          mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
          float delta = mAccelCurrent - mAccelLast;
          mAccel = mAccel * 0.9f + delta; // perform low-cut filter

        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
      };

      @Override
      protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
      }

      @Override
      protected void onStop() {
        mSensorManager.unregisterListener(mSensorListener);
        super.onStop();
      }

}

please help me to understand this two lines

mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));//I guess this is for computing the value of the acceleration

and this line I don't understand

 mAccel = mAccel * 0.9f + delta;

thanks in advance.

Vipul
  • 27,808
  • 7
  • 60
  • 75
lk7630
  • 21
  • 3

1 Answers1

12

The sensor will return three values, for acceleration along the three axis directions; these are placed in x, y and z in your code sample. Imagine three masses on springs all at right angles to each other; as you move the device around and the springs stretch and contract, x, y and z contain their lengths.

mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));

This is computing the magnitude of the acceleration. Imagine if, instead of the three masses on springs, you had just one, always pointing exactly in the direction the device is being accelerated in. It's actually possible to work out what that system would look like from the values we have, and that's what's being done here: mAccelCurrent is how much such a spring would get stretched. This is the calculation being performed.

mAccel = mAccel * 0.9f + delta;

This is a high pass filter on the input. Here it has the effect of making sudden changes in acceleration give bigger values. It's not clear from just the code you've posted why this is being done; I am guessing it is to make the code elsewhere that ultimately checks mAccel more sensitive to the forces at the extremes of each shake when the device is being shaken.

moonshadow
  • 86,889
  • 7
  • 82
  • 122
  • yeah you're right about the mAccel. 1 more thing I wonder is that why do we have to multiply with "0.9f" and what does "0.9f" mean (I mean the letter f). and thanks for your wondeful answer – lk7630 Jun 12 '12 at 14:09
  • The letter f tells the compiler you intend to make the value a single precision floating point number (see, e.g., http://www.cplusplus.com/doc/tutorial/constants/ ). The multiplication is part of the high-pass filter, see the linked article; the value controls the rate at which the output signal decays following a spike of delta. – moonshadow Jun 12 '12 at 14:54