CMDeviceMotion yaw values unstable when iPhone is vertical

后端 未结 3 1117
悲哀的现实
悲哀的现实 2020-12-31 09:15

In a iOS prototype I use a combination of CMDeviceMotion.deviceMotion.yaw and CLHeading.trueHeading to make stable compass heading that is responsive and accurate. This work

3条回答
  •  情深已故
    2020-12-31 09:31

    I was just searching for an answer to this problem. It broke my heart a bit to see that you posted this over a year ago, but I figured maybe you or someone else could benefit from the solution.

    The issue is gimbal lock. When pitch is about 90 degrees, yaw and roll match up and the gyro loses a degree of freedom. Quaternions are one way of avoiding gimbal lock, but I honestly didn't feel like wrapping my mind around that. Instead, I noticed that yaw and roll actually match up and can simply be summed to to solve the problem (assuming you only care about yaw).

    SOLUTION:

        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.
    

    I hope this helps someone else!

提交回复
热议问题