如何找到初始的CMAttitude来规范后续排队的设备运动姿态拉取?

问题描述 投票:0回答:1

我有一个简单的Swift运动跟踪应用,可以拉动手机的三轴旋转速率和姿态。我试图以初始姿态为参考,使用乘法(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)]

            }
        }
}

当我尝试在deviceMotionUpdates之前先拉出initialAttitude(如下图),它的结果是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)]

            }
        }
}
swift core-motion cmattitude
1个回答
0
投票

当我尝试在deviceMotionUpdates之前先拉出initialAttitude(如下图),它的结果是nil。

是的,所以你只是不断地丢弃这些结果,直到你首先得到的结果是 nil.

© www.soinside.com 2019 - 2024. All rights reserved.