ErgometerTest.cs 14 KB

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