ErgometerTest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 >= 80 && !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 75%, anders stoppen
  118. if (workloadHearthbeat > (CalculateMaximumHeartRate() * 0.70))
  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.");
  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 = 0;
  161. if(failed)
  162. {
  163. MainClient.Client.updateStepsText("De test is mislukt omdat u te vaak heeft afgeweken van 50rpm. Onze excuses voor het ongemak.");
  164. }
  165. else if (workloads.Count > 1)
  166. {
  167. 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()));
  168. }
  169. else
  170. {
  171. MainClient.Client.updateStepsText("Er zijn te weinig workload tests afgenomen om een resultaat te maken. Onze excuses voor het ongemak.");
  172. }
  173. break;
  174. }
  175. }
  176. // WORKLOAD //
  177. private int GetWorkloadPower(int workload)
  178. {
  179. if (gender == 'V')
  180. {
  181. if (workload == 1)
  182. {
  183. if (workloadHearthbeat < 80)
  184. return 125;
  185. else if (workloadHearthbeat < 89)
  186. return 100;
  187. else if (workloadHearthbeat < 100)
  188. return 75;
  189. else
  190. return 50;
  191. }
  192. else
  193. {
  194. return MainClient.GetLastMeting().Power + 25;
  195. }
  196. }
  197. else if(gender == 'M')
  198. {
  199. if(workload == 1)
  200. {
  201. if (workloadHearthbeat < 90)
  202. return 150;
  203. else if (workloadHearthbeat < 105)
  204. return 125;
  205. else
  206. return 100;
  207. }
  208. else if(workload == 2)
  209. {
  210. if(workloadHearthbeat < 120)
  211. {
  212. if (MainClient.GetLastMeting().Power == 150)
  213. return 225;
  214. else if (MainClient.GetLastMeting().Power == 125)
  215. return 200;
  216. else
  217. return 175;
  218. }
  219. else if(workloadHearthbeat < 135)
  220. {
  221. if (MainClient.GetLastMeting().Power == 150)
  222. return 200;
  223. else if (MainClient.GetLastMeting().Power == 125)
  224. return 175;
  225. else
  226. return 150;
  227. }
  228. else
  229. {
  230. if (MainClient.GetLastMeting().Power == 150)
  231. return 175;
  232. else if (MainClient.GetLastMeting().Power == 125)
  233. return 150;
  234. else
  235. return 125;
  236. }
  237. }
  238. else
  239. {
  240. return MainClient.GetLastMeting().Power + 25;
  241. }
  242. }
  243. return 25;
  244. }
  245. // CALCULATOR FUNCTIONS //
  246. public double CalculateVOMax()
  247. {
  248. Workload wa = workloads[workloads.Count - 2];
  249. Workload wb = workloads.Last();
  250. var LmA = 0.32643 + 0.55275 * wa.getKiloponds() + 0.014429 * Math.Pow(wa.getKiloponds(), 2) + 0.00025271 * Math.Pow(wa.getKiloponds(), 3);
  251. var LmB = 0.32643 + 0.55275 * wb.getKiloponds() + 0.014429 * Math.Pow(wb.getKiloponds(), 2) + 0.00025271 * Math.Pow(wb.getKiloponds(), 3);
  252. var VOABS = LmB + ((LmB - LmA) / (wb.heartrate - wa.heartrate)) * (220 - age - wb.heartrate);
  253. return VOABS * 1000 / weight;
  254. }
  255. public double CalculateMET()
  256. {
  257. return CalculateVOMax() / 3.5;
  258. }
  259. public double CalculatePopulationAverage()
  260. {
  261. if(gender == 'M')
  262. return 51.86 - 0.28 * age;
  263. return 41.435 - 0.23 * age;
  264. }
  265. public double CalculateZScore()
  266. {
  267. var dev = (gender == 'M') ? 6 : 5.5;
  268. return (CalculateVOMax() - CalculatePopulationAverage()) / dev;
  269. }
  270. public string CalculateRating()
  271. {
  272. var Zscore = CalculateZScore();
  273. if (Zscore >= 1)
  274. {
  275. return "Geweldig";
  276. }
  277. else if (Zscore < 1 && Zscore >= 0.5)
  278. {
  279. return "Goed";
  280. }
  281. else if (Zscore < 0.5 && Zscore >= -0.5)
  282. {
  283. return "Gemiddeld";
  284. }
  285. else if (Zscore < -0.5 && Zscore >= -1)
  286. {
  287. return "Redelijk";
  288. }
  289. else if (Zscore < -1)
  290. {
  291. return "Slecht";
  292. }
  293. return "Fout";
  294. }
  295. // HELPER FUNCTIONS //
  296. private int GetCurrentWorkload()
  297. {
  298. return workloads.Count + 1;
  299. }
  300. private double CalculateMaximumHeartRate()
  301. {
  302. return 208 - (0.7 * age);
  303. }
  304. public int FindMaxValue<T>(List<T> list, Converter<T, int> projection)
  305. {
  306. if (list.Count == 0)
  307. {
  308. throw new InvalidOperationException("Empty list");
  309. }
  310. int maxValue = int.MinValue;
  311. foreach (T item in list)
  312. {
  313. int value = projection(item);
  314. if (value > maxValue)
  315. {
  316. maxValue = value;
  317. }
  318. }
  319. return maxValue;
  320. }
  321. public int FindMinValue<T>(List<T> list, Converter<T, int> projection)
  322. {
  323. if (list.Count == 0)
  324. {
  325. throw new InvalidOperationException("Empty list");
  326. }
  327. int minValue = int.MaxValue;
  328. foreach (T item in list)
  329. {
  330. int value = projection(item);
  331. if (value < minValue)
  332. {
  333. minValue = value;
  334. }
  335. }
  336. return minValue;
  337. }
  338. public int FindAverageValue<T>(List<T> list, Converter<T, int> projection)
  339. {
  340. if (list.Count == 0)
  341. {
  342. throw new InvalidOperationException("Empty list");
  343. }
  344. int totalvalue = 0;
  345. foreach (T item in list)
  346. {
  347. totalvalue += projection(item);
  348. }
  349. return totalvalue / list.Count;
  350. }
  351. public string NumToText(int num)
  352. {
  353. switch(num)
  354. {
  355. case 1:
  356. return "eerste";
  357. case 2:
  358. return "tweede";
  359. case 3:
  360. return "derde";
  361. case 4:
  362. return "vierde";
  363. case 5:
  364. return "vijfde";
  365. case 6:
  366. return "zesde";
  367. case 7:
  368. return "zevende";
  369. case 8:
  370. return "achste";
  371. case 9:
  372. return "negende";
  373. default:
  374. return "volgende";
  375. }
  376. }
  377. }
  378. }