- Home /

# 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;
}
}
```

,

I believe the error is that it is incorrect to use rotation.eulerAngles.y as the heading value. It is correct as long as the angle with the vertical axis is big enough, but as you look up or down, euler.y does not give the geographic heading anymore... but I don't understand why and I don't know what to use ins$$anonymous$$d.

Where did you get the "convert attitude to camera pose" code? I spent all morning trying to figure out how to do it, and I couldn't get it.

I don't remember exactly but I think I wrote an app that was able to swap axes and also apply an angle to each axis to figure it out. Quite a bit of trial and error.

I see. Well, it worked perfectly, so I appreciate you posting it here. For some reason the Tracked Pose Driver component crashes on my Android device, so I had to build the functionality myself.

**Answer** by jamesvhyde
·
Jul 23 at 08:00 PM

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.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Reversed axes with quaternions and orientation sensor 0 Answers

Random.rotationUniform clarification 4 Answers

Rotate a GameObject in the Vector3 direction? 1 Answer

Rotate children (Cards) on a panel (maybe using lerp, or another method?) 1 Answer

Can this be rewitten to control axis locally instead of globally? 1 Answer