# Calculate distance an accelerometer moved

I am reading accelerometer data and want to determine the distance that the accelerometer is moved. ( My question is similar to the one placed here, Calculate speed from accelerometer . I just feel as if it lacks in formulating a complete example for me to use )

How my accelerometer output looks like:

x,y,time
-1.046700,0.120410,2015-02-02 07:22:33.609


How my accelerometer moves:

My accelerometer is placed on the ankle of a person. As position changes from $1$ to $2$, the accelerometer x and y values move from second quadrant to first quadrant. The image below depicts this scenario:

Note, while not shown, the persons leg can go back and forth, so $z$ axis would incur some change. I don't want "what happens on the $z$ axis" to interfere with distance estimates using $x$ and $y$.

What I (think) I know and what I know:

So far I know that acceleration will be present on both of these axis, $x$ and $y$. Hence, I think I know that the total acceleration between the two axis can be represented with this formula:

$total_{accl} = sqrt(x^2 + y^2)$

Given $total_{accl}$, it would make sense to me to convert the accelerometer data measurements to $m/s^2$, and subtract standard gravity from it. Hence, I think I know that my above formula would be as follows:

$total_{accl} = sqrt(x^2 + y^2) * 9.81 - 9.81$

I know that I can get velocity from $total_{accl}$ by taking the first integral. I can do this in matlab using this funciton, trapz. And then, I can solve for distance, using $d = t * v$, where $v$ is my first integral of $total_{accl}$ ( velocity ), and $t$ the amount of elapsed time that the leg was moving.

My question:

Is my logic here correct?

I ask because when I try this method out, I get really bad error, almost $+100cm$ from the actual distance. I understand that accumulated error can be bad, but it's so big that I am questioning if I am doing this right.

UPDATE:

This post, Instantaneous velocity calculation from accelerometer, really nails down why the eventual drift and error accumulation will destroy and form of velocity and distance estimation in the example above. ( Especially as time continues )

• I think that you're going to have a hard time getting accurate distances with accelerations measured by a smart phone accelerometer, if that's what you're thinking. In order to go from acceleration to distance you have to integrate the acceleration time history twice and the accelerometers on smart phones don't seem to be precise enough to get accurate results for distances. – user93237 May 18 '18 at 20:19
• @SamuelWeir I completely agree. But I want to try it out the "right" way to at least check if it is worth my time exploring further. – angryip May 18 '18 at 20:23

The logic is, I believe, incorrect. You cannot subtract the acceleration of gravity from your readings that easily.

The major problem is that the frame of reference of the data measurement is changing: the accelerometer is rotating as the leg swings out and back. In this frame, the acceleration of gravity is constantly changing direction.

To work in the lab frame, you would need to:

1. Find the instantaneous angle between the lab horizontal axis and the accelerometer x-axis;
2. Convert the accelerometer x-y readings to lab horizontal and vertical readings;
3. Subtract the acceleration of gravity from the y-value,

Then integrate numerically in both x and y directions over time to get velocities. Integrate in both directions over time again to get distance moved.

Consider using a digital camera and a strobe light... Google Harold Edgerton

• Can you send me a link for "lab horizontal axis". Not sure what the "lab" prefix means – angryip May 18 '18 at 19:35
• Any measurement takes place relative to some frame of reference, a fixed background. In this case, "lab" refers to the room, building, experimenter, the rest of the subjects body... Unfortunately, the accelerometer uses the structure of the accelerometer itself to define a frame of reference for directions. – DJohnM May 18 '18 at 19:49
• while I did not follow your recommendation to the fullest, the comment you make about instantaneous angles is very important. Thanks for the extra set of eyes. – angryip May 19 '18 at 22:36