ErgometerTest.cs 8.4 KB

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