ErgometerTest.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 workloadStarted;
  17. private int workloadHearthbeat;
  18. private int workloadnumber;
  19. public ErgometerTest(int weight, int length , int age, char gender)
  20. {
  21. this.weight = weight;
  22. this.length = length;
  23. this.age = age;
  24. this.gender = gender;
  25. currentstate = state.WARMUP;
  26. MainClient.ComPort.Write("SET PW 25");
  27. }
  28. public void timerTick()
  29. {
  30. switch(currentstate)
  31. {
  32. case state.WARMUP:
  33. if (MainClient.GetLastMeting().Seconds > 30)
  34. {
  35. List<ErgometerLibrary.Meting> last10 = MainClient.Metingen.GetRange(MainClient.Metingen.Count - 10, 10);
  36. int max = FindMaxValue(MainClient.Metingen, x => x.HeartBeat);
  37. int min = FindMaxValue(MainClient.Metingen, x => x.HeartBeat);
  38. if(max - min > 20) //Hartslag niet stabiel
  39. {
  40. return;
  41. }
  42. else
  43. {
  44. currentstate = state.WORKLOAD;
  45. workloadStarted = MainClient.GetLastMeting().Seconds;
  46. Console.WriteLine("Warmup is goed, hartslag stabiel, ga naar workload test");
  47. }
  48. }
  49. break;
  50. case state.WORKLOAD:
  51. if (MainClient.GetLastMeting().Seconds - workloadStarted > 180)
  52. {
  53. //Checken of de heartrate niet groter is dan 75%, anders stoppen
  54. if(workloadnumber == 0)
  55. {
  56. if(workloadHearthbeat < 80)
  57. MainClient.ComPort.Write("SET PW 125");
  58. else if (workloadHearthbeat < 89)
  59. MainClient.ComPort.Write("SET PW 100");
  60. else if (workloadHearthbeat < 100)
  61. MainClient.ComPort.Write("SET PW 75");
  62. else if (workloadHearthbeat > 100)
  63. MainClient.ComPort.Write("SET PW 50");
  64. }
  65. else
  66. {
  67. MainClient.ComPort.Write((MainClient.GetLastMeting().Power + 25).ToString());
  68. }
  69. workloadStarted = MainClient.GetLastMeting().Seconds;
  70. workloadHearthbeat = 0;
  71. workloadnumber++;
  72. Console.WriteLine("3:00 gefietst, workload" + workloadnumber + " af, nieuwe waardes maken");
  73. }
  74. else if (MainClient.GetLastMeting().Seconds - workloadStarted > 160 && workloadHearthbeat == 0)
  75. {
  76. List<ErgometerLibrary.Meting> last80 = MainClient.Metingen.GetRange(MainClient.Metingen.Count - 80, 80);
  77. workloadHearthbeat = FindAvergeValue(MainClient.Metingen, x => x.HeartBeat);
  78. Console.WriteLine("2:40 gefiets, gemiddelde harstslag berekenen:" + workloadHearthbeat);
  79. }
  80. break;
  81. case state.COOLINGDOWN:
  82. break;
  83. }
  84. }
  85. // HELPER FUNCTIONS //
  86. private double CalculateMaximumHeartRate()
  87. {
  88. return 208 - (0.7 * age);
  89. }
  90. public int FindMaxValue<T>(List<T> list, Converter<T, int> projection)
  91. {
  92. if (list.Count == 0)
  93. {
  94. throw new InvalidOperationException("Empty list");
  95. }
  96. int maxValue = int.MinValue;
  97. foreach (T item in list)
  98. {
  99. int value = projection(item);
  100. if (value > maxValue)
  101. {
  102. maxValue = value;
  103. }
  104. }
  105. return maxValue;
  106. }
  107. public int FindMinValue<T>(List<T> list, Converter<T, int> projection)
  108. {
  109. if (list.Count == 0)
  110. {
  111. throw new InvalidOperationException("Empty list");
  112. }
  113. int minValue = int.MaxValue;
  114. foreach (T item in list)
  115. {
  116. int value = projection(item);
  117. if (value < minValue)
  118. {
  119. minValue = value;
  120. }
  121. }
  122. return minValue;
  123. }
  124. public int FindAvergeValue<T>(List<T> list, Converter<T, int> projection)
  125. {
  126. if (list.Count == 0)
  127. {
  128. throw new InvalidOperationException("Empty list");
  129. }
  130. int totalvalue = 0;
  131. foreach (T item in list)
  132. {
  133. totalvalue += projection(item);
  134. }
  135. return totalvalue / list.Count;
  136. }
  137. }
  138. }