8 option EMG
Dependencies: HIDScope MODSERIAL QEI biquadFilter mbed
main.cpp@27:ca07f895f999, 2017-10-10 (annotated)
- Committer:
- ralphg_92
- Date:
- Tue Oct 10 13:46:49 2017 +0000
- Revision:
- 27:ca07f895f999
- Parent:
- 26:c9ba45bdd5c9
- Child:
- 28:ec0763106a2e
new try
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:32bb76391d89 | 1 | #include "mbed.h" |
vsluiter | 11:ce72ec658a95 | 2 | #include "HIDScope.h" |
relvorelvo | 21:136a1ab8163c | 3 | #include "BiQuad.h" |
relvorelvo | 21:136a1ab8163c | 4 | #include "MODSERIAL.h" |
vsluiter | 0:32bb76391d89 | 5 | |
vsluiter | 4:8b298dfada81 | 6 | //Define objects |
relvorelvo | 25:07187cf76863 | 7 | AnalogIn emg1_in( A0 ); /* read out the signal */ |
relvorelvo | 23:e5db011bd410 | 8 | AnalogIn emg2_in( A1 ); |
relvorelvo | 23:e5db011bd410 | 9 | AnalogIn emg3_in( A2 ); |
ralphg_92 | 27:ca07f895f999 | 10 | AnalogIn emg4_in( A3 ); |
ralphg_92 | 27:ca07f895f999 | 11 | DigitalIn max_reader12( SW2 ); |
ralphg_92 | 27:ca07f895f999 | 12 | DigitalIn max_reader34( SW3 ); |
relvorelvo | 23:e5db011bd410 | 13 | |
relvorelvo | 23:e5db011bd410 | 14 | Ticker main_timer; |
relvorelvo | 23:e5db011bd410 | 15 | Ticker max_read1; |
relvorelvo | 23:e5db011bd410 | 16 | Ticker max_read3; |
ralphg_92 | 27:ca07f895f999 | 17 | HIDScope scope( 4 ); |
relvorelvo | 23:e5db011bd410 | 18 | DigitalOut red(LED_RED); |
relvorelvo | 23:e5db011bd410 | 19 | DigitalOut blue(LED_BLUE); |
relvorelvo | 23:e5db011bd410 | 20 | DigitalOut green(LED_GREEN); |
relvorelvo | 23:e5db011bd410 | 21 | MODSERIAL pc(USBTX, USBRX); |
relvorelvo | 21:136a1ab8163c | 22 | |
relvorelvo | 21:136a1ab8163c | 23 | |
relvorelvo | 21:136a1ab8163c | 24 | // EMG variables |
relvorelvo | 23:e5db011bd410 | 25 | //Right Biceps |
relvorelvo | 23:e5db011bd410 | 26 | double emg1; |
relvorelvo | 23:e5db011bd410 | 27 | double emg1highfilter; |
relvorelvo | 23:e5db011bd410 | 28 | double emg1notchfilter; |
relvorelvo | 23:e5db011bd410 | 29 | double emg1abs; |
relvorelvo | 23:e5db011bd410 | 30 | double emg1lowfilter; |
relvorelvo | 23:e5db011bd410 | 31 | double emg1peak; |
ralphg_92 | 27:ca07f895f999 | 32 | double max1; |
relvorelvo | 24:26659f1de039 | 33 | double maxpart1; |
relvorelvo | 23:e5db011bd410 | 34 | // Left Biceps |
relvorelvo | 23:e5db011bd410 | 35 | double emg2; |
relvorelvo | 23:e5db011bd410 | 36 | double emg2highfilter; |
relvorelvo | 23:e5db011bd410 | 37 | double emg2notchfilter; |
relvorelvo | 23:e5db011bd410 | 38 | double emg2abs; |
relvorelvo | 23:e5db011bd410 | 39 | double emg2lowfilter; |
relvorelvo | 23:e5db011bd410 | 40 | double emg2peak; |
ralphg_92 | 27:ca07f895f999 | 41 | double max2; |
relvorelvo | 24:26659f1de039 | 42 | double maxpart2; |
ralphg_92 | 27:ca07f895f999 | 43 | // Left Lower Arm |
relvorelvo | 23:e5db011bd410 | 44 | double emg3; |
relvorelvo | 23:e5db011bd410 | 45 | double emg3highfilter; |
relvorelvo | 23:e5db011bd410 | 46 | double emg3notchfilter; |
relvorelvo | 23:e5db011bd410 | 47 | double emg3abs; |
relvorelvo | 23:e5db011bd410 | 48 | double emg3lowfilter; |
relvorelvo | 23:e5db011bd410 | 49 | double emg3peak; |
relvorelvo | 23:e5db011bd410 | 50 | double max3; |
relvorelvo | 23:e5db011bd410 | 51 | double maxpart3; |
ralphg_92 | 27:ca07f895f999 | 52 | // Right Lower Arm |
ralphg_92 | 27:ca07f895f999 | 53 | double emg4; |
ralphg_92 | 27:ca07f895f999 | 54 | double emg4highfilter; |
ralphg_92 | 27:ca07f895f999 | 55 | double emg4notchfilter; |
ralphg_92 | 27:ca07f895f999 | 56 | double emg4abs; |
ralphg_92 | 27:ca07f895f999 | 57 | double emg4lowfilter; |
ralphg_92 | 27:ca07f895f999 | 58 | double emg4peak; |
ralphg_92 | 27:ca07f895f999 | 59 | double max4; |
ralphg_92 | 27:ca07f895f999 | 60 | double maxpart4; |
vsluiter | 2:e314bb3b2d99 | 61 | |
relvorelvo | 25:07187cf76863 | 62 | // BiQuad Filter Settings |
relvorelvo | 23:e5db011bd410 | 63 | // Right Biceps |
relvorelvo | 23:e5db011bd410 | 64 | BiQuad filterhigh1(9.704e-01,-1.9408,9.704e-01,-1.9389,9.427e-01); /* Filter at 10 Hz */ |
relvorelvo | 23:e5db011bd410 | 65 | BiQuad filternotch1(9.495e-01,-1.8062,9.495e-01,-1.8062,8.992e-01); /* Filter at 50 Hz */ |
relvorelvo | 25:07187cf76863 | 66 | BiQuad filterpeak1(1.0033,-1.984,9.852e-01,-1.9838,9.8855e-01); /* 4dB Gain peak at 11 Hz */ |
relvorelvo | 24:26659f1de039 | 67 | BiQuad filterlow1(1.368e-03,2.737e-03,1.369e-03,-1.9219,9.274e-01); /* Filter at 15 Hz */ |
relvorelvo | 23:e5db011bd410 | 68 | // Left Biceps |
relvorelvo | 23:e5db011bd410 | 69 | BiQuad filterhigh2(9.704e-01,-1.9408,9.704e-01,-1.9389,9.427e-01); |
relvorelvo | 23:e5db011bd410 | 70 | BiQuad filternotch2(9.495e-01,-1.8062,9.495e-01,-1.8062,8.992e-01); |
relvorelvo | 24:26659f1de039 | 71 | BiQuad filterpeak2(1.0033,-1.984,9.852e-01,-1.9838,9.8855e-01); |
relvorelvo | 24:26659f1de039 | 72 | BiQuad filterlow2(1.368e-03,2.737e-03,1.369e-03,-1.9219,9.274e-01); |
relvorelvo | 23:e5db011bd410 | 73 | // Left Lower Arm OR Triceps |
relvorelvo | 23:e5db011bd410 | 74 | BiQuad filterhigh3(9.704e-01,-1.9408,9.704e-01,-1.9389,9.427e-01); |
relvorelvo | 23:e5db011bd410 | 75 | BiQuad filternotch3(9.495e-01,-1.8062,9.495e-01,-1.8062,8.992e-01); |
relvorelvo | 24:26659f1de039 | 76 | BiQuad filterpeak3(1.0033,-1.984,9.852e-01,-1.9838,9.8855e-01); |
relvorelvo | 24:26659f1de039 | 77 | BiQuad filterlow3(1.368e-03,2.737e-03,1.369e-03,-1.9219,9.274e-01); |
ralphg_92 | 27:ca07f895f999 | 78 | // Right Lower Arm OR Triceps |
ralphg_92 | 27:ca07f895f999 | 79 | BiQuad filterhigh4(9.704e-01,-1.9408,9.704e-01,-1.9389,9.427e-01); |
ralphg_92 | 27:ca07f895f999 | 80 | BiQuad filternotch4(9.495e-01,-1.8062,9.495e-01,-1.8062,8.992e-01); |
ralphg_92 | 27:ca07f895f999 | 81 | BiQuad filterpeak4(1.0033,-1.984,9.852e-01,-1.9838,9.8855e-01); |
ralphg_92 | 27:ca07f895f999 | 82 | BiQuad filterlow4(1.368e-03,2.737e-03,1.369e-03,-1.9219,9.274e-01); |
relvorelvo | 23:e5db011bd410 | 83 | // |
relvorelvo | 21:136a1ab8163c | 84 | |
relvorelvo | 25:07187cf76863 | 85 | // Finding max values for correct motor switch if the button is pressed |
relvorelvo | 23:e5db011bd410 | 86 | void get_max1(){ |
ralphg_92 | 27:ca07f895f999 | 87 | if (max_reader12==0){ |
relvorelvo | 23:e5db011bd410 | 88 | green = !green; |
relvorelvo | 23:e5db011bd410 | 89 | red = 1; |
relvorelvo | 23:e5db011bd410 | 90 | blue = 1; |
relvorelvo | 25:07187cf76863 | 91 | for(int n=0;n<2000;n++){ /* measure 2000 samples and filter it */ |
ralphg_92 | 27:ca07f895f999 | 92 | |
relvorelvo | 25:07187cf76863 | 93 | emg1 = emg1_in.read(); /* read out emg */ |
relvorelvo | 25:07187cf76863 | 94 | emg1highfilter = filterhigh1.step(emg1); /* high pass filtered */ |
relvorelvo | 25:07187cf76863 | 95 | emg1notchfilter = filternotch1.step(emg1highfilter); /* notch filtered */ |
relvorelvo | 25:07187cf76863 | 96 | emg1abs = fabs(emg1notchfilter); /* take the absolute value */ |
relvorelvo | 25:07187cf76863 | 97 | emg1lowfilter = filterlow1.step(emg1abs); /* low pass filtered */ |
relvorelvo | 25:07187cf76863 | 98 | emg1peak = filterpeak1.step(emg1lowfilter); /* 4dB gain peak */ |
relvorelvo | 23:e5db011bd410 | 99 | |
ralphg_92 | 27:ca07f895f999 | 100 | emg2 = emg2_in.read(); /* read out emg */ |
ralphg_92 | 27:ca07f895f999 | 101 | emg2highfilter = filterhigh2.step(emg2); /* high pass filtered */ |
ralphg_92 | 27:ca07f895f999 | 102 | emg2notchfilter = filternotch2.step(emg2highfilter); /* notch filtered */ |
ralphg_92 | 27:ca07f895f999 | 103 | emg2abs = fabs(emg2notchfilter); /* take the absolute value */ |
ralphg_92 | 27:ca07f895f999 | 104 | emg2lowfilter = filterlow2.step(emg2abs); /* low pass filtered */ |
ralphg_92 | 27:ca07f895f999 | 105 | emg2peak = filterpeak2.step(emg2lowfilter); /* 4dB gain peak */ |
ralphg_92 | 27:ca07f895f999 | 106 | |
relvorelvo | 23:e5db011bd410 | 107 | if (max1<emg1peak){ |
relvorelvo | 26:c9ba45bdd5c9 | 108 | max1 = emg1peak; /* set the max value at the highest measured value */ |
relvorelvo | 23:e5db011bd410 | 109 | } |
ralphg_92 | 27:ca07f895f999 | 110 | if (max2<emg2peak){ |
ralphg_92 | 27:ca07f895f999 | 111 | max2 = emg2peak; /* set the max value at the highest measured value */ |
ralphg_92 | 27:ca07f895f999 | 112 | } |
relvorelvo | 26:c9ba45bdd5c9 | 113 | wait(0.001f); /* measure at 1000Hz */ |
relvorelvo | 23:e5db011bd410 | 114 | } |
relvorelvo | 23:e5db011bd410 | 115 | wait(0.2f); |
relvorelvo | 23:e5db011bd410 | 116 | green = 1; |
relvorelvo | 23:e5db011bd410 | 117 | } |
ralphg_92 | 27:ca07f895f999 | 118 | maxpart1 = 0.15*max1; /* set cut off voltage at 15% of max for right biceps */ |
relvorelvo | 26:c9ba45bdd5c9 | 119 | maxpart2 = 0.15*max1; /* set cut off votage at 15% of max for left biceps */ |
relvorelvo | 23:e5db011bd410 | 120 | } |
relvorelvo | 21:136a1ab8163c | 121 | |
relvorelvo | 23:e5db011bd410 | 122 | void get_max3(){ |
ralphg_92 | 27:ca07f895f999 | 123 | if (max_reader34==0){ |
relvorelvo | 23:e5db011bd410 | 124 | green = 1; |
relvorelvo | 23:e5db011bd410 | 125 | blue = 1; |
relvorelvo | 23:e5db011bd410 | 126 | red = !red; |
relvorelvo | 23:e5db011bd410 | 127 | for(int n=0;n<2000;n++){ |
relvorelvo | 23:e5db011bd410 | 128 | |
relvorelvo | 23:e5db011bd410 | 129 | emg3 = emg3_in.read(); |
relvorelvo | 23:e5db011bd410 | 130 | emg3highfilter = filterhigh3.step(emg3); |
relvorelvo | 23:e5db011bd410 | 131 | emg3notchfilter = filternotch3.step(emg3highfilter); |
relvorelvo | 23:e5db011bd410 | 132 | emg3abs = fabs(emg3notchfilter); |
relvorelvo | 23:e5db011bd410 | 133 | emg3lowfilter = filterlow3.step(emg3abs); |
relvorelvo | 23:e5db011bd410 | 134 | emg3peak = filterpeak3.step(emg3lowfilter); |
relvorelvo | 23:e5db011bd410 | 135 | |
ralphg_92 | 27:ca07f895f999 | 136 | emg4 = emg4_in.read(); |
ralphg_92 | 27:ca07f895f999 | 137 | emg4highfilter = filterhigh4.step(emg4); |
ralphg_92 | 27:ca07f895f999 | 138 | emg4notchfilter = filternotch4.step(emg4highfilter); |
ralphg_92 | 27:ca07f895f999 | 139 | emg4abs = fabs(emg4notchfilter); |
ralphg_92 | 27:ca07f895f999 | 140 | emg4lowfilter = filterlow4.step(emg4abs); |
ralphg_92 | 27:ca07f895f999 | 141 | emg4peak = filterpeak4.step(emg4lowfilter); |
ralphg_92 | 27:ca07f895f999 | 142 | |
relvorelvo | 23:e5db011bd410 | 143 | if (max3<emg3peak){ |
relvorelvo | 26:c9ba45bdd5c9 | 144 | max3 = emg3peak; /* set the max value at the highest measured value */ |
relvorelvo | 23:e5db011bd410 | 145 | } |
ralphg_92 | 27:ca07f895f999 | 146 | if (max4<emg4peak){ |
ralphg_92 | 27:ca07f895f999 | 147 | max4 = emg4peak; /* set the max value at the highest measured value */ |
ralphg_92 | 27:ca07f895f999 | 148 | } |
relvorelvo | 23:e5db011bd410 | 149 | wait(0.001f); |
relvorelvo | 23:e5db011bd410 | 150 | } |
relvorelvo | 23:e5db011bd410 | 151 | wait(0.2f); |
relvorelvo | 23:e5db011bd410 | 152 | red = 1; |
relvorelvo | 23:e5db011bd410 | 153 | } |
ralphg_92 | 27:ca07f895f999 | 154 | maxpart3 = 0.25*max3; /* set cut off voltage at 25% of max for left lower arm */ |
ralphg_92 | 27:ca07f895f999 | 155 | maxpart4 = 0.25*max4; /* set cut off voltage at 25% of max for right lower arm */ |
vsluiter | 2:e314bb3b2d99 | 156 | } |
vsluiter | 0:32bb76391d89 | 157 | |
relvorelvo | 23:e5db011bd410 | 158 | // Filtering & Scope |
relvorelvo | 23:e5db011bd410 | 159 | void filter() { |
relvorelvo | 23:e5db011bd410 | 160 | // Right Biceps |
relvorelvo | 23:e5db011bd410 | 161 | emg1 = emg1_in.read(); |
relvorelvo | 23:e5db011bd410 | 162 | emg1highfilter = filterhigh1.step(emg1); |
relvorelvo | 23:e5db011bd410 | 163 | emg1notchfilter = filternotch1.step(emg1highfilter); |
relvorelvo | 23:e5db011bd410 | 164 | emg1abs = fabs(emg1notchfilter); |
relvorelvo | 23:e5db011bd410 | 165 | emg1lowfilter = filterlow1.step(emg1abs); |
relvorelvo | 26:c9ba45bdd5c9 | 166 | emg1peak = filterpeak1.step(emg1lowfilter); /* Final Right Biceps values to be sent */ |
relvorelvo | 23:e5db011bd410 | 167 | // Left Biceps |
relvorelvo | 23:e5db011bd410 | 168 | emg2 = emg2_in.read(); |
relvorelvo | 23:e5db011bd410 | 169 | emg2highfilter = filterhigh2.step(emg2); |
relvorelvo | 23:e5db011bd410 | 170 | emg2notchfilter = filternotch2.step(emg2highfilter); |
relvorelvo | 23:e5db011bd410 | 171 | emg2abs = fabs(emg2notchfilter); |
relvorelvo | 23:e5db011bd410 | 172 | emg2lowfilter = filterlow2.step(emg2abs); |
relvorelvo | 26:c9ba45bdd5c9 | 173 | emg2peak = filterpeak2.step(emg2lowfilter); /* Final Left Biceps values to be sent */ |
relvorelvo | 23:e5db011bd410 | 174 | // Left Lower Arm OR Triceps |
relvorelvo | 23:e5db011bd410 | 175 | emg3 = emg3_in.read(); |
relvorelvo | 23:e5db011bd410 | 176 | emg3highfilter = filterhigh3.step(emg3); |
relvorelvo | 23:e5db011bd410 | 177 | emg3notchfilter = filternotch3.step(emg3highfilter); |
relvorelvo | 23:e5db011bd410 | 178 | emg3abs = fabs(emg3notchfilter); |
relvorelvo | 23:e5db011bd410 | 179 | emg3lowfilter = filterlow3.step(emg3abs); |
relvorelvo | 26:c9ba45bdd5c9 | 180 | emg3peak = filterpeak3.step(emg3lowfilter); /* Final Lower Arm values to be sent */ |
ralphg_92 | 27:ca07f895f999 | 181 | // Right Lower Arm OR Triceps |
ralphg_92 | 27:ca07f895f999 | 182 | emg4 = emg4_in.read(); |
ralphg_92 | 27:ca07f895f999 | 183 | emg4highfilter = filterhigh4.step(emg4); |
ralphg_92 | 27:ca07f895f999 | 184 | emg4notchfilter = filternotch4.step(emg4highfilter); |
ralphg_92 | 27:ca07f895f999 | 185 | emg4abs = fabs(emg4notchfilter); |
ralphg_92 | 27:ca07f895f999 | 186 | emg4lowfilter = filterlow4.step(emg4abs); |
ralphg_92 | 27:ca07f895f999 | 187 | emg4peak = filterpeak4.step(emg4lowfilter); /* Final Lower Arm values to be sent */ |
relvorelvo | 23:e5db011bd410 | 188 | |
relvorelvo | 23:e5db011bd410 | 189 | |
relvorelvo | 23:e5db011bd410 | 190 | /* Compare measurement to the calibrated value to decide actions */ |
relvorelvo | 26:c9ba45bdd5c9 | 191 | if (maxpart1<emg1peak){ /* See if right biceps is contracting */ |
relvorelvo | 23:e5db011bd410 | 192 | red = 0; |
relvorelvo | 23:e5db011bd410 | 193 | blue = 1; |
relvorelvo | 23:e5db011bd410 | 194 | green = 1; |
relvorelvo | 23:e5db011bd410 | 195 | } |
relvorelvo | 23:e5db011bd410 | 196 | else { |
relvorelvo | 26:c9ba45bdd5c9 | 197 | if (maxpart2<emg2peak){ /* See if left biceps is contracting */ |
relvorelvo | 23:e5db011bd410 | 198 | red = 1; |
relvorelvo | 23:e5db011bd410 | 199 | blue = 0; |
relvorelvo | 23:e5db011bd410 | 200 | green = 1; |
relvorelvo | 23:e5db011bd410 | 201 | } |
relvorelvo | 23:e5db011bd410 | 202 | |
relvorelvo | 23:e5db011bd410 | 203 | else { |
ralphg_92 | 27:ca07f895f999 | 204 | if (maxpart3<emg3peak){ /* See if lower left arm is contracting */ |
relvorelvo | 23:e5db011bd410 | 205 | red = 1; |
relvorelvo | 23:e5db011bd410 | 206 | blue = 1; |
relvorelvo | 23:e5db011bd410 | 207 | green = 0; |
relvorelvo | 23:e5db011bd410 | 208 | } |
ralphg_92 | 27:ca07f895f999 | 209 | else { |
ralphg_92 | 27:ca07f895f999 | 210 | if (maxpart4<emg3peak){ /* See if lower right arm is contracting */ |
ralphg_92 | 27:ca07f895f999 | 211 | red = 0; |
ralphg_92 | 27:ca07f895f999 | 212 | blue = 0; |
ralphg_92 | 27:ca07f895f999 | 213 | green = 0; |
ralphg_92 | 27:ca07f895f999 | 214 | } |
relvorelvo | 23:e5db011bd410 | 215 | else { |
relvorelvo | 26:c9ba45bdd5c9 | 216 | red = 1; /* Shut down all led colors if no movement is registered */ |
relvorelvo | 23:e5db011bd410 | 217 | blue = 1; |
relvorelvo | 23:e5db011bd410 | 218 | green = 1; |
relvorelvo | 23:e5db011bd410 | 219 | } |
relvorelvo | 23:e5db011bd410 | 220 | } |
relvorelvo | 23:e5db011bd410 | 221 | } |
ralphg_92 | 27:ca07f895f999 | 222 | } |
relvorelvo | 23:e5db011bd410 | 223 | /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */ |
relvorelvo | 26:c9ba45bdd5c9 | 224 | scope.set(0, emg1peak ); /* plot Right biceps voltage */ |
relvorelvo | 26:c9ba45bdd5c9 | 225 | scope.set(1, emg2peak ); /* Plot Left biceps voltage */ |
ralphg_92 | 27:ca07f895f999 | 226 | scope.set(2, emg3peak ); /* Plot Lower Left Arm voltage */ |
ralphg_92 | 27:ca07f895f999 | 227 | scope.set(3, emg4peak ); /* Plot Lower Right Arm Voltage */ |
relvorelvo | 23:e5db011bd410 | 228 | |
relvorelvo | 25:07187cf76863 | 229 | scope.send(); /* send everything to the HID scope */ |
relvorelvo | 23:e5db011bd410 | 230 | } |
tomlankhorst | 15:0da764eea774 | 231 | |
relvorelvo | 23:e5db011bd410 | 232 | int main(){ |
relvorelvo | 22:68ab712b62b2 | 233 | |
relvorelvo | 25:07187cf76863 | 234 | main_timer.attach(&filter, 0.001); /* set frequency for the filters at 1000Hz */ |
relvorelvo | 25:07187cf76863 | 235 | max_read1.attach(&get_max1, 2); /* set the frequency of the calibration loop at 0.5Hz */ |
relvorelvo | 23:e5db011bd410 | 236 | max_read3.attach(&get_max3, 2); |
tomlankhorst | 15:0da764eea774 | 237 | while(1) {} |
vsluiter | 0:32bb76391d89 | 238 | } |