我正在开发带有指南针功能的iOS应用程序。我尝试使用
之后,我尝试获取
可能磁场为零是因为传感器未校准,但我找不到任何执行校准的方法。
如何解决这个问题?
更新: 在这里建议使用
最后,问题已解决。根据我的观察:
1)使用
2)将
在几次精度为
CMMagnetometerData
更新,这些更新提供未校准但正常的结果。之后,我尝试获取
CMDeviceMotion
更新,结果始终为零磁场,并且具有CMMagneticFieldCalibrationAccuracyUncalibrated
精度。我只有一个iPad设备,因此无法在其他设备上进行测试。可能磁场为零是因为传感器未校准,但我找不到任何执行校准的方法。
如何解决这个问题?
更新: 在这里建议使用
startDeviceMotionUpdatesUsingReferenceFrame:toQueue:withHandler:
,但对我没有用。
在这里建议将showsDeviceMovementDisplay
设置为true
。但它也没有起作用,校准窗口只是没有弹出。最后,问题已解决。根据我的观察:
1)使用
referenceFrame
不等于allZeros
或XArbitraryZVertical
的startDeviceMotionUpdatesUsingReferenceFrame:toQueue:withHandler:
。2)将
showsDeviceMovementDisplay
设置为true
。在几次精度为
CMMagneticFieldCalibrationAccuracyUncalibrated
的零值更新后,它将被归一化。
代码:
...
motionManager.deviceMotionUpdateInterval = 0.05
motionManager.showsDeviceMovementDisplay = true
motionManager.startDeviceMotionUpdatesUsingReferenceFrame(CMAttitudeReferenceFrame.XArbitraryCorrectedZVertical, toQueue: NSOperationQueue.mainQueue(), withHandler:handleUpdate)
...
private func handleUpdate(data: CMDeviceMotion!, error: NSError!) {
if data != nil {
let field = data.magneticField.field
println("\(field.x), \(field.y), \(field.z)")
}
}