Android Acceleration Sensor: Low-Pass Linear Acceleration

With an acceleration sensor alone, it is difficult to reliably sequester the gravity component (tilt) of the signal from the linear acceleration component (the real acceleration) without the addition of a gyroscope (rotation sensor).

The acceleration sensor itself cannot determine the difference between the tilt of the device and the acceleration of the device. This does not mean that an Android application cannot estimate the rotation of the device and derive linear acceleration The force of gravity can be isolated with a low-pass filter and then the gravity can be subtracted from the raw signal, essentially creating a high-pass filter that isolates the linear acceleration.

Acceleration Explorer:

Acceleration Explorer is an open source Android application that allows users to visualize the behavior of the acceleration sensor. A number of configurable  filters have been implemented as part of the application allowing users to quickly visualize the behavior of their filters.

Acceleration Explorer Homescreen

The Implementation:

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

   final float alpha = 0.8;

   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];

   linear_acceleration[0] = event.values[0] - gravity[0];
   linear_acceleration[1] = event.values[1] - gravity[1];
   linear_acceleration[2] = event.values[2] - gravity[2];
}

The value to pay attention to is alpha. Alpha defines how responsive the filter is to changes in the devices orientation and has large implications depending on your requirements.

Alpha = ~0.9:

A value for alpha that is approaching 1 weights the filter heavily towards the existing gravity estimation and applies only a small portion of the current values of the acceleration sensor to the new estimation. This makes the filter use a long term estimation of gravity that is ideal for situations where the orientation of the device remains relatively static.

Alpha = ~0.1:

A value for alpha that is approaching 0 weights the filter heavily towards the current acceleration values and applies only a small portion of the existing gravity estimation to the new estimation. This makes the filter use a short term estimation of gravity that is ideal for situations where the orientation of the device is relatively dynamic.

Caveats:

You cannot have a low-pass filter that has both the simultaneous qualities. You must chose only one.

  • The filter resists accumulating error over extended periods of linear acceleration
  • A quick response to changes in the devices orientation

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *