ErgometerTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. 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()));
  106. MainClient.SendNetCommand(new ErgometerLibrary.NetCommand(CalculateVOMax(), CalculateMET(), CalculatePopulationAverage(), CalculateZScore(), CalculateRating(), MainClient.Session));
  107. break;
  108. }
  109. }
  110. // WORKLOAD //
  111. private int GetWorkloadPower(int workload)
  112. {
  113. if (gender == 'V')
  114. {
  115. if (workload == 1)
  116. {
  117. if (workloadHearthbeat < 80)
  118. return 125;
  119. else if (workloadHearthbeat < 89)
  120. return 100;
  121. else if (workloadHearthbeat < 100)
  122. return 75;
  123. else
  124. return 50;
  125. }
  126. else
  127. {
  128. return MainClient.GetLastMeting().Power + 25;
  129. }
  130. }
  131. else if(gender == 'M')
  132. {
  133. if(workload == 1)
  134. {
  135. if (workloadHearthbeat < 90)
  136. return 150;
  137. else if (workloadHearthbeat < 105)
  138. return 125;
  139. else
  140. return 100;
  141. }
  142. else if(workload == 2)
  143. {
  144. if(workloadHearthbeat < 120)
  145. {
  146. if (MainClient.GetLastMeting().Power == 150)
  147. return 225;
  148. else if (MainClient.GetLastMeting().Power == 125)
  149. return 200;
  150. else
  151. return 175;
  152. }
  153. else if(workloadHearthbeat < 135)
  154. {
  155. if (MainClient.GetLastMeting().Power == 150)
  156. return 200;
  157. else if (MainClient.GetLastMeting().Power == 125)
  158. return 175;
  159. else
  160. return 150;
  161. }
  162. else
  163. {
  164. if (MainClient.GetLastMeting().Power == 150)
  165. return 175;
  166. else if (MainClient.GetLastMeting().Power == 125)
  167. return 150;
  168. else
  169. return 125;
  170. }
  171. }
  172. else
  173. {
  174. return MainClient.GetLastMeting().Power + 25;
  175. }
  176. }
  177. return 25;
  178. }
  179. // CALCULATOR FUNCTIONS //
  180. public double CalculateVOMax()
  181. {
  182. Workload wa = workloads[workloads.Count - 2];
  183. Workload wb = workloads.Last();
  184. var LmA = 0.32643 + 0.55275 * wa.getKiloponds() + 0.014429 * Math.Pow(wa.getKiloponds(), 2) + 0.00025271 * Math.Pow(wa.getKiloponds(), 3);
  185. var LmB = 0.32643 + 0.55275 * wb.getKiloponds() + 0.014429 * Math.Pow(wb.getKiloponds(), 2) + 0.00025271 * Math.Pow(wb.getKiloponds(), 3);
  186. var VOABS = LmB + ((LmB - LmA) / (wb.heartrate - wa.heartrate)) * (220 - age - wb.heartrate);
  187. return VOABS * 1000 / weight;
  188. }
  189. public double CalculateMET()
  190. {
  191. return CalculateVOMax() / 3.5;
  192. }
  193. public double CalculatePopulationAverage()
  194. {
  195. if(gender == 'M')
  196. return 51.86 - 0.28 * age;
  197. return 41.435 - 0.23 * age;
  198. }
  199. public double CalculateZScore()
  200. {
  201. var dev = (gender == 'M') ? 6 : 5.5;
  202. return (CalculateVOMax() - CalculatePopulationAverage()) / dev;
  203. }
  204. public string CalculateRating()
  205. {
  206. var Zscore = CalculateZScore();
  207. if (Zscore >= 1)
  208. {
  209. return "Geweldig";
  210. }
  211. else if (Zscore < 1 && Zscore >= 0.5)
  212. {
  213. return "Goed";
  214. }
  215. else if (Zscore < 0.5 && Zscore >= -0.5)
  216. {
  217. return "Gemiddeld";
  218. }
  219. else if (Zscore < -0.5 && Zscore >= -1)
  220. {
  221. return "Redelijk";
  222. }
  223. else if (Zscore < -1)
  224. {
  225. return "Slecht";
  226. }
  227. return "Fout";
  228. }
  229. // HELPER FUNCTIONS //
  230. private int GetCurrentWorkload()
  231. {
  232. return workloads.Count + 1;
  233. }
  234. private double CalculateMaximumHeartRate()
  235. {
  236. return 208 - (0.7 * age);
  237. }
  238. public int FindMaxValue<T>(List<T> list, Converter<T, int> projection)
  239. {
  240. if (list.Count == 0)
  241. {
  242. throw new InvalidOperationException("Empty list");
  243. }
  244. int maxValue = int.MinValue;
  245. foreach (T item in list)
  246. {
  247. int value = projection(item);
  248. if (value > maxValue)
  249. {
  250. maxValue = value;
  251. }
  252. }
  253. return maxValue;
  254. }
  255. public int FindMinValue<T>(List<T> list, Converter<T, int> projection)
  256. {
  257. if (list.Count == 0)
  258. {
  259. throw new InvalidOperationException("Empty list");
  260. }
  261. int minValue = int.MaxValue;
  262. foreach (T item in list)
  263. {
  264. int value = projection(item);
  265. if (value < minValue)
  266. {
  267. minValue = value;
  268. }
  269. }
  270. return minValue;
  271. }
  272. public int FindAvergeValue<T>(List<T> list, Converter<T, int> projection)
  273. {
  274. if (list.Count == 0)
  275. {
  276. throw new InvalidOperationException("Empty list");
  277. }
  278. int totalvalue = 0;
  279. foreach (T item in list)
  280. {
  281. totalvalue += projection(item);
  282. }
  283. return totalvalue / list.Count;
  284. }
  285. public string NumToText(int num)
  286. {
  287. switch(num)
  288. {
  289. case 1:
  290. return "eerste";
  291. case 2:
  292. return "tweede";
  293. case 3:
  294. return "derde";
  295. case 4:
  296. return "vierde";
  297. case 5:
  298. return "vijfde";
  299. case 6:
  300. return "zesde";
  301. case 7:
  302. return "zevende";
  303. case 8:
  304. return "achste";
  305. case 9:
  306. return "negende";
  307. default:
  308. return "volgende";
  309. }
  310. }
  311. }
  312. }