ErgometerTest.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ErgometerApplication
  7. {
  8. public class ErgometerTest
  9. {
  10. private int weight;
  11. private int length;
  12. private int age;
  13. private char gender;
  14. private enum state {WARMUP, WORKLOAD, COOLINGDOWN};
  15. private state currentstate;
  16. private int stateStarted;
  17. private int stateHearthbeat;
  18. public ErgometerTest(int weight, int length , int age, char gender)
  19. {
  20. this.weight = weight;
  21. this.length = length;
  22. this.age = age;
  23. this.gender = gender;
  24. currentstate = state.WARMUP;
  25. stateStarted = 0;
  26. }
  27. public void timerTick()
  28. {
  29. switch(currentstate)
  30. {
  31. case state.WARMUP:
  32. if (MainClient.Metingen.Last().Seconds > 30)
  33. {
  34. List<ErgometerLibrary.Meting> last10 = MainClient.Metingen.GetRange(MainClient.Metingen.Count - 10, 10);
  35. int max = FindMaxValue(MainClient.Metingen, x => x.HeartBeat);
  36. int min = FindMaxValue(MainClient.Metingen, x => x.HeartBeat);
  37. MainClient.ComPort.Write("SET PW 25");
  38. if(max - min > 20) //Hartslag niet stabiel
  39. {
  40. return;
  41. }
  42. else
  43. {
  44. currentstate = state.WORKLOAD;
  45. stateStarted = MainClient.Metingen.Last().Seconds;
  46. }
  47. }
  48. break;
  49. case state.WORKLOAD:
  50. if (stateStarted - MainClient.Metingen.Last().Seconds > 180)
  51. {
  52. //Bereken nieuwe kracht
  53. //Zet nieuwe kracht
  54. //State started weer op de huidige tijd zetten
  55. //Checken of het niet groter dan 75% is
  56. //stateHeartbeat weer op 0 zetten
  57. }
  58. else if (stateStarted - MainClient.Metingen.Last().Seconds > 160 && stateHearthbeat != 0)
  59. {
  60. List<ErgometerLibrary.Meting> last80 = MainClient.Metingen.GetRange(MainClient.Metingen.Count - 80, 80);
  61. stateHearthbeat = FindAvergeValue(MainClient.Metingen, x => x.HeartBeat);
  62. }
  63. break;
  64. case state.COOLINGDOWN:
  65. break;
  66. }
  67. }
  68. // HELPER FUNCTIONS //
  69. private double CalculateMaximumHeartRate()
  70. {
  71. return 208 - (0.7 * age);
  72. }
  73. public int FindMaxValue<T>(List<T> list, Converter<T, int> projection)
  74. {
  75. if (list.Count == 0)
  76. {
  77. throw new InvalidOperationException("Empty list");
  78. }
  79. int maxValue = int.MinValue;
  80. foreach (T item in list)
  81. {
  82. int value = projection(item);
  83. if (value > maxValue)
  84. {
  85. maxValue = value;
  86. }
  87. }
  88. return maxValue;
  89. }
  90. public int FindMinValue<T>(List<T> list, Converter<T, int> projection)
  91. {
  92. if (list.Count == 0)
  93. {
  94. throw new InvalidOperationException("Empty list");
  95. }
  96. int minValue = int.MaxValue;
  97. foreach (T item in list)
  98. {
  99. int value = projection(item);
  100. if (value < minValue)
  101. {
  102. minValue = value;
  103. }
  104. }
  105. return minValue;
  106. }
  107. public int FindAvergeValue<T>(List<T> list, Converter<T, int> projection)
  108. {
  109. if (list.Count == 0)
  110. {
  111. throw new InvalidOperationException("Empty list");
  112. }
  113. int totalvalue = 0;
  114. foreach (T item in list)
  115. {
  116. totalvalue += projection(item);
  117. }
  118. return totalvalue / list.Count;
  119. }
  120. }
  121. }