# Tracking Distance on one Axis with an Accelerometer

1. Jun 27, 2012

### ferret_guy

I need to track Distance on one Axis with an Accelerometer for a rocket project (i know there are bettor ways t do this but i am dead set on this one) I was thinking as follows,

Code (Text):

Accelerometer*Δt=Δv
Current speed+Δv=New speed
distance+New speed*Δt=New distance

2. Jun 27, 2012

### bahamagreen

distance+New speed*Δt=New distance

Isn't "New speed" the speed only at the end of the interval "delta t", not for the whole duration of delta t?

3. Jun 27, 2012

### ferret_guy

This function is repeated many times so that is the speed that is dirived from the inatil meshurment and the Δt is the time since the last meshurment so were assuming that your travling at a constant speed for the time it takes to exicute the pice of code

4. Jun 27, 2012

### A.T.

Yes. It will still work, but is inaccurate. A better simple method is averaging accelerations and velocities at the ends of the interval:

PHP:

velocityX[0] = 0 // we assume the device was static when we started recording acc
positionX[0] = 0

for every sample i > 0:
dt = time[i-1] - time[i] // if sampling rate is constant simply use 1 / frequency here
velocityX[i] = velocityX[i-1] + (accX[i] + accX[i-1]) / (2 * dt)
positionX[i] = positionX[i-1] + (velocityX[i] + velocityX[i-1]) / (2 * dt)

Better is:
http://doswa.com/2009/01/02/fourth-order-runge-kutta-numerical-integration.html

5. Jun 27, 2012

### ferret_guy

Thank you I was hesitant about posting code because I dident know how many people wold be able to interpret it