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