private void OutputData() { foreach (var repetition in UserDataObject.GetCurrentRepetitionsArray()) { GameObject timeGameObject = Instantiate(timePanel); RepetitionTimePanel currentTimePanel = timeGameObject.GetComponent <RepetitionTimePanel>(); GameObject confidenceGameObject = Instantiate(confidencePanel); RepetitionConfidencePanel currentConfidencePanel = confidenceGameObject.GetComponent <RepetitionConfidencePanel>(); GameObject attemptsGameObject = Instantiate(attemptsPanel); RepetitionAttemptsPanel currentAttemptsPanel = attemptsGameObject.GetComponent <RepetitionAttemptsPanel>(); int repetitionId = Array.IndexOf(UserDataObject.GetCurrentRepetitionsArray(), repetition) + 1; string reptitionText = repetitionId.ToString(); switch (repetitionId) { case 1: reptitionText += "st rep"; break; case 2: reptitionText += "nd rep"; break; case 3: reptitionText += "rd rep"; break; default: reptitionText += "th rep"; break; } currentTimePanel.repetitionText.text = reptitionText; currentTimePanel.timeText.text = repetition.userTime.ToString("F1"); // currentTimePanel.GetComponent<Image>().fillAmount = repetition.userTime / UserDataObject.GetCurrentExerciseLongestRepetitionTime(); imageTimeList.Add(currentTimePanel.GetComponent <Image>()); imageTimeValue.Add(repetition.userTime / UserDataObject.GetCurrentExerciseLongestRepetitionTime()); currentAttemptsPanel.repetitionText.text = reptitionText; currentAttemptsPanel.attemptsText.text = repetition.attempts.ToString(); imageAttemptsList.Add(currentAttemptsPanel.GetComponent <Image>()); imageAttemptsValue.Add((float)repetition.attempts / UserDataObject.GetCurrentExerciseSideHighestAttempt()); currentConfidencePanel.repetitionText.text = reptitionText; currentConfidencePanel.confidenceText.text = Mathf.Round(repetition.confidence).ToString(); // currentConfidencePanel.GetComponent<Image>().fillAmount = repetition.confidence/100; imageConfidenceList.Add(currentConfidencePanel.GetComponent <Image>()); imageConfidenceValue.Add(repetition.confidence / 100); attemptsGameObject.transform.SetParent(attemptsSpacer, false); timeGameObject.transform.SetParent(timeSpacer, false); confidenceGameObject.transform.SetParent(confidenceSpacer, false); } avgTimePanel.GetComponent <AverageTimePanel>().averageTimeText.text = UserDataObject.GetCurrentExerciseAverageRepetitionTime().ToString("F1") + " sec"; avgAttemptsPanel.GetComponent <AverageAttemptsPanel>().avgAttemptsText.text = UserDataObject.GetCurrentExerciseAverageRepetitionAttempts().ToString("F1"); avgConfidencePanel.GetComponent <AverageConfidencePanel>().averageConfidenceText.text = UserDataObject.GetCurrentExerciseAverageRepetitionConfidence().ToString("F1") + " %"; startConfidenceAnim = true; startTimeAnim = true; startAttemptsAnim = true; startAnim = true; }
private void ToggleAndCheckRepetition() { StopTracking(); _minTimeAlreadyReached = false; _bothFeetUp = false; _inStartingPosition = false; textReps.text = "Reps " + _repsIterator + "/" + UserDataObject.GetCurrentRepetitionsArray().Length; Debug.Log("ACCOMPLISHED REPETITION"); Debug.Log("Activate Handcursor"); // if (!CanvasHandCursor.activeSelf) // { // CanvasHandCursor.gameObject.SetActive(true); // _interactionManager.enabled = true; // } // Save the time and confidence for the current repetition _currentRepetition.userTime = _durationManager.GetlatestTimeInSeconds(); _currentRepetition.confidence = _currentRepetitionConfidence / confidenceIterator; _currentRepetition.attempts = attemptsIterator; Debug.Log("Repetition time: " + _currentRepetition.userTime + " || confidence: " + _currentRepetition.confidence); // Toggle current repetition _currentRepetition.accomplished = true; _toggleArray[Array.IndexOf(UserDataObject.GetCurrentRepetitionsArray(), _currentRepetition)].isOn = true; // Check if last repetition if (_currentRepetition == UserDataObject.GetCurrentRepetitionsArray().Last()) { // Exercise and side accomplished _currentExerciseData.accomplished = true; UserDataObject.GetCurrentSide().accomplished = true; // Set values for side attempts, confidence, and time regarding repetitions UserDataObject.GetCurrentSide().userTime = 0.0f; UserDataObject.GetCurrentSide().confidence = 0.0f; UserDataObject.GetCurrentSide().attempts = 0; foreach (var repetition in UserDataObject.GetCurrentRepetitionsArray()) { UserDataObject.GetCurrentSide().userTime += repetition.userTime; UserDataObject.GetCurrentSide().confidence += repetition.confidence; UserDataObject.GetCurrentSide().attempts += repetition.attempts; } // Set the avg usertime and confidence for the current side UserDataObject.GetCurrentSide().userTime /= UserDataObject.GetCurrentRepetitionsArray().Length; UserDataObject.GetCurrentSide().confidence /= UserDataObject.GetCurrentRepetitionsArray().Length; // Set values for exercise attempts, confidence, and time regarding sides sideAccomplishedCounter = 0; _currentExerciseData.confidence = 0.0f; _currentExerciseData.userTime = 0.0f; _currentExerciseData.attempts = 0; foreach (var side in UserDataObject.GetCurrentExercise().sides) { if (side.accomplished) { sideAccomplishedCounter++; _currentExerciseData.confidence += side.confidence; _currentExerciseData.userTime += side.userTime; _currentExerciseData.attempts += side.attempts; } } // Set the avg usertime and confidence for the current exercise _currentExerciseData.userTime /= sideAccomplishedCounter; _currentExerciseData.confidence /= sideAccomplishedCounter; // If all sides accomplished bool allSidesAccomplished = true; SideData nextSide; foreach (var side in _currentExerciseData.sides) { if (side.accomplished == false) { allSidesAccomplished = false; } } if (allSidesAccomplished) { // If last exercise --> accomplish current tier Debug.Log("PlayerPrefs.GetInt(CurrentExerciseId)" + PlayerPrefs.GetInt("CurrentExerciseId")); Debug.Log("UserDataObject.GetCurrentTierErcisesLength()" + UserDataObject.GetCurrentTierErcisesLength()); if (PlayerPrefs.GetInt("CurrentExerciseId") == UserDataObject.GetCurrentTierErcisesLength() - 1) { // todo All exercises accomplished congratulations or so UserDataObject.GetCurrentTier().accomplished = true; // If last tier reached --> do nothing if (PlayerPrefs.GetInt("CurrentTierId") == UserDataObject.GetAllTiers().Count - 1) { } else // If not last tier --> unlock next tier { TierData nextTier = UserDataObject.GetNextTier(); nextTier.isInteractable = true; } } else // If not last exercise --> unlock next exercise { ExerciseData nextExerciseData = UserDataObject.GetNextExercise(); nextExerciseData.isInteractable = true; nextExerciseData.unlocked = 1; // UserDataObject.currentUser.exerciseData[PlayerPrefs.GetInt("CurrentExerciseId") + 1].isInteractable = true; // UserDataObject.currentUser.exerciseData[PlayerPrefs.GetInt("CurrentExerciseId") + 1].unlocked = 1; } } // Save data to user json file SaveCurrentExerciseData(); // Load the summary scene StartCoroutine(loadSummaryScene()); } // If not last repetition --> current repetition is next repetition else { _currentRepetition = UserDataObject.GetCurrentRepetitionsArray()[Array.IndexOf(UserDataObject.GetCurrentRepetitionsArray(), _currentRepetition) + 1]; PlayerPrefs.SetInt("CurrentRepetitionId", Array.IndexOf(UserDataObject.GetCurrentRepetitionsArray(), _currentRepetition)); startTrackingAgain = true; Debug.Log("STARTTRAKCINGAGAIN" + startTrackingAgain); // Save data to user json file SaveCurrentExerciseData(); } Debug.Log("-----------------------"); Debug.Log("TOGGLEANDSAVE"); Debug.Log("-----------------------"); // Reset all variables needed for next repetition _currentRepetitionConfidence = 0; confidenceIterator = 0; attemptsIterator = 0; _durationManager.ResetlatestTimeInSeconds(); _durationManager.ResetProgress(); _firstCheckpoint = false; _secondCheckpoint = false; pufferList.Clear(); if (startTrackingAgain) { StartCoroutine("StartTracking"); // StartCoroutine("RewindAnimateSuccess"); } }
// Use this for initialization void Start() { // ----------------------------------------- // ------------ INITIALIZATIONS ------------ // ----------------------------------------- CanvasHandCursor = GameObject.Find("CanvasHandCursor"); // Disable handcursor in execution mode TODO all disable in testmode, enable in production bodyManager = GameObject.Find("BodyManager"); KinectManager = GameObject.Find("KinectManager"); if (KinectManager == null) { return; } _kinectManager = KinectManager.GetComponent <KinectManager>(); _interactionManager = _kinectManager.GetComponent <InteractionManager>(); if (CanvasHandCursor.activeSelf) { _interactionManager.enabled = false; CanvasHandCursor.gameObject.SetActive(false); } Debug.Log("IsUserDetected: " + _kinectManager.IsUserDetected()); _exerciseExecutionValidationManager = ExerciseExecutionValidationManager.GetComponent <ExerciseExecutionValidationManager>(); // Reference to exercise data of current user // _currentExerciseData = UserDataObject.currentUser.exerciseData[PlayerPrefs.GetInt("CurrentExerciseId")]; _currentExerciseData = UserDataObject.GetCurrentExercise(); _lastExercise = UserDataObject.GetLastTierExercise(); // Duration manager _durationManager = durationManager.GetComponent <DurationManager>(); _minTimeAlreadyReached = false; _repsIterator = 0; _methodCheckedArray = new bool[UserDataObject.GetCurrentChecksArray().Length]; // ----------------------------------------- // ------------- UI COMPONENTS ------------- // ----------------------------------------- // Exercise name titleText.text = UserDataObject.GetCurrentExerciseName().ToUpper(); standingLegText.text = UserDataObject.GetCurrentSide().direction; successPanel = successPanel.GetComponent <CanvasGroup>(); // TODO implement success animation for rep foreach (var check in UserDataObject.GetCurrentChecksArray()) { GameObject gameObjectCheckItem = Instantiate(checkItem); CheckItem currentCheckItem = gameObjectCheckItem.GetComponent <CheckItem>(); currentCheckItem.methodName = check.methodName; currentCheckItem.description.text = check.description; checkItemList.Add(currentCheckItem); gameObjectCheckItem.transform.SetParent(checkSpacer, false); } // Array of Toggles _toggleArray = new Toggle[UserDataObject.GetCurrentRepetitionsArray().Length]; // Create and check toggles for each rep of current exercise foreach (var repetition in UserDataObject.GetCurrentRepetitionsArray()) { GameObject gameObjectToggle = Instantiate(toggle); Toggle currentToggle = gameObjectToggle.GetComponent <Toggle>(); _toggleArray[Array.IndexOf(UserDataObject.GetCurrentRepetitionsArray(), repetition)] = currentToggle; // Check if exercise not already accomplished if (!_currentExerciseData.accomplished) { // look for accomplished reps, check regarding toggles and set current rep if (repetition.accomplished) { _repsIterator += 1; currentToggle.GetComponent <Toggle>().isOn = true; } else if (_currentRepetition == null) { _currentRepetition = repetition; } } else // If exercise already accomplished { // Set first rep as current rep if (_currentRepetition == null) { repetition.attempts = 0; repetition.confidence = 0.0f; repetition.userTime = 0.0f; _currentRepetition = repetition; } } // Append GO to group gameObjectToggle.transform.SetParent(toggleGroup, false); } textReps.text = "Reps " + _repsIterator + "/" + UserDataObject.GetCurrentRepetitionsArray().Length; // Set ID of current repetition PlayerPrefs.SetInt("CurrentRepetitionId", Array.IndexOf(UserDataObject.GetCurrentRepetitionsArray(), _currentRepetition)); // ----------------------------------------- // ---------------- KINECT ----------------- // ----------------------------------------- _bodyManager = bodyManager.GetComponent <BodyManager>(); if (_bodyManager == null) { return; } _kinectSensor = _bodyManager.GetSensor(); _bodies = _bodyManager.GetBodies(); Debug.Log(_bodyManager + " | " + _bodies); // Initialize gesture detector object _gestureDetectorList = new List <GestureDetector>(); for (int bodyIndex = 0; bodyIndex < _bodies.Length; bodyIndex++) { _gestureDetectorList.Add(new GestureDetector(_kinectSensor)); } // Foot joints for getting positions _jointFootRight = KinectInterop.JointType.FootRight; _jointFootLeft = KinectInterop.JointType.FootLeft; _startingHeightDifference = UserDataObject.GetCurrentExerciseStartingHeightDifference(); heightTestIterator = 0; // Initial foot position if (_kinectManager.IsUserDetected()) { long userId = _kinectManager.GetPrimaryUserID(); if (_kinectManager.IsJointTracked(userId, (int)_jointFootRight) && _kinectManager.IsJointTracked(userId, (int)_jointFootLeft)) { _initialStartingHeightLeft = _kinectManager.GetJointKinectPosition(userId, (int)_jointFootLeft).y; _initialStartingHeightRight = _kinectManager.GetJointKinectPosition(userId, (int)_jointFootRight).y; initialLeftFootText.text += _initialStartingHeightLeft.ToString(); initialRightFootText.text += _initialStartingHeightRight.ToString(); } } _bothFeetUp = false; _inStartingPosition = false; if (UserDataObject.GetCurrentExerciseFileName() == "WalkForward") { _progressMinConfidence = 0.8f; } if (UserDataObject.GetCurrentExerciseFileName() == "BobOne") { _progressMinConfidence = 0.4f; } Debug.Log("_progressMinConfidence: " + _progressMinConfidence); }
private void OnGestureDetected(object sender, GestureEventArgs e, int bodyIndex) { var isDetected = e.IsBodyTrackingIdValid && e.IsGestureDetected; if (_kinectManager && _kinectManager.IsInitialized() && _kinectManager.IsUserDetected()) { long userId = _kinectManager.GetPrimaryUserID(); if (_kinectManager.IsJointTracked(userId, (int)_jointFootRight) && _kinectManager.IsJointTracked(userId, (int)_jointFootLeft)) { float jointPosFootLeftHeight = _kinectManager.GetJointKinectPosition(userId, (int)_jointFootLeft).y; float jointPosFootRightHeight = _kinectManager.GetJointKinectPosition(userId, (int)_jointFootRight).y; float jointPosFootLeftDepth = _kinectManager.GetJointKinectPosition(userId, (int)_jointFootLeft).z; float jointPosFootRightDepth = _kinectManager.GetJointKinectPosition(userId, (int)_jointFootRight).z; // if ((jointPosFootLeftDepth > jointPosFootRightDepth - _footDepthTolerance) && (jointPosFootLeftDepth < jointPosFootRightDepth + _footDepthTolerance) && // (jointPosFootRightDepth > jointPosFootLeftDepth - _footDepthTolerance) && (jointPosFootRightDepth < jointPosFootLeftDepth + _footDepthTolerance)) // { // _inStartingPosition = true; // } // else if (!_bothFeetUp) // { // _inStartingPosition = false; // } _inStartingPosition = true; rightFootHeightText.text = "R Height: " + jointPosFootRightHeight.ToString(); leftFootHeightText.text = "L Height: " + jointPosFootLeftHeight.ToString(); rightFootDepthText.text = "R Depth: " + jointPosFootRightDepth.ToString(); leftFootDepthText.text = "L Depth: " + jointPosFootLeftDepth.ToString(); // Check if in starting position and foot currentFootHeight is between initialFootHeight + difference tolerance if (_inStartingPosition && (jointPosFootLeftHeight > _initialStartingHeightLeft + _startingHeightDifference) && (jointPosFootRightHeight > _initialStartingHeightRight + _startingHeightDifference)) { _bothFeetUp = true; } else { _bothFeetUp = false; } } } // Check positions of joints of current exercise foreach (var check in UserDataObject.GetCurrentChecksArray()) { if (_exerciseExecutionValidationManager.GetMethodToCheck(check)) { checkItemList[Array.IndexOf(UserDataObject.GetCurrentChecksArray(), check)].checkToggle.isOn = true; checkItemList[Array.IndexOf(UserDataObject.GetCurrentChecksArray(), check)].checkImage.sprite = Resources.Load <Sprite>("Images/Toggle"); checkItemList[Array.IndexOf(UserDataObject.GetCurrentChecksArray(), check)].checkImage.color = MainColors.ToggleIsOn(); _methodCheckedArray[Array.IndexOf(UserDataObject.GetCurrentChecksArray(), check)] = true; } else { checkItemList[Array.IndexOf(UserDataObject.GetCurrentChecksArray(), check)].checkToggle.isOn = false; checkItemList[Array.IndexOf(UserDataObject.GetCurrentChecksArray(), check)].checkImage.sprite = Resources.Load <Sprite>("Images/IconNotChecked"); checkItemList[Array.IndexOf(UserDataObject.GetCurrentChecksArray(), check)].checkImage.color = MainColors.White(); _methodCheckedArray[Array.IndexOf(UserDataObject.GetCurrentChecksArray(), check)] = false; _checksPassed = false; } // Debug.Log(_exerciseExecutionValidationManager.GetMethodToCheck(check)); armsUpText.text = _checksPassed.ToString(); } bothFeetUpText.text = "DIFF: " + _startingHeightDifference + " |UP: " + _bothFeetUp + "| INPOS: " + _inStartingPosition; if (_exerciseExecutionValidationManager.LeftFootOnLine() || _exerciseExecutionValidationManager.RightFootOnLine()) { if (CanvasHandCursor.activeSelf) { _interactionManager.enabled = false; CanvasHandCursor.gameObject.SetActive(false); } } // Discrete Gesture tracking if ((e.GestureType == GestureType.Discrete)) { _gestureAccuracy = e.DetectionConfidence; for (int i = 0; i < _methodCheckedArray.Length; i++) { if (_methodCheckedArray[i] == false) { _gestureAccuracy -= 0.1f; } testText.text = _gestureAccuracy.ToString(); } if (_gestureAccuracy > 1.0f) { _gestureAccuracy = 1.0f; } _durationManager.SetProgress(_gestureAccuracy); if (GestureDetected(_gestureAccuracy, 0.4f, 1f) && _bothFeetUp) { if (CanvasHandCursor.activeSelf) { _interactionManager.enabled = false; CanvasHandCursor.gameObject.SetActive(false); } _durationManager.StartTimer(); if (MinTimeReached()) { audioSuccess.Play(); if (_currentRepetition == UserDataObject.GetCurrentRepetitionsArray().Last()) { Debug.Log("LASTREP"); successMainText.text = "Exercise finished!"; successSubText.text = "Preparing next exercise"; StartCoroutine("StartLastAnimateSuccess"); } else { StartCoroutine("StartAnimateSuccess"); } } confidenceIterator++; _currentRepetitionConfidence += _gestureAccuracy * 100; // testText.text = "if DISCRETE: " + e.IsGestureDetected.ToString() + " " + _gestureAccuracy; } else { if (_durationManager.IsTimerRunning() && _durationManager.GetlatestTimeInSeconds() <= _currentRepetition.minTime) { audioFail.Play(); Debug.Log("Activate handcursor"); // if (!CanvasHandCursor.activeSelf) // { // CanvasHandCursor.gameObject.SetActive(true); // _interactionManager.enabled = true; // } } if (_durationManager.IsTimerRunning()) { attemptsIterator++; } _durationManager.StopTimer(); // if tracked time is greater than given time of the repetition if (_durationManager.GetlatestTimeInSeconds() >= _currentRepetition.minTime) { _repsIterator += 1; ToggleAndCheckRepetition(); } // testText.text = "else DISCRETE: " + e.IsGestureDetected.ToString() + " " + _gestureAccuracy; } // testText.text = "DISCRETE: " + e.IsGestureDetected.ToString() + " " + e.DetectionConfidence; } else if ((e.GestureType == GestureType.Continuous)) { _gestureAccuracy = e.Progress; for (int i = 0; i < _methodCheckedArray.Length; i++) { if (_methodCheckedArray[i] == false) { _gestureAccuracy -= 0.1f; } testText.text = _gestureAccuracy.ToString(); } if (_gestureAccuracy > 1.0f) { _gestureAccuracy = 1.0f; } _durationManager.SetProgress(_gestureAccuracy); // if (_thirdCheckpoint) // { // _durationManager.StartTimer(); // // confidenceIterator++; // _currentRepetitionConfidence += e.Progress * 100; // // if (e.Progress <= 0.4f) // { // Debug.Log(e.Progress); // if (_durationManager.IsTimerRunning()) // { // _currentRepetition.attempts++; // Debug.Log("CURRENT REPETITION ATTEMPT: " + _currentRepetition.attempts); // // } // // _durationManager.StopTimer(); // // _thirdCheckpoint = false; // // // if tracked time is greater than given time of the repetition // if (_durationManager.GetlatestTimeInSeconds() >= _currentRepetition.minTime) // { // ToggleAndCheckRepetition(); // } // } // } // else // { // if (_secondCheckpoint && GestureDetected(e.Progress, 0.7f, 1f)) // { // _thirdCheckpoint = true; // } // else if (_firstCheckpoint && GestureDetected(e.Progress, 0.4f, 0.7f)) // Debug.Log(_bothFeetUp + " | " + _gestureAccuracy + " | " + e.Progress + " | "+ GestureDetected(_gestureAccuracy, 0.7f, 1f) + " | " + GestureDetected(e.Progress, 0.7f, 1f)); if (GestureDetected(_gestureAccuracy, _progressMinConfidence, 1f) && _bothFeetUp) { if (CanvasHandCursor.activeSelf) { _interactionManager.enabled = false; CanvasHandCursor.gameObject.SetActive(false); } _durationManager.StartTimer(); if (MinTimeReached()) { audioSuccess.Play(); if (_currentRepetition == UserDataObject.GetCurrentRepetitionsArray().Last()) { Debug.Log("LASTREP"); successMainText.text = "Exercise finished!"; successSubText.text = "Preparing next exercise"; StartCoroutine("StartLastAnimateSuccess"); } else { StartCoroutine("StartAnimateSuccess"); } } confidenceIterator++; _currentRepetitionConfidence += _gestureAccuracy * 100; // testText.text = "if CONTINUOUS: " + _gestureAccuracy; } else { if (_durationManager.IsTimerRunning() && _durationManager.GetlatestTimeInSeconds() <= _currentRepetition.minTime) { audioFail.Play(); Debug.Log("Activate handcursor"); // if (!CanvasHandCursor.activeSelf) // { // CanvasHandCursor.gameObject.SetActive(true); // _interactionManager.enabled = true; // } } if (_durationManager.IsTimerRunning()) { attemptsIterator++; Debug.Log("CURRENT REPETITION ATTEMPT: " + attemptsIterator); _durationManager.StopTimer(); } // if tracked time is greater than given time of the repetition if (_durationManager.GetlatestTimeInSeconds() >= _currentRepetition.minTime) { _repsIterator += 1; ToggleAndCheckRepetition(); } // testText.text = "else CONTINUOUS: " + _gestureAccuracy; } // } } }