ErgometerTest.cs 14 KB

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