Search code examples
androidaccelerometerdistancegyroscopecompass

How to find distance of displacement using accelerometer sensor in android smartphone?


I am having one android smart phone containing accelerator sensor, compass sensor and gyroscope sensor . i want to calculate the distance of displacement using this sensors.

I already tried with the basic method ie.,

final velocity = initial velocity + ( acceleration * time taken)
distance = time taken * speed

But i am unable to get the correct displacement. Every time i tried for same displacement i am gettng diffrent results.


Solution

  • The equation you may be looking for looking for is:

    Velocity = (Gravity*Acceleration)/(2*PI*freq)
    

    A correct use of units for this equation (metric) would be

    Gravity = mm/s squared = 9806.65
    Acceleration = average acceleration over 1 second
    Frequency = Hz (of the acceleration waveform over 1 second)
    

    For example, if you gathered data from all 3 axes of the accelerometer, you would do the following to get a acceleration waveform (in raw values) for a 3D space:

    inputArray[i] = sqrt(X*X + Y*Y + Z*Z);
    

    Once the data is collected, only use the amount of samples in the waveform that would have been collected (if there is a 1ms delay between values only use 1000 values).

    Add the values together and divide by the amount of samples to get your average (you may need to make all values positive if the accelerometer data have minus values) you could use this algorithm to do this before finding the average.

    for(i = 0; i < 1000; i++){
        if(inputArray[i] < 0){
            inputArray[i] = inputArray[i] - (inputArray[i]*2);
        }
    }
    

    Once you have the acceleration average output you need to perform the equation above.

    static double PI = 3.1415926535897932384626433832795;
    static double gravity = 9806.65;
    
    double Accel2mms(double accel, double freq){
        double result = 0;
        result = (gravity*accel)/(2*PI*freq);
        return result;
    }
    

    An example could be that the average acceleration is 3 gs over 1 second in a swing:

    NOTE: This calculation is based on a sinusoidal waveform, so the frequency would be representative of the physical movement of the accelerometer not the frequency of the sampling rate

    Accel2mms(3, 1);
    

    3 gs over 1 second with a frequency of 1 (1 swing in one direction) = 4682.330468 mm/s or 4.7 meters.

    Hope this is something like what you're looking for.

    Bear in mind this calculation is based on a sinusoidal waveform but is being adapted to calculate based on a single movement (frequency 1) so it may not be very accurate. But in theory should work.