Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 nviverosb · Apr 24, 2018 at 12:18 AM · rotationquaternioneuleranglesaccelerometergyroscope

Rotate with raw gyro data.

So I have this code that I get from my phone's raw gyro data:

 public class IMU : MonoBehaviour {
 
     [Range(0,1)]
     public float a;
     public static Vector3 angularVelocity,accel;
     public static double Roll, Pitch, Yaw;
 
     private float LSB = 131;//LEAST SIGNIFICATN BIT
     public double aRoll, aPitch, magX, magY, time;
     private UDPClient server;
     private static Vector3 transition;
 
     // Use this for initialization
     void Start () {
 
         server = FindObjectOfType<UDPClient> ();
         transition  = angularVelocity = accel = Vector3.zero;
         Roll = Pitch = Yaw = 1;
         aRoll = aPitch = 0;
         magX = magY = 0;
         time = 0;
     }
     
     // Update is called once per frame
     void Update () {
 
         double t = Time.time;
         if(server.IsActive()){
 
             Vector3 m = Transmitter.magnetometer.normalized;
             angularVelocity = Transmitter.gyroscope/131;
             accel = Transmitter.accelerometer;
 
 
             aRoll = Math.Atan2 (accel.x,accel.z);
             aPitch = Math.Atan2 (accel.y,accel.z);
 //            aRoll = Math.Atan(accel.y/(Math.Sqrt(accel.x*accel.x + accel.z*accel.z)));
 //            aPitch = Math.Atan(-accel.x/(Math.Sqrt(accel.y*accel.y + accel.z*accel.z)));
 //            print (aRoll + "  " + aPitch);
 
             if (double.IsNaN(Roll) || double.IsNaN(Pitch)) {
                 Roll = 1;
                 Pitch = 1;
             } 
             else {
                 Roll = a * (angularVelocity.x * time + Roll) + (1 - a) * aRoll * Mathf.Rad2Deg;
                 Pitch = a * (angularVelocity.y * time + Pitch) + (1 - a) * aPitch * Mathf.Rad2Deg;
             }
 
             magX = ( m.z * Math.Sin(Roll) ) - ( m.y * Math.Cos(Roll) );
             magY = ( m.x * Math.Cos (Pitch) ) + ( m.y*Math.Sin(Pitch) * Math.Sin(Roll) ) + ( m.z * Math.Sin(Pitch) * Math.Cos(Roll) );
 
             Yaw = Mathf.Rad2Deg * (Math.Atan ( magX/magY ));
 
             print (Roll + " " + Pitch + " " + Yaw);
 
 //            transform.Rotate (new Vector3((float)Roll,(float)Pitch,(float)Yaw));
 //            transform.rotation.eulerAngles = new Vector3 ((float)Roll, (float)Pitch, (float)Yaw);
 //            transition = Vector3.Lerp (transition,new Vector3((float)Roll,(float)Pitch,(float)Yaw),Time.deltaTime*1);
 //            transform.eulerAngles = new Vector3 ((float)Roll, (float)Pitch, (float)Yaw);
 //            transform.rotation = Quaternion.Euler (new Vector3((float)Roll,(float)Pitch,(float)Yaw));
             print(Quaternion.Euler(new Vector3 ((float)Roll, (float)Pitch, (float)Yaw)));
         }
         time = Time.time - t;
     }
 }


and the Roll/Pitch/Yaw angles make sense, but the problem comes when I try to rotate the object using those angles. I'm familiar with the gimbal lock problem that comes with Euler angles, but not familiar with Quaternions though. My object currently rotates like crazy, just spinning very rapidly. Is there ay way I can rotate my object with those angles?

  • I can't use gyro.attitude, since I am not actually running the app on the phone, I'm just sending data using an UDP stream.

Any suggestions on how to calculate Roll/Pitch/Yaw from raw gyro/accelerometer data?

Thank you very much in advance.

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

111 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

Related Questions

Clarifying the ambiguities of rotation in Unity 1 Answer

How to handler rotation of a Gameobject while aligning it with normals 1 Answer

Object makes random jumps in rotation 1 Answer

How do I clamp the Z-Axis Rotation for this code? 1 Answer

How do I make create a rotateable object that follows a raycast hit point? 2 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