emg with text

Dependencies:   HIDScope MODSERIAL biquadFilter mbed

Fork of emg_import by Daniqe Kottelenberg

Committer:
daniQQue
Date:
Fri Oct 28 10:13:13 2016 +0000
Revision:
33:cd3b5168cc8f
Parent:
31:9333d3f48c88
??

Who changed what in which revision?

UserRevisionLine numberNew contents of line
daniQQue 0:34c739fcc3e0 1 //libraries
daniQQue 0:34c739fcc3e0 2 #include "mbed.h"
daniQQue 0:34c739fcc3e0 3 #include "HIDScope.h"
daniQQue 14:5b17697cf775 4 #include "BiQuad.h"
daniQQue 23:123d65f76236 5 #include "MODSERIAL.h"
daniQQue 0:34c739fcc3e0 6
daniQQue 0:34c739fcc3e0 7 //Define objects
daniQQue 21:9b8be3dff01a 8 AnalogIn emg_biceps_right_in( A0 ); //analog in to get EMG biceps (r) in to c++
daniQQue 21:9b8be3dff01a 9 AnalogIn emg_triceps_right_in(A1); //analog in to get EMG triceps (r) in to c++
daniQQue 23:123d65f76236 10 AnalogIn emg_biceps_left_in (A2); //analog in to get EMG biceps (l) in to c++
daniQQue 29:76b2cc33690c 11 InterruptIn button_calibration_biceps (SW3); //button to start calibration biceps
daniQQue 29:76b2cc33690c 12 InterruptIn button_calibration_triceps (SW2); // button to start calibration tricps
daniQQue 26:38dc21b9ba7d 13
daniQQue 26:38dc21b9ba7d 14 Ticker sample_timer; //ticker
daniQQue 26:38dc21b9ba7d 15 Ticker switch_function; //ticker
daniQQue 26:38dc21b9ba7d 16 HIDScope scope(5); //open 3 channels in hidscope
daniQQue 26:38dc21b9ba7d 17 MODSERIAL pc(USBTX, USBRX); //pc connection
daniQQue 23:123d65f76236 18
daniQQue 23:123d65f76236 19 //motors
daniQQue 30:a7bae1c036a3 20 DigitalOut richting_motor1(D4);
daniQQue 23:123d65f76236 21 PwmOut pwm_motor1(D5);
daniQQue 23:123d65f76236 22 DigitalOut richting_motor2(D7);
daniQQue 23:123d65f76236 23 PwmOut pwm_motor2(D6);
daniQQue 26:38dc21b9ba7d 24
daniQQue 26:38dc21b9ba7d 25 //digital out
daniQQue 26:38dc21b9ba7d 26
daniQQue 30:a7bae1c036a3 27 DigitalOut led(LED_GREEN); //led included to check where code is
daniQQue 0:34c739fcc3e0 28
daniQQue 0:34c739fcc3e0 29 //define variables
daniQQue 21:9b8be3dff01a 30 //other
daniQQue 21:9b8be3dff01a 31 int onoffsignal_rightarm=0; // on/off signal: 1; biceps activation, 0: nothing, -1, triceps activation
daniQQue 23:123d65f76236 32 int switch_signal_leftarm=0; // switching between motors.
daniQQue 26:38dc21b9ba7d 33 double cut_off_value_biceps =0.06; //gespecificeerd door floortje
daniQQue 26:38dc21b9ba7d 34 double cut_off_value_triceps=-0.03; //gespecificeerd door floorte
daniQQue 26:38dc21b9ba7d 35 double signal_right_arm; //signal right arm
daniQQue 26:38dc21b9ba7d 36 double max_biceps; //calibration maximum biceps
daniQQue 26:38dc21b9ba7d 37 double max_triceps; //calibration maximum triceps
daniQQue 30:a7bae1c036a3 38 int n = 0; //start of counter is zero, to make switch work. It depens on even and odd.
daniQQue 23:123d65f76236 39
daniQQue 30:a7bae1c036a3 40 double emg_biceps_right; //emg biceps signal in, arm 1
daniQQue 30:a7bae1c036a3 41 double emg_triceps_right; //emg triceps signal in, arm 1
daniQQue 30:a7bae1c036a3 42 double emg_biceps_left; //emg biceps signal in, arm 2
daniQQue 30:a7bae1c036a3 43
daniQQue 30:a7bae1c036a3 44 float percentage_max_triceps=0.2;
daniQQue 30:a7bae1c036a3 45 float percentage_max_biceps =0.3;
daniQQue 21:9b8be3dff01a 46
daniQQue 28:e2553066989e 47 //Biquads defined
daniQQue 23:123d65f76236 48
daniQQue 23:123d65f76236 49 //b1 = biceps right arm
daniQQue 33:cd3b5168cc8f 50 <<<<<<< local
daniQQue 33:cd3b5168cc8f 51 BiQuad filterhigh_b1 (9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01);
daniQQue 33:cd3b5168cc8f 52 =======
daniQQue 22:eb8411807cca 53 BiQuad filterhigh_b1(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01);
daniQQue 33:cd3b5168cc8f 54 >>>>>>> other
daniQQue 22:eb8411807cca 55 BiQuad filternotch1_b1 (9.9115e-01, -1.8853e+00, 9.9115e-01 ,-1.8909e+00 , 9.9103e-01);
daniQQue 22:eb8411807cca 56 BiQuad filternotch2_b1(1.0000e+00, -1.9022e+00, 1.0000e+00, -1.8965e+00 , 9.9127e-01);
daniQQue 23:123d65f76236 57
daniQQue 23:123d65f76236 58 //t1= triceps right arm
daniQQue 22:eb8411807cca 59 BiQuad filterhigh_t1(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01);
daniQQue 22:eb8411807cca 60 BiQuad filternotch1_t1 (9.9115e-01, -1.8853e+00, 9.9115e-01 ,-1.8909e+00 , 9.9103e-01);
daniQQue 22:eb8411807cca 61 BiQuad filternotch2_t1(1.0000e+00, -1.9022e+00, 1.0000e+00, -1.8965e+00 , 9.9127e-01);
daniQQue 21:9b8be3dff01a 62
daniQQue 23:123d65f76236 63 //b2= biceps left arm
daniQQue 23:123d65f76236 64 BiQuad filterhigh_b2(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01);
daniQQue 23:123d65f76236 65 BiQuad filternotch1_b2 (9.9115e-01, -1.8853e+00, 9.9115e-01 ,-1.8909e+00 , 9.9103e-01);
daniQQue 23:123d65f76236 66 BiQuad filternotch2_b2(1.0000e+00, -1.9022e+00, 1.0000e+00, -1.8965e+00 , 9.9127e-01);
daniQQue 23:123d65f76236 67
daniQQue 21:9b8be3dff01a 68 //after abs filtering
daniQQue 22:eb8411807cca 69 BiQuad filterlow_b1 (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01);
daniQQue 22:eb8411807cca 70 BiQuad filterlow_t1 (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01);
daniQQue 23:123d65f76236 71 BiQuad filterlow_b2 (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01);
daniQQue 10:7255b59224cc 72
daniQQue 23:123d65f76236 73 //function teller
daniQQue 23:123d65f76236 74 void SwitchN() { // maakt simpele functie die 1 bij n optelt
daniQQue 23:123d65f76236 75 if(switch_signal_leftarm==1)
daniQQue 23:123d65f76236 76 {
daniQQue 23:123d65f76236 77 n++;
daniQQue 25:58f351d9fcc4 78 wait(0.5f);
daniQQue 23:123d65f76236 79 }
daniQQue 23:123d65f76236 80 }
daniQQue 23:123d65f76236 81
daniQQue 21:9b8be3dff01a 82 //functions which are called in ticker to sample the analog signal
daniQQue 23:123d65f76236 83
daniQQue 0:34c739fcc3e0 84 void filter(){
daniQQue 21:9b8be3dff01a 85 //biceps right arm read+filtering
daniQQue 15:bb4a6c7836d8 86 emg_biceps_right=emg_biceps_right_in.read(); //read the emg value from the elektrodes
daniQQue 28:e2553066989e 87 double emg_filtered_high_biceps_right= filterhigh_b1.step(emg_biceps_right);
daniQQue 28:e2553066989e 88 double emg_filtered_high_notch_1_biceps_right=filternotch1_b1.step(emg_filtered_high_biceps_right);
daniQQue 28:e2553066989e 89 double emg_filtered_high_notch_1_2_biceps_right=filternotch2_b1.step(emg_filtered_high_notch_1_biceps_right);
daniQQue 28:e2553066989e 90 double emg_abs_biceps_right=fabs(emg_filtered_high_notch_1_2_biceps_right); //fabs because float
daniQQue 28:e2553066989e 91 double emg_filtered_biceps_right=filterlow_b1.step(emg_abs_biceps_right);
daniQQue 21:9b8be3dff01a 92
daniQQue 21:9b8be3dff01a 93 //triceps right arm read+filtering
daniQQue 21:9b8be3dff01a 94 emg_triceps_right=emg_triceps_right_in.read(); //read the emg value from the elektrodes
daniQQue 28:e2553066989e 95 double emg_filtered_high_triceps_right= filterhigh_t1.step(emg_triceps_right);
daniQQue 28:e2553066989e 96 double emg_filtered_high_notch_1_triceps_right=filternotch1_t1.step(emg_filtered_high_triceps_right);
daniQQue 28:e2553066989e 97 double emg_filtered_high_notch_1_2_triceps_right=filternotch2_t1.step(emg_filtered_high_notch_1_triceps_right);
daniQQue 28:e2553066989e 98 double emg_abs_triceps_right=fabs(emg_filtered_high_notch_1_2_triceps_right); //fabs because float
daniQQue 28:e2553066989e 99 double emg_filtered_triceps_right=filterlow_t1.step(emg_abs_triceps_right);
daniQQue 7:42d0e38196f1 100
daniQQue 23:123d65f76236 101 //biceps left arm read+filtering
daniQQue 23:123d65f76236 102 emg_biceps_left=emg_biceps_left_in.read(); //read the emg value from the elektrodes
daniQQue 28:e2553066989e 103 double emg_filtered_high_biceps_left= filterhigh_b2.step(emg_biceps_left);
daniQQue 28:e2553066989e 104 double emg_filtered_high_notch_1_biceps_left=filternotch1_b2.step(emg_filtered_high_biceps_left);
daniQQue 28:e2553066989e 105 double emg_filtered_high_notch_1_2_biceps_left=filternotch2_b2.step(emg_filtered_high_notch_1_biceps_left);
daniQQue 28:e2553066989e 106 double emg_abs_biceps_left=fabs(emg_filtered_high_notch_1_2_biceps_left); //fabs because float
daniQQue 28:e2553066989e 107 double emg_filtered_biceps_left=filterlow_b2.step(emg_abs_biceps_left);
daniQQue 23:123d65f76236 108
daniQQue 21:9b8be3dff01a 109 //signal substraction of filter biceps and triceps. Biceps +,triceps -
daniQQue 28:e2553066989e 110 double signal_right_arm=emg_filtered_biceps_right-emg_filtered_triceps_right;
daniQQue 21:9b8be3dff01a 111
daniQQue 23:123d65f76236 112 //creating of on/off signal with the created on/off signals, with if statement for right arm!
daniQQue 21:9b8be3dff01a 113 if (signal_right_arm>cut_off_value_biceps)
daniQQue 21:9b8be3dff01a 114 {onoffsignal_rightarm=1;}
daniQQue 7:42d0e38196f1 115
daniQQue 21:9b8be3dff01a 116 else if (signal_right_arm<cut_off_value_triceps)
daniQQue 21:9b8be3dff01a 117 {
daniQQue 21:9b8be3dff01a 118 onoffsignal_rightarm=-1;
daniQQue 21:9b8be3dff01a 119 }
daniQQue 21:9b8be3dff01a 120
daniQQue 21:9b8be3dff01a 121 else
daniQQue 21:9b8be3dff01a 122 {onoffsignal_rightarm=0;}
daniQQue 7:42d0e38196f1 123
daniQQue 23:123d65f76236 124 //creating on/off signal for switch (left arm)
daniQQue 23:123d65f76236 125
daniQQue 23:123d65f76236 126 if (emg_filtered_biceps_left>cut_off_value_biceps)
daniQQue 23:123d65f76236 127 {
daniQQue 23:123d65f76236 128 switch_signal_leftarm=1;
daniQQue 23:123d65f76236 129 }
daniQQue 23:123d65f76236 130
daniQQue 23:123d65f76236 131 else
daniQQue 23:123d65f76236 132 {
daniQQue 23:123d65f76236 133 switch_signal_leftarm=0;
daniQQue 23:123d65f76236 134 }
daniQQue 23:123d65f76236 135
daniQQue 0:34c739fcc3e0 136 //send signals to scope
daniQQue 30:a7bae1c036a3 137 scope.set(0, emg_biceps_right); //set emg signal to scope in channel 0 // change into raw signal!
daniQQue 30:a7bae1c036a3 138 scope.set(1, emg_triceps_right); // set emg signal to scope in channel 1// change into raw signal!
daniQQue 31:9333d3f48c88 139 scope.set(2, emg_filtered_triceps_right); // set emg signal to scope in channel 2
daniQQue 31:9333d3f48c88 140 scope.set(3, onoffsignal_rightarm); // set emg signal to scope in channel 3
daniQQue 23:123d65f76236 141 scope.set(4, switch_signal_leftarm);
daniQQue 23:123d65f76236 142
daniQQue 0:34c739fcc3e0 143 scope.send(); //send all the signals to the scope
daniQQue 0:34c739fcc3e0 144 }
daniQQue 0:34c739fcc3e0 145
daniQQue 29:76b2cc33690c 146 //calibration function for biceps
daniQQue 27:b8d61361d709 147 void calibration_biceps(){
daniQQue 30:a7bae1c036a3 148 for(int n =0; n<2000;n++) //read for 2000 samples as calibration
daniQQue 26:38dc21b9ba7d 149 {
daniQQue 28:e2553066989e 150 emg_biceps_right=emg_biceps_right_in.read(); //read the emg value from the elektrodes
daniQQue 28:e2553066989e 151 double emg_filtered_high_biceps_right= filterhigh_b1.step(emg_biceps_right);
daniQQue 28:e2553066989e 152 double emg_filtered_high_notch_1_biceps_right=filternotch1_b1.step(emg_filtered_high_biceps_right);
daniQQue 28:e2553066989e 153 double emg_filtered_high_notch_1_2_biceps_right=filternotch2_b1.step(emg_filtered_high_notch_1_biceps_right);
daniQQue 28:e2553066989e 154 double emg_abs_biceps_right=fabs(emg_filtered_high_notch_1_2_biceps_right); //fabs because float
daniQQue 28:e2553066989e 155 double emg_filtered_biceps_right=filterlow_b1.step(emg_abs_biceps_right);
daniQQue 28:e2553066989e 156
daniQQue 28:e2553066989e 157 if (emg_filtered_biceps_right > max_biceps) //determine what the highest reachable emg signal is
daniQQue 26:38dc21b9ba7d 158 {
daniQQue 28:e2553066989e 159 max_biceps = emg_filtered_biceps_right;
daniQQue 27:b8d61361d709 160
daniQQue 26:38dc21b9ba7d 161 }
daniQQue 27:b8d61361d709 162 wait(0.001f); //to sample at same freq; 1000Hz
daniQQue 26:38dc21b9ba7d 163 }
daniQQue 30:a7bae1c036a3 164 cut_off_value_biceps=percentage_max_biceps*max_biceps;
daniQQue 30:a7bae1c036a3 165 pc.printf(" change of cv biceps: %f ",cut_off_value_biceps );
daniQQue 30:a7bae1c036a3 166
daniQQue 30:a7bae1c036a3 167 }
daniQQue 30:a7bae1c036a3 168
daniQQue 30:a7bae1c036a3 169 //calibration function for biceps
daniQQue 30:a7bae1c036a3 170 void calibration_triceps(){
daniQQue 30:a7bae1c036a3 171 for(int n =0; n<2000;n++) //read for 2000 samples as calibration
daniQQue 30:a7bae1c036a3 172 {
daniQQue 31:9333d3f48c88 173 emg_triceps_right=emg_triceps_right_in.read(); //read the emg value from the elektrodes
daniQQue 30:a7bae1c036a3 174 double emg_filtered_high_triceps_right= filterhigh_b1.step(emg_triceps_right);
daniQQue 30:a7bae1c036a3 175 double emg_filtered_high_notch_1_triceps_right=filternotch1_b1.step(emg_filtered_high_triceps_right);
daniQQue 30:a7bae1c036a3 176 double emg_filtered_high_notch_1_2_triceps_right=filternotch2_b1.step(emg_filtered_high_notch_1_triceps_right);
daniQQue 30:a7bae1c036a3 177 double emg_abs_triceps_right=fabs(emg_filtered_high_notch_1_2_triceps_right); //fabs because float
daniQQue 30:a7bae1c036a3 178 double emg_filtered_triceps_right=filterlow_b1.step(emg_abs_triceps_right);
daniQQue 30:a7bae1c036a3 179
daniQQue 30:a7bae1c036a3 180 if (emg_filtered_triceps_right > max_triceps) //determine what the highest reachable emg signal is
daniQQue 30:a7bae1c036a3 181 {
daniQQue 30:a7bae1c036a3 182 max_triceps = emg_filtered_triceps_right;
daniQQue 30:a7bae1c036a3 183
daniQQue 30:a7bae1c036a3 184 }
daniQQue 30:a7bae1c036a3 185 wait(0.001f); //to sample at same freq; 1000Hz
daniQQue 30:a7bae1c036a3 186 }
daniQQue 31:9333d3f48c88 187 cut_off_value_triceps=-percentage_max_triceps*max_triceps;
daniQQue 30:a7bae1c036a3 188 pc.printf(" change of cv triceps: %f ",cut_off_value_triceps );
daniQQue 30:a7bae1c036a3 189
daniQQue 26:38dc21b9ba7d 190 }
daniQQue 26:38dc21b9ba7d 191
daniQQue 0:34c739fcc3e0 192 //program
daniQQue 0:34c739fcc3e0 193
daniQQue 0:34c739fcc3e0 194 int main()
daniQQue 0:34c739fcc3e0 195 {
daniQQue 23:123d65f76236 196 pc.baud(115200);
daniQQue 0:34c739fcc3e0 197 sample_timer.attach(&filter, 0.001); //continously execute the EMG reader and filter, it ensures that filter and sampling is executed every 1/frequency seconds
daniQQue 30:a7bae1c036a3 198 switch_function.attach(&SwitchN,0.01);
daniQQue 31:9333d3f48c88 199 button_calibration_biceps.fall (&calibration_biceps); //to call calibration biceps, stop everything else
daniQQue 30:a7bae1c036a3 200 button_calibration_triceps.fall(&calibration_triceps); //to call calibration triceps, stop everything else
daniQQue 0:34c739fcc3e0 201 //endless loop
daniQQue 0:34c739fcc3e0 202
daniQQue 23:123d65f76236 203 while (true) { // zorgt er voor dat de code oneindig doorgelopen wordt
daniQQue 23:123d65f76236 204
daniQQue 24:bfc240e381b4 205 if (onoffsignal_rightarm==1) // als s ingedrukt wordt gebeurd het volgende
daniQQue 23:123d65f76236 206 {
daniQQue 23:123d65f76236 207 if (n%2==0) // als s ingedrukt wordt en het getal is even gebeurd het onderstaande
daniQQue 23:123d65f76236 208 {
daniQQue 28:e2553066989e 209
daniQQue 24:bfc240e381b4 210 richting_motor1 = 2.5;
daniQQue 23:123d65f76236 211 pwm_motor1 = 1;
daniQQue 23:123d65f76236 212
daniQQue 23:123d65f76236 213 }
daniQQue 23:123d65f76236 214
daniQQue 23:123d65f76236 215 else // als s is ingedrukt maar het getal is niet even (dus oneven) gebeurdt het onderstaande
daniQQue 23:123d65f76236 216 {
daniQQue 28:e2553066989e 217
daniQQue 24:bfc240e381b4 218 richting_motor2 = 2.5;
daniQQue 23:123d65f76236 219 pwm_motor2 = 1;
daniQQue 23:123d65f76236 220
daniQQue 23:123d65f76236 221 }
daniQQue 23:123d65f76236 222
daniQQue 23:123d65f76236 223 }
daniQQue 24:bfc240e381b4 224 else if (onoffsignal_rightarm==-1) // als d ingedrukt wordt gebeurd het volgende
daniQQue 23:123d65f76236 225 {
daniQQue 23:123d65f76236 226 if (n%2==0) // als d is ingedrukt en n is even dan gebeurd het volgende
daniQQue 23:123d65f76236 227 {
daniQQue 28:e2553066989e 228
daniQQue 24:bfc240e381b4 229 richting_motor1 = 0;
daniQQue 23:123d65f76236 230 pwm_motor1 = 1;
daniQQue 23:123d65f76236 231
daniQQue 23:123d65f76236 232 }
daniQQue 23:123d65f76236 233 else // als d is ingedrukt maar het getal is niet even (dus oneven) gebeurdt het onderstaande
daniQQue 23:123d65f76236 234 {
daniQQue 28:e2553066989e 235
daniQQue 24:bfc240e381b4 236 richting_motor2 = 0;
daniQQue 23:123d65f76236 237 pwm_motor2 = 1;
daniQQue 23:123d65f76236 238
daniQQue 23:123d65f76236 239 }
daniQQue 23:123d65f76236 240 }
daniQQue 23:123d65f76236 241 else{
daniQQue 23:123d65f76236 242 pwm_motor2=0;
daniQQue 23:123d65f76236 243 pwm_motor1=0;
daniQQue 23:123d65f76236 244 }
daniQQue 23:123d65f76236 245
daniQQue 23:123d65f76236 246 }
daniQQue 0:34c739fcc3e0 247
daniQQue 0:34c739fcc3e0 248 }