1

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:

    /// <summary>
    /// This is the coroutine called in the start Method. It keeps running in the background and keeps track of the user making steps.
    /// </summary>
    /// <returns>WaitForSeconds</returns>
    IEnumerator StepcounterLeft()
    {
        float prev_zVel = 0;
        float prev_zAcc = 0;

        for (var a = 0; a < 1;)
        {
            yield return new WaitForSeconds(0.0001f);

            float zAcc = (float)vn100l.CurrentMeasurements.LinearAccelerationBody.Z;
            float zVel = prev_zVel + (prev_zAcc + ((zAcc - prev_zAcc)/2.0f)*Time.deltaTime);
            if (zVel > -0.1 && zVel < 0.1)
                zVel = 0;
            speed = zVel / 10;
            prev_zVel = zVel;
            prev_zAcc = zAcc;
            if ((zAcc > -0.1 && zAcc < 0.1) && (prev_zAcc > -0.1 && prev_zAcc < 0.1))
                prev_zVel = 0;
        }
    }
Slaventsiy
  • 53
  • 4
  • 4
    @ZoomVirus: No, programmers.se does not do code reviews. Don't spread wrong information. On http://codereview.stackexchange.com/ we review **working** code that needs to be improved which doesn't seem to be the case here: this code doesn't work as intended. – Jeroen Vannevel Mar 02 '15 at 14:18
  • 4
    @ZoomVirus This is not on-topic for either Programmers or Code Review. There is a problem that needs to be fixed in this code, which belongs on SO. – Simon Forsberg Mar 02 '15 at 14:18
  • 2
    @ZoomVirus If you'll read the question, there is defined issues that the op has said he needs help with. That does fall under the realm of SO. – CalebB Mar 02 '15 at 14:19
  • @Slaventsiy, If you're wanting it to run more often have you tried using a [Dispatch Timer](https://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer(v=vs.110).aspx)? Also using code blocks "`{...}`" for your if statements may be a good idea for cleaner formatting. – CalebB Mar 02 '15 at 14:26
  • @CalebB, tried using DispatcherTimer, but apparently it's [not compatible with Unity](http://forum.unity3d.com/threads/using-net.305890/#post-1991966) – Slaventsiy Mar 03 '15 at 10:17

1 Answers1

0

Making a new thread through System.Threading provided speed of 0.001 seconds. To read all the values from accelerometer I would need speed of 0.000008 seconds, but seems like this is as fast as it can reach.

Slaventsiy
  • 53
  • 4