Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by g-augview · May 07, 2019 at 04:33 AM · rotationquaternionsensor

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

,

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image g-augview · May 09, 2019 at 03:45 AM 0
Share

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.

avatar image jamesvhyde · Jul 23, 2020 at 08:02 PM 0
Share

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.

avatar image g-augview jamesvhyde · Jul 23, 2020 at 09:27 PM 0
Share

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.

avatar image jamesvhyde g-augview · Jul 23, 2020 at 10:30 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges