emg with text

Dependencies:   HIDScope MODSERIAL biquadFilter mbed

Fork of emg_import by Daniqe Kottelenberg

Committer:
daniQQue
Date:
Tue Nov 01 09:14:55 2016 +0000
Revision:
41:9ea3d5921f07
Parent:
40:187ef29de53d
Child:
42:7164ccd2aa14
aangepaste versie: bicepsen motoren aansturen, triceps switch

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