Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
7.8 kB
2
Indexable
Never
import 'dart:math' as math;

import '../../../../core/init/enum/angle_enum.dart';
import '../../../../core/init/enum/exercise_code_enum.dart';
import '../../../../core/init/enum/exercise_type_enum.dart';
import '../../../../core/init/enum/points_dic_enum.dart';
import '../../../../core/init/extension/app_extension.dart';
import '../../../audio/text_to_speech/text_to_speech_view_model.dart';
import '../../exercise_plan_view_model.dart';
import '../base_exercise_model.dart';
import '../exercise_plan_model.dart';
import '../point_model.dart';

class StandingSideBend extends BaseExercise {
  StandingSideBend() {
    exerciseTypeEnum = ExerciseTypeEnum.STANDING_SIDE_BEND;
    initialState = false;
    correctExitTime = DateTime.now().millisecondsSinceEpoch;
    upState = false;
    standState = true;
    wrongState = false;
    acc = 0;
    acc1 = 0;
    notSeen = 0;
    notSeen2 = 0;
    currentRepTime = 0;
    paceState = ExerciseCode.S_SP3;
    isVertical = true;
    isRepeated = true;
    repLimits!.add(2500);
    repLimits!.add(4000);
  }
  Map<PointsDicEnum, dynamic>? pointsDic;
  @override
  void exerciseRunner(UpdaterFunctions updaterFunctions, ExercisePlanModel exercisePlanModel, List<PointF> keypoints, List<double> confidenceList) {
    isExerciseTrue = true;
    if (exercisePlanModel.currentStates!.initialState == false) {
      exercisePlanModel.currentStates!.stateCode = ExerciseCode.S1;
      //speech

      TextToSpeech().speak(nextVoiceText: ExerciseCode.S1.getValue());
    }

    pointsDic = pointMap(keypoints, confidenceList, exercisePlanModel, updaterFunctions);

    if (exercisePlanModel.currentStates!.thresholdState == true) {
      if (exercisePlanModel.currentStates!.initialState == true) {
        exercisePlanModel.currentStates!.stateCode = ExerciseCode.CORRECT;
        exercisePlanModel.currentStates!.notSeen = 0;
        if (pointsDic![PointsDicEnum.STANDING_SIDE_BEND_CONDITION] == true) {
          exercisePlanModel.currentStates!.notSeen2 = 0;
          if ((pointsDic![PointsDicEnum.LEFT_HIP_ANGLE] > 190 || pointsDic![PointsDicEnum.RIGHT_HIP_ANGLE] > 190) &&
              pointsDic![PointsDicEnum.RIGHT_SHOULDER_ANGLE] > 230 &&
              pointsDic![PointsDicEnum.LEFT_SHOULDER_ANGLE] > 230 &&
              (pointsDic![PointsDicEnum.RIGHT_ARM_ANGLE] > 130 ||
                  pointsDic![PointsDicEnum.LEFT_ARM_ANGLE] > 130 ||
                  pointsDic![PointsDicEnum.LEFT_ARM_ANGLE] < 240 ||
                  pointsDic![PointsDicEnum.RIGHT_ARM_ANGLE] < 240)) {
            exercisePlanModel.currentStates!.acc = math.min(
                (100 * updaterFunctions.accuracyInterpolation(30, 1, 15, 0.0, (pointsDic![PointsDicEnum.LEFT_HIP_ANGLE] - pointsDic![PointsDicEnum.RIGHT_HIP_ANGLE]).abs())!)
                        .round() /
                    100,
                1);

            exercisePlanModel.currentStates!.accuracyList!.add(exercisePlanModel.currentStates!.acc!);
            exercisePlanModel.currentStates!.upState = true;
          }

          if (pointsDic![PointsDicEnum.LEFT_HIP_ANGLE] > 170 &&
              pointsDic![PointsDicEnum.RIGHT_HIP_ANGLE] > 170 &&
              pointsDic![PointsDicEnum.LEFT_HIP_ANGLE] < 190 &&
              pointsDic![PointsDicEnum.RIGHT_HIP_ANGLE] < 190) {
            if (exercisePlanModel.currentStates!.upState == true) {
              exercisePlanModel.currentStates!.accuracy!.add((exercisePlanModel.currentStates!.accuracyList!).max);

              if (((exercisePlanModel.currentStates!.accuracyList!).max) >= 0.8) {
                exercisePlanModel.currentRepsDone = exercisePlanModel.currentRepsDone! + 1;
              } else if (((exercisePlanModel.currentStates!.accuracyList!).max) >= 0.35) {
                exercisePlanModel.currentStates!.stateCode = ExerciseCode.S_M50;
                //speech

                TextToSpeech().speak(nextVoiceText: ExerciseCode.S_M50.getValue());
              }

              exercisePlanModel.currentStates!.accuracyList = [];
            }

            exercisePlanModel.currentStates!.upState = false;
            exercisePlanModel.currentStates!.acc = 0;
          }
        } else {
          exercisePlanModel.currentStates!.notSeen2 = exercisePlanModel.currentStates!.notSeen2! + 1;
          if (exercisePlanModel.currentStates!.notSeen2 == 50) {
            exercisePlanModel.currentStates!.stateCode = ExerciseCode.S_M48B;
            //speech

            TextToSpeech().speak(nextVoiceText: ExerciseCode.S_M48B.getValue());

            exercisePlanModel.currentStates!.accuracyList = [];
            exercisePlanModel.currentStates!.acc = 0;
            exercisePlanModel.currentStates!.accuracyList!.add(exercisePlanModel.currentStates!.acc!);
          }
        }
      }
    } else {
      exercisePlanModel.currentStates!.notSeen = exercisePlanModel.currentStates!.notSeen! + 1;
      if (exercisePlanModel.currentStates!.notSeen == 50) {
        notSeen = 0;
        exercisePlanModel.currentStates!.stateCode = ExerciseCode.S7;
        //speech

        speechComeBack();
      }
    }
  }

