ErgometerTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. Console.WriteLine(CalculateVOMax());
  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 = FindMaxValue(MainClient.Metingen, x => x.HeartBeat);
  43. if(max - min > 10) //Hartslag niet stabiel
  44. {
  45. client.updateStepsText("Uw hartslag is not 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())
  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" + (workloads.Count - 1) + " 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. // HELPER FUNCTIONS //
  180. private int GetCurrentWorkload()
  181. {
  182. return workloads.Count - 1;
  183. }
  184. private double CalculateMaximumHeartRate()
  185. {
  186. return 208 - (0.7 * age);
  187. }
  188. public int FindMaxValue<T>(List<T> list, Converter<T, int> projection)
  189. {
  190. if (list.Count == 0)
  191. {
  192. throw new InvalidOperationException("Empty list");
  193. }
  194. int maxValue = int.MinValue;
  195. foreach (T item in list)
  196. {
  197. int value = projection(item);
  198. if (value > maxValue)
  199. {
  200. maxValue = value;
  201. }
  202. }
  203. return maxValue;
  204. }
  205. public int FindMinValue<T>(List<T> list, Converter<T, int> projection)
  206. {
  207. if (list.Count == 0)
  208. {
  209. throw new InvalidOperationException("Empty list");
  210. }
  211. int minValue = int.MaxValue;
  212. foreach (T item in list)
  213. {
  214. int value = projection(item);
  215. if (value < minValue)
  216. {
  217. minValue = value;
  218. }
  219. }
  220. return minValue;
  221. }
  222. public int FindAvergeValue<T>(List<T> list, Converter<T, int> projection)
  223. {
  224. if (list.Count == 0)
  225. {
  226. throw new InvalidOperationException("Empty list");
  227. }
  228. int totalvalue = 0;
  229. foreach (T item in list)
  230. {
  231. totalvalue += projection(item);
  232. }
  233. return totalvalue / list.Count;
  234. }
  235. public string NumToText(int num)
  236. {
  237. switch(num)
  238. {
  239. case 1:
  240. return "eerste";
  241. case 2:
  242. return "tweede";
  243. case 3:
  244. return "derde";
  245. case 4:
  246. return "vierde";
  247. case 5:
  248. return "vijfde";
  249. case 6:
  250. return "zesde";
  251. case 7:
  252. return "zevende";
  253. case 8:
  254. return "achste";
  255. case 9:
  256. return "negende";
  257. default:
  258. return "volgende";
  259. }
  260. }
  261. }
  262. }