| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ErgometerApplication
- {
- public class ErgometerTest
- {
- private int weight;
- private int length;
- private int age;
- private char gender;
- private enum state {WARMUP, WORKLOAD, COOLINGDOWN};
- private state currentstate;
- private int workloadStarted;
- private int workloadHearthbeat;
- private int workloadnumber;
- public ErgometerTest(int weight, int length , int age, char gender)
- {
- this.weight = weight;
- this.length = length;
- this.age = age;
- this.gender = gender;
- currentstate = state.WARMUP;
- workloadnumber = 1;
- MainClient.ComPort.Write("SET PW 25");
- }
- public void timerTick()
- {
- switch(currentstate)
- {
- case state.WARMUP:
- if (MainClient.GetLastMeting().Seconds > 30)
- {
- List<ErgometerLibrary.Meting> last10 = MainClient.Metingen.GetRange(MainClient.Metingen.Count - 10, 10);
- int max = FindMaxValue(MainClient.Metingen, x => x.HeartBeat);
- int min = FindMaxValue(MainClient.Metingen, x => x.HeartBeat);
- if(max - min > 10) //Hartslag niet stabiel
- {
- return;
- }
- else
- {
- currentstate = state.WORKLOAD;
- workloadStarted = MainClient.GetLastMeting().Seconds;
- Console.WriteLine("Warmup is goed, hartslag stabiel, ga naar workload test");
- }
- }
- break;
- case state.WORKLOAD:
- if (MainClient.GetLastMeting().Seconds - workloadStarted > 180)
- {
- //Checken of de heartrate niet groter is dan 75%, anders stoppen
- if (workloadHearthbeat > CalculateMaximumHeartRate())
- {
- currentstate = state.COOLINGDOWN;
- }
- MainClient.ComPort.Write("PW " + GetWorkloadPower(workloadnumber));
- workloadStarted = MainClient.GetLastMeting().Seconds;
- workloadHearthbeat = 0;
- workloadnumber++;
- Console.WriteLine("3:00 gefietst, workload" + workloadnumber + " af, nieuwe waardes maken");
- }
- else if (MainClient.GetLastMeting().Seconds - workloadStarted > 160 && workloadHearthbeat == 0)
- {
- List<ErgometerLibrary.Meting> last80 = MainClient.Metingen.GetRange(MainClient.Metingen.Count - 80, 80);
- workloadHearthbeat = FindAvergeValue(MainClient.Metingen, x => x.HeartBeat);
- Console.WriteLine("2:40 gefiets, gemiddelde harstslag berekenen:" + workloadHearthbeat);
- }
- break;
- case state.COOLINGDOWN:
- break;
- }
- }
- // WORKLOAD //
- private int GetWorkloadPower(int workload)
- {
- if (gender == 'V')
- {
- if (workloadnumber == 1)
- {
- if (workloadHearthbeat < 80)
- return 125;
- else if (workloadHearthbeat < 89)
- return 100;
- else if (workloadHearthbeat < 100)
- return 75;
- else
- return 50;
- }
- else
- {
- return MainClient.GetLastMeting().Power + 25;
- }
- }
- else if(gender == 'M')
- {
- if(workloadnumber == 1)
- {
- if (workloadHearthbeat < 90)
- return 150;
- else if (workloadHearthbeat < 105)
- return 125;
- else
- return 100;
- }
- else if(workloadnumber == 2)
- {
- if(workloadHearthbeat < 120)
- {
- if (MainClient.GetLastMeting().Power == 150)
- return 225;
- else if (MainClient.GetLastMeting().Power == 125)
- return 200;
- else
- return 175;
- }
- else if(workloadHearthbeat < 135)
- {
- if (MainClient.GetLastMeting().Power == 150)
- return 200;
- else if (MainClient.GetLastMeting().Power == 125)
- return 175;
- else
- return 150;
- }
- else
- {
- if (MainClient.GetLastMeting().Power == 150)
- return 175;
- else if (MainClient.GetLastMeting().Power == 125)
- return 150;
- else
- return 125;
- }
- }
- else
- {
- return MainClient.GetLastMeting().Power + 25;
- }
- }
- return 25;
- }
- // HELPER FUNCTIONS //
- private double CalculateMaximumHeartRate()
- {
- return 208 - (0.7 * age);
- }
- public int FindMaxValue<T>(List<T> list, Converter<T, int> projection)
- {
- if (list.Count == 0)
- {
- throw new InvalidOperationException("Empty list");
- }
- int maxValue = int.MinValue;
- foreach (T item in list)
- {
- int value = projection(item);
- if (value > maxValue)
- {
- maxValue = value;
- }
- }
- return maxValue;
- }
- public int FindMinValue<T>(List<T> list, Converter<T, int> projection)
- {
- if (list.Count == 0)
- {
- throw new InvalidOperationException("Empty list");
- }
- int minValue = int.MaxValue;
- foreach (T item in list)
- {
- int value = projection(item);
- if (value < minValue)
- {
- minValue = value;
- }
- }
- return minValue;
- }
- public int FindAvergeValue<T>(List<T> list, Converter<T, int> projection)
- {
- if (list.Count == 0)
- {
- throw new InvalidOperationException("Empty list");
- }
- int totalvalue = 0;
- foreach (T item in list)
- {
- totalvalue += projection(item);
- }
- return totalvalue / list.Count;
- }
- }
- }
|