ErgometerTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 currentPower;
  17. private int workloadStarted;
  18. private int workloadHearthbeat;
  19. private List<Workload> workloads;
  20. private ClientApplicatie client;
  21. public ErgometerTest(int weight, int length , int age, char gender, ClientApplicatie client)
  22. {
  23. this.weight = weight;
  24. this.length = length;
  25. this.age = age;
  26. this.gender = gender;
  27. currentstate = state.WARMUP;
  28. MainClient.SwitchTestModeAudio();
  29. this.client = client;
  30. client.updateStepsText("U begint nu aan een warmup, probeer een tempo van 50 rpm aan te houden. De test gaat automatisch verder.");
  31. workloads = new List<Workload>();
  32. MainClient.ComPort.Write("PW 25");
  33. MainClient.ComPort.Read();
  34. currentPower = 25;
  35. MainClient.Client.heartBeat.max = (int)CalculateMaximumHeartRate();
  36. }
  37. public void timerTick()
  38. {
  39. if (MainClient.GetLastMeting().Seconds > 5 && currentPower != MainClient.GetLastMeting().Power)
  40. {
  41. MainClient.ComPort.Write("PW " + currentPower);
  42. MainClient.ComPort.Read();
  43. MainClient.QuickBeepAudio();
  44. }
  45. switch(currentstate)
  46. {
  47. case state.WARMUP:
  48. if (MainClient.GetLastMeting().Seconds > 30)
  49. {
  50. List<ErgometerLibrary.Meting> last10 = MainClient.Metingen.GetRange(MainClient.Metingen.Count - 10, 10);
  51. int max = FindMaxValue(last10, x => x.HeartBeat);
  52. int min = FindMinValue(last10, x => x.HeartBeat);
  53. if (max + min < 20)
  54. {
  55. client.updateStepsText("We detecteren geen hartslag. Controleer of de hartslagmeter is verbonden. De test gaat automatisch verder.");
  56. }
  57. else if (max - min > 10) //Hartslag niet stabiel
  58. {
  59. client.updateStepsText("Uw hartslag is niet stabiel, probeer een tempo van 50 rpm aan te houden. De test gaat automatisch verder.");
  60. return;
  61. }
  62. else
  63. {
  64. currentstate = state.WORKLOAD;
  65. MainClient.SwitchTestModeAudio();
  66. workloadStarted = MainClient.GetLastMeting().Seconds;
  67. client.updateStepsText("De warmup is voltooid. De test gaat nu beginnen. U begint nu aan de " + NumToText(GetCurrentWorkload()) + " workload.");
  68. }
  69. }
  70. if (MainClient.GetLastMeting().Seconds > 9 && MainClient.GetLastMeting().Seconds < 11)
  71. {
  72. if (MainClient.GetLastMeting().HeartBeat < 20)
  73. {
  74. client.updateStepsText("We detecteren geen hartslag. Controleer of de hartslagmeter is verbonden.");
  75. }
  76. }
  77. break;
  78. case state.WORKLOAD:
  79. if (MainClient.GetLastMeting().Seconds - workloadStarted > 180)
  80. {
  81. currentPower = GetWorkloadPower(GetCurrentWorkload());
  82. workloads.Add(new Workload(MainClient.GetLastMeting().Power, workloadHearthbeat));
  83. MainClient.ComPort.Write("PW " + currentPower);
  84. MainClient.ComPort.Read();
  85. client.updateStepsText("U heeft de workload afgerond, u begint nu aan de " + NumToText(GetCurrentWorkload()) + " workload. Uw nieuwe weerstand is " + currentPower + " Watt.");
  86. MainClient.SwitchWorkloadAudio();
  87. workloadStarted = MainClient.GetLastMeting().Seconds;
  88. workloadHearthbeat = 0;
  89. Console.WriteLine("3:00 gefietst, workload" + (GetCurrentWorkload()) + " af, nieuwe waardes maken");
  90. //Checken of de heartrate niet groter is dan 75%, anders stoppen
  91. if (workloadHearthbeat > (CalculateMaximumHeartRate() * 0.80))
  92. {
  93. workloadStarted = MainClient.GetLastMeting().Seconds;
  94. currentstate = state.COOLINGDOWN;
  95. MainClient.SwitchTestModeAudio();
  96. client.updateStepsText("Uw hartslag heeft het kritieke punt bereikt, we beginnen nu aan de cooldown.");
  97. MainClient.ComPort.Write("PW 25");
  98. MainClient.ComPort.Read();
  99. currentPower = 25;
  100. }
  101. }
  102. else if (MainClient.GetLastMeting().Seconds - workloadStarted > 160 && workloadHearthbeat == 0)
  103. {
  104. List<ErgometerLibrary.Meting> last80 = MainClient.Metingen.GetRange(MainClient.Metingen.Count - 300, 300);
  105. workloadHearthbeat = FindAverageValue(MainClient.Metingen, x => x.HeartBeat);
  106. Console.WriteLine("2:40 gefiets, gemiddelde harstslag berekenen:" + workloadHearthbeat);
  107. client.updateStepsText("U bent nu met de " + NumToText(GetCurrentWorkload()) + " workload bezig. Uw gemiddelde hartslag is berekend als " + workloadHearthbeat + "bpm.");
  108. }
  109. else if(MainClient.GetLastMeting().Seconds - workloadStarted > 9 && MainClient.GetLastMeting().Seconds - workloadStarted < 11)
  110. {
  111. client.updateStepsText("U bent nu met de " + NumToText(GetCurrentWorkload()) + " workload bezig. De fiets staat nu ingesteld op " + MainClient.GetLastMeting().Power + " Watt");
  112. }
  113. break;
  114. case state.COOLINGDOWN:
  115. if(MainClient.GetLastMeting().Seconds - workloadStarted > 360)
  116. {
  117. currentstate = state.STOP;
  118. MainClient.SwitchTestModeAudio();
  119. client.updateStepsText("De test is afgelopen.");
  120. }
  121. else if(MainClient.GetLastMeting().Seconds - workloadStarted > 9 && MainClient.GetLastMeting().Seconds - workloadStarted < 11)
  122. {
  123. client.updateStepsText("U bent momenteel met de cooldown bezig.");
  124. }
  125. break;
  126. case state.STOP:
  127. MainClient.Client.updateTimer.Stop();
  128. MainClient.Client.beeptimer.Stop();
  129. MainClient.ComPort.Write("RS");
  130. if(workloads.Count > 1)
  131. {
  132. MainClient.Client.updateStepsText(String.Format("De test is afgelopen. Uw test resultaten zijn: \n VO2MAX: {0:0.00} MET: {1:0.00} Gemiddelde: {2:0.00} \n {3} ", CalculateVOMax(), CalculateMET(), CalculatePopulationAverage(), CalculateRating()));
  133. MainClient.SendNetCommand(new ErgometerLibrary.NetCommand(CalculateVOMax(), CalculateMET(), CalculatePopulationAverage(), CalculateZScore(), CalculateRating(), MainClient.Session));
  134. }
  135. else
  136. {
  137. MainClient.Client.updateStepsText("Er zijn te weinig workload tests afgenomen om een resultaat te maken. Onze excuses voor het ongemak.");
  138. }
  139. break;
  140. }
  141. }
  142. // WORKLOAD //
  143. private int GetWorkloadPower(int workload)
  144. {
  145. if (gender == 'V')
  146. {
  147. if (workload == 1)
  148. {
  149. if (workloadHearthbeat < 80)
  150. return 125;
  151. else if (workloadHearthbeat < 89)
  152. return 100;
  153. else if (workloadHearthbeat < 100)
  154. return 75;
  155. else
  156. return 50;
  157. }
  158. else
  159. {
  160. return MainClient.GetLastMeting().Power + 25;
  161. }
  162. }
  163. else if(gender == 'M')
  164. {
  165. if(workload == 1)
  166. {
  167. if (workloadHearthbeat < 90)
  168. return 150;
  169. else if (workloadHearthbeat < 105)
  170. return 125;
  171. else
  172. return 100;
  173. }
  174. else if(workload == 2)
  175. {
  176. if(workloadHearthbeat < 120)
  177. {
  178. if (MainClient.GetLastMeting().Power == 150)
  179. return 225;
  180. else if (MainClient.GetLastMeting().Power == 125)
  181. return 200;
  182. else
  183. return 175;
  184. }
  185. else if(workloadHearthbeat < 135)
  186. {
  187. if (MainClient.GetLastMeting().Power == 150)
  188. return 200;
  189. else if (MainClient.GetLastMeting().Power == 125)
  190. return 175;
  191. else
  192. return 150;
  193. }
  194. else
  195. {
  196. if (MainClient.GetLastMeting().Power == 150)
  197. return 175;
  198. else if (MainClient.GetLastMeting().Power == 125)
  199. return 150;
  200. else
  201. return 125;
  202. }
  203. }
  204. else
  205. {
  206. return MainClient.GetLastMeting().Power + 25;
  207. }
  208. }
  209. return 25;
  210. }
  211. // CALCULATOR FUNCTIONS //
  212. public double CalculateVOMax()
  213. {
  214. Workload wa = workloads[workloads.Count - 2];
  215. Workload wb = workloads.Last();
  216. var LmA = 0.32643 + 0.55275 * wa.getKiloponds() + 0.014429 * Math.Pow(wa.getKiloponds(), 2) + 0.00025271 * Math.Pow(wa.getKiloponds(), 3);
  217. var LmB = 0.32643 + 0.55275 * wb.getKiloponds() + 0.014429 * Math.Pow(wb.getKiloponds(), 2) + 0.00025271 * Math.Pow(wb.getKiloponds(), 3);
  218. var VOABS = LmB + ((LmB - LmA) / (wb.heartrate - wa.heartrate)) * (220 - age - wb.heartrate);
  219. return VOABS * 1000 / weight;
  220. }
  221. public double CalculateMET()
  222. {
  223. return CalculateVOMax() / 3.5;
  224. }
  225. public double CalculatePopulationAverage()
  226. {
  227. if(gender == 'M')
  228. return 51.86 - 0.28 * age;
  229. return 41.435 - 0.23 * age;
  230. }
  231. public double CalculateZScore()
  232. {
  233. var dev = (gender == 'M') ? 6 : 5.5;
  234. return (CalculateVOMax() - CalculatePopulationAverage()) / dev;
  235. }
  236. public string CalculateRating()
  237. {
  238. var Zscore = CalculateZScore();
  239. if (Zscore >= 1)
  240. {
  241. return "Geweldig";
  242. }
  243. else if (Zscore < 1 && Zscore >= 0.5)
  244. {
  245. return "Goed";
  246. }
  247. else if (Zscore < 0.5 && Zscore >= -0.5)
  248. {
  249. return "Gemiddeld";
  250. }
  251. else if (Zscore < -0.5 && Zscore >= -1)
  252. {
  253. return "Redelijk";
  254. }
  255. else if (Zscore < -1)
  256. {
  257. return "Slecht";
  258. }
  259. return "Fout";
  260. }
  261. // HELPER FUNCTIONS //
  262. private int GetCurrentWorkload()
  263. {
  264. return workloads.Count + 1;
  265. }
  266. private double CalculateMaximumHeartRate()
  267. {
  268. return 208 - (0.7 * age);
  269. }
  270. public int FindMaxValue<T>(List<T> list, Converter<T, int> projection)
  271. {
  272. if (list.Count == 0)
  273. {
  274. throw new InvalidOperationException("Empty list");
  275. }
  276. int maxValue = int.MinValue;
  277. foreach (T item in list)
  278. {
  279. int value = projection(item);
  280. if (value > maxValue)
  281. {
  282. maxValue = value;
  283. }
  284. }
  285. return maxValue;
  286. }
  287. public int FindMinValue<T>(List<T> list, Converter<T, int> projection)
  288. {
  289. if (list.Count == 0)
  290. {
  291. throw new InvalidOperationException("Empty list");
  292. }
  293. int minValue = int.MaxValue;
  294. foreach (T item in list)
  295. {
  296. int value = projection(item);
  297. if (value < minValue)
  298. {
  299. minValue = value;
  300. }
  301. }
  302. return minValue;
  303. }
  304. public int FindAverageValue<T>(List<T> list, Converter<T, int> projection)
  305. {
  306. if (list.Count == 0)
  307. {
  308. throw new InvalidOperationException("Empty list");
  309. }
  310. int totalvalue = 0;
  311. foreach (T item in list)
  312. {
  313. totalvalue += projection(item);
  314. }
  315. return totalvalue / list.Count;
  316. }
  317. public string NumToText(int num)
  318. {
  319. switch(num)
  320. {
  321. case 1:
  322. return "eerste";
  323. case 2:
  324. return "tweede";
  325. case 3:
  326. return "derde";
  327. case 4:
  328. return "vierde";
  329. case 5:
  330. return "vijfde";
  331. case 6:
  332. return "zesde";
  333. case 7:
  334. return "zevende";
  335. case 8:
  336. return "achste";
  337. case 9:
  338. return "negende";
  339. default:
  340. return "volgende";
  341. }
  342. }
  343. }
  344. }