У меня есть простое приложение для отслеживания движений Swift, которое определяет скорость трехосного вращения и положение телефона. Я пытаюсь нормализовать отношение, используя multiply (byInverseOf :) с исходным отношением в качестве ссылки. В настоящее время initialAttitude возвращается 0,0,0. В моем классе viewController у меня есть следующее:
var results : [(rotationRateX: Double, rotationRateY: Double, rotationRateZ: Double, pitch: Double, roll: Double, yaw:Double)] = []
var motionManager = CMMotionManager()
var attitudes : [(CMAttitude)] = []
func startCollectingData()
{
motionManager.deviceMotionUpdateInterval = 1.0 / 100
motionManager.startDeviceMotionUpdates(using: CMAttitudeReferenceFrame.xArbitraryZVertical, to: OperationQueue.current!) { (data, error) in
if let myData = data
{
let rotationRateX = (myData.rotationRate.x)
let rotationRateY = (myData.rotationRate.y)
let rotationRateZ = (myData.rotationRate.z)
let attitude = (myData.attitude)
self.attitudes += [(attitude)]
let initialAttitude = self.attitudes.first
attitude.multiply(byInverseOf: initialAttitude!)
let pitch = attitude.pitch
let roll = attitude.roll
let yaw = attitude.yaw
self.results += [(rotationRateX:rotationRateX, rotationRateY:rotationRateY, rotationRateZ:rotationRateZ, pitch:pitch, roll:roll, yaw:yaw)]
}
}
}
Когда я пытаюсь сначала вытащить initialAttitude, перед deviceMotionUpdates (как показано ниже) он приходит на nil.
var results : [(rotationRateX: Double, rotationRateY: Double, rotationRateZ: Double, pitch: Double, roll: Double, yaw:Double)] = []
var motionManager = CMMotionManager()
func startCollectingData()
{
let initialAttitude = motionManager.deviceMotion!.attitude
motionManager.deviceMotionUpdateInterval = 1.0 / 100
motionManager.startDeviceMotionUpdates(using: CMAttitudeReferenceFrame.xArbitraryZVertical, to: OperationQueue.current!) { (data, error) in
if let myData = data
{
let rotationRateX = (myData.rotationRate.x)
let rotationRateY = (myData.rotationRate.y)
let rotationRateZ = (myData.rotationRate.z)
attitude.multiply(byInverseOf: initialAttitude!)
let pitch = attitude.pitch
let roll = attitude.roll
let yaw = attitude.yaw
self.results += [(rotationRateX:rotationRateX, rotationRateY:rotationRateY, rotationRateZ:rotationRateZ, pitch:pitch, roll:roll, yaw:yaw)]
}
}
}