当iPhone垂直时,CMDeviceMotion偏航值不稳定

Spi*_*eas 12 iphone cocoa-touch sensor ios cmmotionmanager

在iOS原型中,我使用CMDeviceMotion.deviceMotion.yaw和CLHeading.trueHeading的组合来制作响应准确的稳定罗盘标题.当iPhone保持平坦时,这种方法很有效,我有一个指向稳定罗盘标题的图形箭头.

当iPhone在移植模式下保持垂直时,会出现问题.UIDeviceOrientation不断从UIDeviceOrientationFaceDown更改为UIDeviceOrientationFaceUp并返回.这使得偏航值基于音调的微小变化来回跳跃+/- 180度.是否可以将设备锁定在一个方向上,该方向可以提供稳定的偏航值,预测变化时没有毛刺或以其他方式计算陀螺偏航(或在此方向上滚动)?

这个可怜的家伙有同样的问题,没有答案.双点可能的人!:) /sf/ask/732965691/

blk*_*p19 14

我只是在寻找这个问题的答案.它让我心碎了一段时间才看到你发布了这个,但我想也许你或其他人可以从解决方案中受益.

问题是万向节锁定.当俯仰大约90度时,偏航和滚动匹配并且陀螺仪失去一定的自由度.四元数是避免万向节锁定的一种方法,但老实说,我并不想绕过它.相反,我注意到偏航和滚动实际上匹配并且可以简单地求和来解决问题(假设你只关心偏航).

解:

    float yawDegrees = currentAttitude.yaw * (180.0 / M_PI);
    float pitchDegrees = currentAttitude.pitch  * (180.0 / M_PI);
    float rollDegrees = currentAttitude.roll * (180.0 / M_PI);

    double rotationDegrees;
    if(rollDegrees < 0 && yawDegrees < 0) // This is the condition where simply
                                          // summing yawDegrees with rollDegrees
                                          // wouldn't work.
                                          // Suppose yaw = -177 and pitch = -165. 
                                          // rotationDegrees would then be -342, 
                                          // making your rotation angle jump all
                                          // the way around the circle.
    {
        rotationDegrees = 360 - (-1 * (yawDegrees + rollDegrees));
    }
    else
    {
        rotationDegrees = yawDegrees + rollDegrees;
    }

    // Use rotationDegrees with range 0 - 360 to do whatever you want.
Run Code Online (Sandbox Code Playgroud)

我希望这有助于其他人!