ErgometerTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 > 8 && MainClient.GetLastMeting().Seconds < 10)
  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. //Checken of de heartrate niet groter is dan 75%, anders stoppen
  72. if (workloadHearthbeat > (CalculateMaximumHeartRate() * 0.80))
  73. {
  74. workloadStarted = MainClient.GetLastMeting().Seconds;
  75. currentstate = state.COOLINGDOWN;
  76. MainClient.SwitchTestModeAudio();
  77. client.updateStepsText("Uw hartslag heeft het kritieke punt bereikt, we beginnen nu aan de cooldown.");
  78. }
  79. int pw = GetWorkloadPower(GetCurrentWorkload());
  80. workloads.Add(new Workload(MainClient.GetLastMeting().Power, workloadHearthbeat));
  81. MainClient.ComPort.Write("PW " + pw);
  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. }
  88. else if (MainClient.GetLastMeting().Seconds - workloadStarted > 160 && workloadHearthbeat == 0)
  89. {
  90. List<ErgometerLibrary.Meting> last80 = MainClient.Metingen.GetRange(MainClient.Metingen.Count - 80, 80);
  91. workloadHearthbeat = FindAvergeValue(MainClient.Metingen, x => x.HeartBeat);
  92. Console.WriteLine("2:40 gefiets, gemiddelde harstslag berekenen:" + workloadHearthbeat);
  93. client.updateStepsText("U bent nu met de " + NumToText(GetCurrentWorkload()) + " workload bezig. Uw gemiddelde hartslag is berekend als " + workloadHearthbeat + "bpm.");
  94. }
  95. else if(MainClient.GetLastMeting().Seconds - workloadStarted > 8 && MainClient.GetLastMeting().Seconds - workloadStarted < 10)
  96. {
  97. client.updateStepsText("U bent nu met de " + NumToText(GetCurrentWorkload()) + " workload bezig. De fiets staat nu ingesteld op " + MainClient.GetLastMeting().Power + " Watt");
  98. }
  99. break;
  100. case state.COOLINGDOWN:
  101. MainClient.ComPort.Write("PW 25");
  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 > 8 && MainClient.GetLastMeting().Seconds - workloadStarted < 10)
  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. MainClient.Client.updateStepsText(String.Format("De test is afgelopen. Uw test resultaten zijn: \n VO2MAX: {0} MET: {1} Gemiddelde: {3} \n {4} ", CalculateVOMax(), CalculateMET(), CalculatePopulationAverage(), CalculateRating()));
  117. MainClient.SendNetCommand(new ErgometerLibrary.NetCommand(CalculateVOMax(), CalculateMET(), CalculatePopulationAverage(), CalculateZScore(), CalculateRating(), MainClient.Session));
  118. break;
  119. }
  120. }
  121. // WORKLOAD //
  122. private int GetWorkloadPower(int workload)
  123. {
  124. if (gender == 'V')
  125. {
  126. if (workload == 1)
  127. {
  128. if (workloadHearthbeat < 80)
  129. return 125;
  130. else if (workloadHearthbeat < 89)
  131. return 100;
  132. else if (workloadHearthbeat < 100)
  133. return 75;
  134. else
  135. return 50;
  136. }
  137. else
  138. {
  139. return MainClient.GetLastMeting().Power + 25;
  140. }
  141. }
  142. else if(gender == 'M')
  143. {
  144. if(workload == 1)
  145. {
  146. if (workloadHearthbeat < 90)
  147. return 150;
  148. else if (workloadHearthbeat < 105)
  149. return 125;
  150. else
  151. return 100;
  152. }
  153. else if(workload == 2)
  154. {
  155. if(workloadHearthbeat < 120)
  156. {
  157. if (MainClient.GetLastMeting().Power == 150)
  158. return 225;
  159. else if (MainClient.GetLastMeting().Power == 125)
  160. return 200;
  161. else
  162. return 175;
  163. }
  164. else if(workloadHearthbeat < 135)
  165. {
  166. if (MainClient.GetLastMeting().Power == 150)
  167. return 200;
  168. else if (MainClient.GetLastMeting().Power == 125)
  169. return 175;
  170. else
  171. return 150;
  172. }
  173. else
  174. {
  175. if (MainClient.GetLastMeting().Power == 150)
  176. return 175;
  177. else if (MainClient.GetLastMeting().Power == 125)
  178. return 150;
  179. else
  180. return 125;
  181. }
  182. }
  183. else
  184. {
  185. return MainClient.GetLastMeting().Power + 25;
  186. }
  187. }
  188. return 25;
  189. }
  190. // CALCULATOR FUNCTIONS //
  191. public double CalculateVOMax()
  192. {
  193. Workload wa = workloads[workloads.Count - 2];
  194. Workload wb = workloads.Last();
  195. var LmA = 0.32643 + 0.55275 * wa.getKiloponds() + 0.014429 * Math.Pow(wa.getKiloponds(), 2) + 0.00025271 * Math.Pow(wa.getKiloponds(), 3);
  196. var LmB = 0.32643 + 0.55275 * wb.getKiloponds() + 0.014429 * Math.Pow(wb.getKiloponds(), 2) + 0.00025271 * Math.Pow(wb.getKiloponds(), 3);
  197. var VOABS = LmB + ((LmB - LmA) / (wb.heartrate - wa.heartrate)) * (220 - age - wb.heartrate);
  198. return VOABS * 1000 / weight;
  199. }
  200. public double CalculateMET()
  201. {
  202. return CalculateVOMax() / 3.5;
  203. }
  204. public double CalculatePopulationAverage()
  205. {
  206. if(gender == 'M')
  207. return 51.86 - 0.28 * age;
  208. return 41.435 - 0.23 * age;
  209. }
  210. public double CalculateZScore()
  211. {
  212. var dev = (gender == 'M') ? 6 : 5.5;
  213. return (CalculateVOMax() - CalculatePopulationAverage()) / dev;
  214. }
  215. public string CalculateRating()
  216. {
  217. var Zscore = CalculateZScore();
  218. if (Zscore >= 1)
  219. {
  220. return "Geweldig";
  221. }
  222. else if (Zscore < 1 && Zscore >= 0.5)
  223. {
  224. return "Goed";
  225. }
  226. else if (Zscore < 0.5 && Zscore >= -0.5)
  227. {
  228. return "Gemiddeld";
  229. }
  230. else if (Zscore < -0.5 && Zscore >= -1)
  231. {
  232. return "Redelijk";
  233. }
  234. else if (Zscore < -1)
  235. {
  236. return "Slecht";
  237. }
  238. return "Fout";
  239. }
  240. // HELPER FUNCTIONS //
  241. private int GetCurrentWorkload()
  242. {
  243. return workloads.Count + 1;
  244. }
  245. private double CalculateMaximumHeartRate()
  246. {
  247. return 208 - (0.7 * age);
  248. }
  249. public int FindMaxValue<T>(List<T> list, Converter<T, int> projection)
  250. {
  251. if (list.Count == 0)
  252. {
  253. throw new InvalidOperationException("Empty list");
  254. }
  255. int maxValue = int.MinValue;
  256. foreach (T item in list)
  257. {
  258. int value = projection(item);
  259. if (value > maxValue)
  260. {
  261. maxValue = value;
  262. }
  263. }
  264. return maxValue;
  265. }
  266. public int FindMinValue<T>(List<T> list, Converter<T, int> projection)
  267. {
  268. if (list.Count == 0)
  269. {
  270. throw new InvalidOperationException("Empty list");
  271. }
  272. int minValue = int.MaxValue;
  273. foreach (T item in list)
  274. {
  275. int value = projection(item);
  276. if (value < minValue)
  277. {
  278. minValue = value;
  279. }
  280. }
  281. return minValue;
  282. }
  283. public int FindAvergeValue<T>(List<T> list, Converter<T, int> projection)
  284. {
  285. if (list.Count == 0)
  286. {
  287. throw new InvalidOperationException("Empty list");
  288. }
  289. int totalvalue = 0;
  290. foreach (T item in list)
  291. {
  292. totalvalue += projection(item);
  293. }
  294. return totalvalue / list.Count;
  295. }
  296. public string NumToText(int num)
  297. {
  298. switch(num)
  299. {
  300. case 1:
  301. return "eerste";
  302. case 2:
  303. return "tweede";
  304. case 3:
  305. return "derde";
  306. case 4:
  307. return "vierde";
  308. case 5:
  309. return "vijfde";
  310. case 6:
  311. return "zesde";
  312. case 7:
  313. return "zevende";
  314. case 8:
  315. return "achste";
  316. case 9:
  317. return "negende";
  318. default:
  319. return "volgende";
  320. }
  321. }
  322. }
  323. }