Exemplo n.º 1
0
 void OnDisable()                                   //reset when we disable things
 {
     exerciseList    = new List <KinectExercise>(); //clear out the list from previous use
     _trackingState  = ExerciseTrackingState.idleState;
     _successfulReps = 0;
     _failedReps     = 0;
     _completedSets  = 1;
     _exerciseIndex  = 0;
     _positionIndex  = 0;
 }
Exemplo n.º 2
0
 void OnEnable()
 {
     exerciseList = new List <KinectExercise>(); //clear out the list from previous use
     importUserExercises();                      //get our exercises
     if (_trackingState == ExerciseTrackingState.idleState && exerciseList.Count > 0)
     {
         //move to tracking state when we have exercises to test with
         _trackingState = ExerciseTrackingState.waitForTracking;
     }
 }
Exemplo n.º 3
0
    private IEnumerator waitForNextExercise(float sec)
    {
        while (_trackingState == ExerciseTrackingState.waitBetweenExercises)
        {
            RestTimer.text = "wait";
            RestTimer.gameObject.SetActive(true);
            Debug.Log("waiting");
            yield return(new WaitForSeconds(sec));

            _trackingState = ExerciseTrackingState.waitForTracking;
            RestTimer.text = "GO!";
        }
    }
Exemplo n.º 4
0
    private void Start()
    {
        _defaultRestTime = _restTime;
        RestTimer.text   = "GO!";
        EvalHudText.text = "";
        SetsHudText.text = "Set: " + _completedSets;
        Debug.Log("Started app");
        importUserExercises();
        if (_trackingState == ExerciseTrackingState.idleState && exerciseList.Count > 0)
        {
            //move to tracking state when we have exercises to test with
            _trackingState = ExerciseTrackingState.waitForTracking;
        }

        //Debug.Log ("Doug added line: " + TempExercise.Instance.ExerciseName.ToString ());
    }
