Search code examples
javaandroidaccelerometer

Calculate moving speed of the phone


I need to calculate the speed with which the android device is moving. I'm interested in cases with short distances where the device would often return to the starting point (like when a user is spinning on the office chair with an extended arm while holding the phone) so GPS is probably not applicable in my situation. How can I do it with an accelerometer?

I was thinking about something like:

double delta_t = current_time - previous_time

double delta_v_x = a_x * delta_t
double delta_v_y = a_y * delta_t
double delta_v_z = a_z * delta_t

current_v_x += delta_v_x
current_v_y += delta_v_y
current_v_z += delta_v_z

double v = Math.sqrt(Math.pow(current_v_x, 2) +  Math.pow(current_v_y, 2) +  Math.pow(current_v_z, 2))
  • Would it work as intended?
  • Do I have to take care of gravity too? How to do it if the orientation of the device can change?
  • Do I have to somehow remove the noise from acceleration measurements first?
  • Can high-pass filter solve both noise and gravity issues?

Solution

  • Just in case someone will ever have a similar question. For the problem with gravity, I've used Sensor.TYPE_LINEAR_ACCELERATION instead of Sensor.TYPE_ACCELEROMETER. And for noise, I've used Kalman Filter from Apache Commons Math.