- 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, 2020 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
Follow this Question
Related Questions
Reversed axes with quaternions and orientation sensor 0 Answers
Rotate with raw gyro data. 0 Answers
Simple Rotation 1 Answer
make projectiles always hit target 1 Answer
Player model rotates forward with camera 0 Answers