ErgometerTest.cs 14 KB

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