ErgometerTest.cs 12 KB

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