我需要对3轴磁场、3轴加速度和3轴转速进行采样,所以我使用DeviceMotion,这是我的代码:
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,我得到的3轴磁场值都固定为零。你能帮帮我吗???
您似乎调用了错误的方法来获取磁力计数据。
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)
})
}
}