我需要对三轴磁场、3轴加速度和3轴旋转速度进行采样,所以我使用DeVICEHECT,这是我的代码:

 if manager.deviceMotionAvailable {
        if manager.magnetometerAvailable {
        manager.magnetometerUpdateInterval=0.01
        manager.deviceMotionUpdateInterval = 0.01
             manager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: {(data: CMDeviceMotion?, error: NSError?) -> Void in
         if(standard_time) {
         let data = [
                    "ax":data!.userAcceleration.x,"ay":data!.userAcceleration.y,"az":data!.userAcceleration.z,
                    "gax":data!.gravity.x,"gay":data!.gravity.y,"gaz":data!.gravity.z,
                    "gx":data!.rotationRate.x,"gy":data!.rotationRate.y,"gz":data!.rotationRate.z,
                    "mmx":data!.magneticField.field.x,"mmy":data!.magneticField.field.y,"mmz":data!.magneticField.field.z,
                   "roll":data!.attitude.roll, "pitch":data!.attitude.pitch, "yaw":data!.attitude.yaw]
                 }
                )}
  }
 }

然而,不管我如何努力移动我的iPhone,我得到的三轴磁场的值都是固定的。你能帮帮我吗???

最佳答案

你似乎调用了错误的方法来获取磁强计数据。

    if manager.deviceMotionAvailable {
        if manager.magnetometerAvailable {
            manager.magnetometerUpdateInterval = 0.01
            manager.deviceMotionUpdateInterval = 0.01
            manager.startMagnetometerUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { (data: CMMagnetometerData?, error: NSError?) in
                // Do something with data
                print(data)
            })

        }
    }

关于ios - 为什么磁场场的值固定为零?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38680379/

10-10 10:59