How to detect walking with Android accelerometer

havanakoda picture havanakoda · Feb 14, 2011 · Viewed 26.3k times · Source

I'm writing an application and my aim is to detect when a user is walking. I'm using a Kalman filter like this:

float kFilteringFactor=0.6f;

        gravity[0] = (accelerometer_values[0] * kFilteringFactor) + (gravity[0] * (1.0f - kFilteringFactor));
        gravity[1] = (accelerometer_values[1] * kFilteringFactor) + (gravity[1] * (1.0f - kFilteringFactor));
        gravity[2] = (accelerometer_values[2] * kFilteringFactor) + (gravity[2] * (1.0f - kFilteringFactor));

        linear_acceleration[0] = (accelerometer_values[0] - gravity[0]);
        linear_acceleration[1] = (accelerometer_values[1] - gravity[1]);
        linear_acceleration[2] = (accelerometer_values[2] - gravity[2]);

        float magnitude = 0.0f;
        magnitude = (float)Math.sqrt(linear_acceleration[0]*linear_acceleration[0]+linear_acceleration[1]*linear_acceleration[1]+linear_acceleration[2]*linear_acceleration[2]);
        magnitude = Math.abs(magnitude);
if(magnitude>0.2)
  //walking

The array gravity[] is initialized with 0s.

I can detect when a user is walking or not (looking at the value of the magnitude of the acceleration vector), but my problem is that when a user is not walking and he moves the phones, it seems that he is walking.

Am I using the right filter?

Is it right to watch only the magnitude of the vector or have I to look at the single values ??

Answer

Dick Lucas picture Dick Lucas · Jul 16, 2015

Google provides an API for this called DetectedActivity that can be obtained using the ActivityRecognitionApi. Those docs can be accessed here and here.

DetectedActivity has the method public int getType() to get the current activity of the user and also public int getConfidence() which returns a value from 0 to 100. The higher the value returned by getConfidence(), the more certain the API is that the user is performing the returned activity.

Here is a constant summary of what is returned by getType():

  • int IN_VEHICLE The device is in a vehicle, such as a car.
  • int ON_BICYCLE The device is on a bicycle.
  • int ON_FOOT The device is on a user who is walking or running.
  • int RUNNING The device is on a user who is running.
  • int STILL The device is still (not moving).
  • int TILTING The device angle relative to gravity changed significantly.
  • int UNKNOWN Unable to detect the current activity.
  • int WALKING The device is on a user who is walking.