Search code examples
ioscore-motion

iOS CoreMotion - Calculating Height Difference


Is it possible to calculate the difference in height when an iPhone moves using the data from CoreMotion?

For example, the initial position of the iPhone is saved, then the iPhone is moved which results in a new position. I am interested in detecting if the height of the iPhone moved from its initial position. I don't need absolute units, just a relative value indicating what percentage the height has changed from its original height.

let manager = CMMotionManager()

// Save initial attitude
var initialAttitude = manager.deviceMotion.attitude

if manager.deviceMotionAvailable {
manager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue()) {
    [weak self] (data: CMDeviceMotion!, error: NSError!) in

    // Get the difference between the initial and new attitude
    data.attitude.multiplyByInverseOfAttitude(initialAttitude)

    // Is there a way to calculate the relative change in height?

    }
}

Solution

  • No, this isn't possible. Sorry.

    There is a nice paper explaining why this isn't possible but I' can't find it at the moment. If I find it I'll post a link.

    But yeah, measuring distance moved by just using the accelerometer etc... isn't possible due mainly to noise that is vastly increased by double integration.

    Edit

    Also, I think you're confusing Attitude and Altitude.

    The former is an object describing the orientation of the device using roll, pitch, yaw, etc... The attitude property of the CMDeviceMotion.

    The latter is the height above sea level and isn't available through iOS. The only altitude you can get in iOS is the height a particular point on the earth is above sea-level. But you can't get the device altitude, just the ground's altitude.

    Either way, it's still not possible.

    Edit

    It wasn't a paper. It was a video.

    https://www.youtube.com/watch?v=_q_8d0E3tDk