ErgometerTest.cs 12 KB

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