emg with text

Dependencies:   HIDScope MODSERIAL biquadFilter mbed

Fork of emg_import by Daniqe Kottelenberg

Committer:
daniQQue
Date:
Fri Oct 28 07:19:58 2016 +0000
Revision:
25:58f351d9fcc4
Parent:
24:bfc240e381b4
Child:
26:38dc21b9ba7d
change at hidscope: raw signal!

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 0:34c739fcc3e0 11 Ticker sample_timer; //ticker
daniQQue 23:123d65f76236 12 Ticker switch_function; //ticker
daniQQue 23:123d65f76236 13 HIDScope scope(5); //open 3 channels in hidscope
daniQQue 23:123d65f76236 14 MODSERIAL pc(USBTX, USBRX); //pc connection
daniQQue 23:123d65f76236 15
daniQQue 23:123d65f76236 16 //motors
daniQQue 23:123d65f76236 17 DigitalOut richting_motor1(D4);
daniQQue 23:123d65f76236 18 PwmOut pwm_motor1(D5);
daniQQue 23:123d65f76236 19 DigitalOut richting_motor2(D7);
daniQQue 23:123d65f76236 20 PwmOut pwm_motor2(D6);
daniQQue 21:9b8be3dff01a 21 DigitalOut led(LED_GREEN); //led included to check where code is
daniQQue 0:34c739fcc3e0 22
daniQQue 0:34c739fcc3e0 23 //define variables
daniQQue 21:9b8be3dff01a 24 //other
daniQQue 21:9b8be3dff01a 25 int onoffsignal_rightarm=0; // on/off signal: 1; biceps activation, 0: nothing, -1, triceps activation
daniQQue 23:123d65f76236 26 int switch_signal_leftarm=0; // switching between motors.
daniQQue 21:9b8be3dff01a 27 double cut_off_value_biceps =0.06; //gespecificeerd door floortje
daniQQue 22:eb8411807cca 28 double cut_off_value_triceps=-0.03; //gespecificeerd door floortje
daniQQue 21:9b8be3dff01a 29 double signal_right_arm;
daniQQue 23:123d65f76236 30 int n = 0; //start van de teller wordt op nul gesteld
daniQQue 23:123d65f76236 31
daniQQue 21:9b8be3dff01a 32 //biceps arm 1, right arm
daniQQue 15:bb4a6c7836d8 33 double emg_biceps_right;
daniQQue 15:bb4a6c7836d8 34 double emg_filtered_high_biceps_right;
daniQQue 15:bb4a6c7836d8 35 double emg_abs_biceps_right;
daniQQue 15:bb4a6c7836d8 36 double emg_filtered_biceps_right;
daniQQue 21:9b8be3dff01a 37 double emg_filtered_high_notch_1_biceps_right;
daniQQue 21:9b8be3dff01a 38 double emg_filtered_high_notch_1_2_biceps_right;
daniQQue 5:688b1b5530d8 39
daniQQue 21:9b8be3dff01a 40 //triceps arm 1, right arm
daniQQue 21:9b8be3dff01a 41 double emg_triceps_right;
daniQQue 21:9b8be3dff01a 42 double emg_filtered_high_triceps_right;
daniQQue 21:9b8be3dff01a 43 double emg_abs_triceps_right;
daniQQue 21:9b8be3dff01a 44 double emg_filtered_triceps_right;
daniQQue 21:9b8be3dff01a 45 double emg_filtered_high_notch_1_triceps_right;
daniQQue 21:9b8be3dff01a 46 double emg_filtered_high_notch_1_2_triceps_right;
daniQQue 21:9b8be3dff01a 47
daniQQue 23:123d65f76236 48 //biceps arm 1, left arm
daniQQue 23:123d65f76236 49 double emg_biceps_left;
daniQQue 23:123d65f76236 50 double emg_filtered_high_biceps_left;
daniQQue 23:123d65f76236 51 double emg_abs_biceps_left;
daniQQue 23:123d65f76236 52 double emg_filtered_biceps_left;
daniQQue 23:123d65f76236 53 double emg_filtered_high_notch_1_biceps_left;
daniQQue 23:123d65f76236 54 double emg_filtered_high_notch_1_2_biceps_left;
daniQQue 23:123d65f76236 55
daniQQue 21:9b8be3dff01a 56 //before abs filtering
daniQQue 23:123d65f76236 57
daniQQue 23:123d65f76236 58 //b1 = biceps right arm
daniQQue 22:eb8411807cca 59 BiQuad filterhigh_b1(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01);
daniQQue 22:eb8411807cca 60 BiQuad filternotch1_b1 (9.9115e-01, -1.8853e+00, 9.9115e-01 ,-1.8909e+00 , 9.9103e-01);
daniQQue 22:eb8411807cca 61 BiQuad filternotch2_b1(1.0000e+00, -1.9022e+00, 1.0000e+00, -1.8965e+00 , 9.9127e-01);
daniQQue 23:123d65f76236 62
daniQQue 23:123d65f76236 63 //t1= triceps right arm
daniQQue 22:eb8411807cca 64 BiQuad filterhigh_t1(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01);
daniQQue 22:eb8411807cca 65 BiQuad filternotch1_t1 (9.9115e-01, -1.8853e+00, 9.9115e-01 ,-1.8909e+00 , 9.9103e-01);
daniQQue 22:eb8411807cca 66 BiQuad filternotch2_t1(1.0000e+00, -1.9022e+00, 1.0000e+00, -1.8965e+00 , 9.9127e-01);
daniQQue 21:9b8be3dff01a 67
daniQQue 23:123d65f76236 68 //b2= biceps left arm
daniQQue 23:123d65f76236 69 BiQuad filterhigh_b2(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01);
daniQQue 23:123d65f76236 70 BiQuad filternotch1_b2 (9.9115e-01, -1.8853e+00, 9.9115e-01 ,-1.8909e+00 , 9.9103e-01);
daniQQue 23:123d65f76236 71 BiQuad filternotch2_b2(1.0000e+00, -1.9022e+00, 1.0000e+00, -1.8965e+00 , 9.9127e-01);
daniQQue 23:123d65f76236 72
daniQQue 21:9b8be3dff01a 73 //after abs filtering
daniQQue 22:eb8411807cca 74 BiQuad filterlow_b1 (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01);
daniQQue 22:eb8411807cca 75 BiQuad filterlow_t1 (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01);
daniQQue 23:123d65f76236 76 BiQuad filterlow_b2 (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01);
daniQQue 10:7255b59224cc 77
daniQQue 23:123d65f76236 78 //function teller
daniQQue 23:123d65f76236 79 void SwitchN() { // maakt simpele functie die 1 bij n optelt
daniQQue 23:123d65f76236 80 if(switch_signal_leftarm==1)
daniQQue 23:123d65f76236 81 {
daniQQue 23:123d65f76236 82 n++;
daniQQue 25:58f351d9fcc4 83 wait(0.5f);
daniQQue 23:123d65f76236 84 }
daniQQue 23:123d65f76236 85 }
daniQQue 23:123d65f76236 86
daniQQue 21:9b8be3dff01a 87 //functions which are called in ticker to sample the analog signal
daniQQue 23:123d65f76236 88
daniQQue 0:34c739fcc3e0 89 void filter(){
daniQQue 21:9b8be3dff01a 90 //biceps right arm read+filtering
daniQQue 15:bb4a6c7836d8 91 emg_biceps_right=emg_biceps_right_in.read(); //read the emg value from the elektrodes
daniQQue 22:eb8411807cca 92 emg_filtered_high_biceps_right= filterhigh_b1.step(emg_biceps_right);
daniQQue 22:eb8411807cca 93 emg_filtered_high_notch_1_biceps_right=filternotch1_b1.step(emg_filtered_high_biceps_right);
daniQQue 22:eb8411807cca 94 emg_filtered_high_notch_1_2_biceps_right=filternotch2_b1.step(emg_filtered_high_notch_1_biceps_right);
daniQQue 21:9b8be3dff01a 95 emg_abs_biceps_right=fabs(emg_filtered_high_notch_1_2_biceps_right); //fabs because float
daniQQue 22:eb8411807cca 96 emg_filtered_biceps_right=filterlow_b1.step(emg_abs_biceps_right);
daniQQue 21:9b8be3dff01a 97
daniQQue 21:9b8be3dff01a 98 //triceps right arm read+filtering
daniQQue 21:9b8be3dff01a 99 emg_triceps_right=emg_triceps_right_in.read(); //read the emg value from the elektrodes
daniQQue 22:eb8411807cca 100 emg_filtered_high_triceps_right= filterhigh_t1.step(emg_triceps_right);
daniQQue 22:eb8411807cca 101 emg_filtered_high_notch_1_triceps_right=filternotch1_t1.step(emg_filtered_high_triceps_right);
daniQQue 22:eb8411807cca 102 emg_filtered_high_notch_1_2_triceps_right=filternotch2_t1.step(emg_filtered_high_notch_1_triceps_right);
daniQQue 21:9b8be3dff01a 103 emg_abs_triceps_right=fabs(emg_filtered_high_notch_1_2_triceps_right); //fabs because float
daniQQue 22:eb8411807cca 104 emg_filtered_triceps_right=filterlow_t1.step(emg_abs_triceps_right);
daniQQue 7:42d0e38196f1 105
daniQQue 23:123d65f76236 106 //biceps left arm read+filtering
daniQQue 23:123d65f76236 107 emg_biceps_left=emg_biceps_left_in.read(); //read the emg value from the elektrodes
daniQQue 23:123d65f76236 108 emg_filtered_high_biceps_left= filterhigh_b2.step(emg_biceps_left);
daniQQue 23:123d65f76236 109 emg_filtered_high_notch_1_biceps_left=filternotch1_b2.step(emg_filtered_high_biceps_left);
daniQQue 23:123d65f76236 110 emg_filtered_high_notch_1_2_biceps_left=filternotch2_b2.step(emg_filtered_high_notch_1_biceps_left);
daniQQue 23:123d65f76236 111 emg_abs_biceps_left=fabs(emg_filtered_high_notch_1_2_biceps_left); //fabs because float
daniQQue 23:123d65f76236 112 emg_filtered_biceps_left=filterlow_b2.step(emg_abs_biceps_left);
daniQQue 23:123d65f76236 113
daniQQue 21:9b8be3dff01a 114 //signal substraction of filter biceps and triceps. Biceps +,triceps -
daniQQue 21:9b8be3dff01a 115 signal_right_arm=emg_filtered_biceps_right-emg_filtered_triceps_right;
daniQQue 21:9b8be3dff01a 116
daniQQue 23:123d65f76236 117 //creating of on/off signal with the created on/off signals, with if statement for right arm!
daniQQue 21:9b8be3dff01a 118 if (signal_right_arm>cut_off_value_biceps)
daniQQue 21:9b8be3dff01a 119 {onoffsignal_rightarm=1;}
daniQQue 7:42d0e38196f1 120
daniQQue 21:9b8be3dff01a 121 else if (signal_right_arm<cut_off_value_triceps)
daniQQue 21:9b8be3dff01a 122 {
daniQQue 21:9b8be3dff01a 123 onoffsignal_rightarm=-1;
daniQQue 21:9b8be3dff01a 124 }
daniQQue 21:9b8be3dff01a 125
daniQQue 21:9b8be3dff01a 126 else
daniQQue 21:9b8be3dff01a 127 {onoffsignal_rightarm=0;}
daniQQue 7:42d0e38196f1 128
daniQQue 23:123d65f76236 129 //creating on/off signal for switch (left arm)
daniQQue 23:123d65f76236 130
daniQQue 23:123d65f76236 131 if (emg_filtered_biceps_left>cut_off_value_biceps)
daniQQue 23:123d65f76236 132 {
daniQQue 23:123d65f76236 133 switch_signal_leftarm=1;
daniQQue 23:123d65f76236 134 }
daniQQue 23:123d65f76236 135
daniQQue 23:123d65f76236 136 else
daniQQue 23:123d65f76236 137 {
daniQQue 23:123d65f76236 138 switch_signal_leftarm=0;
daniQQue 23:123d65f76236 139 }
daniQQue 23:123d65f76236 140
daniQQue 0:34c739fcc3e0 141 //send signals to scope
daniQQue 25:58f351d9fcc4 142 scope.set(0, emg_filtered_biceps_right); //set emg signal to scope in channel 0 // change into raw signal!
daniQQue 25:58f351d9fcc4 143 scope.set(1, emg_filtered_triceps_right); // set emg signal to scope in channel 1// change into raw signal!
daniQQue 25:58f351d9fcc4 144 scope.set(2, emg_filtered_biceps_left); // set emg signal to scope in channel 2
daniQQue 25:58f351d9fcc4 145 scope.set(3, onoffsignal_rightarm); // set emg signal to scope in channel 3
daniQQue 23:123d65f76236 146 scope.set(4, switch_signal_leftarm);
daniQQue 23:123d65f76236 147
daniQQue 0:34c739fcc3e0 148 scope.send(); //send all the signals to the scope
daniQQue 0:34c739fcc3e0 149 }
daniQQue 0:34c739fcc3e0 150
daniQQue 0:34c739fcc3e0 151 //program
daniQQue 0:34c739fcc3e0 152
daniQQue 0:34c739fcc3e0 153 int main()
daniQQue 0:34c739fcc3e0 154 {
daniQQue 23:123d65f76236 155 pc.baud(115200);
daniQQue 0:34c739fcc3e0 156 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 25:58f351d9fcc4 157 switch_function.attach(&SwitchN,1);
daniQQue 0:34c739fcc3e0 158 //endless loop
daniQQue 0:34c739fcc3e0 159
daniQQue 23:123d65f76236 160 while (true) { // zorgt er voor dat de code oneindig doorgelopen wordt
daniQQue 23:123d65f76236 161
daniQQue 24:bfc240e381b4 162 if (onoffsignal_rightarm==1) // als s ingedrukt wordt gebeurd het volgende
daniQQue 23:123d65f76236 163 {
daniQQue 23:123d65f76236 164 if (n%2==0) // als s ingedrukt wordt en het getal is even gebeurd het onderstaande
daniQQue 23:123d65f76236 165 {
daniQQue 23:123d65f76236 166 pc.printf("n is even \n\r"); // print lijn "n is even"
daniQQue 23:123d65f76236 167 pc.printf("up \n\r"); // print lijn "up"
daniQQue 24:bfc240e381b4 168 richting_motor1 = 2.5;
daniQQue 23:123d65f76236 169 pwm_motor1 = 1;
daniQQue 23:123d65f76236 170
daniQQue 23:123d65f76236 171 }
daniQQue 23:123d65f76236 172
daniQQue 23:123d65f76236 173 else // als s is ingedrukt maar het getal is niet even (dus oneven) gebeurdt het onderstaande
daniQQue 23:123d65f76236 174 {
daniQQue 23:123d65f76236 175 pc.printf("n is odd \n\r"); // print lijn "n is odd"
daniQQue 23:123d65f76236 176 pc.printf("left \n\r"); // print lijn "left"
daniQQue 24:bfc240e381b4 177 richting_motor2 = 2.5;
daniQQue 23:123d65f76236 178 pwm_motor2 = 1;
daniQQue 23:123d65f76236 179
daniQQue 23:123d65f76236 180 }
daniQQue 23:123d65f76236 181
daniQQue 23:123d65f76236 182 }
daniQQue 24:bfc240e381b4 183 else if (onoffsignal_rightarm==-1) // als d ingedrukt wordt gebeurd het volgende
daniQQue 23:123d65f76236 184 {
daniQQue 23:123d65f76236 185 if (n%2==0) // als d is ingedrukt en n is even dan gebeurd het volgende
daniQQue 23:123d65f76236 186 {
daniQQue 23:123d65f76236 187 pc.printf("n is even \n\r"); // print lijn "n is even"
daniQQue 23:123d65f76236 188 pc.printf("down \n\r"); // print lijn "down"
daniQQue 24:bfc240e381b4 189 richting_motor1 = 0;
daniQQue 23:123d65f76236 190 pwm_motor1 = 1;
daniQQue 23:123d65f76236 191
daniQQue 23:123d65f76236 192 }
daniQQue 23:123d65f76236 193 else // als d is ingedrukt maar het getal is niet even (dus oneven) gebeurdt het onderstaande
daniQQue 23:123d65f76236 194 {
daniQQue 23:123d65f76236 195 pc.printf("n is odd \n\r"); // print lijn "n is odd"
daniQQue 23:123d65f76236 196 pc.printf("right \n\r"); // print lijn "right"
daniQQue 24:bfc240e381b4 197 richting_motor2 = 0;
daniQQue 23:123d65f76236 198 pwm_motor2 = 1;
daniQQue 23:123d65f76236 199
daniQQue 23:123d65f76236 200 }
daniQQue 23:123d65f76236 201 }
daniQQue 23:123d65f76236 202 else{
daniQQue 23:123d65f76236 203 pc.printf("motor staat stil \n\r");
daniQQue 23:123d65f76236 204 pwm_motor2=0;
daniQQue 23:123d65f76236 205 pwm_motor1=0;
daniQQue 23:123d65f76236 206 }
daniQQue 23:123d65f76236 207
daniQQue 23:123d65f76236 208 }
daniQQue 0:34c739fcc3e0 209
daniQQue 0:34c739fcc3e0 210 }