Exemplo n.º 5
0
    void Update()
    {
        if (_trackingState == ExerciseTrackingState.waitBetweenExercises)
        {
            StartCoroutine(waitForNextExercise(_restTime));
        }

        if (_trackingState == ExerciseTrackingState.finishedTrackingState)
        {
            Debug.Log("return to previous page");
            //go back to previous ui
            OnDisable();

            evaluationMenuPanel.SetActive(true);
            evaluationPanel.SetActive(false);
        }

        if (BodySourceManager == null)
        {
            return;
        }

        _BodyManager = BodySourceManager.GetComponent <BodySourceManager>();
        if (_BodyManager == null)
        {
            return;
        }

        Kinect.Body[] data = _BodyManager.GetData();
        if (data == null)
        {
            return;
        }

        List <ulong> trackedIds = new List <ulong>();

        foreach (var body in data)
        {
            if (body == null)
            {
                continue;
            }

            if (body.IsTracked)
            {
                trackedIds.Add(body.TrackingId);
            }
        }

        List <ulong> knownIds = new List <ulong>(_Bodies.Keys);

        // First delete untracked bodies
        foreach (ulong trackingId in knownIds)
        {
            if (!trackedIds.Contains(trackingId))
            {
                Destroy(_Bodies[trackingId]);
                _Bodies.Remove(trackingId);
            }
        }

        var currentExerciseTextObj = ExerciseNameText.GetComponent <Text>();

        currentExerciseTextObj.text = exerciseList[_exerciseIndex].GetExerciseName();

        if (_trackingState != ExerciseTrackingState.waitBetweenExercises)
        {
            //RestTimer.gameObject.SetActive(false);//turn off the timer if we aren't using it
        }

        foreach (var body in data)
        {
            if (body == null)
            {
                continue;
            }

            currentExercise = KinectExerciseUtils.GetExerciseFromString(exerciseList[_exerciseIndex].GetExerciseName());
            angleJoints     = KinectExerciseUtils.GetExerciseJoints(currentExercise);
            jointAngles     = KinectExerciseUtils.GetExerciseAngles(currentExercise);
            //as long as we're not finished or idle we track body
            if (body.IsTracked && _trackingState != ExerciseTrackingState.finishedTrackingState && _trackingState != ExerciseTrackingState.idleState && _trackingState != ExerciseTrackingState.waitBetweenExercises)
            {
                _trackingState = ExerciseTrackingState.tracking;
                if (!_Bodies.ContainsKey(body.TrackingId))
                {
                    _Bodies[body.TrackingId] = CreateBodyObject(body.TrackingId);
                }
                RefreshBodyObject(body, _Bodies[body.TrackingId]);
                if (_trackingState == ExerciseTrackingState.tracking)
                {
                    double rightPrimaryAngle   = GetAngleBetweenJoints(body.Joints[angleJoints[0]], body.Joints[angleJoints[1]], body.Joints[angleJoints[2]]);//Vector3.Angle(rightPos, spineBasePos);
                    double rightSecondaryAngle = GetAngleBetweenJoints(body.Joints[angleJoints[6]], body.Joints[angleJoints[7]], body.Joints[angleJoints[8]]);
                    double leftPrimaryAngle    = GetAngleBetweenJoints(body.Joints[angleJoints[3]], body.Joints[angleJoints[4]], body.Joints[angleJoints[5]]);
                    double leftSecondaryAngle  = GetAngleBetweenJoints(body.Joints[angleJoints[9]], body.Joints[angleJoints[10]], body.Joints[angleJoints[11]]);
                    //spine angle should always be constant since we aren't pointing to different ones.
                    double spineAngle = GetAngleBetweenJoints(body.Joints[Kinect.JointType.SpineShoulder], body.Joints[Kinect.JointType.SpineMid], body.Joints[Kinect.JointType.SpineBase]);
                    rightPrimaryAngle   = Math.Round(rightPrimaryAngle);
                    leftPrimaryAngle    = Math.Round(leftPrimaryAngle);
                    leftSecondaryAngle  = Math.Round(leftSecondaryAngle);
                    rightSecondaryAngle = Math.Round(rightSecondaryAngle);
                    spineAngle          = Math.Round(spineAngle);
                    //Debug.Log("Right: " + rightAngle + " Left: " + leftAngle);

                    var evaluationTextView = EvalHudText.GetComponent <Text>();
                    //textView.text = "Right Primary: " + rightPrimaryAngle +"\nRight Secondary: "+rightSecondaryAngle+ "\nLeft Primary: " + leftPrimaryAngle+"\nLeft Secondary: "+leftSecondaryAngle+"\nSpine: "+spineAngle;

                    //angle between hand and hips at resting is 25 - 30
                    //angle btw hand and hips at half rep is 80 - 85
                    //angle btw hand and hips at full rep is 100 - 105
                    //if we go over 105, there's something wrong
                    if (rightPrimaryAngle > jointAngles[0] &&
                        rightPrimaryAngle <jointAngles[1] &&
                                           leftPrimaryAngle> jointAngles[0] &&
                        leftPrimaryAngle <jointAngles[1] &&
                                          rightSecondaryAngle> jointAngles[7] &&
                        rightSecondaryAngle <jointAngles[8] &&
                                             leftSecondaryAngle> jointAngles[7] &&
                        leftSecondaryAngle < jointAngles[8] &&
                        spineAngle > 170 &&
                        spineAngle < 181 &&
                        _positionIndex == 0)
                    {
                        Debug.Log("Ready");
                        _positionIndex          = 1;
                        _lastSuccessfulPosition = ExercisePosition.readyPosition;
                    }
                    if (rightPrimaryAngle > jointAngles[2] &&
                        rightPrimaryAngle <jointAngles[3] &&
                                           leftPrimaryAngle> jointAngles[2] &&
                        leftPrimaryAngle <jointAngles[3] &&
                                          rightSecondaryAngle> jointAngles[9] &&
                        rightSecondaryAngle <jointAngles[10] &&
                                             leftSecondaryAngle> jointAngles[9] &&
                        leftSecondaryAngle < jointAngles[10] &&
                        spineAngle > 170 &&
                        spineAngle < 181 &&
                        _positionIndex == 1)
                    {
                        Debug.Log("Got to halfway first rep");
                        _positionIndex          = 2;
                        _lastSuccessfulPosition = ExercisePosition.halfwayPosition;
                    }
                    if (rightPrimaryAngle > jointAngles[4] &&
                        rightPrimaryAngle <jointAngles[5] &&
                                           leftPrimaryAngle> jointAngles[4] &&
                        leftPrimaryAngle <jointAngles[5] &&
                                          rightSecondaryAngle> jointAngles[11] &&
                        rightSecondaryAngle <jointAngles[12] &&
                                             leftSecondaryAngle> jointAngles[11] &&
                        leftSecondaryAngle < jointAngles[12] &&
                        spineAngle > 170 &&
                        spineAngle < 181 &&
                        _positionIndex == 2)
                    {
                        Debug.Log("Got to end of first rep");
                        _positionIndex          = 3;
                        _lastSuccessfulPosition = ExercisePosition.finalPosition;
                    }
                    if ((rightPrimaryAngle > jointAngles[6] ||
                         leftPrimaryAngle > jointAngles[6] ||
                         rightSecondaryAngle > jointAngles[13] ||
                         leftSecondaryAngle > jointAngles[13]) &&
                        _positionIndex == 3)
                    {
                        Debug.Log("Went too far");
                        _flawedPostureReps += 1;
                        Debug.Log("Flawed Reps: " + _flawedPostureReps);
                        evaluationTextView.text += "Went too far!\n";
                    }
                    //TODO: split up check for flawed reps so we have better feedback
                    if (spineAngle > 180 || spineAngle < 170)
                    {
                        Debug.Log("Something's Wrong with your spine");
                        evaluationTextView.text += "Watch your Spine!\n";
                    }
                    if (rightPrimaryAngle > jointAngles[2] &&
                        rightPrimaryAngle <jointAngles[3] &&
                                           leftPrimaryAngle> jointAngles[2] &&
                        leftPrimaryAngle <jointAngles[3] &&
                                          rightSecondaryAngle> jointAngles[9] &&
                        rightSecondaryAngle <jointAngles[10] &&
                                             leftSecondaryAngle> jointAngles[9] &&
                        leftSecondaryAngle < jointAngles[10] &&
                        spineAngle > 170 &&
                        spineAngle < 181 &&
                        _positionIndex == 3)
                    {
                        Debug.Log("Halfway finished first rep");
                        _positionIndex          = 4;
                        _lastSuccessfulPosition = ExercisePosition.halfwayPosition;
                    }
                    if (rightPrimaryAngle > jointAngles[0] &&
                        rightPrimaryAngle <jointAngles[1] &&
                                           leftPrimaryAngle> jointAngles[0] &&
                        leftPrimaryAngle <jointAngles[1] &&
                                          rightSecondaryAngle> jointAngles[7] &&
                        rightSecondaryAngle <jointAngles[8] &&
                                             leftSecondaryAngle> jointAngles[7] &&
                        leftSecondaryAngle < jointAngles[8] &&
                        spineAngle > 170 &&
                        spineAngle < 181 &&
                        _positionIndex == 4)
                    {
                        Debug.Log("Finished the rep");
                        _lastSuccessfulPosition = ExercisePosition.beforeStart;
                        _positionIndex          = 0;//reset the index for the next rep
                        _successfulReps        += 1;
                        Debug.Log("Reps: " + _successfulReps);
                        var repsText = RepsHudText.GetComponent <Text>();
                        RepsHudText.text        = "Reps: " + _successfulReps;
                        evaluationTextView.text = "";//clear out the text view for the next rep
                    }
                }

                if (_trackingState == ExerciseTrackingState.waitBetweenExercises)
                {
                    //Debug.Log("Waited between exercises");
                    StartCoroutine(waitForNextExercise(_restTime));
                    //_trackingState = ExerciseTrackingState.tracking;
                }

                if (_trackingState == ExerciseTrackingState.finishedTrackingState)
                {
                    Debug.Log("Finished!");
                    //_trackingState = ExerciseTrackingState.idleState;//now that we're done, set back to idle
                }
            }

            //Debug.Log("Current Reps: " + _successfulReps + " Logged Reps " + exerciseList[_exerciseIndex].GetReps());
            //if we're done our reps, proceed
            if (_successfulReps >= exerciseList[_exerciseIndex].GetReps())
            {
                //add a completed set
                _completedSets  += 1;
                _successfulReps  = 0;//clear it the f**k out
                SetsHudText.text = "Set: " + _completedSets;
                //if this is our last set, proceed
                if (_completedSets >= exerciseList[_exerciseIndex].getSets())
                {
                    //check if its our last exercise
                    if (_exerciseIndex < exerciseList.Count - 1)
                    {
                        //if its not go up
                        //then move to waitbetweenexercises state
                        _trackingState = ExerciseTrackingState.waitBetweenExercises;
                        //reset everything
                        _exerciseIndex    += 1;
                        _successfulReps    = 0;
                        _flawedPostureReps = 0;
                        _completedSets     = 1;
                        Debug.Log("wait between exercises");
                        StartCoroutine(waitForNextExercise(_restTime));
                    }
                    else
                    {
                        //otherwise we move to finished state
                        //Debug.Log("Move to finished state");
                        _trackingState = ExerciseTrackingState.finishedTrackingState;
                    }
                }
                else
                {
                    //otherwise we wait a bit and move to next set
                    //set state to waitbetweenexercises
                    _trackingState  = ExerciseTrackingState.waitBetweenExercises;
                    _successfulReps = 0;//reset reps
                    //Debug.Log("wait at last else");
                    StartCoroutine(waitForNextExercise(_restTime));
                }
            }
        }
    }