ErgometerTest.cs 12 KB

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