ErgometerTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. private ClientApplicatie client;
  20. public ErgometerTest(int weight, int length , int age, char gender, ClientApplicatie client)
  21. {
  22. this.weight = weight;
  23. this.length = length;
  24. this.age = age;
  25. this.gender = gender;
  26. currentstate = state.WARMUP;
  27. this.client = client;
  28. client.updateStepsText("U begint nu aan een warmup, probeer een tempo van 50 rpm aan te houden. De test gaat automatisch verder.");
  29. workloads = new List<Workload>();
  30. MainClient.ComPort.Write("PW 25");
  31. }
  32. public void timerTick()
  33. {
  34. switch(currentstate)
  35. {
  36. case state.WARMUP:
  37. if (MainClient.GetLastMeting().Seconds > 30)
  38. {
  39. List<ErgometerLibrary.Meting> last10 = MainClient.Metingen.GetRange(MainClient.Metingen.Count - 10, 10);
  40. int max = FindMaxValue(MainClient.Metingen, x => x.HeartBeat);
  41. int min = FindMaxValue(MainClient.Metingen, x => x.HeartBeat);
  42. if(max - min > 10) //Hartslag niet stabiel
  43. {
  44. client.updateStepsText("Uw hartslag is not niet stabiel, probeer een tempo van 50 rpm aan te houden. De test gaat automatisch verder.");
  45. return;
  46. }
  47. else
  48. {
  49. currentstate = state.WORKLOAD;
  50. workloadStarted = MainClient.GetLastMeting().Seconds;
  51. client.updateStepsText("De warmup is voltooid. U begint nu aan de " + NumToText(GetCurrentWorkload()) + " workload.");
  52. }
  53. }
  54. break;
  55. case state.WORKLOAD:
  56. if (MainClient.GetLastMeting().Seconds - workloadStarted > 180)
  57. {
  58. //Checken of de heartrate niet groter is dan 75%, anders stoppen
  59. if (workloadHearthbeat > CalculateMaximumHeartRate())
  60. {
  61. workloadStarted = MainClient.GetLastMeting().Seconds;
  62. currentstate = state.COOLINGDOWN;
  63. client.updateStepsText("Uw hartslag heeft het kritieke punt bereikt, we beginnen nu aan de cooldown.");
  64. }
  65. int pw = GetWorkloadPower(GetCurrentWorkload());
  66. workloads.Add(new Workload(MainClient.GetLastMeting().Power, workloadHearthbeat));
  67. MainClient.ComPort.Write("PW " + pw);
  68. client.updateStepsText("U heeft de workload afgerond, u begint nu aan de " + NumToText(GetCurrentWorkload()) + " workload. Uw nieuwe weerstand is " + pw + " Watt.");
  69. workloadStarted = MainClient.GetLastMeting().Seconds;
  70. workloadHearthbeat = 0;
  71. Console.WriteLine("3:00 gefietst, workload" + (workloads.Count - 1) + " af, nieuwe waardes maken");
  72. }
  73. else if (MainClient.GetLastMeting().Seconds - workloadStarted > 160 && workloadHearthbeat == 0)
  74. {
  75. List<ErgometerLibrary.Meting> last80 = MainClient.Metingen.GetRange(MainClient.Metingen.Count - 80, 80);
  76. workloadHearthbeat = FindAvergeValue(MainClient.Metingen, x => x.HeartBeat);
  77. Console.WriteLine("2:40 gefiets, gemiddelde harstslag berekenen:" + workloadHearthbeat);
  78. client.updateStepsText("U bent nu met de " + NumToText(GetCurrentWorkload()) + " workload bezig. Uw gemiddelde hartslag is berekend als " + workloadHearthbeat + "bpm.");
  79. }
  80. else if(MainClient.GetLastMeting().Seconds - workloadStarted > 8 && MainClient.GetLastMeting().Seconds - workloadStarted < 10)
  81. {
  82. client.updateStepsText("U bent nu met de " + NumToText(GetCurrentWorkload()) + " workload bezig. De fiets staat nu ingesteld op " + MainClient.GetLastMeting().Power + " Watt");
  83. }
  84. break;
  85. case state.COOLINGDOWN:
  86. MainClient.ComPort.Write("PW 25");
  87. if(MainClient.GetLastMeting().Seconds - workloadStarted > 360)
  88. {
  89. currentstate = state.STOP;
  90. client.updateStepsText("De test is afgelopen.");
  91. }
  92. else if(MainClient.GetLastMeting().Seconds - workloadStarted > 8 && MainClient.GetLastMeting().Seconds - workloadStarted < 10)
  93. {
  94. client.updateStepsText("U bent momenteel met de cooldown bezig.");
  95. }
  96. break;
  97. }
  98. }
  99. // WORKLOAD //
  100. private int GetWorkloadPower(int workload)
  101. {
  102. if (gender == 'V')
  103. {
  104. if (workload == 1)
  105. {
  106. if (workloadHearthbeat < 80)
  107. return 125;
  108. else if (workloadHearthbeat < 89)
  109. return 100;
  110. else if (workloadHearthbeat < 100)
  111. return 75;
  112. else
  113. return 50;
  114. }
  115. else
  116. {
  117. return MainClient.GetLastMeting().Power + 25;
  118. }
  119. }
  120. else if(gender == 'M')
  121. {
  122. if(workload == 1)
  123. {
  124. if (workloadHearthbeat < 90)
  125. return 150;
  126. else if (workloadHearthbeat < 105)
  127. return 125;
  128. else
  129. return 100;
  130. }
  131. else if(workload == 2)
  132. {
  133. if(workloadHearthbeat < 120)
  134. {
  135. if (MainClient.GetLastMeting().Power == 150)
  136. return 225;
  137. else if (MainClient.GetLastMeting().Power == 125)
  138. return 200;
  139. else
  140. return 175;
  141. }
  142. else if(workloadHearthbeat < 135)
  143. {
  144. if (MainClient.GetLastMeting().Power == 150)
  145. return 200;
  146. else if (MainClient.GetLastMeting().Power == 125)
  147. return 175;
  148. else
  149. return 150;
  150. }
  151. else
  152. {
  153. if (MainClient.GetLastMeting().Power == 150)
  154. return 175;
  155. else if (MainClient.GetLastMeting().Power == 125)
  156. return 150;
  157. else
  158. return 125;
  159. }
  160. }
  161. else
  162. {
  163. return MainClient.GetLastMeting().Power + 25;
  164. }
  165. }
  166. return 25;
  167. }
  168. // CALCULATOR FUNCTIONS //
  169. public double CalculateVOMax()
  170. {
  171. Workload wa = workloads[workloads.Count - 2];
  172. Workload wb = workloads.Last();
  173. var LmA = 0.32643 + 0.55275 * wa.getKiloponds() + 0.014429 * Math.Pow(wa.getKiloponds(), 2) + 0.00025271 * Math.Pow(wa.getKiloponds(), 3);
  174. var LmB = 0.32643 + 0.55275 * wb.getKiloponds() + 0.014429 * Math.Pow(wb.getKiloponds(), 2) + 0.00025271 * Math.Pow(wb.getKiloponds(), 3);
  175. var VOABS = LmB + ((LmB - LmA) / (wb.heartrate - wa.heartrate)) * (220 - age - wb.heartrate);
  176. return VOABS * 1000 / weight;
  177. }
  178. // HELPER FUNCTIONS //
  179. private int GetCurrentWorkload()
  180. {
  181. return workloads.Count + 1;
  182. }
  183. private double CalculateMaximumHeartRate()
  184. {
  185. return 208 - (0.7 * age);
  186. }
  187. public int FindMaxValue<T>(List<T> list, Converter<T, int> projection)
  188. {
  189. if (list.Count == 0)
  190. {
  191. throw new InvalidOperationException("Empty list");
  192. }
  193. int maxValue = int.MinValue;
  194. foreach (T item in list)
  195. {
  196. int value = projection(item);
  197. if (value > maxValue)
  198. {
  199. maxValue = value;
  200. }
  201. }
  202. return maxValue;
  203. }
  204. public int FindMinValue<T>(List<T> list, Converter<T, int> projection)
  205. {
  206. if (list.Count == 0)
  207. {
  208. throw new InvalidOperationException("Empty list");
  209. }
  210. int minValue = int.MaxValue;
  211. foreach (T item in list)
  212. {
  213. int value = projection(item);
  214. if (value < minValue)
  215. {
  216. minValue = value;
  217. }
  218. }
  219. return minValue;
  220. }
  221. public int FindAvergeValue<T>(List<T> list, Converter<T, int> projection)
  222. {
  223. if (list.Count == 0)
  224. {
  225. throw new InvalidOperationException("Empty list");
  226. }
  227. int totalvalue = 0;
  228. foreach (T item in list)
  229. {
  230. totalvalue += projection(item);
  231. }
  232. return totalvalue / list.Count;
  233. }
  234. public string NumToText(int num)
  235. {
  236. switch(num)
  237. {
  238. case 1:
  239. return "eerste";
  240. case 2:
  241. return "tweede";
  242. case 3:
  243. return "derde";
  244. case 4:
  245. return "vierde";
  246. case 5:
  247. return "vijfde";
  248. case 6:
  249. return "zesde";
  250. case 7:
  251. return "zevende";
  252. case 8:
  253. return "achste";
  254. case 9:
  255. return "negende";
  256. default:
  257. return "volgende";
  258. }
  259. }
  260. }
  261. }