问题
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?
}
}
回答1:
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
回答2:
On newer iPhones it is indeed possible to get relative altitude from the included barometer (available from iphone 6). Core Motion has been extended to accomodate this.
CMAltimeter
- provides delivery of change-of-altitude events.
CMAltitudeData
- encapsulates change-of-altitude events (to metre-level resolution).
This kind of thing should work...
if CMAltimeter.isRelativeAltitudeAvailable() {
altimeter.startRelativeAltitudeUpdatesToQueue(altimeterQueue,
withHandler:
{ ( data:CMAltitudeData?,
error: NSError?) in
if let altitudeData = data {
self.timeStamp = altitudeData.timestamp
self.relativeAltitude = altitudeData.relativeAltitude
}
}
)
}
We don't get absolute height, just relative height change. Initial height is considered to be 0. So you can't really get a percentage change (wouldn't make any sense), but you can get absolute change in metre-scale accuracy.
来源:https://stackoverflow.com/questions/36038491/ios-coremotion-calculating-height-difference