我怎样才能找到初始的 CMAttitude 来标准化后续排队的设备运动姿态拉动?

How can I find the initial CMAttitude to normalize subsequent queued device motion attitude pulls?

我有一个简单的 Swift 运动跟踪应用程序,可以提取 phone 的三轴旋转速率和姿态。我正在尝试使用 multiply(byInverseOf: ) 以初始态度作为参考来规范化态度。目前 initialAttitude 返回 0,0,0。在我的 viewController class 中,我有以下内容:

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(如下所示)之前,它为零。

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)]

            }
        }
}

When I try to pull initialAttitude first, before deviceMotionUpdates (like below) it comes by nil.

是的,所以您一直丢弃这些结果,直到您第一次得到 不是 nil.

的结果