ErgometerTest.cs 12 KB

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