the emg filtering part of the program

Dependencies:   HIDScope biquadFilter mbed MODSERIAL

Fork of EMG by Tom Tom

Committer:
RiP
Date:
Thu Oct 20 13:24:39 2016 +0000
Revision:
26:91d48c0b722d
Parent:
25:1a71424b05ff
Child:
27:1ff7fa636f1c
calibration interrupt is kapot

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vsluiter 0:32bb76391d89 1 #include "mbed.h"
vsluiter 11:ce72ec658a95 2 #include "HIDScope.h"
RiP 21:3aecd735319d 3 #include "BiQuad.h"
RiP 22:f38a15e851d2 4 #include "MODSERIAL.h"
RiP 23:54d28f9eef53 5 #include <string.h>
vsluiter 0:32bb76391d89 6
vsluiter 4:8b298dfada81 7 //Define objects
RiP 24:01b4b51b5dc6 8
RiP 24:01b4b51b5dc6 9 InterruptIn button_calibrate(PTA4);
RiP 21:3aecd735319d 10 AnalogIn emg1( A0 );
RiP 21:3aecd735319d 11 AnalogIn emg2( A1 );
RiP 21:3aecd735319d 12 AnalogIn emg3( A2 );
tomlankhorst 19:2bf824669684 13
tomlankhorst 14:f83354387756 14 Ticker sample_timer;
RiP 21:3aecd735319d 15 HIDScope scope( 6 );
RiP 22:f38a15e851d2 16 MODSERIAL pc(USBTX, USBRX);
tomlankhorst 18:21d8e7a81cf5 17 DigitalOut led(LED1);
vsluiter 2:e314bb3b2d99 18
RiP 22:f38a15e851d2 19 volatile bool sampletimer = false;
RiP 24:01b4b51b5dc6 20 volatile bool buttonflag = false;
RiP 25:1a71424b05ff 21 volatile bool newcase = false;
RiP 25:1a71424b05ff 22
RiP 23:54d28f9eef53 23 double threshold = 0.04;
RiP 23:54d28f9eef53 24 double samplefreq=0.002;
RiP 25:1a71424b05ff 25 double emg02;
RiP 25:1a71424b05ff 26 double emg12;
RiP 25:1a71424b05ff 27 double emg22;
RiP 26:91d48c0b722d 28 //double ref_x=0.000;
RiP 26:91d48c0b722d 29 //double ref_y=0.000;
RiP 23:54d28f9eef53 30 typedef enum { STATE_CALIBRATION, STATE_PAUZE, STATE_X, STATE_X_NEG, STATE_Y, STATE_Y_NEG, STATE_XY, STATE_XY_NEG } states;
RiP 22:f38a15e851d2 31
RiP 21:3aecd735319d 32 BiQuadChain bqc11;
RiP 21:3aecd735319d 33 BiQuadChain bqc13;
RiP 21:3aecd735319d 34 BiQuadChain bqc21;
RiP 21:3aecd735319d 35 BiQuadChain bqc23;
RiP 21:3aecd735319d 36 BiQuadChain bqc31;
RiP 21:3aecd735319d 37 BiQuadChain bqc33;
RiP 21:3aecd735319d 38 //BiQuad bq11( 9.87589e-01, -1.59795e+00, 9.87589e-01, -1.59795e+00, 9.75178e-01 ); //oude BiQuad waardes
RiP 21:3aecd735319d 39 /* BiQuads for filter emg1
RiP 21:3aecd735319d 40 notch filter*/
RiP 21:3aecd735319d 41 BiQuad bq111(0.9795, -1.5849, 0.9795, 1.0000, -1.5849, 0.9589);
RiP 21:3aecd735319d 42 BiQuad bq112(0.9833, -1.5912, 0.9833, 1.0000, -1.5793, 0.9787);
RiP 21:3aecd735319d 43 BiQuad bq113(0.9957, -1.6111, 0.9957, 1.0000, -1.6224, 0.9798);
RiP 21:3aecd735319d 44 /* High pass filter*/
RiP 21:3aecd735319d 45 BiQuad bq121( 9.56543e-01, -1.91309e+00, 9.56543e-01, -1.91120e+00, 9.14976e-01 );
RiP 21:3aecd735319d 46 /* low pass filter*/
RiP 21:3aecd735319d 47 BiQuad bq131( 3.91302e-05, 7.82604e-05, 3.91302e-05, -1.98223e+00, 9.82385e-01 );
RiP 21:3aecd735319d 48
RiP 21:3aecd735319d 49
RiP 21:3aecd735319d 50 /* BiQuads for filter emg2
RiP 21:3aecd735319d 51 notch filter*/
RiP 21:3aecd735319d 52 BiQuad bq211(0.9795, -1.5849, 0.9795, 1.0000, -1.5849, 0.9589);
RiP 21:3aecd735319d 53 BiQuad bq212(0.9833, -1.5912, 0.9833, 1.0000, -1.5793, 0.9787);
RiP 21:3aecd735319d 54 BiQuad bq213(0.9957, -1.6111, 0.9957, 1.0000, -1.6224, 0.9798);
RiP 21:3aecd735319d 55 /* High pass filter*/
RiP 21:3aecd735319d 56 BiQuad bq221( 9.56543e-01, -1.91309e+00, 9.56543e-01, -1.91120e+00, 9.14976e-01 );
RiP 21:3aecd735319d 57 /* low pass filter*/
RiP 21:3aecd735319d 58 BiQuad bq231( 3.91302e-05, 7.82604e-05, 3.91302e-05, -1.98223e+00, 9.82385e-01 );
RiP 21:3aecd735319d 59
RiP 21:3aecd735319d 60 /* BiQuads for filter emg3
RiP 21:3aecd735319d 61 notch filter*/
RiP 21:3aecd735319d 62 BiQuad bq311(0.9795, -1.5849, 0.9795, 1.0000, -1.5849, 0.9589);
RiP 21:3aecd735319d 63 BiQuad bq312(0.9833, -1.5912, 0.9833, 1.0000, -1.5793, 0.9787);
RiP 21:3aecd735319d 64 BiQuad bq313(0.9957, -1.6111, 0.9957, 1.0000, -1.6224, 0.9798);
RiP 21:3aecd735319d 65 /* High pass filter*/
RiP 21:3aecd735319d 66 BiQuad bq321( 9.56543e-01, -1.91309e+00, 9.56543e-01, -1.91120e+00, 9.14976e-01 );
RiP 21:3aecd735319d 67 /* low pass filter*/
RiP 21:3aecd735319d 68 BiQuad bq331( 3.91302e-05, 7.82604e-05, 3.91302e-05, -1.98223e+00, 9.82385e-01 );
RiP 21:3aecd735319d 69
RiP 22:f38a15e851d2 70
RiP 22:f38a15e851d2 71 void sampleflag()
RiP 22:f38a15e851d2 72 {
RiP 22:f38a15e851d2 73 sampletimer=true;
RiP 22:f38a15e851d2 74 }
RiP 22:f38a15e851d2 75
RiP 24:01b4b51b5dc6 76 void buttonflag_go()
RiP 24:01b4b51b5dc6 77 {
RiP 24:01b4b51b5dc6 78 buttonflag=true;
RiP 24:01b4b51b5dc6 79 }
RiP 22:f38a15e851d2 80
RiP 25:1a71424b05ff 81 void sample_button(states &mystate)
RiP 25:1a71424b05ff 82 {
RiP 25:1a71424b05ff 83 states myoldstate=mystate;
RiP 25:1a71424b05ff 84 char key=pc.getc();
RiP 25:1a71424b05ff 85
RiP 26:91d48c0b722d 86 // pc.printf("%c/n",key);
RiP 25:1a71424b05ff 87
RiP 25:1a71424b05ff 88 switch (key) {
RiP 25:1a71424b05ff 89 case 'p' : // run
RiP 25:1a71424b05ff 90 emg02=0.0;
RiP 25:1a71424b05ff 91 emg12=0.0;
RiP 25:1a71424b05ff 92 emg22=0.0;
RiP 25:1a71424b05ff 93 break;
RiP 25:1a71424b05ff 94 case 'q' : // run
RiP 25:1a71424b05ff 95 emg02=0.0;
RiP 25:1a71424b05ff 96 emg12=1.0;
RiP 25:1a71424b05ff 97 emg22=0.0;
RiP 25:1a71424b05ff 98 break;
RiP 25:1a71424b05ff 99 case 'w' : // run
RiP 25:1a71424b05ff 100 emg02=0.0;
RiP 25:1a71424b05ff 101 emg12=0.0;
RiP 25:1a71424b05ff 102 emg22=1.0;
RiP 25:1a71424b05ff 103 break;
RiP 25:1a71424b05ff 104 case 'e' : // run
RiP 25:1a71424b05ff 105 emg02=0.0;
RiP 25:1a71424b05ff 106 emg12=1.0;
RiP 25:1a71424b05ff 107 emg22=1.0;
RiP 25:1a71424b05ff 108 break;
RiP 25:1a71424b05ff 109 case 'a' : // run
RiP 25:1a71424b05ff 110 emg02=1.0;
RiP 25:1a71424b05ff 111 emg12=1.0;
RiP 25:1a71424b05ff 112 emg22=0.0;
RiP 25:1a71424b05ff 113 break;
RiP 25:1a71424b05ff 114 case 's' : // run
RiP 25:1a71424b05ff 115 emg02=1.0;
RiP 25:1a71424b05ff 116 emg12=0.0;
RiP 25:1a71424b05ff 117 emg22=1.0;
RiP 25:1a71424b05ff 118 break;
RiP 25:1a71424b05ff 119 case 'd' : // run
RiP 25:1a71424b05ff 120 emg02=1.0;
RiP 25:1a71424b05ff 121 emg12=1.0;
RiP 25:1a71424b05ff 122 emg22=1.0;
RiP 25:1a71424b05ff 123 break;
RiP 25:1a71424b05ff 124 }
RiP 25:1a71424b05ff 125
RiP 25:1a71424b05ff 126
RiP 25:1a71424b05ff 127
RiP 25:1a71424b05ff 128
RiP 25:1a71424b05ff 129
RiP 25:1a71424b05ff 130 led = !led;
RiP 25:1a71424b05ff 131 if (emg02>threshold) {
RiP 25:1a71424b05ff 132 if (emg12>threshold&&emg22>threshold) {
RiP 25:1a71424b05ff 133 mystate = STATE_XY_NEG;
RiP 25:1a71424b05ff 134 } else if (emg12>threshold) {
RiP 25:1a71424b05ff 135 mystate = STATE_X_NEG;
RiP 25:1a71424b05ff 136
RiP 25:1a71424b05ff 137 } else if (emg22>threshold) {
RiP 25:1a71424b05ff 138 mystate = STATE_Y_NEG;
RiP 25:1a71424b05ff 139 } else {
RiP 25:1a71424b05ff 140 mystate = STATE_PAUZE;
RiP 25:1a71424b05ff 141 }
RiP 25:1a71424b05ff 142 } else {
RiP 25:1a71424b05ff 143 if (emg12>threshold&&emg22>threshold) {
RiP 25:1a71424b05ff 144 mystate = STATE_XY;
RiP 25:1a71424b05ff 145 } else if (emg12>threshold) {
RiP 25:1a71424b05ff 146 mystate = STATE_X;
RiP 25:1a71424b05ff 147
RiP 25:1a71424b05ff 148 } else if (emg22>threshold) {
RiP 25:1a71424b05ff 149 mystate = STATE_Y;
RiP 25:1a71424b05ff 150 } else {
RiP 25:1a71424b05ff 151 mystate = STATE_PAUZE;
RiP 25:1a71424b05ff 152 }
RiP 25:1a71424b05ff 153 }
RiP 26:91d48c0b722d 154
RiP 25:1a71424b05ff 155 if (buttonflag==true) {
RiP 25:1a71424b05ff 156 mystate = STATE_CALIBRATION;
RiP 26:91d48c0b722d 157 newcase=true;
RiP 25:1a71424b05ff 158 }
RiP 25:1a71424b05ff 159
RiP 25:1a71424b05ff 160 if (myoldstate==mystate) {
RiP 25:1a71424b05ff 161 newcase=false;
RiP 25:1a71424b05ff 162 } else {
RiP 25:1a71424b05ff 163 newcase=true;
RiP 25:1a71424b05ff 164 }
RiP 25:1a71424b05ff 165 sampletimer = false;
RiP 25:1a71424b05ff 166 }
RiP 25:1a71424b05ff 167
RiP 23:54d28f9eef53 168 void sample(states &mystate)
vsluiter 2:e314bb3b2d99 169 {
RiP 23:54d28f9eef53 170
RiP 21:3aecd735319d 171 /* Read the emg signals and filter it*/
RiP 22:f38a15e851d2 172
RiP 21:3aecd735319d 173 scope.set(0, emg1.read()); //original signal
RiP 21:3aecd735319d 174 double emg02=bqc13.step(fabs(bqc11.step(emg1.read())));
RiP 21:3aecd735319d 175 scope.set(1, emg02);
RiP 21:3aecd735319d 176 /* Read the second emg signal and filter it*/
RiP 21:3aecd735319d 177 scope.set(2, emg2.read()); //original signal
RiP 21:3aecd735319d 178 double emg12=bqc23.step(fabs(bqc21.step(emg2.read())));
RiP 21:3aecd735319d 179 scope.set(3, emg12);
RiP 21:3aecd735319d 180 /* Read the third emg signal and filter it*/
RiP 21:3aecd735319d 181 scope.set(4, emg3.read()); //original signal
RiP 21:3aecd735319d 182 double emg22=bqc33.step(fabs(bqc31.step(emg3.read())));
RiP 21:3aecd735319d 183 scope.set(5, emg22);
RiP 22:f38a15e851d2 184 //pc.printf("Hello World!\n");
RiP 21:3aecd735319d 185 /* Ensure that enough channels are available (HIDScope scope( 2 ))
RiP 21:3aecd735319d 186 * Finally, send all channels to the PC at once */
vsluiter 11:ce72ec658a95 187 scope.send();
RiP 21:3aecd735319d 188 /* To indicate that the function is working, the LED is toggled */
RiP 25:1a71424b05ff 189
tomlankhorst 18:21d8e7a81cf5 190 led = !led;
RiP 23:54d28f9eef53 191 if (emg02>threshold) {
RiP 23:54d28f9eef53 192 if (emg12>threshold&&emg22>threshold) {
RiP 24:01b4b51b5dc6 193 mystate = STATE_XY_NEG;
RiP 23:54d28f9eef53 194 } else if (emg12>threshold) {
RiP 24:01b4b51b5dc6 195 mystate = STATE_X_NEG;
RiP 23:54d28f9eef53 196
RiP 23:54d28f9eef53 197 } else if (emg22>threshold) {
RiP 24:01b4b51b5dc6 198 mystate = STATE_Y_NEG;
RiP 23:54d28f9eef53 199 } else {
RiP 24:01b4b51b5dc6 200 mystate = STATE_PAUZE;
RiP 23:54d28f9eef53 201 }
RiP 22:f38a15e851d2 202 } else {
RiP 23:54d28f9eef53 203 if (emg12>threshold&&emg22>threshold) {
RiP 24:01b4b51b5dc6 204 mystate = STATE_XY;
RiP 23:54d28f9eef53 205 } else if (emg12>threshold) {
RiP 24:01b4b51b5dc6 206 mystate = STATE_X;
RiP 23:54d28f9eef53 207
RiP 23:54d28f9eef53 208 } else if (emg22>threshold) {
RiP 24:01b4b51b5dc6 209 mystate = STATE_Y;
RiP 23:54d28f9eef53 210 } else {
RiP 24:01b4b51b5dc6 211 mystate = STATE_PAUZE;
RiP 23:54d28f9eef53 212 }
RiP 22:f38a15e851d2 213 }
RiP 23:54d28f9eef53 214
RiP 24:01b4b51b5dc6 215 if (buttonflag==true) {
RiP 24:01b4b51b5dc6 216 mystate = STATE_CALIBRATION;
RiP 24:01b4b51b5dc6 217 }
RiP 24:01b4b51b5dc6 218
RiP 22:f38a15e851d2 219 sampletimer = false;
vsluiter 2:e314bb3b2d99 220 }
vsluiter 0:32bb76391d89 221
RiP 26:91d48c0b722d 222
vsluiter 0:32bb76391d89 223 int main()
RiP 21:3aecd735319d 224 {
RiP 22:f38a15e851d2 225 pc.baud(115200);
RiP 22:f38a15e851d2 226
RiP 23:54d28f9eef53 227 // create a variable called 'state', define it
RiP 24:01b4b51b5dc6 228 states mystate = STATE_PAUZE;
RiP 23:54d28f9eef53 229
RiP 21:3aecd735319d 230 //de biquad chains instellen
RiP 21:3aecd735319d 231 bqc11.add( &bq111 ).add( &bq112 ).add( &bq113 ).add( &bq121 );
RiP 21:3aecd735319d 232 bqc13.add( &bq131);
RiP 21:3aecd735319d 233 bqc21.add( &bq211 ).add( &bq212 ).add( &bq213 ).add( &bq221 );
RiP 21:3aecd735319d 234 bqc23.add( &bq231);
RiP 21:3aecd735319d 235 bqc31.add( &bq311 ).add( &bq312 ).add( &bq313 ).add( &bq321 );
RiP 21:3aecd735319d 236 bqc33.add( &bq331);
RiP 21:3aecd735319d 237 /*Attach the 'sample' function to the timer 'sample_timer'.
RiP 21:3aecd735319d 238 this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz
vsluiter 4:8b298dfada81 239 */
RiP 23:54d28f9eef53 240 sample_timer.attach(&sampleflag, samplefreq);
RiP 24:01b4b51b5dc6 241 button_calibrate.fall(&buttonflag_go);
RiP 24:01b4b51b5dc6 242
tomlankhorst 15:0da764eea774 243
RiP 22:f38a15e851d2 244 while(1) {
RiP 22:f38a15e851d2 245 if (sampletimer==true) {
RiP 25:1a71424b05ff 246 //sample(mystate);
RiP 25:1a71424b05ff 247 sample_button(mystate);
RiP 25:1a71424b05ff 248 }
RiP 25:1a71424b05ff 249 // switch, case
RiP 25:1a71424b05ff 250 switch (mystate) {
RiP 26:91d48c0b722d 251 case STATE_CALIBRATION : { // calibration
RiP 26:91d48c0b722d 252 pc.printf("calibration\n");
RiP 25:1a71424b05ff 253 while (button_calibrate==0) {}
RiP 26:91d48c0b722d 254 newcase=false;
RiP 25:1a71424b05ff 255 buttonflag=false;
RiP 25:1a71424b05ff 256 break;
RiP 26:91d48c0b722d 257 }
RiP 25:1a71424b05ff 258 case STATE_X : // run
RiP 26:91d48c0b722d 259 if (newcase==true) {
RiP 26:91d48c0b722d 260 pc.printf("X\n");
RiP 26:91d48c0b722d 261 newcase=false;
RiP 25:1a71424b05ff 262 }
RiP 26:91d48c0b722d 263
RiP 25:1a71424b05ff 264 break;
RiP 25:1a71424b05ff 265 case STATE_X_NEG : // run
RiP 25:1a71424b05ff 266 if (newcase==true) {
RiP 25:1a71424b05ff 267 pc.printf("Xneg\n");
RiP 25:1a71424b05ff 268 newcase=false;
RiP 25:1a71424b05ff 269 }
RiP 25:1a71424b05ff 270 break;
RiP 25:1a71424b05ff 271 case STATE_Y : // execute mode 1
RiP 25:1a71424b05ff 272 if (newcase==true) {
RiP 25:1a71424b05ff 273 pc.printf("Y\n");
RiP 25:1a71424b05ff 274 newcase=false;
RiP 25:1a71424b05ff 275 }
RiP 25:1a71424b05ff 276 break;
RiP 25:1a71424b05ff 277 case STATE_Y_NEG : // execute mode 1
RiP 25:1a71424b05ff 278 if (newcase==true) {
RiP 25:1a71424b05ff 279 pc.printf("Yneg\n");
RiP 25:1a71424b05ff 280 newcase=false;
RiP 25:1a71424b05ff 281 }
RiP 25:1a71424b05ff 282 break;
RiP 25:1a71424b05ff 283 case STATE_XY : // execute mode 2
RiP 25:1a71424b05ff 284 if (newcase==true) {
RiP 25:1a71424b05ff 285 pc.printf("XY\n");
RiP 25:1a71424b05ff 286 newcase=false;
RiP 25:1a71424b05ff 287 }
RiP 25:1a71424b05ff 288 break;
RiP 25:1a71424b05ff 289 case STATE_XY_NEG : // execute mode 2
RiP 25:1a71424b05ff 290 if (newcase==true) {
RiP 25:1a71424b05ff 291 pc.printf("XYneg\n");
RiP 25:1a71424b05ff 292 newcase=false;
RiP 25:1a71424b05ff 293 }
RiP 25:1a71424b05ff 294 break;
RiP 25:1a71424b05ff 295 case STATE_PAUZE : // default
RiP 25:1a71424b05ff 296 if (newcase==true) {
RiP 25:1a71424b05ff 297 pc.printf("PAUZE\n");
RiP 25:1a71424b05ff 298 newcase=false;
RiP 26:91d48c0b722d 299 }
RiP 26:91d48c0b722d 300 break;
RiP 22:f38a15e851d2 301 }
RiP 25:1a71424b05ff 302
RiP 22:f38a15e851d2 303 }
vsluiter 0:32bb76391d89 304 }