# Getting the velocity and position with a accelerometer

Hi everyone!

I have a question. I have e GY-521 and i have alreade removed the gravity and it gives me the accelerations that i want. But I have a mathematical problem and I dont know how to solve it.

When I start the accelerometer with the Arduino i get a lot of data in 3-dimensions.

I can give you an example of the data i get.

X = [2 1 0 2 1 0 0 1 4 0 ] Y = [0 -6 -4 -3 -1 0 1 4 1 0] Z = [-1666 -1663 -1664 -1662 -1661 -1657 -1659 -1664 -1663 -1662]

It is much more than this but i will just give you the first ten.

The problem is how am i going to use this information to get the velocity and position. I know that i have to integrate the acceleration but i dont know how to do it.

How do i find the functions of the acceleration with this data?

I've just being doing something similar myself.

To get velocity you just multiply the acceleration by the time over which the acceleration occured

from my school physics this used to be represented as

V = U + AT

where V is new velocity U is original velocity A = acceleration T = time that acceleration was occurring

So AFIK

So if you continuously sample the acceleration values and monitor the time between each sample e.g. using millis() or micros()

then

just taking one dimension the code would look a bit like this

``````float vX=0;
float pX=0;
long lastTime=0;

loop()
{

long newTime=millis();
float accelX= getAccelX();

vX = vX + accelX * (newTime - lastTime)/1000;

pX = pX + vX * (newTime - lastTime)/1000;
lastTime=newTime;
}
``````

However

It won't actually work.

Because you are almost certainly going to have offsets in your acceleration data which bias it in one direction

Which is the problem I currently have.

I have not solved this issue as yet.

I've investigating various approaches, including

a) rolling average to remove offset in acceleration b) assuming that the body is subject to friction and that velocity will tend towards zero over time

(Note point (b) will not apply if you are trying to determine the position of something like an aircraft or boat, its only of any use for items subject to friction that don't have their own means of propulsion

You could also look at the Kalman filter for helping to resolve the problems with acceleration offsets

Also, though my research, I think that perhaps the Arduino Uno etc is not a fast enough CPU. I'm investigating using a Due, and also investigating other high performance embedded controllers e.g. running at several hundred mhz to attempt to achieve better results

But depending on precisely what you are trying to determine the position of, it may not actually be possible to do it at all.

i have alreade removed the gravity and it gives me the accelerations that i want.

How have you "removed gravity"? The data that you posted don't support this claim.

Here is a very useful discussion on the pitfalls in deriving position and velocity from accelerometer measurements. I strongly recommend that you go through it carefully, although beware of numerical errors in Table 1. http://www.chrobotics.com/library/accel-position-velocity