3

I'm trying to measure the distance a device slides using the accelerometer. This is how I'm doing it:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    CGFloat interval = [NSDate timeIntervalSinceReferenceDate] - lastTime;

    CGFloat velocity = ((acceleration.y + lastAcceleration) / 2) * interval * 9.8 + lastVelocity;

    totalDistance += velocity * interval;

    lastAcceleration = acceleration.y;
    lastVelocity = velocity;

    lastTime = [NSDate timeIntervalSinceReferenceDate];
}

The results are extremely erratic, ranging +/- 30% of the actual value. Are my calculations correct, and if so, how can I make this code more accurate?

Sergiu Dumitriu
  • 11,455
  • 3
  • 39
  • 62
eric.mitchell
  • 8,817
  • 12
  • 54
  • 92
  • Have you considered that friction along a surface will cause vibrations to change the sensor values as it moves while moving it in the air is a much more smooth transition? What you're trying to do is one of the hardest problems in motion sensing and without a way to re-orient the estimated distance traveled over time will get worse and worse. – FaultyJuggler Sep 10 '12 at 22:27
  • Seems like you are trying to do the [infamous double-integral](http://stackoverflow.com/a/7835988/341970). I am surprised you only have 30% error. – Ali Sep 10 '12 at 22:53
  • Hmmm... so what you're saying is that this isn't possible with the iPhone's current hardware? – eric.mitchell Sep 10 '12 at 23:15

1 Answers1

0

You can get rid of some of the inaccuracies of the hardware by applying a filter on the results.

here is a library to do a fast fourier transform on the values.

http://sourceforge.net/projects/kissfft/

TheThibz
  • 71
  • 5