ErgometerTest.cs 16 KB

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