the emg filtering part of the program

Dependencies:   HIDScope biquadFilter mbed MODSERIAL

Fork of EMG by Tom Tom

Committer:
RiP
Date:
Mon Oct 24 22:28:43 2016 +0000
Revision:
29:ac08c1a32c54
Parent:
28:3b1b29193851
Child:
30:7cc564d38bc4
referentie werkt, maar alleen met de emg signalen, niet met de knoppen

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