emg with text

Dependencies:   HIDScope MODSERIAL biquadFilter mbed

Fork of emg_import by Daniqe Kottelenberg

Committer:
daniQQue
Date:
Wed Nov 02 16:24:59 2016 +0000
Revision:
46:4a8889f9dc9f
Parent:
45:08bddea67bd8
Child:
47:ddaa59d48aca
zelfde als net maar ccw en cw en speed als variabelen erin!

Who changed what in which revision?

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