ErgometerTest.cs 13 KB

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