【问题标题】:CMDeviceMotion returns 0 values for magnetic fieldCMDeviceMotion 为磁场返回 0 值
【发布时间】:2015-08-30 16:38:32
【问题描述】:

我正在开发具有指南针功能的 iOS 应用程序。我曾尝试使用CMMagnetometerData 更新,它给出未校准但正常的结果。

之后,我尝试获取CMDeviceMotion 更新,结果证明始终以CMMagneticFieldCalibrationAccuracyUncalibrated 的精度提供零磁场。我唯一的设备是 iPad,所以无法在其他设备上进行测试。

可能字段为零,因为传感器未校准,但我找不到任何方法来执行校准。

如何解决?

更新:

建议Here 使用startDeviceMotionUpdatesUsingReferenceFrame:toQueue:withHandler:,但它对我不起作用。

Here 建议将showsDeviceMovementDisplay 设置为true。但是它也不起作用,只是没有弹出校准窗口。

终于,解决了。根据我的观察:

1) 使用 startDeviceMotionUpdatesUsingReferenceFrame:toQueue:withHandler:referenceFrame 不等于 allZerosXArbitraryZVertical

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)")
    }
}

【问题讨论】:

  • 请分享您的代码

标签: ios objective-c swift ipad magnetometer


【解决方案1】:

最后,根据我自己的观察:

1) 使用 startDeviceMotionUpdatesUsingReferenceFrame:toQueue:withHandler:referenceFrame 不等于 allZerosXArbitraryZVertical

2) 将showsDeviceMovementDisplay 设置为true

经过几次准确的零值更新CMMagneticFieldCalibrationAccuracyUncalibrated 后,它将正常化。

【讨论】:

  • 感谢您回答自己的问题。在我的情况下真的帮助了我!
猜你喜欢
  • 1970-01-01
  • 2012-03-22
  • 1970-01-01
  • 2012-06-15
  • 1970-01-01
  • 2020-08-15
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
相关资源
最近更新 更多