Code om 4 EMG te filteren, thresholds te zetten met kalibratie en met putty te kijken of die thresholds overschreden worden
Dependencies: MovingAverage mbed biquadFilter MODSERIAL
main.cpp@0:c9891e1c62f0, 2019-04-16 (annotated)
- Committer:
- aschut
- Date:
- Tue Apr 16 11:03:47 2019 +0000
- Revision:
- 0:c9891e1c62f0
- Child:
- 1:ecd6dc3c839b
voor het goede script er in staat
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aschut | 0:c9891e1c62f0 | 1 | #include "mbed.h" |
aschut | 0:c9891e1c62f0 | 2 | #include "BiQuad.h" |
aschut | 0:c9891e1c62f0 | 3 | #include "MODSERIAL.h" |
aschut | 0:c9891e1c62f0 | 4 | #include <iostream> |
aschut | 0:c9891e1c62f0 | 5 | #include "MovingAverage.h" |
aschut | 0:c9891e1c62f0 | 6 | #define NSAMPLE 100 |
aschut | 0:c9891e1c62f0 | 7 | |
aschut | 0:c9891e1c62f0 | 8 | DigitalOut led1(LED_GREEN); |
aschut | 0:c9891e1c62f0 | 9 | DigitalOut led2(LED_RED); |
aschut | 0:c9891e1c62f0 | 10 | DigitalOut led3(LED_BLUE); |
aschut | 0:c9891e1c62f0 | 11 | MODSERIAL pc(USBTX, USBRX); |
aschut | 0:c9891e1c62f0 | 12 | |
aschut | 0:c9891e1c62f0 | 13 | //MovingAverage |
aschut | 0:c9891e1c62f0 | 14 | MovingAverage <float>Movag_1(NSAMPLE,0.0); //Make Moving Average, Define NSAMPLE above |
aschut | 0:c9891e1c62f0 | 15 | MovingAverage <float>Movag_2(NSAMPLE,0.0); //Make Moving Average, Define NSAMPLE above |
aschut | 0:c9891e1c62f0 | 16 | MovingAverage <float>Movag_3(NSAMPLE,0.0); //Make Moving Average, Define NSAMPLE above |
aschut | 0:c9891e1c62f0 | 17 | MovingAverage <float>Movag_4(NSAMPLE,0.0); //Make Moving Average, Define NSAMPLE above |
aschut | 0:c9891e1c62f0 | 18 | |
aschut | 0:c9891e1c62f0 | 19 | //EMG tickers, these tickers are called in the mainscript with fsample 500Hz, also sends to HIDscope with same fsample |
aschut | 0:c9891e1c62f0 | 20 | Ticker sample_ticker; //ticker for filtering pref. with 1000Hz, define in tick.attach |
aschut | 0:c9891e1c62f0 | 21 | Ticker threshold_check_ticker; |
aschut | 0:c9891e1c62f0 | 22 | Timer t; //timer try out for Astrid |
aschut | 0:c9891e1c62f0 | 23 | Timer timer_calibration; //timer for EMG calibration |
aschut | 0:c9891e1c62f0 | 24 | |
aschut | 0:c9891e1c62f0 | 25 | //Input |
aschut | 0:c9891e1c62f0 | 26 | AnalogIn emg1( A1 ); |
aschut | 0:c9891e1c62f0 | 27 | AnalogIn emg2( A2 ); |
aschut | 0:c9891e1c62f0 | 28 | AnalogIn emg3( A3 ); |
aschut | 0:c9891e1c62f0 | 29 | AnalogIn emg4( A4 ); |
aschut | 0:c9891e1c62f0 | 30 | |
aschut | 0:c9891e1c62f0 | 31 | // GLOBALS EMG |
aschut | 0:c9891e1c62f0 | 32 | //Filtered EMG signals from the end of the chains |
aschut | 0:c9891e1c62f0 | 33 | volatile double emg1_filtered, emg2_filtered, emg3_filtered, emg4_filtered; |
aschut | 0:c9891e1c62f0 | 34 | int i = 0; |
aschut | 0:c9891e1c62f0 | 35 | |
aschut | 0:c9891e1c62f0 | 36 | //Define doubles for calibration and ticker |
aschut | 0:c9891e1c62f0 | 37 | double ts = 0.001; //tijdsstap |
aschut | 0:c9891e1c62f0 | 38 | double calibration_time = 55; //time EMG calibration should take |
aschut | 0:c9891e1c62f0 | 39 | |
aschut | 0:c9891e1c62f0 | 40 | volatile double temp_highest_emg1 = 0; //highest detected value right biceps |
aschut | 0:c9891e1c62f0 | 41 | volatile double temp_highest_emg2 = 0; |
aschut | 0:c9891e1c62f0 | 42 | volatile double temp_highest_emg3 = 0; |
aschut | 0:c9891e1c62f0 | 43 | volatile double temp_highest_emg4 = 0; |
aschut | 0:c9891e1c62f0 | 44 | |
aschut | 0:c9891e1c62f0 | 45 | //Doubles for calculation threshold |
aschut | 0:c9891e1c62f0 | 46 | double duim_p_t = 0.4; //set threshold at percentage of highest value |
aschut | 0:c9891e1c62f0 | 47 | double bicep_p_t = 0.4; // |
aschut | 0:c9891e1c62f0 | 48 | double tricep_p_t = 0.4; //set threshold at percentage of highest value |
aschut | 0:c9891e1c62f0 | 49 | double onderarm_p_t = 0.4; |
aschut | 0:c9891e1c62f0 | 50 | |
aschut | 0:c9891e1c62f0 | 51 | volatile double threshold1; |
aschut | 0:c9891e1c62f0 | 52 | volatile double threshold2; |
aschut | 0:c9891e1c62f0 | 53 | volatile double threshold3; |
aschut | 0:c9891e1c62f0 | 54 | volatile double threshold4; |
aschut | 0:c9891e1c62f0 | 55 | |
aschut | 0:c9891e1c62f0 | 56 | // thresholdreads bools |
aschut | 0:c9891e1c62f0 | 57 | int Duim; |
aschut | 0:c9891e1c62f0 | 58 | int Bicep; |
aschut | 0:c9891e1c62f0 | 59 | int Tricep; |
aschut | 0:c9891e1c62f0 | 60 | int Onderarm; |
aschut | 0:c9891e1c62f0 | 61 | |
aschut | 0:c9891e1c62f0 | 62 | // EMG OUTPUT |
aschut | 0:c9891e1c62f0 | 63 | int EMGxplus; |
aschut | 0:c9891e1c62f0 | 64 | int EMGxmin ; |
aschut | 0:c9891e1c62f0 | 65 | int EMGyplus; |
aschut | 0:c9891e1c62f0 | 66 | int EMGymin ; |
aschut | 0:c9891e1c62f0 | 67 | |
aschut | 0:c9891e1c62f0 | 68 | |
aschut | 0:c9891e1c62f0 | 69 | //EMG1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
aschut | 0:c9891e1c62f0 | 70 | //Highpass |
aschut | 0:c9891e1c62f0 | 71 | BiQuadChain highp1; |
aschut | 0:c9891e1c62f0 | 72 | BiQuad highp1_1( 9.21171e-01, -1.84234e+00, 9.21171e-01, -1.88661e+00, 8.90340e-01 ); |
aschut | 0:c9891e1c62f0 | 73 | BiQuad highp1_2( 1.00000e+00, -2.00000e+00, 1.00000e+00, -1.94922e+00, 9.53070e-01 ); |
aschut | 0:c9891e1c62f0 | 74 | |
aschut | 0:c9891e1c62f0 | 75 | //Notch |
aschut | 0:c9891e1c62f0 | 76 | BiQuadChain notch1; |
aschut | 0:c9891e1c62f0 | 77 | BiQuad notch1_1( 9.56543e-01, -1.82035e+00, 9.56543e-01, -1.84459e+00, 9.53626e-01 ); |
aschut | 0:c9891e1c62f0 | 78 | BiQuad notch1_2( 1.00000e+00, -1.90305e+00, 1.00000e+00, -1.87702e+00, 9.59471e-01 ); |
aschut | 0:c9891e1c62f0 | 79 | |
aschut | 0:c9891e1c62f0 | 80 | //Lowpass 4th order cutoff 3Hz |
aschut | 0:c9891e1c62f0 | 81 | BiQuadChain lowp1; |
aschut | 0:c9891e1c62f0 | 82 | BiQuad lowp1_1( 7.69910e-09, 1.53982e-08, 7.69910e-09, -1.96542e+00, 9.65769e-01 ); |
aschut | 0:c9891e1c62f0 | 83 | BiQuad lowp1_2( 1.00000e+00, 2.00000e+00, 1.00000e+00, -1.98532e+00, 9.85677e-01 ); |
aschut | 0:c9891e1c62f0 | 84 | |
aschut | 0:c9891e1c62f0 | 85 | //EMG2!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
aschut | 0:c9891e1c62f0 | 86 | //Highpass |
aschut | 0:c9891e1c62f0 | 87 | BiQuadChain highp2; |
aschut | 0:c9891e1c62f0 | 88 | BiQuad highp2_1( 9.21171e-01, -1.84234e+00, 9.21171e-01, -1.88661e+00, 8.90340e-01 ); |
aschut | 0:c9891e1c62f0 | 89 | BiQuad highp2_2( 1.00000e+00, -2.00000e+00, 1.00000e+00, -1.94922e+00, 9.53070e-01 ); |
aschut | 0:c9891e1c62f0 | 90 | |
aschut | 0:c9891e1c62f0 | 91 | //Notch |
aschut | 0:c9891e1c62f0 | 92 | BiQuadChain notch2; |
aschut | 0:c9891e1c62f0 | 93 | BiQuad notch2_1( 9.56543e-01, -1.82035e+00, 9.56543e-01, -1.84459e+00, 9.53626e-01 ); |
aschut | 0:c9891e1c62f0 | 94 | BiQuad notch2_2( 1.00000e+00, -1.90305e+00, 1.00000e+00, -1.87702e+00, 9.59471e-01 ); |
aschut | 0:c9891e1c62f0 | 95 | |
aschut | 0:c9891e1c62f0 | 96 | //Lowpass 4th order cutoff 3Hz |
aschut | 0:c9891e1c62f0 | 97 | BiQuadChain lowp2; |
aschut | 0:c9891e1c62f0 | 98 | BiQuad lowp2_1( 7.69910e-09, 1.53982e-08, 7.69910e-09, -1.96542e+00, 9.65769e-01 ); |
aschut | 0:c9891e1c62f0 | 99 | BiQuad lowp2_2( 1.00000e+00, 2.00000e+00, 1.00000e+00, -1.98532e+00, 9.85677e-01 ); |
aschut | 0:c9891e1c62f0 | 100 | |
aschut | 0:c9891e1c62f0 | 101 | //EMG3!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
aschut | 0:c9891e1c62f0 | 102 | //Highpass |
aschut | 0:c9891e1c62f0 | 103 | BiQuadChain highp3; |
aschut | 0:c9891e1c62f0 | 104 | BiQuad highp3_1( 9.21171e-01, -1.84234e+00, 9.21171e-01, -1.88661e+00, 8.90340e-01 ); |
aschut | 0:c9891e1c62f0 | 105 | BiQuad highp3_2( 1.00000e+00, -2.00000e+00, 1.00000e+00, -1.94922e+00, 9.53070e-01 ); |
aschut | 0:c9891e1c62f0 | 106 | |
aschut | 0:c9891e1c62f0 | 107 | //Notch |
aschut | 0:c9891e1c62f0 | 108 | BiQuadChain notch3; |
aschut | 0:c9891e1c62f0 | 109 | BiQuad notch3_1( 9.56543e-01, -1.82035e+00, 9.56543e-01, -1.84459e+00, 9.53626e-01 ); |
aschut | 0:c9891e1c62f0 | 110 | BiQuad notch3_2( 1.00000e+00, -1.90305e+00, 1.00000e+00, -1.87702e+00, 9.59471e-01 ); |
aschut | 0:c9891e1c62f0 | 111 | |
aschut | 0:c9891e1c62f0 | 112 | //Lowpass 4th order cutoff 3Hz |
aschut | 0:c9891e1c62f0 | 113 | BiQuadChain lowp3; |
aschut | 0:c9891e1c62f0 | 114 | BiQuad lowp3_1( 7.69910e-09, 1.53982e-08, 7.69910e-09, -1.96542e+00, 9.65769e-01 ); |
aschut | 0:c9891e1c62f0 | 115 | BiQuad lowp3_2( 1.00000e+00, 2.00000e+00, 1.00000e+00, -1.98532e+00, 9.85677e-01 ); |
aschut | 0:c9891e1c62f0 | 116 | |
aschut | 0:c9891e1c62f0 | 117 | //EMG4!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
aschut | 0:c9891e1c62f0 | 118 | //Highpass |
aschut | 0:c9891e1c62f0 | 119 | BiQuadChain highp4; |
aschut | 0:c9891e1c62f0 | 120 | BiQuad highp4_1( 9.21171e-01, -1.84234e+00, 9.21171e-01, -1.88661e+00, 8.90340e-01 ); |
aschut | 0:c9891e1c62f0 | 121 | BiQuad highp4_2( 1.00000e+00, -2.00000e+00, 1.00000e+00, -1.94922e+00, 9.53070e-01 ); |
aschut | 0:c9891e1c62f0 | 122 | |
aschut | 0:c9891e1c62f0 | 123 | //Notch |
aschut | 0:c9891e1c62f0 | 124 | BiQuadChain notch4; |
aschut | 0:c9891e1c62f0 | 125 | BiQuad notch4_1( 9.56543e-01, -1.82035e+00, 9.56543e-01, -1.84459e+00, 9.53626e-01 ); |
aschut | 0:c9891e1c62f0 | 126 | BiQuad notch4_2( 1.00000e+00, -1.90305e+00, 1.00000e+00, -1.87702e+00, 9.59471e-01 ); |
aschut | 0:c9891e1c62f0 | 127 | |
aschut | 0:c9891e1c62f0 | 128 | //Lowpass 4th order cutoff 3Hz |
aschut | 0:c9891e1c62f0 | 129 | BiQuadChain lowp4; |
aschut | 0:c9891e1c62f0 | 130 | BiQuad lowp4_1( 7.69910e-09, 1.53982e-08, 7.69910e-09, -1.96542e+00, 9.65769e-01 ); |
aschut | 0:c9891e1c62f0 | 131 | BiQuad lowp4_2( 1.00000e+00, 2.00000e+00, 1.00000e+00, -1.98532e+00, 9.85677e-01 ); |
aschut | 0:c9891e1c62f0 | 132 | |
aschut | 0:c9891e1c62f0 | 133 | // ~~~~~~~~~~~~~~~~~~~EMG FUNCTIONS~~~~~~~~~~~~~~~~~~ |
aschut | 0:c9891e1c62f0 | 134 | void emgsample() |
aschut | 0:c9891e1c62f0 | 135 | { |
aschut | 0:c9891e1c62f0 | 136 | |
aschut | 0:c9891e1c62f0 | 137 | //All EMG signal through Highpass |
aschut | 0:c9891e1c62f0 | 138 | double emgread1 = emg1.read(); |
aschut | 0:c9891e1c62f0 | 139 | double emgread2 = emg2.read(); |
aschut | 0:c9891e1c62f0 | 140 | double emgread3 = emg3.read(); |
aschut | 0:c9891e1c62f0 | 141 | double emgread4 = emg4.read(); |
aschut | 0:c9891e1c62f0 | 142 | |
aschut | 0:c9891e1c62f0 | 143 | double emg1_highpassed = highp1.step(emgread1); |
aschut | 0:c9891e1c62f0 | 144 | double emg2_highpassed = highp2.step(emgread2); |
aschut | 0:c9891e1c62f0 | 145 | double emg3_highpassed = highp3.step(emgread3); |
aschut | 0:c9891e1c62f0 | 146 | double emg4_highpassed = highp4.step(emgread4); |
aschut | 0:c9891e1c62f0 | 147 | |
aschut | 0:c9891e1c62f0 | 148 | //All EMG highpassed through Notch |
aschut | 0:c9891e1c62f0 | 149 | double emg1_notched = notch1.step(emg1_highpassed); |
aschut | 0:c9891e1c62f0 | 150 | double emg2_notched = notch2.step(emg2_highpassed); |
aschut | 0:c9891e1c62f0 | 151 | double emg3_notched = notch3.step(emg3_highpassed); |
aschut | 0:c9891e1c62f0 | 152 | double emg4_notched = notch4.step(emg4_highpassed); |
aschut | 0:c9891e1c62f0 | 153 | |
aschut | 0:c9891e1c62f0 | 154 | //All EMG notched rectify |
aschut | 0:c9891e1c62f0 | 155 | double emg1_abs = abs(emg1_notched); |
aschut | 0:c9891e1c62f0 | 156 | double emg2_abs = abs(emg2_notched); |
aschut | 0:c9891e1c62f0 | 157 | double emg3_abs = abs(emg3_notched); |
aschut | 0:c9891e1c62f0 | 158 | double emg4_abs = abs(emg4_notched); |
aschut | 0:c9891e1c62f0 | 159 | |
aschut | 0:c9891e1c62f0 | 160 | //All EMG abs into lowpass |
aschut | 0:c9891e1c62f0 | 161 | double emg1_lp = lowp1.step(emg1_abs); |
aschut | 0:c9891e1c62f0 | 162 | double emg2_lp = lowp2.step(emg2_abs); |
aschut | 0:c9891e1c62f0 | 163 | double emg3_lp = lowp3.step(emg3_abs); |
aschut | 0:c9891e1c62f0 | 164 | double emg4_lp = lowp4.step(emg4_abs); |
aschut | 0:c9891e1c62f0 | 165 | |
aschut | 0:c9891e1c62f0 | 166 | Movag_1.Insert(emg1_lp); |
aschut | 0:c9891e1c62f0 | 167 | Movag_2.Insert(emg2_lp); |
aschut | 0:c9891e1c62f0 | 168 | Movag_3.Insert(emg3_lp); |
aschut | 0:c9891e1c62f0 | 169 | Movag_4.Insert(emg4_lp); |
aschut | 0:c9891e1c62f0 | 170 | |
aschut | 0:c9891e1c62f0 | 171 | emg1_filtered = Movag_1.GetAverage(); |
aschut | 0:c9891e1c62f0 | 172 | emg2_filtered = Movag_2.GetAverage(); |
aschut | 0:c9891e1c62f0 | 173 | emg3_filtered = Movag_3.GetAverage(); |
aschut | 0:c9891e1c62f0 | 174 | emg4_filtered = Movag_4.GetAverage(); |
aschut | 0:c9891e1c62f0 | 175 | |
aschut | 0:c9891e1c62f0 | 176 | |
aschut | 0:c9891e1c62f0 | 177 | |
aschut | 0:c9891e1c62f0 | 178 | } |
aschut | 0:c9891e1c62f0 | 179 | |
aschut | 0:c9891e1c62f0 | 180 | void CalibrationEMG() |
aschut | 0:c9891e1c62f0 | 181 | { |
aschut | 0:c9891e1c62f0 | 182 | //static float samples = calibration_time/ts; |
aschut | 0:c9891e1c62f0 | 183 | while(timer_calibration<37) { |
aschut | 0:c9891e1c62f0 | 184 | if(timer_calibration>0 && timer_calibration<7) { |
aschut | 0:c9891e1c62f0 | 185 | led1=!led1; |
aschut | 0:c9891e1c62f0 | 186 | if(emg1_filtered>temp_highest_emg1) { |
aschut | 0:c9891e1c62f0 | 187 | temp_highest_emg1= emg1_filtered; |
aschut | 0:c9891e1c62f0 | 188 | pc.printf("Temp1 = %f \r\n",temp_highest_emg1); |
aschut | 0:c9891e1c62f0 | 189 | } |
aschut | 0:c9891e1c62f0 | 190 | } |
aschut | 0:c9891e1c62f0 | 191 | if(timer_calibration>7 && timer_calibration<10) { |
aschut | 0:c9891e1c62f0 | 192 | led1=0; |
aschut | 0:c9891e1c62f0 | 193 | led2=0; |
aschut | 0:c9891e1c62f0 | 194 | led3=0; |
aschut | 0:c9891e1c62f0 | 195 | } |
aschut | 0:c9891e1c62f0 | 196 | if(timer_calibration>10 && timer_calibration<17) { |
aschut | 0:c9891e1c62f0 | 197 | led2=!led2; |
aschut | 0:c9891e1c62f0 | 198 | if(emg2_filtered>temp_highest_emg2) { |
aschut | 0:c9891e1c62f0 | 199 | temp_highest_emg2= emg2_filtered; |
aschut | 0:c9891e1c62f0 | 200 | pc.printf("Temp2 = %f \r\n",temp_highest_emg2); |
aschut | 0:c9891e1c62f0 | 201 | } |
aschut | 0:c9891e1c62f0 | 202 | } |
aschut | 0:c9891e1c62f0 | 203 | if(timer_calibration>17 && timer_calibration<20) { |
aschut | 0:c9891e1c62f0 | 204 | led1=0; |
aschut | 0:c9891e1c62f0 | 205 | led2=0; |
aschut | 0:c9891e1c62f0 | 206 | led3=0; |
aschut | 0:c9891e1c62f0 | 207 | } |
aschut | 0:c9891e1c62f0 | 208 | if(timer_calibration>20 && timer_calibration<27) { |
aschut | 0:c9891e1c62f0 | 209 | led3=!led3; |
aschut | 0:c9891e1c62f0 | 210 | if(emg3_filtered>temp_highest_emg3) { |
aschut | 0:c9891e1c62f0 | 211 | temp_highest_emg3= emg3_filtered; |
aschut | 0:c9891e1c62f0 | 212 | pc.printf("Temp3 = %f \r\n",temp_highest_emg3); |
aschut | 0:c9891e1c62f0 | 213 | } |
aschut | 0:c9891e1c62f0 | 214 | } |
aschut | 0:c9891e1c62f0 | 215 | if(timer_calibration>27 && timer_calibration<30) { |
aschut | 0:c9891e1c62f0 | 216 | led1=0; |
aschut | 0:c9891e1c62f0 | 217 | led2=0; |
aschut | 0:c9891e1c62f0 | 218 | led3=0; |
aschut | 0:c9891e1c62f0 | 219 | } |
aschut | 0:c9891e1c62f0 | 220 | if(timer_calibration>30 && timer_calibration<37) { |
aschut | 0:c9891e1c62f0 | 221 | led2=!led2; |
aschut | 0:c9891e1c62f0 | 222 | led3=!led3; |
aschut | 0:c9891e1c62f0 | 223 | if(emg4_filtered>temp_highest_emg4) { |
aschut | 0:c9891e1c62f0 | 224 | temp_highest_emg4= emg4_filtered; |
aschut | 0:c9891e1c62f0 | 225 | pc.printf("Temp4 = %f \r\n",temp_highest_emg4); |
aschut | 0:c9891e1c62f0 | 226 | } |
aschut | 0:c9891e1c62f0 | 227 | } |
aschut | 0:c9891e1c62f0 | 228 | led1=1; |
aschut | 0:c9891e1c62f0 | 229 | led2=1; |
aschut | 0:c9891e1c62f0 | 230 | led3=1; |
aschut | 0:c9891e1c62f0 | 231 | |
aschut | 0:c9891e1c62f0 | 232 | |
aschut | 0:c9891e1c62f0 | 233 | } |
aschut | 0:c9891e1c62f0 | 234 | |
aschut | 0:c9891e1c62f0 | 235 | pc.printf("Highest value Duim= %f \r\n", temp_highest_emg1); |
aschut | 0:c9891e1c62f0 | 236 | pc.printf("Highest value Bicep= %f \r\n", temp_highest_emg2); |
aschut | 0:c9891e1c62f0 | 237 | pc.printf("Highest value Tricep= %f \r\n", temp_highest_emg3); |
aschut | 0:c9891e1c62f0 | 238 | pc.printf("Highest value Onderarm= %f \r\n", temp_highest_emg4); |
aschut | 0:c9891e1c62f0 | 239 | |
aschut | 0:c9891e1c62f0 | 240 | |
aschut | 0:c9891e1c62f0 | 241 | threshold1 = temp_highest_emg1*duim_p_t; //Right Biceps |
aschut | 0:c9891e1c62f0 | 242 | threshold2 = temp_highest_emg2*bicep_p_t; //Right Triceps |
aschut | 0:c9891e1c62f0 | 243 | threshold3 = temp_highest_emg3*tricep_p_t; //Left Biceps |
aschut | 0:c9891e1c62f0 | 244 | threshold4 = temp_highest_emg4*onderarm_p_t; //Left Triceps |
aschut | 0:c9891e1c62f0 | 245 | } |
aschut | 0:c9891e1c62f0 | 246 | |
aschut | 0:c9891e1c62f0 | 247 | //Check if emg_filtered has reached their threshold |
aschut | 0:c9891e1c62f0 | 248 | void threshold_check() |
aschut | 0:c9891e1c62f0 | 249 | { |
aschut | 0:c9891e1c62f0 | 250 | |
aschut | 0:c9891e1c62f0 | 251 | //EMG1 threshold check |
aschut | 0:c9891e1c62f0 | 252 | if(emg1_filtered>threshold1) { |
aschut | 0:c9891e1c62f0 | 253 | Duim = 1; |
aschut | 0:c9891e1c62f0 | 254 | led1 = !led1; |
aschut | 0:c9891e1c62f0 | 255 | } else { |
aschut | 0:c9891e1c62f0 | 256 | Duim= 0; |
aschut | 0:c9891e1c62f0 | 257 | } |
aschut | 0:c9891e1c62f0 | 258 | //EMG2 threshold check |
aschut | 0:c9891e1c62f0 | 259 | if(emg2_filtered>threshold2) { |
aschut | 0:c9891e1c62f0 | 260 | Bicep = 1; |
aschut | 0:c9891e1c62f0 | 261 | led2 = !led2; |
aschut | 0:c9891e1c62f0 | 262 | } else { |
aschut | 0:c9891e1c62f0 | 263 | Bicep= 0; |
aschut | 0:c9891e1c62f0 | 264 | } |
aschut | 0:c9891e1c62f0 | 265 | //EMG3 threshold check |
aschut | 0:c9891e1c62f0 | 266 | if(emg3_filtered>threshold3) { |
aschut | 0:c9891e1c62f0 | 267 | Tricep = 1; |
aschut | 0:c9891e1c62f0 | 268 | led3 = !led3; |
aschut | 0:c9891e1c62f0 | 269 | } else { |
aschut | 0:c9891e1c62f0 | 270 | Tricep= 0; |
aschut | 0:c9891e1c62f0 | 271 | } |
aschut | 0:c9891e1c62f0 | 272 | //EMG4 threshold check |
aschut | 0:c9891e1c62f0 | 273 | if(emg4_filtered>threshold4) { |
aschut | 0:c9891e1c62f0 | 274 | Onderarm = 1; |
aschut | 0:c9891e1c62f0 | 275 | led1 = !led1; |
aschut | 0:c9891e1c62f0 | 276 | led2 = !led1; |
aschut | 0:c9891e1c62f0 | 277 | led3 = !led1; |
aschut | 0:c9891e1c62f0 | 278 | } else { |
aschut | 0:c9891e1c62f0 | 279 | Onderarm= 0; |
aschut | 0:c9891e1c62f0 | 280 | } |
aschut | 0:c9891e1c62f0 | 281 | |
aschut | 0:c9891e1c62f0 | 282 | |
aschut | 0:c9891e1c62f0 | 283 | |
aschut | 0:c9891e1c62f0 | 284 | } |
aschut | 0:c9891e1c62f0 | 285 | |
aschut | 0:c9891e1c62f0 | 286 | |
aschut | 0:c9891e1c62f0 | 287 | Ticker sample_timer; |
aschut | 0:c9891e1c62f0 | 288 | |
aschut | 0:c9891e1c62f0 | 289 | |
aschut | 0:c9891e1c62f0 | 290 | |
aschut | 0:c9891e1c62f0 | 291 | void sample() |
aschut | 0:c9891e1c62f0 | 292 | { |
aschut | 0:c9891e1c62f0 | 293 | pc.printf("Duim Right = %i\r\n", Duim); |
aschut | 0:c9891e1c62f0 | 294 | pc.printf("Bicep Right = %i\r\n",Bicep); |
aschut | 0:c9891e1c62f0 | 295 | pc.printf("tricep Left = %i\r\n", Tricep); |
aschut | 0:c9891e1c62f0 | 296 | pc.printf("onderarm Left = %i\r\n", Onderarm); |
aschut | 0:c9891e1c62f0 | 297 | wait(0.01); |
aschut | 0:c9891e1c62f0 | 298 | |
aschut | 0:c9891e1c62f0 | 299 | |
aschut | 0:c9891e1c62f0 | 300 | } |
aschut | 0:c9891e1c62f0 | 301 | |
aschut | 0:c9891e1c62f0 | 302 | int main() |
aschut | 0:c9891e1c62f0 | 303 | { |
aschut | 0:c9891e1c62f0 | 304 | sample_ticker.attach(&emgsample, ts); |
aschut | 0:c9891e1c62f0 | 305 | pc.baud(115200); |
aschut | 0:c9891e1c62f0 | 306 | |
aschut | 0:c9891e1c62f0 | 307 | //BiQuad Chain add |
aschut | 0:c9891e1c62f0 | 308 | highp1.add( &highp1_1 ).add( &highp1_2 ); |
aschut | 0:c9891e1c62f0 | 309 | notch1.add( ¬ch1_1 ).add( ¬ch1_2 ); |
aschut | 0:c9891e1c62f0 | 310 | lowp1.add( &lowp1_1 ).add(&lowp1_2); |
aschut | 0:c9891e1c62f0 | 311 | |
aschut | 0:c9891e1c62f0 | 312 | highp2.add( &highp2_1 ).add( &highp2_2 ); |
aschut | 0:c9891e1c62f0 | 313 | notch2.add( ¬ch2_1 ).add( ¬ch2_2 ); |
aschut | 0:c9891e1c62f0 | 314 | lowp2.add( &lowp2_1 ).add(&lowp2_2); |
aschut | 0:c9891e1c62f0 | 315 | |
aschut | 0:c9891e1c62f0 | 316 | highp3.add( &highp3_1 ).add( &highp3_2 ); |
aschut | 0:c9891e1c62f0 | 317 | notch3.add( ¬ch3_1 ).add( ¬ch3_2 ); |
aschut | 0:c9891e1c62f0 | 318 | lowp3.add( &lowp3_1 ).add(&lowp3_2); |
aschut | 0:c9891e1c62f0 | 319 | |
aschut | 0:c9891e1c62f0 | 320 | highp4.add( &highp4_1 ).add( &highp4_2 ); |
aschut | 0:c9891e1c62f0 | 321 | notch4.add( ¬ch4_1 ).add( ¬ch4_2 ); |
aschut | 0:c9891e1c62f0 | 322 | lowp4.add( &lowp4_1 ).add(&lowp4_2); |
aschut | 0:c9891e1c62f0 | 323 | |
aschut | 0:c9891e1c62f0 | 324 | |
aschut | 0:c9891e1c62f0 | 325 | |
aschut | 0:c9891e1c62f0 | 326 | temp_highest_emg1 = 0; //highest detected value right biceps |
aschut | 0:c9891e1c62f0 | 327 | temp_highest_emg2 = 0; |
aschut | 0:c9891e1c62f0 | 328 | temp_highest_emg3 = 0; |
aschut | 0:c9891e1c62f0 | 329 | temp_highest_emg4 = 0; |
aschut | 0:c9891e1c62f0 | 330 | |
aschut | 0:c9891e1c62f0 | 331 | timer_calibration.reset(); |
aschut | 0:c9891e1c62f0 | 332 | timer_calibration.start(); |
aschut | 0:c9891e1c62f0 | 333 | |
aschut | 0:c9891e1c62f0 | 334 | |
aschut | 0:c9891e1c62f0 | 335 | CalibrationEMG(); |
aschut | 0:c9891e1c62f0 | 336 | threshold_check_ticker.attach(threshold_check, 0.1); |
aschut | 0:c9891e1c62f0 | 337 | sample_timer.attach(&sample, 0.1); |
aschut | 0:c9891e1c62f0 | 338 | //sample_ticker.detach(); |
aschut | 0:c9891e1c62f0 | 339 | timer_calibration.stop(); |
aschut | 0:c9891e1c62f0 | 340 | |
aschut | 0:c9891e1c62f0 | 341 | |
aschut | 0:c9891e1c62f0 | 342 | |
aschut | 0:c9891e1c62f0 | 343 | |
aschut | 0:c9891e1c62f0 | 344 | /*empty loop, sample() is executed periodically*/ |
aschut | 0:c9891e1c62f0 | 345 | while(1) { |
aschut | 0:c9891e1c62f0 | 346 | wait(0.01); |
aschut | 0:c9891e1c62f0 | 347 | } |
aschut | 0:c9891e1c62f0 | 348 | } |