the emg filtering part of the program

Dependencies:   HIDScope biquadFilter mbed MODSERIAL

Fork of EMG by Tom Tom

Committer:
RiP
Date:
Wed Oct 26 10:04:38 2016 +0000
Revision:
36:344588e69589
Parent:
35:4905144c1123
Child:
37:af85a7b57a25
Working code with comments

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
RiP 36:344588e69589 6 MODSERIAL pc(USBTX, USBRX);
RiP 36:344588e69589 7
vsluiter 4:8b298dfada81 8 //Define objects
RiP 31:9c3b022f1dc3 9 //Define the button interrupt for the calibration
RiP 31:9c3b022f1dc3 10 InterruptIn button_calibrate(PTA4);
RiP 24:01b4b51b5dc6 11
RiP 31:9c3b022f1dc3 12 //Define the EMG inputs
RiP 21:3aecd735319d 13 AnalogIn emg1( A0 );
RiP 21:3aecd735319d 14 AnalogIn emg2( A1 );
RiP 21:3aecd735319d 15 AnalogIn emg3( A2 );
tomlankhorst 19:2bf824669684 16
RiP 31:9c3b022f1dc3 17 //Define the Tickers
RiP 27:1ff7fa636f1c 18 Ticker pos_timer;
tomlankhorst 14:f83354387756 19 Ticker sample_timer;
RiP 31:9c3b022f1dc3 20
RiP 21:3aecd735319d 21 HIDScope scope( 6 );
vsluiter 2:e314bb3b2d99 22
RiP 36:344588e69589 23 //Initialize all variables
RiP 22:f38a15e851d2 24 volatile bool sampletimer = false;
RiP 24:01b4b51b5dc6 25 volatile bool buttonflag = false;
RiP 25:1a71424b05ff 26 volatile bool newcase = false;
RiP 25:1a71424b05ff 27
RiP 33:fcd4568f1c86 28 double threshold = 0.04;
RiP 23:54d28f9eef53 29 double samplefreq=0.002;
RiP 25:1a71424b05ff 30 double emg02;
RiP 25:1a71424b05ff 31 double emg12;
RiP 25:1a71424b05ff 32 double emg22;
RiP 27:1ff7fa636f1c 33 double ref_x=0.000;
RiP 27:1ff7fa636f1c 34 double ref_y=0.000;
RiP 36:344588e69589 35 double speed_emg=0.00002;
RiP 36:344588e69589 36 double speed_key=0.000326;
RiP 36:344588e69589 37 double speed;
RiP 36:344588e69589 38
RiP 31:9c3b022f1dc3 39 const int negative=-1;
RiP 33:fcd4568f1c86 40 char key;
RiP 31:9c3b022f1dc3 41
RiP 27:1ff7fa636f1c 42
RiP 36:344588e69589 43 // create a variable called 'mystate', define it
RiP 23:54d28f9eef53 44 typedef enum { STATE_CALIBRATION, STATE_PAUZE, STATE_X, STATE_X_NEG, STATE_Y, STATE_Y_NEG, STATE_XY, STATE_XY_NEG } states;
RiP 28:3b1b29193851 45 states mystate = STATE_PAUZE;
RiP 28:3b1b29193851 46
RiP 31:9c3b022f1dc3 47 //Define the needed Biquad chains
RiP 21:3aecd735319d 48 BiQuadChain bqc11;
RiP 21:3aecd735319d 49 BiQuadChain bqc13;
RiP 21:3aecd735319d 50 BiQuadChain bqc21;
RiP 21:3aecd735319d 51 BiQuadChain bqc23;
RiP 21:3aecd735319d 52 BiQuadChain bqc31;
RiP 21:3aecd735319d 53 BiQuadChain bqc33;
RiP 31:9c3b022f1dc3 54
RiP 31:9c3b022f1dc3 55 //Define the BiQuads for the filter of the first emg signal
RiP 31:9c3b022f1dc3 56 //Notch filter
RiP 21:3aecd735319d 57 BiQuad bq111(0.9795, -1.5849, 0.9795, 1.0000, -1.5849, 0.9589);
RiP 21:3aecd735319d 58 BiQuad bq112(0.9833, -1.5912, 0.9833, 1.0000, -1.5793, 0.9787);
RiP 21:3aecd735319d 59 BiQuad bq113(0.9957, -1.6111, 0.9957, 1.0000, -1.6224, 0.9798);
RiP 31:9c3b022f1dc3 60 //High pass filter
RiP 36:344588e69589 61 //BiQuad bq121( 9.56543e-01, -1.91309e+00, 9.56543e-01, -1.91120e+00, 9.14976e-01 ); //Old biquad values
RiP 36:344588e69589 62 BiQuad bq121( 0.8956, -1.7911, 0.8956, 1.0000, -1.7814, 0.7941);
RiP 36:344588e69589 63 BiQuad bq122( 0.9192, -1.8385, 0.9192, 1.0000, -1.8319, 0.8450);
RiP 36:344588e69589 64 BiQuad bq123( 0.9649, -1.9298, 0.9649, 1.0000, -1.9266, 0.9403);
RiP 31:9c3b022f1dc3 65 //Low pass filter
RiP 21:3aecd735319d 66 BiQuad bq131( 3.91302e-05, 7.82604e-05, 3.91302e-05, -1.98223e+00, 9.82385e-01 );
RiP 21:3aecd735319d 67
RiP 31:9c3b022f1dc3 68 //Define the BiQuads for the filter of the second emg signal
RiP 31:9c3b022f1dc3 69 //Notch filter
RiP 31:9c3b022f1dc3 70 BiQuad bq211 = bq111;
RiP 31:9c3b022f1dc3 71 BiQuad bq212 = bq112;
RiP 31:9c3b022f1dc3 72 BiQuad bq213 = bq113;
RiP 21:3aecd735319d 73 /* High pass filter*/
RiP 31:9c3b022f1dc3 74 BiQuad bq221 = bq121;
RiP 36:344588e69589 75 BiQuad bq222 = bq122;
RiP 36:344588e69589 76 BiQuad bq223 = bq123;
RiP 31:9c3b022f1dc3 77 /* Low pass filter*/
RiP 31:9c3b022f1dc3 78 BiQuad bq231 = bq131;
RiP 21:3aecd735319d 79
RiP 31:9c3b022f1dc3 80 //Define the BiQuads for the filter of the third emg signal
RiP 31:9c3b022f1dc3 81 //notch filter
RiP 31:9c3b022f1dc3 82 BiQuad bq311 = bq111;
RiP 31:9c3b022f1dc3 83 BiQuad bq312 = bq112;
RiP 31:9c3b022f1dc3 84 BiQuad bq313 = bq113;
RiP 31:9c3b022f1dc3 85 //High pass filter
RiP 31:9c3b022f1dc3 86 BiQuad bq321 = bq121;
RiP 36:344588e69589 87 BiQuad bq323 = bq122;
RiP 36:344588e69589 88 BiQuad bq322 = bq123;
RiP 31:9c3b022f1dc3 89 //low pass filter
RiP 31:9c3b022f1dc3 90 BiQuad bq331 = bq131;
RiP 22:f38a15e851d2 91
RiP 22:f38a15e851d2 92 void sampleflag()
RiP 22:f38a15e851d2 93 {
RiP 36:344588e69589 94 //This sets the go flag for when the function sample needs to be called
RiP 22:f38a15e851d2 95 sampletimer=true;
RiP 22:f38a15e851d2 96 }
RiP 22:f38a15e851d2 97
RiP 36:344588e69589 98 void calibrate()
RiP 24:01b4b51b5dc6 99 {
RiP 36:344588e69589 100 //This resets the reference signals so that the robot can be calibrated
RiP 36:344588e69589 101 ref_x=0.0;
RiP 36:344588e69589 102 ref_y=0.0;
RiP 24:01b4b51b5dc6 103 }
RiP 22:f38a15e851d2 104
RiP 23:54d28f9eef53 105 void sample(states &mystate)
vsluiter 2:e314bb3b2d99 106 {
RiP 27:1ff7fa636f1c 107 states myoldstate=mystate;
RiP 36:344588e69589 108
RiP 36:344588e69589 109 //This checks if a key is pressed and adjusts the speed to the needed speed
RiP 33:fcd4568f1c86 110 if (pc.readable()==1) {
RiP 33:fcd4568f1c86 111 key=pc.getc();
RiP 36:344588e69589 112 speed=speed_key;
RiP 33:fcd4568f1c86 113 } else {
RiP 33:fcd4568f1c86 114 key ='p';
RiP 36:344588e69589 115 speed=speed_emg;
RiP 33:fcd4568f1c86 116 }
RiP 33:fcd4568f1c86 117
RiP 36:344588e69589 118 //Read the emg signals and filter it
RiP 22:f38a15e851d2 119
RiP 36:344588e69589 120 scope.set(0, emg1.read()); //original signal 0
RiP 36:344588e69589 121 emg02=bqc13.step(fabs(bqc11.step(emg1.read()))); //filtered signal 0
RiP 21:3aecd735319d 122 scope.set(1, emg02);
RiP 36:344588e69589 123 scope.set(2, emg2.read()); //original signal 1
RiP 36:344588e69589 124 emg12=bqc23.step(fabs(bqc21.step(emg2.read()))); //filtered signal 1
RiP 21:3aecd735319d 125 scope.set(3, emg12);
RiP 36:344588e69589 126 scope.set(4, emg3.read()); //original signal 2
RiP 36:344588e69589 127 emg22=bqc33.step(fabs(bqc31.step(emg3.read()))); //filtered signal 2
RiP 21:3aecd735319d 128 scope.set(5, emg22);
RiP 29:ac08c1a32c54 129
RiP 36:344588e69589 130 //Ensure that enough channels are available at the top (HIDScope scope( 6 ))
RiP 36:344588e69589 131 //Finally, send all channels to the PC at once
vsluiter 11:ce72ec658a95 132 scope.send();
RiP 25:1a71424b05ff 133
RiP 36:344588e69589 134 //look if the emg signals go over the threshold and change the state to the cooresponding state. Also change the reference.
RiP 33:fcd4568f1c86 135 if (emg02>threshold&&emg12>threshold&&emg22>threshold || key=='d') {
RiP 31:9c3b022f1dc3 136 mystate = STATE_XY_NEG;
RiP 31:9c3b022f1dc3 137 ref_x=ref_x+speed*negative;
RiP 31:9c3b022f1dc3 138 ref_y=ref_y+speed*negative;
RiP 31:9c3b022f1dc3 139
RiP 33:fcd4568f1c86 140 } else if (emg02>threshold&&emg12>threshold || key == 'a' ) {
RiP 31:9c3b022f1dc3 141 mystate = STATE_X_NEG;
RiP 31:9c3b022f1dc3 142 ref_x=ref_x+speed*negative;
RiP 23:54d28f9eef53 143
RiP 33:fcd4568f1c86 144 } else if (emg02>threshold&&emg22>threshold || key == 's') {
RiP 31:9c3b022f1dc3 145 mystate = STATE_Y_NEG;
RiP 31:9c3b022f1dc3 146 ref_y=ref_y+speed*negative;
RiP 31:9c3b022f1dc3 147
RiP 33:fcd4568f1c86 148 } else if (emg12>threshold&&emg22>threshold || key == 'e' ) {
RiP 31:9c3b022f1dc3 149 mystate = STATE_XY;
RiP 31:9c3b022f1dc3 150 ref_x=ref_x+speed;
RiP 31:9c3b022f1dc3 151 ref_y=ref_y+speed;
RiP 31:9c3b022f1dc3 152
RiP 33:fcd4568f1c86 153 } else if (emg12>threshold || key == 'q' ) {
RiP 31:9c3b022f1dc3 154 mystate = STATE_X;
RiP 31:9c3b022f1dc3 155 ref_x=ref_x+speed;
RiP 31:9c3b022f1dc3 156
RiP 33:fcd4568f1c86 157 } else if (emg22>threshold || key == 'w') {
RiP 31:9c3b022f1dc3 158 mystate = STATE_Y;
RiP 31:9c3b022f1dc3 159 ref_y=ref_y+speed;
RiP 22:f38a15e851d2 160 } else {
RiP 36:344588e69589 161 //mystate = STATE_PAUZE;
RiP 22:f38a15e851d2 162 }
RiP 23:54d28f9eef53 163
RiP 36:344588e69589 164 //change newcase so that the state will only be printed once
RiP 27:1ff7fa636f1c 165 if (myoldstate==mystate) {
RiP 27:1ff7fa636f1c 166 newcase=false;
RiP 31:9c3b022f1dc3 167
RiP 27:1ff7fa636f1c 168 } else {
RiP 27:1ff7fa636f1c 169 newcase=true;
RiP 27:1ff7fa636f1c 170 }
vsluiter 2:e314bb3b2d99 171 }
vsluiter 0:32bb76391d89 172
RiP 27:1ff7fa636f1c 173 void my_pos()
RiP 27:1ff7fa636f1c 174 {
RiP 36:344588e69589 175 //This function is attached to a ticker so that the reference position is printed every second.
RiP 27:1ff7fa636f1c 176 pc.printf("x_pos=%.4f\ty_pos=%.4f\n\r",ref_x,ref_y);
RiP 27:1ff7fa636f1c 177
RiP 27:1ff7fa636f1c 178 }
RiP 26:91d48c0b722d 179
RiP 29:ac08c1a32c54 180 void print_state()
RiP 28:3b1b29193851 181 {
RiP 36:344588e69589 182 //This code looks in which state the robot is in and prints it to the screen
RiP 31:9c3b022f1dc3 183 if (newcase==true) {
RiP 28:3b1b29193851 184 switch (mystate) {
RiP 28:3b1b29193851 185 case STATE_CALIBRATION : { // calibration
RiP 28:3b1b29193851 186 pc.printf("calibration\n\r");
RiP 28:3b1b29193851 187 break;
RiP 28:3b1b29193851 188 }
RiP 36:344588e69589 189 case STATE_X : // X direction
RiP 29:ac08c1a32c54 190 pc.printf("X\n\r");
RiP 28:3b1b29193851 191 break;
RiP 36:344588e69589 192 case STATE_X_NEG : // negative X direction
RiP 29:ac08c1a32c54 193 pc.printf("Xneg\n\r");
RiP 28:3b1b29193851 194 break;
RiP 36:344588e69589 195 case STATE_Y : // Y direction
RiP 29:ac08c1a32c54 196 pc.printf("Y\n\r");
RiP 28:3b1b29193851 197 break;
RiP 36:344588e69589 198 case STATE_Y_NEG : // negative Y direction
RiP 29:ac08c1a32c54 199 pc.printf("Yneg\n\r");
RiP 28:3b1b29193851 200 break;
RiP 36:344588e69589 201 case STATE_XY : // X&Y direction
RiP 29:ac08c1a32c54 202 pc.printf("XY\n\r");
RiP 28:3b1b29193851 203 break;
RiP 36:344588e69589 204 case STATE_XY_NEG : // negative X&Y direction
RiP 29:ac08c1a32c54 205 pc.printf("XYneg\n\r");
RiP 28:3b1b29193851 206 break;
RiP 36:344588e69589 207 case STATE_PAUZE : // Pauze: do nothing
RiP 29:ac08c1a32c54 208 pc.printf("PAUZE\n\r");
RiP 28:3b1b29193851 209 break;
RiP 28:3b1b29193851 210 }
RiP 28:3b1b29193851 211 }
RiP 28:3b1b29193851 212 }
RiP 28:3b1b29193851 213
vsluiter 0:32bb76391d89 214 int main()
RiP 21:3aecd735319d 215 {
RiP 29:ac08c1a32c54 216 pc.printf("RESET\n\r");
RiP 22:f38a15e851d2 217 pc.baud(115200);
RiP 22:f38a15e851d2 218
RiP 36:344588e69589 219 //Initialize the Biquad chains
RiP 36:344588e69589 220 bqc11.add( &bq111 ).add( &bq112 ).add( &bq113 ).add( &bq121 ).add( &bq122 ).add( &bq123 );
RiP 21:3aecd735319d 221 bqc13.add( &bq131);
RiP 36:344588e69589 222 bqc21.add( &bq211 ).add( &bq212 ).add( &bq213 ).add( &bq221 ).add( &bq222 ).add( &bq223 );
RiP 21:3aecd735319d 223 bqc23.add( &bq231);
RiP 36:344588e69589 224 bqc31.add( &bq311 ).add( &bq312 ).add( &bq313 ).add( &bq321 ).add( &bq322 ).add( &bq323 );
RiP 21:3aecd735319d 225 bqc33.add( &bq331);
RiP 36:344588e69589 226
RiP 36:344588e69589 227 //Attach the 'sample' function to the timer 'sample_timer'.
RiP 36:344588e69589 228 //this ensures that 'sample' is executed every 0.002 seconds = 500 Hz
RiP 23:54d28f9eef53 229 sample_timer.attach(&sampleflag, samplefreq);
RiP 36:344588e69589 230 //Attach the function calibrate to the button interrupt
RiP 36:344588e69589 231 button_calibrate.fall(&calibrate);
RiP 36:344588e69589 232 //Attach the function my_pos to the timer pos_timer.
RiP 36:344588e69589 233 //This ensures that the position is printed every second.
RiP 31:9c3b022f1dc3 234 pos_timer.attach(&my_pos, 1);
tomlankhorst 15:0da764eea774 235
RiP 22:f38a15e851d2 236 while(1) {
RiP 36:344588e69589 237 //Only take samples when the go flag is true.
RiP 22:f38a15e851d2 238 if (sampletimer==true) {
RiP 31:9c3b022f1dc3 239 sample(mystate);
RiP 36:344588e69589 240 print_state();
RiP 29:ac08c1a32c54 241 sampletimer = false;
RiP 25:1a71424b05ff 242 }
RiP 22:f38a15e851d2 243 }
vsluiter 0:32bb76391d89 244 }