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 /
  • Help Room /
avatar image
0
Question by charlesleoni31 · Feb 28, 2018 at 01:08 PM · unity 5gyroscopearduino

Gyro calibration - Unity + MPU6050

Hi everyone!

I'm a design student and actually working on a prototype of headphones with a gyroscope on it. I'm quite new to Unity, but I manage to find a working script to link a gyroscope MPU 6050 to Unity.

My problem is that I would like to calibrate the gyroscope when I click on play, to reset the gyroscope data (0,0,0) to whatever the position of the gyro is. For example if my gyro is (84,52,14) before I click play, I would like the (84,52,14) to be the new (0,0,0), whatever is the initial position of the gyro, I want it to be the starting point.

Here is the code I'm using:


  • SerialCommUnity (Serial Communication for Unity)

  • Author: Daniel Wilches

  • This work is released under the Creative Commons Attributions license.

  • https://creativecommons.org/licenses/by/2.0/ / / move = 0x0001; left mouse down = 0x0002; left mouse up = 0x0004; right mouse down = 0x0008; right mouse up = 0x0010; middle mouse down = 0x0020; middle mouse up = 0x0040; private const int absolute = 0x8000;

more info about mouse_event https://msdn.microsoft.com/en-us/library/windows/desktop/ms646260(v=vs.85).aspx */

using UnityEngine; using System.Collections; using System.Runtime.InteropServices;

/** Sample for reading using polling by yourself. In case you are fond of that. / public class SampleUserPolling_JustRead : MonoBehaviour { public SerialController serialController;

 public string[] messages;

 public Transform obj;
 public Vector3 oldRot;
 public Vector3 newRot;

 public float differenceX;
 public float differenceY;

 public bool clicking1;
 public bool clicking2;

 [DllImport("user32.dll")]
 static extern void mouse_event (int flag, int x, int y, int data, int extraInfo);

 public void Move(int x, int y){
     mouse_event (0x0001, x, y, 0, 0);
 }

 public void MouseDown(){
     mouse_event (0x0002, 0, 0, 0, 0);
 }

 public void MouseUp(){
     mouse_event (0x0004, 0, 0, 0, 0);
 }

 public void ButtonDown(){
     //Second button code goes here
 }

 public void ButtonUp(){
     //Second button code goes here
 }

 // Initialization
 void Start()
 {
     serialController = GameObject.Find("SerialController").GetComponent<SerialController>();
 }

 // Executed each frame
 void Update()
 {
     string message = serialController.ReadSerialMessage();

     if (message == null)
         return;

     // Check if the message is plain data or a connect/disconnect event.
     if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_CONNECTED))
         Debug.Log("Connection established");
     else if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_DISCONNECTED))
         Debug.Log("Connection attempt failed or disconnection detected");

     messages = message.Split (" " [0]);


     if (messages [3] == "0" && !clicking1) {
         clicking1 = true;
         MouseDown ();
     }
     if (messages [3] == "1" && clicking1) {
         clicking1 = false;
         MouseUp ();
     }

     if (messages [4] == "0" && !clicking2) {
         clicking2 = true;
         ButtonDown ();
     }
     if (messages [4] == "1" && clicking2) {
         clicking2 = false;
         ButtonUp ();
     }

     oldRot = obj.eulerAngles;
     obj.eulerAngles = new Vector3 (-float.Parse (messages [2]), float.Parse (messages [0]), float.Parse (messages [1]));

     newRot = obj.eulerAngles;
     newRot = new Vector3 (newRot.x > 0 ? newRot.x : (360 + newRot.x), newRot.y > 0 ? newRot.y : (360 + newRot.y), newRot.z > 0 ? newRot.z : (360 + newRot.z));

     differenceX = obj.eulerAngles.x - oldRot.x;
     differenceY = obj.eulerAngles.y - oldRot.y;

     if (oldRot.x < 180 && newRot.x > 180)
         differenceX = 360 - differenceX;
     if (oldRot.y < 180 && newRot.y > 180)
         differenceY = 360 - differenceY;

     if (oldRot.x > 180 && newRot.x < 180)
         differenceX = differenceX + 360;
     if (oldRot.y > 180 && newRot.y < 180)
         differenceY = differenceY + 360;

     Move ((int)(differenceY * 23), (int)(differenceX * 23));
 }

}

Thanks in advance if anyone have any idea how to do that!

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

203 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 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

How do I can use Arduino push button trigger unity UI button (on click ()) , no direct active effect? code can function but it had duplicate code and not the result that I want. 0 Answers

Problem with swipe and Gyro 0 Answers

IMU data in void Update() 1 Answer

Gyroscope controller jittering when holding phone still 1 Answer

Hi I am trying to write some data to an Arduino via COM port when my player is hit by a raycast, as simple as possible. Any Help or Code for this would be great thanks ?? 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