0

I have used the following code to calculate the distance when the device moved from one place to another.Is it correct or not please look at my code.

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    NSDate *now = [NSDate date];
    NSTimeInterval intervalDate = [now timeIntervalSinceDate:now_prev];

    sx = acceleration.x * kFilteringFactor + sx * (1.0 - kFilteringFactor);
    sy = acceleration.y * kFilteringFactor + sy * (1.0 - kFilteringFactor);
    sz = acceleration.z * kFilteringFactor + sz * (1.0 - kFilteringFactor);

    [xLabel setText:[NSString stringWithFormat:@"%.2f",sx]];
    [yLabel setText:[NSString stringWithFormat:@"%.2f",sy]];
    [zLabel setText:[NSString stringWithFormat:@"%.2f",sz]];

    float aValue = sqrtf(sx*sx+sy*sy+sz*sz);
    [gLabel setText:[NSString stringWithFormat:@"%.2f g",aValue]];

    velX += (sx * intervalDate);
    distX += (velX * intervalDate);

    velY += (sy * intervalDate);
    distY += (velY * intervalDate);

    velZ += (sz * intervalDate);
    distZ += (velZ * intervalDate);

    float distance = sqrtf(distX*distX+distY*distY+distZ*distZ);
    [distanceLabel setText:[NSString stringWithFormat:@"%.2f",distance*0.0006213f]];

    now_prev = [now retain];

}
SRI
  • 1,514
  • 21
  • 39
  • You are going to run into problems either way. This kind of 'dead reconing' is horribly inaccurate, not to mention you are not handling the deceleration of the phone either. – Timothy Groote Sep 12 '12 at 12:23
  • I didn't get you.Can you tell me some thing to calculate the distance from UIAccelerationValues? – SRI Sep 12 '12 at 12:27
  • 1
    I don't know why some of the people downvote to the question.If any thing wrong then answer to my question?Downvote is ok,also correct if any thing wrong – SRI Sep 12 '12 at 13:01

1 Answers1

0

There are a lot of reasons why trying to get distance from accelerator data is going to be terribly inefficient. In order to keep track of your initial location, you need to continuously integrate all acceleration values into a vector, and preferably at a very high sampling rate (big gaps in your calculations will lead to inaccuracies)

The method itself isn't terribly hard to figure out (or google for that matter) ; This question explains how to do the maths and pseudocode for such an integration approach

Community
  • 1
  • 1
Timothy Groote
  • 8,614
  • 26
  • 52