measuring displacement with mma7260 accelerometer

hey,

i have an olimex mod-mma7260 and i’ve been trying to measure displacement in an elevator with it. i’m very close, though it’s a little inaccurate. for example, when going up 50 floors i get 171m instead of 165m which is the right distance.

what i’ve been doing so far is getting an average of 100,000 samples on startup.

using the PIT to fire an interrupt every 10ms, and in the interrupt handler what i do is:

  1. get an acceleration sample for z axis.

  2. substract that average of 100,000 sample i calculated on startup from the current sample

  3. if the result is higher than 5ms (millivolts), i calculate the speed with this formula → velocity = previous_velocity + ((previous_acceleration + acceleration) * 0.01 / 2)

  4. then i calculate the displacement using the following formula: displacement = previous_displacement + (0.01 * (previous_velocity+velocity) / 2)

that’s pretty much how i calculate the displacement.

one more problem is that when going downward on the elevator the displacement i calculate is shorter than the one i measure going up.

i’ve attached my main.c, does anyone have a clue where does this inaccuracy come from?

thanks