ErgometerTest.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. workloadnumber = 1;
  27. MainClient.ComPort.Write("SET PW 25");
  28. }
  29. public void timerTick()
  30. {
  31. switch(currentstate)
  32. {
  33. case state.WARMUP:
  34. if (MainClient.GetLastMeting().Seconds > 30)
  35. {
  36. List<ErgometerLibrary.Meting> last10 = MainClient.Metingen.GetRange(MainClient.Metingen.Count - 10, 10);
  37. int max = FindMaxValue(MainClient.Metingen, x => x.HeartBeat);
  38. int min = FindMaxValue(MainClient.Metingen, x => x.HeartBeat);
  39. if(max - min > 10) //Hartslag niet stabiel
  40. {
  41. return;
  42. }
  43. else
  44. {
  45. currentstate = state.WORKLOAD;
  46. workloadStarted = MainClient.GetLastMeting().Seconds;
  47. Console.WriteLine("Warmup is goed, hartslag stabiel, ga naar workload test");
  48. }
  49. }
  50. break;
  51. case state.WORKLOAD:
  52. if (MainClient.GetLastMeting().Seconds - workloadStarted > 180)
  53. {
  54. //Checken of de heartrate niet groter is dan 75%, anders stoppen
  55. if (workloadHearthbeat > CalculateMaximumHeartRate())
  56. {
  57. currentstate = state.COOLINGDOWN;
  58. }
  59. MainClient.ComPort.Write("PW " + GetWorkloadPower(workloadnumber));
  60. workloadStarted = MainClient.GetLastMeting().Seconds;
  61. workloadHearthbeat = 0;
  62. workloadnumber++;
  63. Console.WriteLine("3:00 gefietst, workload" + workloadnumber + " af, nieuwe waardes maken");
  64. }
  65. else if (MainClient.GetLastMeting().Seconds - workloadStarted > 160 && workloadHearthbeat == 0)
  66. {
  67. List<ErgometerLibrary.Meting> last80 = MainClient.Metingen.GetRange(MainClient.Metingen.Count - 80, 80);
  68. workloadHearthbeat = FindAvergeValue(MainClient.Metingen, x => x.HeartBeat);
  69. Console.WriteLine("2:40 gefiets, gemiddelde harstslag berekenen:" + workloadHearthbeat);
  70. }
  71. break;
  72. case state.COOLINGDOWN:
  73. break;
  74. }
  75. }
  76. // WORKLOAD //
  77. private int GetWorkloadPower(int workload)
  78. {
  79. if (gender == 'V')
  80. {
  81. if (workloadnumber == 1)
  82. {
  83. if (workloadHearthbeat < 80)
  84. return 125;
  85. else if (workloadHearthbeat < 89)
  86. return 100;
  87. else if (workloadHearthbeat < 100)
  88. return 75;
  89. else
  90. return 50;
  91. }
  92. else
  93. {
  94. return MainClient.GetLastMeting().Power + 25;
  95. }
  96. }
  97. else if(gender == 'M')
  98. {
  99. if(workloadnumber == 1)
  100. {
  101. if (workloadHearthbeat < 90)
  102. return 150;
  103. else if (workloadHearthbeat < 105)
  104. return 125;
  105. else
  106. return 100;
  107. }
  108. else if(workloadnumber == 2)
  109. {
  110. if(workloadHearthbeat < 120)
  111. {
  112. if (MainClient.GetLastMeting().Power == 150)
  113. return 225;
  114. else if (MainClient.GetLastMeting().Power == 125)
  115. return 200;
  116. else
  117. return 175;
  118. }
  119. else if(workloadHearthbeat < 135)
  120. {
  121. if (MainClient.GetLastMeting().Power == 150)
  122. return 200;
  123. else if (MainClient.GetLastMeting().Power == 125)
  124. return 175;
  125. else
  126. return 150;
  127. }
  128. else
  129. {
  130. if (MainClient.GetLastMeting().Power == 150)
  131. return 175;
  132. else if (MainClient.GetLastMeting().Power == 125)
  133. return 150;
  134. else
  135. return 125;
  136. }
  137. }
  138. else
  139. {
  140. return MainClient.GetLastMeting().Power + 25;
  141. }
  142. }
  143. return 25;
  144. }
  145. // HELPER FUNCTIONS //
  146. private double CalculateMaximumHeartRate()
  147. {
  148. return 208 - (0.7 * age);
  149. }
  150. public int FindMaxValue<T>(List<T> list, Converter<T, int> projection)
  151. {
  152. if (list.Count == 0)
  153. {
  154. throw new InvalidOperationException("Empty list");
  155. }
  156. int maxValue = int.MinValue;
  157. foreach (T item in list)
  158. {
  159. int value = projection(item);
  160. if (value > maxValue)
  161. {
  162. maxValue = value;
  163. }
  164. }
  165. return maxValue;
  166. }
  167. public int FindMinValue<T>(List<T> list, Converter<T, int> projection)
  168. {
  169. if (list.Count == 0)
  170. {
  171. throw new InvalidOperationException("Empty list");
  172. }
  173. int minValue = int.MaxValue;
  174. foreach (T item in list)
  175. {
  176. int value = projection(item);
  177. if (value < minValue)
  178. {
  179. minValue = value;
  180. }
  181. }
  182. return minValue;
  183. }
  184. public int FindAvergeValue<T>(List<T> list, Converter<T, int> projection)
  185. {
  186. if (list.Count == 0)
  187. {
  188. throw new InvalidOperationException("Empty list");
  189. }
  190. int totalvalue = 0;
  191. foreach (T item in list)
  192. {
  193. totalvalue += projection(item);
  194. }
  195. return totalvalue / list.Count;
  196. }
  197. }
  198. }