Search code examples
androidperformanceaccelerometercalculation

Android calculating Velocity wtih Accelerometer data


Due to the power consuming GPS data, I would like to calculate the device speed with only the accelerometer x,y and z data. I have read a lot of questions about this topic and I tried many set-ups to find a satisfactory solution to calculate the speed when my device is in my car. It seems so simple but nothing works, which drives me crazy. Been trying the Sensor.TYPE_LINEAR_ACCELERATION and the Sensor.TYPE_ACCELEROMETER with removed gravity. Tried a Low Pass Filter on the Linear acceleration data. Unfortunately all with no succes. Looks like the calculated speed is correct but testing in my car the calculated speed doesn't get higher then about 2 m/s.

below a code snip

mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);


public void onSensorChanged(SensorEvent event) {
    if (event.sensor == mAccelerometer) {

        if (timestamp != 0) {
            final float dT = (event.timestamp - timestamp) * NS2S;

            lax = event.values[0];
            lay = event.values[1];
            laz = event.values[2];

            vx = vxo + lax * dT ;
            vy = vyo + lay * dT ;
            vz = vzo + laz * dT ;

            speed = (float) (Math.sqrt(vx*vx + vy*vy + vz*vz)) ;
            if (speed < 0.01) {speed = 0 ; } 
            tv_speed.setText(String.valueOf(speed));

        }          
        timestamp = event.timestamp;
    }
}

Hope someone can help, thanks a lot.


Solution

  • Although at the developer.android.com site it is stated clearly that

    you could use the linear accelerometer to see how fast your car is going"

    But I did not find anything (in testing or in examples or code) that showed this is really true.

    Now, unfortunately, I am convinced that it is not possible to calculate the speed of a car with the linear accelerometer.