ErgometerTest.cs 16 KB

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