Werkcollege opgave 23 september BMT K9
Dependencies: Encoder HIDScope MODSERIAL mbed QEI biquadFilter
main.cpp@37:6c04c15d9bbe, 2015-10-20 (annotated)
- Committer:
- sigert
- Date:
- Tue Oct 20 10:46:17 2015 +0000
- Revision:
- 37:6c04c15d9bbe
- Parent:
- 36:f29a36683b1a
- Child:
- 38:be38b9318849
Hidscope werkt niet : EMG test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bscheltinga | 0:fe3896c6eeb0 | 1 | #include "mbed.h" |
bscheltinga | 12:0a079e86348e | 2 | #include "HIDScope.h" |
bscheltinga | 0:fe3896c6eeb0 | 3 | #include "MODSERIAL.h" |
bscheltinga | 13:04e10692e239 | 4 | #include "biquadFilter.h" //Filter direct form II |
bscheltinga | 0:fe3896c6eeb0 | 5 | |
bscheltinga | 26:1090acf98efc | 6 | // [DEFINE INPUTS] // |
sigert | 37:6c04c15d9bbe | 7 | DigitalOut debug_led_red(LED_RED); // Debug LED |
sigert | 37:6c04c15d9bbe | 8 | DigitalOut debug_led_green(LED_GREEN); // Debug LED |
sigert | 37:6c04c15d9bbe | 9 | DigitalOut debug_led_blue(LED_BLUE); // Debug LED |
ThomasBNL | 36:f29a36683b1a | 10 | |
sigert | 37:6c04c15d9bbe | 11 | const double off=1; // Led off |
sigert | 37:6c04c15d9bbe | 12 | const double on=0; // Led on |
bscheltinga | 21:594915ba2bf9 | 13 | |
bscheltinga | 26:1090acf98efc | 14 | // [DEFINE CONSTANTS] // |
ThomasBNL | 27:85e5d36bb6c5 | 15 | HIDScope scope(3); // Aantal HIDScope kanalen |
bscheltinga | 18:68067ffd169e | 16 | MODSERIAL pc(USBTX,USBRX); |
bscheltinga | 20:d5f5c60adc43 | 17 | Ticker control_tick; |
ThomasBNL | 27:85e5d36bb6c5 | 18 | Ticker T1; |
bscheltinga | 15:7870f7912904 | 19 | |
bscheltinga | 26:1090acf98efc | 20 | // [BIQUAD FILTERS] // |
ThomasBNL | 27:85e5d36bb6c5 | 21 | int Fs = 512; // sampling frequency |
ThomasBNL | 36:f29a36683b1a | 22 | const double low_b0 = 0.05892937945281792; |
ThomasBNL | 36:f29a36683b1a | 23 | const double low_b1 = 0.11785875890563584; |
ThomasBNL | 36:f29a36683b1a | 24 | const double low_b2 = 0.05892937945281792; |
ThomasBNL | 36:f29a36683b1a | 25 | const double low_a1 = -1.205716572226748; |
sigert | 37:6c04c15d9bbe | 26 | const double low_a2 = 0.44143409003801976; //Notch 1 LOW: VIA online biquad calculator Lowpass 520-48-0.7071-6 |
bscheltinga | 15:7870f7912904 | 27 | |
ThomasBNL | 36:f29a36683b1a | 28 | const double high_b0 = 0.6389437261127494; |
ThomasBNL | 36:f29a36683b1a | 29 | const double high_b1 = -1.2778874522254988; |
ThomasBNL | 36:f29a36683b1a | 30 | const double high_b2 = 0.6389437261127494; |
ThomasBNL | 36:f29a36683b1a | 31 | const double high_a1 = -1.1429772843080923; |
sigert | 37:6c04c15d9bbe | 32 | const double high_a2 = 0.41279762014290533; // NOTCH 2 HIGH: VIA online biquad calculator Highpas 520-52-0.7071-6 |
bscheltinga | 22:14abcfdd1554 | 33 | |
sigert | 37:6c04c15d9bbe | 34 | const double high_b0_HP = 0.6389437261127494; |
sigert | 37:6c04c15d9bbe | 35 | const double high_b1_HP = -1.2778874522254988; |
sigert | 37:6c04c15d9bbe | 36 | const double high_b2_HP = 0.6389437261127494; |
sigert | 37:6c04c15d9bbe | 37 | const double high_a1_HP = -1.1429772843080923; |
sigert | 37:6c04c15d9bbe | 38 | const double high_a2_HP = 0.41279762014290533; // HIGHPASS : NOG OPZOEKEN!! : >25Hz? sample rate 512Hz |
sigert | 37:6c04c15d9bbe | 39 | |
sigert | 37:6c04c15d9bbe | 40 | const double low_b0_LP = 0.05892937945281792; |
sigert | 37:6c04c15d9bbe | 41 | const double low_b1_LP = 0.11785875890563584; |
sigert | 37:6c04c15d9bbe | 42 | const double low_b2_LP = 0.05892937945281792; |
sigert | 37:6c04c15d9bbe | 43 | const double low_a1_LP = -1.205716572226748; |
sigert | 37:6c04c15d9bbe | 44 | const double low_a2_LP = 0.44143409003801976; // LOWPASS : NOG OPZOEKEN!! <5-10 Hz? sample rate 512Hz |
sigert | 37:6c04c15d9bbe | 45 | // |
ThomasBNL | 36:f29a36683b1a | 46 | //Left bicep |
sigert | 37:6c04c15d9bbe | 47 | biquadFilter highpassfilter_1(high_a1_HP, high_a2_HP, high_b0_HP, high_b1_HP, high_b2_HP);// moeten nog waardes voor gemaakt worden? (>25Hz doorlaten) |
sigert | 37:6c04c15d9bbe | 48 | biquadFilter notchL1(high_a1, high_a2, high_b0, high_b1, high_b2);// moeten nog waardes voor gemaakt worden (>52Hz doorlaten) |
ThomasBNL | 36:f29a36683b1a | 49 | biquadFilter notchL2(low_a1, low_a2, low_b0, low_b1, low_b2);// moeten nog waardes voor gemaakt worden (<48Hz doorlaten) |
sigert | 37:6c04c15d9bbe | 50 | biquadFilter lowpassfilter_1(low_a1_LP, low_a2_LP, low_b0_LP, low_b1_LP, low_b2_LP); |
bscheltinga | 0:fe3896c6eeb0 | 51 | |
ThomasBNL | 36:f29a36683b1a | 52 | // Right bicep |
sigert | 37:6c04c15d9bbe | 53 | biquadFilter highpassfilter_2(high_a1_HP, high_a2_HP, high_b0_HP, high_b1_HP, high_b2_HP);// moeten nog waardes voor gemaakt worden? |
sigert | 37:6c04c15d9bbe | 54 | biquadFilter notchR1(high_a1, high_a2, high_b0, high_b1, high_b2); // moeten nog waardes voor gemaakt worden |
ThomasBNL | 36:f29a36683b1a | 55 | biquadFilter notchR2(low_a1, low_a2, low_b0, low_b1, low_b2); // moeten nog waardes voor gemaakt worden |
sigert | 37:6c04c15d9bbe | 56 | biquadFilter lowpassfilter_2(low_a1_LP, low_a2_LP, low_b0_LP, low_b1_LP, low_b2_LP);// moeten nog waardes voor gemaakt worden? |
bscheltinga | 20:d5f5c60adc43 | 57 | |
ThomasBNL | 27:85e5d36bb6c5 | 58 | |
ThomasBNL | 36:f29a36683b1a | 59 | AnalogIn input1(A0); // EMG: Two AnalogIn EMG inputs |
ThomasBNL | 36:f29a36683b1a | 60 | AnalogIn input2(A1); // EMG: Two AnalogIn EMG inputs |
ThomasBNL | 36:f29a36683b1a | 61 | |
ThomasBNL | 36:f29a36683b1a | 62 | // __________________________ |
ThomasBNL | 36:f29a36683b1a | 63 | // : [EMG variables] : |
ThomasBNL | 36:f29a36683b1a | 64 | // :__________________________: |
ThomasBNL | 36:f29a36683b1a | 65 | // |
ThomasBNL | 36:f29a36683b1a | 66 | |
ThomasBNL | 36:f29a36683b1a | 67 | volatile bool control_go = false; // EMG: |
ThomasBNL | 36:f29a36683b1a | 68 | volatile bool sample = false; |
ThomasBNL | 36:f29a36683b1a | 69 | volatile bool sample_error = false; |
ThomasBNL | 36:f29a36683b1a | 70 | volatile bool sample_error_strike = false; |
ThomasBNL | 36:f29a36683b1a | 71 | |
ThomasBNL | 36:f29a36683b1a | 72 | double Sample_EMG_L_1, Sample_EMG_L_2, Sample_EMG_L_3, Sample_EMG_L_4, Sample_EMG_L_5, Sample_EMG_L_6, Sample_EMG_L_7, Sample_EMG_L_8, Sample_EMG_L_9, Sample_EMG_L_10, moving_average_left; |
ThomasBNL | 36:f29a36683b1a | 73 | double Sample_EMG_R_1, Sample_EMG_R_2, Sample_EMG_R_3, Sample_EMG_R_4, Sample_EMG_R_5, Sample_EMG_R_6, Sample_EMG_R_7, Sample_EMG_R_8, Sample_EMG_R_9, Sample_EMG_R_10, moving_average_right; |
ThomasBNL | 36:f29a36683b1a | 74 | |
ThomasBNL | 36:f29a36683b1a | 75 | double minimum_L; double maximum_L; |
ThomasBNL | 36:f29a36683b1a | 76 | double EMG_L_min; double EMG_L_max; |
ThomasBNL | 36:f29a36683b1a | 77 | double minimum_R; double maximum_R; |
ThomasBNL | 36:f29a36683b1a | 78 | double EMG_R_min; double EMG_R_max; |
ThomasBNL | 36:f29a36683b1a | 79 | double EMG_left_Bicep; double EMG_Right_Bicep; // input variables |
ThomasBNL | 36:f29a36683b1a | 80 | double EMG_Left_Bicep_filtered_notch_1;double EMG_Right_Bicep_filtered_notch_1; |
ThomasBNL | 36:f29a36683b1a | 81 | double EMG_Left_Bicep_filtered_notch_2;double EMG_Right_Bicep_filtered_notch_2; |
ThomasBNL | 36:f29a36683b1a | 82 | double EMG_Left_Bicep_filtered; double EMG_Right_Bicep_filtered; // output variables |
ThomasBNL | 36:f29a36683b1a | 83 | |
ThomasBNL | 36:f29a36683b1a | 84 | |
ThomasBNL | 36:f29a36683b1a | 85 | double n=0; double c=0; double k=0; double p=0; double er=0; |
ThomasBNL | 36:f29a36683b1a | 86 | |
ThomasBNL | 36:f29a36683b1a | 87 | // FUNCTIONS |
ThomasBNL | 36:f29a36683b1a | 88 | void ControlGo(); |
ThomasBNL | 36:f29a36683b1a | 89 | void take_sample(); |
ThomasBNL | 36:f29a36683b1a | 90 | void Filter(); |
ThomasBNL | 36:f29a36683b1a | 91 | void sample_filter(); |
ThomasBNL | 36:f29a36683b1a | 92 | void countdown_from_5(); |
ThomasBNL | 36:f29a36683b1a | 93 | void calibration(); |
sigert | 37:6c04c15d9bbe | 94 | void red();void blue();void green();void white();void yellow();void cyan();void purple(); void black(); |
sigert | 37:6c04c15d9bbe | 95 | |
ThomasBNL | 36:f29a36683b1a | 96 | |
ThomasBNL | 36:f29a36683b1a | 97 | //==========================// |
ThomasBNL | 27:85e5d36bb6c5 | 98 | // [MAIN FUNCTION] // |
ThomasBNL | 36:f29a36683b1a | 99 | //==========================// |
ThomasBNL | 27:85e5d36bb6c5 | 100 | int main() |
ThomasBNL | 27:85e5d36bb6c5 | 101 | { |
ThomasBNL | 27:85e5d36bb6c5 | 102 | control_tick.attach(&ControlGo, (float)1/Fs); |
ThomasBNL | 27:85e5d36bb6c5 | 103 | pc.baud(9600); |
ThomasBNL | 36:f29a36683b1a | 104 | // calibration(); // calibreer min en max positie |
ThomasBNL | 27:85e5d36bb6c5 | 105 | while(1) |
ThomasBNL | 27:85e5d36bb6c5 | 106 | { |
ThomasBNL | 27:85e5d36bb6c5 | 107 | if(control_go) |
ThomasBNL | 27:85e5d36bb6c5 | 108 | { |
sigert | 37:6c04c15d9bbe | 109 | blue(); |
ThomasBNL | 27:85e5d36bb6c5 | 110 | sample_filter(); |
ThomasBNL | 36:f29a36683b1a | 111 | scope.set(0,EMG_left_Bicep); //left bicep unfiltered |
ThomasBNL | 36:f29a36683b1a | 112 | scope.set(1,EMG_Left_Bicep_filtered); //Filtered signal |
ThomasBNL | 36:f29a36683b1a | 113 | scope.set(2,moving_average_left); // |
ThomasBNL | 27:85e5d36bb6c5 | 114 | scope.send(); |
ThomasBNL | 27:85e5d36bb6c5 | 115 | control_go = 0; |
ThomasBNL | 27:85e5d36bb6c5 | 116 | } |
sigert | 37:6c04c15d9bbe | 117 | yellow(); |
ThomasBNL | 27:85e5d36bb6c5 | 118 | } // while end |
ThomasBNL | 27:85e5d36bb6c5 | 119 | } // main end |
ThomasBNL | 36:f29a36683b1a | 120 | |
ThomasBNL | 36:f29a36683b1a | 121 | // -------------------------------------------------------------------------------------------------------- |
ThomasBNL | 36:f29a36683b1a | 122 | // [FUNCTIONS] // |
ThomasBNL | 36:f29a36683b1a | 123 | void ControlGo() //Control flag |
ThomasBNL | 36:f29a36683b1a | 124 | { |
ThomasBNL | 36:f29a36683b1a | 125 | control_go = true; |
ThomasBNL | 36:f29a36683b1a | 126 | } |
ThomasBNL | 36:f29a36683b1a | 127 | |
ThomasBNL | 36:f29a36683b1a | 128 | void sample_filter() |
ThomasBNL | 36:f29a36683b1a | 129 | { |
ThomasBNL | 36:f29a36683b1a | 130 | Filter(); |
ThomasBNL | 36:f29a36683b1a | 131 | take_sample(); |
ThomasBNL | 36:f29a36683b1a | 132 | if(sample) |
ThomasBNL | 36:f29a36683b1a | 133 | { |
ThomasBNL | 36:f29a36683b1a | 134 | sample=false; |
ThomasBNL | 36:f29a36683b1a | 135 | Sample_EMG_L_1 = EMG_Left_Bicep_filtered; Sample_EMG_R_1 = EMG_Right_Bicep_filtered; |
ThomasBNL | 36:f29a36683b1a | 136 | |
ThomasBNL | 36:f29a36683b1a | 137 | Sample_EMG_L_10= Sample_EMG_L_9; Sample_EMG_R_10= Sample_EMG_R_9; |
ThomasBNL | 36:f29a36683b1a | 138 | Sample_EMG_L_9 = Sample_EMG_L_8; Sample_EMG_R_9 = Sample_EMG_R_8; |
ThomasBNL | 36:f29a36683b1a | 139 | Sample_EMG_L_8 = Sample_EMG_L_7; Sample_EMG_R_8 = Sample_EMG_R_7; |
ThomasBNL | 36:f29a36683b1a | 140 | Sample_EMG_L_7 = Sample_EMG_L_6; Sample_EMG_R_7 = Sample_EMG_R_6; |
ThomasBNL | 36:f29a36683b1a | 141 | Sample_EMG_L_6 = Sample_EMG_L_5; Sample_EMG_R_6 = Sample_EMG_R_5; |
ThomasBNL | 36:f29a36683b1a | 142 | Sample_EMG_L_5 = Sample_EMG_L_4; Sample_EMG_R_5 = Sample_EMG_R_4; |
ThomasBNL | 36:f29a36683b1a | 143 | Sample_EMG_L_4 = Sample_EMG_L_3; Sample_EMG_R_4 = Sample_EMG_R_3; |
ThomasBNL | 36:f29a36683b1a | 144 | Sample_EMG_L_3 = Sample_EMG_L_2; Sample_EMG_R_3 = Sample_EMG_R_2; |
ThomasBNL | 36:f29a36683b1a | 145 | Sample_EMG_L_2 = Sample_EMG_L_1; Sample_EMG_R_2 = Sample_EMG_R_1; |
ThomasBNL | 36:f29a36683b1a | 146 | } |
ThomasBNL | 36:f29a36683b1a | 147 | moving_average_left=Sample_EMG_L_1*0.1+Sample_EMG_L_2*0.1+Sample_EMG_L_3*0.1+Sample_EMG_L_4*0.1+Sample_EMG_L_5*0.1+Sample_EMG_L_6*0.1+Sample_EMG_L_7*0.1+Sample_EMG_L_8*0.1+Sample_EMG_L_9*0.1+Sample_EMG_L_10*0.1; |
ThomasBNL | 36:f29a36683b1a | 148 | moving_average_right=Sample_EMG_R_1*0.1+Sample_EMG_R_2*0.1+Sample_EMG_R_3*0.1+Sample_EMG_R_4*0.1+Sample_EMG_R_5*0.1+Sample_EMG_R_6*0.1+Sample_EMG_R_7*0.1+Sample_EMG_R_8*0.1+Sample_EMG_R_9*0.1+Sample_EMG_R_10*0.1; |
ThomasBNL | 36:f29a36683b1a | 149 | n++; |
ThomasBNL | 36:f29a36683b1a | 150 | } |
ThomasBNL | 36:f29a36683b1a | 151 | |
ThomasBNL | 36:f29a36683b1a | 152 | void take_sample() // Take a sample every 25th sample |
ThomasBNL | 36:f29a36683b1a | 153 | { |
ThomasBNL | 36:f29a36683b1a | 154 | if(n==25) |
ThomasBNL | 36:f29a36683b1a | 155 | { |
ThomasBNL | 36:f29a36683b1a | 156 | sample = true; n=0; |
ThomasBNL | 36:f29a36683b1a | 157 | } |
ThomasBNL | 36:f29a36683b1a | 158 | |
ThomasBNL | 36:f29a36683b1a | 159 | if(er==5) |
ThomasBNL | 36:f29a36683b1a | 160 | { |
ThomasBNL | 36:f29a36683b1a | 161 | sample_error = true; er=0; |
ThomasBNL | 36:f29a36683b1a | 162 | } |
ThomasBNL | 36:f29a36683b1a | 163 | |
ThomasBNL | 36:f29a36683b1a | 164 | sample_error_strike = true; |
ThomasBNL | 36:f29a36683b1a | 165 | } |
ThomasBNL | 36:f29a36683b1a | 166 | |
ThomasBNL | 36:f29a36683b1a | 167 | // [FILTER FUNCTIONS] // |
ThomasBNL | 36:f29a36683b1a | 168 | // [EMG] // |
ThomasBNL | 36:f29a36683b1a | 169 | |
ThomasBNL | 36:f29a36683b1a | 170 | void Filter() // Unfiltered EMG (input) -> highpass filter -> rectify -> lowpass filter -> Filtered EMG (output) |
ThomasBNL | 36:f29a36683b1a | 171 | { |
ThomasBNL | 36:f29a36683b1a | 172 | EMG_left_Bicep = input1; EMG_Right_Bicep = input2; |
ThomasBNL | 36:f29a36683b1a | 173 | |
ThomasBNL | 36:f29a36683b1a | 174 | EMG_Left_Bicep_filtered = highpassfilter_1.step(EMG_left_Bicep); EMG_Right_Bicep_filtered = highpassfilter_2.step(EMG_Right_Bicep); |
ThomasBNL | 36:f29a36683b1a | 175 | EMG_Left_Bicep_filtered = fabs(EMG_Left_Bicep_filtered); EMG_Right_Bicep_filtered = fabs(EMG_Right_Bicep_filtered); |
ThomasBNL | 36:f29a36683b1a | 176 | |
ThomasBNL | 36:f29a36683b1a | 177 | EMG_Left_Bicep_filtered_notch_1 = notchL1.step(EMG_Left_Bicep_filtered); EMG_Right_Bicep_filtered_notch_1 = notchR1.step(EMG_Right_Bicep_filtered); |
ThomasBNL | 36:f29a36683b1a | 178 | EMG_Left_Bicep_filtered_notch_2 = notchL2.step(EMG_Left_Bicep_filtered_notch_1); EMG_Right_Bicep_filtered_notch_2 = notchR2.step(EMG_Right_Bicep_filtered_notch_1); |
ThomasBNL | 36:f29a36683b1a | 179 | |
ThomasBNL | 36:f29a36683b1a | 180 | EMG_Left_Bicep_filtered = lowpassfilter_1.step(EMG_Left_Bicep_filtered_notch_2); EMG_Right_Bicep_filtered = lowpassfilter_2.step(EMG_Right_Bicep_filtered_notch_2); |
ThomasBNL | 36:f29a36683b1a | 181 | } |
ThomasBNL | 36:f29a36683b1a | 182 | |
ThomasBNL | 36:f29a36683b1a | 183 | void countdown_from_5() // Countdown from 5 till 0 inside Putty (interface) |
ThomasBNL | 36:f29a36683b1a | 184 | { |
ThomasBNL | 36:f29a36683b1a | 185 | wait(1); pc.printf("5 \n\r"); wait(1); pc.printf("4 \n\r"); wait(1); pc.printf("3 \n\r"); wait(1); pc.printf("2 Ready \n\r"); |
ThomasBNL | 36:f29a36683b1a | 186 | wait(1); pc.printf("1 Set \n\r"); wait(1); pc.printf("Go \n\r"); |
ThomasBNL | 36:f29a36683b1a | 187 | } |
ThomasBNL | 36:f29a36683b1a | 188 | |
ThomasBNL | 36:f29a36683b1a | 189 | void calibration() |
ThomasBNL | 36:f29a36683b1a | 190 | { |
ThomasBNL | 36:f29a36683b1a | 191 | |
ThomasBNL | 36:f29a36683b1a | 192 | |
ThomasBNL | 36:f29a36683b1a | 193 | // [MINIMUM VALUE BICEPS CALIBRATION] // |
ThomasBNL | 36:f29a36683b1a | 194 | |
ThomasBNL | 36:f29a36683b1a | 195 | pc.printf("Start minimum calibration in 5 seconds \n\r"); |
ThomasBNL | 36:f29a36683b1a | 196 | pc.printf("Keep your biceps as relaxed as possible \n\r"); |
ThomasBNL | 36:f29a36683b1a | 197 | |
ThomasBNL | 36:f29a36683b1a | 198 | countdown_from_5(); |
ThomasBNL | 36:f29a36683b1a | 199 | c=0; |
ThomasBNL | 36:f29a36683b1a | 200 | |
ThomasBNL | 36:f29a36683b1a | 201 | while(c<2560) // 512Hz -> 2560 is equal to five seconds |
ThomasBNL | 36:f29a36683b1a | 202 | { |
ThomasBNL | 36:f29a36683b1a | 203 | Filter(); // Filter EMG signal |
ThomasBNL | 36:f29a36683b1a | 204 | minimum_L=EMG_Left_Bicep_filtered+minimum_L; // Take previous sample EMG_Left_Bicep_filtered and add the new value |
ThomasBNL | 36:f29a36683b1a | 205 | minimum_R=EMG_Right_Bicep_filtered+minimum_R; |
ThomasBNL | 36:f29a36683b1a | 206 | // scope.set(0,EMG_left_Bicep); |
ThomasBNL | 36:f29a36683b1a | 207 | // scope.set(1,EMG_Left_Bicep_filtered); |
ThomasBNL | 36:f29a36683b1a | 208 | // scope.set(2,minimum_L); |
ThomasBNL | 36:f29a36683b1a | 209 | // scope.send(); |
ThomasBNL | 36:f29a36683b1a | 210 | c++; // Every sample c is increased by one until the statement c<2560 is false |
ThomasBNL | 36:f29a36683b1a | 211 | wait(0.001953125); // wait one sample |
ThomasBNL | 36:f29a36683b1a | 212 | } |
ThomasBNL | 36:f29a36683b1a | 213 | |
ThomasBNL | 36:f29a36683b1a | 214 | pc.printf("Finished minimum calibration \n\r"); |
ThomasBNL | 36:f29a36683b1a | 215 | |
ThomasBNL | 36:f29a36683b1a | 216 | EMG_L_min=minimum_L/2560; // Divide the summation by the number of measurements (2560 measurements) to get a mean value over 5 seconds |
ThomasBNL | 36:f29a36683b1a | 217 | EMG_R_min=minimum_R/2560; |
ThomasBNL | 36:f29a36683b1a | 218 | |
ThomasBNL | 36:f29a36683b1a | 219 | pc.printf("EMG_L_min = %f \n\r EMG_R_min = %f \n\r", EMG_L_min, EMG_R_min); |
ThomasBNL | 36:f29a36683b1a | 220 | |
ThomasBNL | 36:f29a36683b1a | 221 | wait (3); //cooldown |
ThomasBNL | 36:f29a36683b1a | 222 | |
ThomasBNL | 36:f29a36683b1a | 223 | |
ThomasBNL | 36:f29a36683b1a | 224 | // [MAXIMUM VALUE BICEPS CALIBRATION] // |
ThomasBNL | 36:f29a36683b1a | 225 | |
ThomasBNL | 36:f29a36683b1a | 226 | |
ThomasBNL | 36:f29a36683b1a | 227 | pc.printf("start maximum calibration in 5 seconds (start contraction at 3) \n\r"); |
ThomasBNL | 36:f29a36683b1a | 228 | |
ThomasBNL | 36:f29a36683b1a | 229 | countdown_from_5(); |
ThomasBNL | 36:f29a36683b1a | 230 | c=0; |
ThomasBNL | 36:f29a36683b1a | 231 | |
ThomasBNL | 36:f29a36683b1a | 232 | while(c<2560) // 512Hz -> 2560 is equal to five seconds |
ThomasBNL | 36:f29a36683b1a | 233 | { |
ThomasBNL | 36:f29a36683b1a | 234 | Filter(); // Filter EMG signal |
ThomasBNL | 36:f29a36683b1a | 235 | maximum_L=EMG_Left_Bicep_filtered+maximum_L; // Take previous sample EMG_Left_Bicep_filtered and add the new value |
ThomasBNL | 36:f29a36683b1a | 236 | maximum_R=EMG_Right_Bicep_filtered+maximum_R; |
ThomasBNL | 36:f29a36683b1a | 237 | c++; // Every sample c is increased by one until the statement c<2560 is false |
ThomasBNL | 36:f29a36683b1a | 238 | wait(0.001953125); |
ThomasBNL | 36:f29a36683b1a | 239 | } |
ThomasBNL | 36:f29a36683b1a | 240 | |
ThomasBNL | 36:f29a36683b1a | 241 | pc.printf("Finished minimum calibration \n\r"); |
ThomasBNL | 36:f29a36683b1a | 242 | |
ThomasBNL | 36:f29a36683b1a | 243 | EMG_L_max=maximum_L/2560; // Divide the summation by the number of measurements (2560 measurements) to get a mean value over 5 seconds |
ThomasBNL | 36:f29a36683b1a | 244 | EMG_R_max=maximum_R/2560; |
ThomasBNL | 36:f29a36683b1a | 245 | |
ThomasBNL | 36:f29a36683b1a | 246 | pc.printf("EMG_L_max = %f \n\r EMG_R_max = %f \n\r", EMG_L_max, EMG_R_max); |
ThomasBNL | 36:f29a36683b1a | 247 | |
ThomasBNL | 36:f29a36683b1a | 248 | wait (3); //cooldown |
ThomasBNL | 36:f29a36683b1a | 249 | |
ThomasBNL | 36:f29a36683b1a | 250 | |
ThomasBNL | 36:f29a36683b1a | 251 | // [MAXIMUM VALUE BICEPS CALIBRATION] // |
ThomasBNL | 36:f29a36683b1a | 252 | // Calculate threshold percentages // |
ThomasBNL | 36:f29a36683b1a | 253 | |
ThomasBNL | 36:f29a36683b1a | 254 | const float Threshold_Bicep_Left_1=((EMG_L_max-EMG_L_min)*0.2)+EMG_L_min;; //(waarde waarop het gemeten EMG signaal 20% van max het maximale is); // LEFT |
ThomasBNL | 36:f29a36683b1a | 255 | const float Threshold_Bicep_Left_2=((EMG_L_max-EMG_L_min)*0.6)+EMG_L_min; //(waarde waarop het gemeten EMG signaal 60% van max het maximale is); |
ThomasBNL | 36:f29a36683b1a | 256 | const float Threshold_Bicep_Right_1=((EMG_R_max-EMG_R_min)*0.2)+EMG_R_min; //(waarde waarop het gemeten EMG signaal 20% van max het maximale is); // RIGHT |
ThomasBNL | 36:f29a36683b1a | 257 | const float Threshold_Bicep_Right_2=((EMG_R_max-EMG_R_min)*0.6)+EMG_R_min; //(waarde waarop het gemeten EMG signaal 60% van max het maximale is); |
ThomasBNL | 36:f29a36683b1a | 258 | |
ThomasBNL | 36:f29a36683b1a | 259 | pc.printf("left 1: %f left 2: %f right 1: %f right 2: %f \n\r", Threshold_Bicep_Left_1, Threshold_Bicep_Left_2, Threshold_Bicep_Right_1, Threshold_Bicep_Right_2); |
ThomasBNL | 36:f29a36683b1a | 260 | |
ThomasBNL | 36:f29a36683b1a | 261 | } |
ThomasBNL | 36:f29a36683b1a | 262 | |
sigert | 37:6c04c15d9bbe | 263 | void red() { debug_led_red=on; debug_led_blue=off; debug_led_green=off; } |
sigert | 37:6c04c15d9bbe | 264 | void blue() { debug_led_red=off; debug_led_blue=on; debug_led_green=off; } |
sigert | 37:6c04c15d9bbe | 265 | void green() { debug_led_red=off; debug_led_blue=off; debug_led_green=on; } |
sigert | 37:6c04c15d9bbe | 266 | void white() { debug_led_red=on; debug_led_blue=on; debug_led_green=on; } |
sigert | 37:6c04c15d9bbe | 267 | void yellow() { debug_led_red=on; debug_led_blue=off; debug_led_green=on; } |
sigert | 37:6c04c15d9bbe | 268 | void cyan() { debug_led_red=off; debug_led_blue=on; debug_led_green=on; } |
sigert | 37:6c04c15d9bbe | 269 | void purple() { debug_led_red=on; debug_led_blue=on; debug_led_green=off; } |
sigert | 37:6c04c15d9bbe | 270 | void black() { debug_led_red=off; debug_led_blue=off; debug_led_green=off; } |
ThomasBNL | 36:f29a36683b1a | 271 |