Quaternions applied to Sensor Fusion: gyro.attitude with compass

This might be strictly a quaternion issue, but for the context I am trying to use Input.gyro.attitude together with Input.compass.trueHeading. I want to be able to get a camera pose that’s oriented relatively to true north. In the code below, both GetQuaternion() and GetQuaternionAbsolute() work fine most of the time, but when I get close to looking exactly downward, GetQuaternionAbsolute() gives very unstable orientation around the world vertical axis (90 or 180deg sudden rotations) although readings from the compass is stable. The main difference between both methods is that GetQuaternion() sets the true heading once initially, then simply applies gyro.attitude whereas GetQuaternionAbsolute() tries to correct gyro.attitude with true north every time. I don’t understand quaternions very well… is there a way to get a stable orientation around the vertical axis? I do want to be able to calculate an “absolute” value every time rather than setting the heading once only at initialization.

using UnityEngine;

public class iOSOrientationTracker : MonoBehaviour
{
    GameObject initialHeading;


    private void Start()
    {
        Input.compensateSensors = true;
        Input.gyro.enabled = false;
        Input.gyro.enabled = true;

        initialHeading = new GameObject("initialHeading");
        initialHeading.transform.parent = transform;
        initialHeading.transform.Rotate(Vector3.up, Input.compass.trueHeading, Space.Self);
    }

    public Quaternion GetQuaternion()
    {
        temp = Input.gyro.attitude;

        //convert attitude to camera pose
        temp.Set(temp.z, temp.w, temp.x, temp.y);
        temp = Quaternion.Euler(-90, 180, 0) * temp;
        return initialHeading.transform.localRotation * temp;
    }

    Quaternion temp = Quaternion.identity, lastPose = Quaternion.identity, lastAttitude = Quaternion.identity;
    public Quaternion GetQuaternionAbsolute()
    {
        temp = Input.gyro.attitude;

        //convert attitude to camera pose
        temp.Set(temp.z, temp.w, temp.x, temp.y);
        temp = Quaternion.Euler(-90, 180, 0) * temp;

        //Get vertical rotation since last frame
        float yRot = temp.eulerAngles.y - lastAttitude.eulerAngles.y;
        if (yRot > 180)
            yRot -= 360;
        else if (yRot <= -180)
            yRot += 360;

        //update lastAttitude
        lastAttitude = temp;

        //apply rotation around Y and correct with compass
        yRot = ((lastPose.eulerAngles.y + yRot) * .9f) + (Input.compass.trueHeading * .1f);
        yRot -= temp.eulerAngles.y;
        if (yRot > 360)
        {
            yRot -= 360;
            if (yRot > 180)
                yRot -= 360;

        }
        else if (yRot <= -180)
        {
            yRot += 360;
            if (yRot <= -360)
                yRot += 360;
        }

        //calculate pose with compass heading
        lastPose = Quaternion.AngleAxis(yRot, Vector3.up) * temp;

        return lastPose;
    }
}

,

Quaternions are completely stable, because they work for any axis. But if you try to force the orientation to be with respect to a particular axis, then the result will be unstable.

When you approach looking downward, and you hit straight downward and continue past the pole, the camera is going upside down. If you are assuming the camera is always “upside up,” then you get unstable behavior as it flips around.