// Use this for initialization
 void Start()
 {
     currentMode = MotorMode.isTrackingHand;
     enc         = gameObject.GetComponent <EncoderController>();
     serialPort  = gameObject.GetComponent <SerialConnector>();
     serialPort.Connect(portNumber);
     enc.resetCount(InitialStringLength + 1000);
     records   = new List <SpeedRecord>();
     isPulling = false;
 }
    // Update is called once per frame
    void Update()
    {
        switch (currentMode)
        {
        case MotorMode.isTrackingHand:
            break;

        case MotorMode.isShowingResistance:

            float pulledStringLength = -enc.getTotalStringLength();

            SpeedRecord sr;             // = new SpeedRecord();
            sr.speed = -enc.getSpeed(); // pull is negative, but we want positive value here
            if (sr.speed < 0)
            {
                enc.resetCount(0);
            }
            sr.timestamp = Time.fixedTime - timeZero;
            sr.length    = pulledStringLength;

            records.Add(sr);



            //               Debug.Log("Pull Speed " + sr.speed + " Length " + pulledStringLength);

            if (isPulling && pulledStringLength > 0.4f)
            {
                isPulling       = false;
                maxPullingSpeed = 0;
                foreach (SpeedRecord r in records)
                {
                    if (maxPullingSpeed < r.speed)
                    {
                        maxPullingSpeed = r.speed;
                    }
                }

                foreach (SpeedRecord r in records)
                {
                    if (r.length == 0)
                    {
                        continue;
                    }

                    if (r.speed > maxPullingSpeed * 0.5f)
                    {
                        timeStartup = r.timestamp;
                        break;
                    }
                }

                timeTotal = records[records.Count - 1].timestamp;

                //Debug.Log(timeStartup + " | " + timeTotal + " | " + maxPullingSpeed);
            }
            break;

        case MotorMode.isRewinding:
            break;
        }
    }