Hi how calculate speed using accelerometer. Any example?

Just recently, member jremington said in this thread:

The general approach of using an accelerometer to estimate position and velocity does not work with consumer grade sensors, as explained here: http://www.chrobotics.com/library/accel-position-velocity

So, it might depend on how much money you spent.

The velocity formula states:

V_{t} = V_{0} + a.t

so you need to integrate the acceleration over time,

but as both timing and acceleration are “noisy” it will drift from the actual value.

If i have this data from accelerometer you say that I must integrate every time raw result. But i wiil receive very different results?

fortran: But i wiil receive very different results?

From what?

The acceleration is not linearly! The problem is that i don’t know how

integrate input data to velocity and then integrate velocity to distance. I found this example:

```
// acceleration[0][j] is x
// acceleration[1][j] is y
// acceleration[2][j] is z
// let j = 0 be initial/previous, j = 1 be current
// outer loop that handles: x, y, z;
for (int j = 0; j < 3; j++)
{
// get the velocity
velocity[j][1] = velocity[j][0] + acceleration[j][0] + ((acceleration[j][1] - acceleration[j][0]) / 2);
// get the distance
position[j][1] = position[j][0] + velocity[j][0] + ((velocity[j][1] - velocity[j][0]) / 2);
// get ready for the next set of readings
acceleration[j][0] = acceleration[j][1];
velocity[j][0] = velocity[j][1];
position[j][0] = position[j][1];
}
}
```

Is this correct?

Since this topic is Nr. 1 google result for “accelerometer arduino speed” and has more than 10’000 views, Im going to update on this issue that I stumbled upon while trying to figure out what the precision of non-super-expensive accelerometers is:

First things first:

You cant add velocities and acceleration, just like mass and height or gamescore and speed cant be added.

To get from acceleration to the current speed the following is needed:

- speed at the beginning (eg. 0 km/h) (acting as the constant in a integration)
- acceleration at the current time
- and of course the amont of time from this measurement to the previous measurement

Now we have the formula:

a = dv / dt

(acceleration) = (difference in speed) divided by (amount of time during which this difference in speed happens)

To get speed we rearrange:

dv = a * dt

So we have to multiply the current acceleration times dt (amount of time since the last measurement). Now we have the change in speed. This has to be integrated to get the absolute velocity. Since we are dealing with discrete values and not some fancy function integrating and differentiation are super easy by just adding or making differences:

new-velocity = old-velocity + dv

Code would have to do this:

```
acceleration = whatever, your input here;
time_acceleration = millis();
dv = acceleration * (time_acceleration - time_acceleration_last);
v = v_old + dv;
time_acceleration_last = time_acceleration;
```

Since the data will be noisy I gues it would be a good idea to smooth the input (acceleration) and/or output (dv). Just ask if you need a simple Klaman filter.

Eheran, can you share the code for the simple kalman filter

And you need to subtract the acceleration due to gravity from the readings before integration. If the orientation is not fixed, you'll need a 6DoF sensor so you can calculate the orientation map between local and global frames.

Explain what you actually are trying to do, its quite likely its not possible with MEMS devices.