Search Unity

Scripting Gyroscope Drift

Discussion in 'AR/VR (XR) Discussion' started by nirv44, Mar 25, 2019.

  1. nirv44

    nirv44

    Joined:
    Feb 16, 2018
    Posts:
    1
    Hello,

    I am developing an AR application.

    In this application I use a "headtracking" that I use on the camera, it allows me to be in a 360 ° environment.

    As I saw on other thread some of you also have a problem of "drift". The problem of "drifting" is only present on certain apparatus.

    In order to limit it, I tried several methods but without concrete results.

    Here are the several cases:


    Code (CSharp):
    1. public Quaternion quat;
    2.  
    3. public Quaternion quatAfter;
    4.  
    5.  
    6. void Start() {
    7.  
    8.     Input.gyro.enabled = true;
    9.  
    10.     quat = new Quaternion(change(Input.gyro.attitude.x), change(Input.gyro.attitude.y), change(Input.gyro.attitude.z), change(Input.gyro.attitude.w));
    11.  
    12.     quatAfter = quat;
    13.  
    14. }
    15.  
    16.  
    17. protected void OnGUI() {
    18.  
    19.     quat.x = change(Input.gyro.attitude.x);
    20.  
    21.     quat.y = change(Input.gyro.attitude.y);
    22.  
    23.     quat.z = change(Input.gyro.attitude.z);
    24.  
    25.     quat.w = change(Input.gyro.attitude.w);
    26.  
    27.  
    28.     if (testIfbouge(quat, quatAfter))
    29.  
    30.     {
    31.  
    32.        this.gameObject.transform.rotation = Quaternion.AngleAxis(90.0f, Vector3.right) * quat * Quaternion.AngleAxis(180.0f, Vector3.forward);
    33.  
    34.     }
    35.  
    36.  
    37.     quatAfter = quat;
    38.  
    39. }
    40.  
    41.  
    42. public bool testIfbouge(Quaternion q, Quaternion qa) {
    43.  
    44.  
    45.     double marge = 0.004;
    46.  
    47.  
    48.     if ((q.x - qa.x) > marge || (qa.x - q.x) > marge) {
    49.  
    50.         return true;
    51.  
    52.     }
    53.  
    54.     else if ((q.y - qa.y) > marge || (qa.y - q.y) > marge) {
    55.  
    56.         return true;
    57.  
    58.     }
    59.  
    60.     else if ((q.z - qa.z) > marge || (qa.z - q.z) > marge) {
    61.  
    62.         return true;
    63.  
    64.     }
    65.  
    66.     else if ((q.w - qa.w) > marge || (qa.w - q.w) > marge) {
    67.  
    68.         return false;   // don't mouve w
    69.  
    70.     }
    71.  
    72.     else {
    73.  
    74.         return false;
    75.  
    76.     }
    77.  
    78.  
    79. }
    80.  
    81.  
    82. public float change(float numb) {
    83.  
    84.     return float.Parse(numb.ToString("0.000"));
    85.  
    86. }



    Code (CSharp):
    1.  
    2.  
    3. public Quaternion quat;
    4.  
    5. public Quaternion quatAcceleration;
    6.  
    7. public Quaternion quatGravity;
    8.  
    9.  
    10. public Quaternion quatAfter;
    11.  
    12. public Quaternion quatAccelerationAfter;
    13.  
    14. public Quaternion quatGravityAfter;
    15.  
    16.  
    17.  
    18. void Start () {
    19.  
    20.     Input.gyro.enabled = true;
    21.  
    22.     quat = new Quaternion(change(Input.gyro.attitude.x), change(Input.gyro.attitude.y), change(Input.gyro.attitude.z), change(Input.gyro.attitude.w));
    23.  
    24.     quatAfter = quat;
    25.  
    26. }
    27.  
    28.  
    29. void Update () {
    30.  
    31.     quat.x = change(Input.gyro.attitude.x);
    32.  
    33.     quat.y = change(Input.gyro.attitude.y);
    34.  
    35.     quat.z = change(Input.gyro.attitude.z);
    36.  
    37.     quat.w = change(Input.gyro.attitude.w);
    38.  
    39.  
    40.     quatAcceleration.x = change(Input.acceleration.x);
    41.  
    42.     quatAcceleration.y = change(Input.acceleration.y);
    43.  
    44.     quatAcceleration.z = change(Input.acceleration.z);
    45.  
    46.  
    47.     quatGravity.x = change(Input.gyro.gravity.x);
    48.  
    49.     quatGravity.y = change(Input.gyro.gravity.y);
    50.  
    51.     quatGravity.z = change(Input.gyro.gravity.z);
    52.  
    53.  
    54.     if (testIfbouge(quat, quatAfter, quatAcceleration, quatAccelerationAfter, quatGravity, quatGravityAfter))
    55.  
    56.     {
    57.  
    58.        this.gameObject.transform.rotation = Quaternion.AngleAxis(90.0f, Vector3.right) * quat * Quaternion.AngleAxis(180.0f, Vector3.forward);
    59.  
    60.     }
    61.  
    62.  
    63.     quatAfter = quat;
    64.  
    65.     quatAccelerationAfter = quatAcceleration;
    66.  
    67.     quatGravityAfter = quatGravity;
    68.  
    69. }
    70.  
    71.  
    72.  
    73.  
    74. public bool testIfbouge(Quaternion q, Quaternion qa, Quaternion qACC, Quaternion qACCa, Quaternion qGrav, Quaternion qGrava)
    75.  
    76. {
    77.  
    78.     double marge = 0.004;
    79.  
    80.     double margeACC = 0.002;
    81.  
    82.     double margeGrav = 0.01;
    83.  
    84.  
    85.     if (((q.x - qa.x) > marge || (qa.x - q.x) > marge) && ((qACC.x - qACCa.x) > margeACC || (qACCa.x - qACC.x) > margeACC) && ((qGrav.x - qGrava.x) > margeGrav || (qGrava.x - qGrav.x) > margeGrav))
    86.  
    87.     {
    88.  
    89.         return true;
    90.  
    91.     }
    92.  
    93.     else if (((q.y - qa.y) > marge || (qa.y - q.y) > marge) && ((qACC.y - qACCa.y) > margeACC || (qACCa.y - qACC.y) > margeACC))
    94.  
    95.     {
    96.  
    97.         return true;
    98.  
    99.     }
    100.  
    101.     else if (((q.z - qa.z) > marge || (qa.z - q.z) > marge) && ((qACC.z - qACCa.z) > margeACC || (qACCa.z - qACC.z) > margeACC))
    102.  
    103.     {
    104.  
    105.         return true;  
    106.  
    107.     }
    108.  
    109.     else if ((q.w - qa.w) > marge || (qa.w - q.w) > marge)
    110.  
    111.     {
    112.  
    113.         return false;   // don’t mouve w
    114.  
    115.     }
    116.  
    117.     else
    118.  
    119.     {
    120.  
    121.         return false;
    122.  
    123.     }
    124.  
    125.  
    126. }
    127.  
    128.  
    129. public float change(float numb)
    130.  
    131. {
    132.  
    133.     return float.Parse(numb.ToString("0.000"));
    134.  
    135. }
    136.  
    137.  
    138.  



    Code (CSharp):
    1.  
    2.  
    3. public Quaternion quatOff;
    4.  
    5.  
    6.  
    7. public Quaternion quat;
    8.  
    9. public Quaternion quatAfter;
    10.  
    11.  
    12. void Start () {
    13.  
    14.     Input.gyro.enabled = true;
    15.  
    16.     quatOff = new Quaternion(change(Input.gyro.attitude.x), change(Input.gyro.attitude.y), change(Input.gyro.attitude.z), change(Input.gyro.attitude.w));
    17.  
    18.     this.gameObject.transform.rotation = Quaternion.AngleAxis(90.0f, Vector3.right) * quatOff * Quaternion.AngleAxis(180.0f, Vector3.forward);
    19.  
    20. }
    21.  
    22.  
    23. protected void OnGUI() {
    24.  
    25.     quatGravity.x = change(Input.gyro.gravity.x);
    26.  
    27.     quatGravity.y = change(Input.gyro.gravity.y);
    28.  
    29.     quatGravity.z = change(Input.gyro.gravity.z);
    30.  
    31.  
    32.     quat.x = change(Input.gyro.attitude.x);
    33.  
    34.     quat.y = change(Input.gyro.attitude.y);
    35.  
    36.     quat.z = change(Input.gyro.attitude.z);
    37.  
    38.     quat.w = change(Input.gyro.attitude.w);
    39.  
    40.  
    41.     testIfbouge(quat, quatAfter, quatGravity, quatGravityAfter);
    42.  
    43.    
    44.  
    45.     quatAfter = quat;
    46.  
    47.     quatGravityAfter = quatGravity;
    48.  
    49. }
    50.  
    51.  
    52.  
    53. public float change(float numb) {
    54.  
    55.     return float.Parse(numb.ToString("0.000"));
    56.  
    57. }
    58.  
    59.  
    60.  
    61. public void testIfbouge(Quaternion qGrav, Quaternion qGrava, Quaternion qGravOFF, Quaternion qGravOFFa) {
    62.  
    63.     double margeGrav = 0.004;
    64.  
    65.     double margeGravFat = 0.05;
    66.  
    67.  
    68.     float xt = (qGrav.x - qGrava.x);
    69.  
    70.     if (xt < 0) xt = xt * -1;
    71.  
    72.     float xt2 = (qGravOFF.x - qGravOFFa.x);
    73.  
    74.     if (xt2 < 0) xt2 = xt2 * -1;
    75.  
    76.  
    77.     if (xt > margeGrav)  {
    78.  
    79.         if (qGrav.x < qGrava.x) {
    80.  
    81.             text2.text = " les X gyro  de gauche a droite";
    82.  
    83.             if(quatOff.x < 1 && quatOff.x > -1) quatOff.x = quatOff.x + xt;
    84.  
    85.  
    86.         } else {
    87.  
    88.             text2.text = " les X gyro  de droite a gauche";
    89.  
    90.             if (quatOff.x < 1 && quatOff.x > -1) quatOff.x = quatOff.x - xt;
    91.  
    92.         }
    93.  
    94.     }
    95.  
    96.  
    97.     float yt = (qGrav.y - qGrava.y);
    98.  
    99.     if (yt < 0) yt = yt * -1;
    100.  
    101.     float yt2 = (qGravOFF.y - qGravOFFa.y);
    102.  
    103.     if (yt2 < 0) yt2 = yt2 * -1;
    104.  
    105.     if (yt > margeGrav) {
    106.  
    107.         if (qGrav.y < qGrava.y) {
    108.  
    109.             if (quatOff.y < 1 && quatOff.y > -1) quatOff.y = quatOff.y + yt;
    110.  
    111.         } else {
    112.  
    113.             if (quatOff.y < 1 && quatOff.y > -1) quatOff.y = quatOff.y - yt;
    114.  
    115.         }
    116.  
    117.     }
    118.  
    119.  
    120.     float zt = (qGrav.z - qGrava.z);
    121.  
    122.     if (zt < 0) zt = zt * -1;
    123.  
    124.     float zt2 = (qGravOFF.z - qGravOFFa.z);
    125.  
    126.     if (zt2 < 0) zt2 = zt2 * -1;
    127.  
    128.     if (zt > margeGravFat)
    129.  
    130.     {
    131.  
    132.         if (qGrav.z < qGrava.z) {
    133.  
    134.             if (quatOff.z < 1 && quatOff.z > -1) quatOff.z = quatOff.z + zt;
    135.  
    136.         } else {
    137.  
    138.             if (quatOff.z < 1 && quatOff.z > -1) quatOff.z = quatOff.z - zt;
    139.  
    140.         }
    141.  
    142.     }
    143.  
    144.  
    145.     float wt = (qGrav.w - qGrava.w);
    146.  
    147.     if (wt < 0) wt = wt * -1;
    148.  
    149.     if (wt > margeGravFat) {
    150.  
    151.         if (qGrav.w < qGrava.w) {
    152.  
    153.             if (quatOff.w < 1 && quatOff.w > -1) quatOff.w = -(quatOff.w + wt);
    154.  
    155.         } else {
    156.  
    157.             text2.text = " les W gyro 2";
    158.  
    159.             if (quatOff.w < 1 && quatOff.w > -1) quatOff.w = -(quatOff.w - wt);
    160.  
    161.         }
    162.  
    163.     }
    164.  
    165.  
    166.     this.gameObject.transform.rotation = Quaternion.AngleAxis(90.0f, Vector3.right) * quatOff * Quaternion.AngleAxis(180.0f, Vector3.forward);
    167.  
    168.  
    169. }



    Code (CSharp):
    1. void Start()
    2.  
    3. {
    4.  
    5.     Input.gyro.enabled = true;
    6.  
    7. }
    8.  
    9.  
    10. protected void OnGUI() {
    11.  
    12.     this.gameObject.transform.Rotate(new Vector3(-Input.gyro.rotationRateUnbiased.x, -Input.gyro.rotationRateUnbiased.y, Input.gyro.rotationRateUnbiased.z));
    13.  
    14. }
    15.  
    16.  

    Thank's
     
  2. sharivary

    sharivary

    Joined:
    Mar 4, 2015
    Posts:
    4