我正在尝试使用加速度计测量设备滑动的距离。这就是我的做法:
- (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];
}
结果非常不稳定,范围为实际值的 +/- 30%。我的计算是否正确,如果是,我怎样才能使这段代码更准确?