Search Unity

Make script run faster or acceleration integration problems

Discussion in 'Scripting' started by Slaventsiy, Feb 27, 2015.

  1. Slaventsiy

    Slaventsiy

    Joined:
    Feb 27, 2015
    Posts:
    4
    I am trying to implement "dead reckoning" using IMU in my Unity project. Currently I can't get good results: the velocity gets negative, so the character moves backwards, or the character sometimes comes forth and back after moving IMU just forward. I suspect the frequency of script being called is not as high as IMU data sending frequency or my script is incorrect.
    Is there a way to make the method/script called more often? Or is there something I'm doing wrong in the code:
    Code (CSharp):
    1.     /// <summary>
    2.     /// This is the coroutine called in the start Method. It keeps running in the background and keeps track of the user making steps.
    3.     /// </summary>
    4.     /// <returns>WaitForSeconds</returns>
    5.     IEnumerator StepcounterLeft()
    6.     {
    7.         float prev_zVel = 0;
    8.         float prev_zAcc = 0;
    9.        
    10.         for (var a = 0; a < 1;)
    11.         {
    12.             yield return new WaitForSeconds(0.0001f);
    13.  
    14.             float zAcc = (float)vn100l.CurrentMeasurements.LinearAccelerationBody.Z;
    15.             float zVel = prev_zVel + (prev_zAcc + ((zAcc - prev_zAcc)/2.0f)*Time.deltaTime);
    16.             if (zVel > -0.1 && zVel < 0.1)
    17.                 zVel = 0;
    18.             speed = zVel / 10;
    19.             prev_zVel = zVel;
    20.             prev_zAcc = zAcc;
    21.             if ((zAcc > -0.1 && zAcc < 0.1) && (prev_zAcc > -0.1 && prev_zAcc < 0.1))
    22.                 prev_zVel = 0;
    23.         }
    24.     }