  @override
  Map<PointsDicEnum, dynamic>? pointMap(List<PointF> points, List<double> confidenceList, ExercisePlanModel exercisePlanModel, UpdaterFunctions updaterFunctions) {
    pointsDic = {};
    List<int> indices = [4, 6, 7, 8, 11, 12, 13, 14];
    exercisePlanModel.currentStates!.thresholdState = updaterFunctions.thresholdCheck(confidenceList, indices, exercisePlanModel.minConfidence!);
    if (exercisePlanModel.currentStates!.thresholdState == false) {
      return null;
    }

    if (exercisePlanModel.currentStates!.initialState == false && (points[8].y! < points[6].y!) && (points[6].y! < points[4].y!)) {
      exercisePlanModel.currentStates!.initialState = true;
    }
    pointsDic![PointsDicEnum.STANDING_SIDE_BEND_CONDITION] = (points[15].y! + points[10].y!) / 2 < (points[11].y! + points[14].y!) / 2 &&
        (points[11].y! + points[14].y!) / 2 < (points[13].y! + points[12].y! + points[7].y!) / 3 &&
        (points[13].y! + points[12].y! + points[7].y!) / 3 < (points[3].y! + points[6].y! + points[2].y!) / 3 &&
        (points[13].y! + points[12].y! + points[7].y!) / 3 < (points[4].y! + points[1].y!) / 2 &&
        (points[4].y! + points[1].y!) / 2 < (points[5].y! + points[0].y!) / 2 &&
        points[8].y! < points[7].y!;
    pointsDic![PointsDicEnum.ELBOWS] = points[14].y! - points[11].y!;
    pointsDic![PointsDicEnum.ELEVEN_Y] = points[11].y!;
    pointsDic![PointsDicEnum.FOURTEEN_Y] = points[14].y!;
    pointsDic![PointsDicEnum.RIGHT_ARM_ELBOW_Y] = (points[10].y! - points[11].y!).abs();
    pointsDic![PointsDicEnum.LEFT_ARM_ELBOW_Y] = (points[14].y! - points[15].y!).abs();
    pointsDic![PointsDicEnum.RIGHT_SHOULDER_Y] = (points[12].y! + points[7].y!) / 2;
    pointsDic![PointsDicEnum.LEFT_SHOULDER_Y] = (points[13].y! + points[7].y!) / 2;
    pointsDic![PointsDicEnum.SHOULDER_ERROR] = (points[14].y! + points[11].y!) / 2 - (points[12].y! + points[7].y! + points[13].y!) / 3;

    pointsDic![PointsDicEnum.RIGHT_SHOULDER_ANGLE] = updaterFunctions.getAngle(points, AngleEnum.RIGHT, AngleEnum.SHOULDER);
    pointsDic![PointsDicEnum.LEFT_SHOULDER_ANGLE] = updaterFunctions.getAngle(points, AngleEnum.LEFT, AngleEnum.SHOULDER);
    pointsDic![PointsDicEnum.RIGHT_ARM_ANGLE] = updaterFunctions.getAngle(points, AngleEnum.RIGHT, AngleEnum.FOREARM);
    pointsDic![PointsDicEnum.LEFT_ARM_ANGLE] = updaterFunctions.getAngle(points, AngleEnum.LEFT, AngleEnum.FOREARM);
    pointsDic![PointsDicEnum.RIGHT_HIP_ANGLE] = updaterFunctions.getAngle(points, AngleEnum.RIGHT, AngleEnum.HIP);
    pointsDic![PointsDicEnum.LEFT_HIP_ANGLE] = updaterFunctions.getAngle(points, AngleEnum.LEFT, AngleEnum.HIP);
    return pointsDic;
  }
}