# How to find offset distance using accelerometer sensor in Android smartphone?

I have one Android smartphone containing an accelerator **sensor, a compass sensor and a gyroscope sensor** . *I want to calculate the travel distance using these sensors.*

I already tried with the main method i.e.

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

But I cannot get the correct movement. Every time I tried the same move, I get different results.

source to share

The equation you might be looking for is:

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

The 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 have collected data from all three axes of an accelerometer, you should do the following to get the acceleration shape (at initial values) for 3D space:

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

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

Add the values โโtogether and divide by the number of samples to get the mean (you may need to make all values โโpositive if the accelerometer data has minus values), you can use this algorithm to do this before finding the mean.

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

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

```
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 would be that the average acceleration is 3 gs for 1 second in a swing:

**NOTE. **... This calculation is based on a sinusoidal waveform, so the frequency will represent the physical movement of the accelerometer, not the sampling rate.

`Accel2mms(3, 1);`

3 gs for 1 second at a rate of 1 (1 swing in one direction) = 4682.330468 mm / s or 4.7 meters.

Hope this is something like what you are looking for.

Remember that this calculation is based on a sinusoidal waveform, but is adapted to be calculated based on a single movement (frequency 1), so it may not be very accurate. But theoretically it should work.

source to share

As @rIHaNJiTHiN mentioned in the comments, there is no reliable way to get displacement from 2nd and 3rd order sensors (sensors that measure derivatives of displacement such as speed and acceleration).

GPS is the only way to measure absolute offset, although its accuracy and accuracy are not as good over short distances and short periods of time (in some places with poor signal).

source to share