I have a simple Swift motion tracking app that pulls the triaxial rotation rate and attitude of the phone. I am trying to normalize the attitude using multiply(byInverseOf: ) with the initial attitude as reference. Currently initialAttitude is coming back 0,0,0. In my viewController class I have the following:
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)]
}
}
}
When I try to pull initialAttitude first, before deviceMotionUpdates (like below) it comes by 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)]
}
}
}
Right, so you just keep throwing away those results until you first get a result that is not
nil
.