ErgometerTest.cs 12 KB

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