# Calculate vertical distance using acceleration

I have the following scenario: an iPhone is connected to a vertical rail (it's motion is restricted to the Y axis). Through mechanical means the phone is forced upwards and then allowed to return to rest. The distance varies (anywhere from 6" to 24"). The total time required for the full trip is roughly 1 - 2 seconds. The goal is to estimate the distance the device travels.

Calculation of Distance from measured Acceleration vs Time

Getting position from an accelerometer on an Android phone

(although I must admit that it is been years since I've had to deal directly with these topics)

Currently I am collecting acceleration data (along the Y axis) and use the following to estimate the integral of acceleration and velocity (via Excel):

1  Time [A]  Acceleration [B]   Velocity [C]                Distance [D]
2  0         a(z)               0                           0
3  1         a(z)               =C2+(A3-A2)*(B2+B3)/2       =D2+(A3-A2)*(C2+C3)/2
4  2         a(z)               =C3+(A4-A3)*(B3+B4)/2       =D3+(A4-A3)*(C3+C4)/2
5  3         a(z)               =C4+(A5-A4)*(B4+B5)/2       =D4+(A5-A4)*(C4+C5)/2
6  4         a(z)               =C5+(A6-A5)*(B5+B6)/2       =D5+(A6-A5)*(C5+C6)/2
7  5         a(z)               =C6+(A7-A6)*(B6+B7)/2       =D6+(A7-A6)*(C6+C7)/2
8  6         a(z)               =C7+(A8-A7)*(B7+B8)/2       =D7+(A8-A7)*(C7+C8)/2
9  7         a(z)               =C8+(A9-A8)*(B8+B9)/2       =D8+(A9-A8)*(C8+C9)/2
10 8         a(z)               =C9+(A10-A9)*(B9+B10)/2     =D9+(A10-A9)*(C9+C10)/2
11 9         a(z)               =C10+(A11-A10)*(B10+B11)/2  =D10+(A11-A100)*(C10+C11)/2
12 10        a(z)               =C11+(A12-A11)*(B11+B12)/2  =D11+(A12-A11)*(C11+C12)/2
...       ...                ...                         ...


Here is a sample of my data: test_data_1.csv

If the units of acceleration are measured in G's (9.8 m/s^2) what are the resulting units of velocity and distance?

This is what a plot of my data looks like - it really doesn't make sense to me (which isn't surprising since I don't have much experience in these areas). Again, the goal is to come up with a distance estimate (it doesn't have to be perfect).

Any pointers/clarifications would be much appreciated.

• If you time intervals are in $\rm seconds$ then the integrals are $\rm meter/second$ and $\rm meter$. Jul 13, 2014 at 14:58
• Are you using CMAccelerometerData#acceleration or CMDeviceMotion#userAcceleration to get the acceleration value? Because the former is proper acceleration, a vector sum of G and the familiar dv/dt acceleration. Jul 13, 2014 at 15:40
• @LeonidShevtsov I am using CMAccelerometerData#acceleration - is that what you suggest I use? Jul 13, 2014 at 23:24
• @RobertJoseph No, you should be using startDeviceMotionUpdates, and CMDeviceMotion#userAcceleration - see the overview on this page. Basically the API filters out gravity and provides dv/dt for you. Jul 14, 2014 at 9:30

The accelerometer measures the negative of gravity plus any upwards acceleration see NOTE#1

$$acc = -(g+\ddot{x})$$

and you want to integrate $\ddot{x}$ to get speed $v=\dot{x}$ and position $x$. So your expressions should be

$$v(t)=-\int_0^t ( acc+g)\,{\rm d}t \\ x(t)=-\int_0^t \int_0^t ( acc+g)\,{\rm d}t\,{\rm d}t$$

You also know that the final position should be equal to the initial, and the final velocity should be at rest. This requires you to tweak the integration results to get the desired result. This can be justified since you have discrete acceleration values This is where you calibrate the model with the measurement data.

So here are the steps to take

1. Find the sampling frequency $n=\frac{\# samples}{second}$
2. Assume intial position is zero
3. Assume initial speed is zero
4. From the first few seconds, find the local $g$ value while the phone is not moving
5. Sample acceleration values $acc$ and calculate times for each sample using the sample frequency and the sample count.
6. Find the start of the event by checking when $acc$ starts the deviate from $g$. Set time equal zero to the start of the event
7. Integrate $-acc-g$ over time from zero time and note the final velocity $v_1$ and time $t_1$ in the end.
8. Now integrate the velocities to get position and note the final position $x_1$ at time $t_1$ in the end.
9. Now have to tweak the acceleration values with the linear function over time in order to get the desired effect of the phone resting in the end. This is done with this piece of analytic magic see NOTE#2 $$v(t)=\int_0^{t} -( acc+g) + \left( \frac{2 v_1}{t_1} - \frac{6 x_1}{t_1^2}\right) + t \left(\frac{12 x_1}{t_1^3}-\frac{6 v_1}{t_1^2}\right) \,{\rm d}t \\ x(t)=\int_0^{t} \int_0^t -( acc+g) + \left( \frac{2 v_1}{t_1} - \frac{6 x_1}{t_1^2}\right) + t \left(\frac{12 x_1}{t_1^3}-\frac{6 v_1}{t_1^2}\right) \,{\rm d}t\,{\rm d}t$$
10. You can now find the peak velocity and peak height.

NOTE #1: To get the accelerometer equation you need to do a free body diagram on the accelerometer itself as it is attached to the phone and follows the phone's motion. The connection force $F$ is sensed used piezoelectic materials such that $F - m_{acc} g = m_{acc} \ddot{x}$. The reported acceleration is calibrated to be $$acc=-\frac{F}{m_{acc}} = -g -\ddot{x}$$ such that an upwards reaction force indicates downwards acceleration (the so called D'Alembert rule). Note that initially when $acc=-g$ the result is $\ddot{x} = -(-g)-g = 0$.

NOTE #2: You can verify that

$$\int_0^{t_1} \left( \frac{2 v_1}{t_1} - \frac{6 x_1}{t_1^2}\right) + t \left(\frac{12 x_1}{t_1^3}-\frac{6 v_1}{t_1^2}\right) \,{\rm d}t =-v_1\\ \int_0^{t_1} \int_0^t \left( \frac{2 v_1}{t_1} - \frac{6 x_1}{t_1^2}\right) + t \left(\frac{12 x_1}{t_1^3}-\frac{6 v_1}{t_1^2}\right) \,{\rm d}t\,{\rm d}t = -x_1$$

• Actually, separating d²x/dt² is so useful that there's an iPhone API that does steps 1 through 6 for you: see CMDeviceMotion#userAcceleration Jul 14, 2014 at 9:38

Like Ross said, you need to try to remove the overall constant part of your acceleration data. Most accelerators report gravity all of the time. So even if your phone was sitting still on a desk, it would report and acceleration of -9.8 m/s^2 "down", however down lined up with the axes of your phone.

Looking at your data a bit, for the first 1/3 of your interval, the acceleration is largely constant around -0.977. I am assuming that for that part of the data your measuring device wasn't actually moving, so I can take it as a baseline and remove it. If I remove that and then do the trapezoidal integration, we get something like: Does that look like what you were expecting?

Also, you've goofed in your description of the trapezoidal integration. In both cases, when we integrate $a$ to get $v$ and when we integrate $v$ to get $x$, we want to perform that integration over time. So in your column D, instead of something like D3=D2+(B3-B2)*(C2+C3)/2 it should be D3=D2+(A3-A2)*(C2+C3)/2

• Thank you very much @alemi! Your graph is exactly what I am looking for. I can't seem to get my distance plot to look like yours (I'm using the data I linked to in my question) - is there any way you could send me the data you used to plot it? FYI: I did correct my trapezoidal integration equations as you suggested. Jul 19, 2014 at 12:59
• The scale is set by the units in which time is measured. I didn't know your sample rate, so I just scaled it until the plot looked nice, but if you want velocity in m/s and x in meters you have to make your time column in seconds and multiple your acceleration column by 9.8. Jul 19, 2014 at 13:28
• @RobertJoseph I'm afraid I don't have it anymore. Jul 19, 2014 at 13:29
• @RobertJoseph If you pop into the physics chat room we could hash it out. No compensation needed. Jul 19, 2014 at 20:58
• Shouldn't the red line (position) go back to zero after the event? Jul 20, 2014 at 15:47

If your accelerations are in $g$, you should multiply the second term in column C by $9.8$. The velocities will then be in m/sec and your integration to make D will be in meters. You may then want to apply any constraints you know of. If you know the velocity starts and ends at zero, you might want to add or subtract a constant acceleration to the data to make it come out so. If you know the phone returns to its starting location, you can apply another correction (linear change in acceleration vs time?) to make that come out. These can improve the accuracy.