ErgometerTest.cs 15 KB

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