ErgometerTest.cs 14 KB

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