emg with text

Dependencies:   HIDScope MODSERIAL biquadFilter mbed

Fork of emg_import by Daniqe Kottelenberg

Committer:
daniQQue
Date:
Tue Oct 25 13:06:23 2016 +0000
Revision:
21:9b8be3dff01a
Parent:
15:bb4a6c7836d8
Child:
22:eb8411807cca
emg biceps en triceps

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 0:34c739fcc3e0 5
daniQQue 0:34c739fcc3e0 6 //Define objects
daniQQue 21:9b8be3dff01a 7 AnalogIn emg_biceps_right_in( A0 ); //analog in to get EMG biceps (r) in to c++
daniQQue 21:9b8be3dff01a 8 AnalogIn emg_triceps_right_in(A1); //analog in to get EMG triceps (r) in to c++
daniQQue 21:9b8be3dff01a 9 //AnalogIn emg_biceps_left_in (A2); //analog in to get EMG biceps (l) in to c++
daniQQue 0:34c739fcc3e0 10 Ticker sample_timer; //ticker
daniQQue 21:9b8be3dff01a 11 HIDScope scope(6); //open 3 channels in hidscope
daniQQue 8:cd0cb71b69f2 12 DigitalOut richting_motor1(D4); //motor1 direction output
daniQQue 8:cd0cb71b69f2 13 PwmOut pwm_motor1(D5); //motor1 velocity output
daniQQue 21:9b8be3dff01a 14 DigitalOut led(LED_GREEN); //led included to check where code is
daniQQue 0:34c739fcc3e0 15
daniQQue 0:34c739fcc3e0 16 //define variables
daniQQue 21:9b8be3dff01a 17 //other
daniQQue 21:9b8be3dff01a 18 int onoffsignal_rightarm=0; // on/off signal: 1; biceps activation, 0: nothing, -1, triceps activation
daniQQue 21:9b8be3dff01a 19 double cut_off_value_biceps =0.06; //gespecificeerd door floortje
daniQQue 21:9b8be3dff01a 20 double cut_off_value_triceps=-0.06; //gespecificeerd door floortje
daniQQue 21:9b8be3dff01a 21 double signal_right_arm;
daniQQue 21:9b8be3dff01a 22
daniQQue 21:9b8be3dff01a 23 //biceps arm 1, right arm
daniQQue 15:bb4a6c7836d8 24 double emg_biceps_right;
daniQQue 15:bb4a6c7836d8 25 double emg_filtered_high_biceps_right;
daniQQue 15:bb4a6c7836d8 26 double emg_abs_biceps_right;
daniQQue 15:bb4a6c7836d8 27 double emg_filtered_biceps_right;
daniQQue 21:9b8be3dff01a 28 double emg_filtered_high_notch_1_biceps_right;
daniQQue 21:9b8be3dff01a 29 double emg_filtered_high_notch_1_2_biceps_right;
daniQQue 5:688b1b5530d8 30
daniQQue 21:9b8be3dff01a 31 //triceps arm 1, right arm
daniQQue 21:9b8be3dff01a 32 double emg_triceps_right;
daniQQue 21:9b8be3dff01a 33 double emg_filtered_high_triceps_right;
daniQQue 21:9b8be3dff01a 34 double emg_abs_triceps_right;
daniQQue 21:9b8be3dff01a 35 double emg_filtered_triceps_right;
daniQQue 21:9b8be3dff01a 36 double emg_filtered_high_notch_1_triceps_right;
daniQQue 21:9b8be3dff01a 37 double emg_filtered_high_notch_1_2_triceps_right;
daniQQue 21:9b8be3dff01a 38
daniQQue 21:9b8be3dff01a 39 //before abs filtering
daniQQue 15:bb4a6c7836d8 40 BiQuad filterhigh(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01);
daniQQue 21:9b8be3dff01a 41 BiQuad filternotch1 (9.9115e-01, -1.8853e+00, 9.9115e-01 ,-1.8909e+00 , 9.9103e-01);
daniQQue 21:9b8be3dff01a 42 BiQuad filternotch2(1.0000e+00, -1.9022e+00, 1.0000e+00, -1.8965e+00 , 9.9127e-01);
daniQQue 21:9b8be3dff01a 43
daniQQue 21:9b8be3dff01a 44 //after abs filtering
daniQQue 15:bb4a6c7836d8 45 BiQuad filterlow (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01);
daniQQue 10:7255b59224cc 46
daniQQue 21:9b8be3dff01a 47 //functions which are called in ticker to sample the analog signal
daniQQue 0:34c739fcc3e0 48 void filter(){
daniQQue 21:9b8be3dff01a 49 //biceps right arm read+filtering
daniQQue 15:bb4a6c7836d8 50 emg_biceps_right=emg_biceps_right_in.read(); //read the emg value from the elektrodes
daniQQue 15:bb4a6c7836d8 51 emg_filtered_high_biceps_right= filterhigh.step(emg_biceps_right);
daniQQue 21:9b8be3dff01a 52 emg_filtered_high_notch_1_biceps_right=filternotch1.step(emg_filtered_high_biceps_right);
daniQQue 21:9b8be3dff01a 53 emg_filtered_high_notch_1_2_biceps_right=filternotch2.step(emg_filtered_high_notch_1_biceps_right);
daniQQue 21:9b8be3dff01a 54 emg_abs_biceps_right=fabs(emg_filtered_high_notch_1_2_biceps_right); //fabs because float
daniQQue 15:bb4a6c7836d8 55 emg_filtered_biceps_right=filterlow.step(emg_abs_biceps_right);
daniQQue 21:9b8be3dff01a 56
daniQQue 21:9b8be3dff01a 57 //triceps right arm read+filtering
daniQQue 21:9b8be3dff01a 58 emg_triceps_right=emg_triceps_right_in.read(); //read the emg value from the elektrodes
daniQQue 21:9b8be3dff01a 59 emg_filtered_high_triceps_right= filterhigh.step(emg_triceps_right);
daniQQue 21:9b8be3dff01a 60 emg_filtered_high_notch_1_triceps_right=filternotch1.step(emg_filtered_high_triceps_right);
daniQQue 21:9b8be3dff01a 61 emg_filtered_high_notch_1_2_triceps_right=filternotch2.step(emg_filtered_high_notch_1_triceps_right);
daniQQue 21:9b8be3dff01a 62 emg_abs_triceps_right=fabs(emg_filtered_high_notch_1_2_triceps_right); //fabs because float
daniQQue 21:9b8be3dff01a 63 emg_filtered_triceps_right=filterlow.step(emg_abs_triceps_right);
daniQQue 7:42d0e38196f1 64
daniQQue 21:9b8be3dff01a 65 //signal substraction of filter biceps and triceps. Biceps +,triceps -
daniQQue 21:9b8be3dff01a 66 signal_right_arm=emg_filtered_biceps_right-emg_filtered_triceps_right;
daniQQue 21:9b8be3dff01a 67
daniQQue 21:9b8be3dff01a 68 //creating of on/off signal with the created on/off signals, with if statement
daniQQue 21:9b8be3dff01a 69 if (signal_right_arm>cut_off_value_biceps)
daniQQue 21:9b8be3dff01a 70 {onoffsignal_rightarm=1;}
daniQQue 7:42d0e38196f1 71
daniQQue 21:9b8be3dff01a 72 else if (signal_right_arm<cut_off_value_triceps)
daniQQue 21:9b8be3dff01a 73 {
daniQQue 21:9b8be3dff01a 74 onoffsignal_rightarm=-1;
daniQQue 21:9b8be3dff01a 75 }
daniQQue 21:9b8be3dff01a 76
daniQQue 21:9b8be3dff01a 77 else
daniQQue 21:9b8be3dff01a 78 {onoffsignal_rightarm=0;}
daniQQue 7:42d0e38196f1 79
daniQQue 0:34c739fcc3e0 80 //send signals to scope
daniQQue 21:9b8be3dff01a 81 scope.set(0, emg_filtered_biceps_right); //set emg signal to scope in channel 0
daniQQue 21:9b8be3dff01a 82 scope.set(1, emg_filtered_triceps_right); // set emg signal to scope in channel 1
daniQQue 21:9b8be3dff01a 83 scope.set(2, signal_right_arm); // set emg signal to scope in channel 2
daniQQue 21:9b8be3dff01a 84 scope.set(3, onoffsignal_rightarm); // set emg signal to scope in channel 3
daniQQue 21:9b8be3dff01a 85 scope.set(4, emg_biceps_right);
daniQQue 21:9b8be3dff01a 86 scope.set(5, emg_triceps_right);
daniQQue 0:34c739fcc3e0 87 scope.send(); //send all the signals to the scope
daniQQue 0:34c739fcc3e0 88 }
daniQQue 0:34c739fcc3e0 89
daniQQue 0:34c739fcc3e0 90 //program
daniQQue 0:34c739fcc3e0 91
daniQQue 0:34c739fcc3e0 92 int main()
daniQQue 0:34c739fcc3e0 93 {
daniQQue 0:34c739fcc3e0 94 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 4:7d9ca9c1dcce 95
daniQQue 0:34c739fcc3e0 96 //endless loop
daniQQue 0:34c739fcc3e0 97
daniQQue 0:34c739fcc3e0 98 while(1)
daniQQue 8:cd0cb71b69f2 99 {
daniQQue 21:9b8be3dff01a 100 if (onoffsignal_rightarm==1)
daniQQue 8:cd0cb71b69f2 101 {
daniQQue 21:9b8be3dff01a 102 richting_motor1 = -1; //motordirection (ccw)
daniQQue 10:7255b59224cc 103 pwm_motor1 = 1; //motorspeed 1
daniQQue 8:cd0cb71b69f2 104
daniQQue 8:cd0cb71b69f2 105 }
daniQQue 21:9b8be3dff01a 106 else if(onoffsignal_rightarm==-1)
daniQQue 8:cd0cb71b69f2 107 {
daniQQue 21:9b8be3dff01a 108 richting_motor1 = 1; //motordirection (cw)
daniQQue 21:9b8be3dff01a 109 pwm_motor1 = 1; //motorspeed 1
daniQQue 21:9b8be3dff01a 110 }
daniQQue 8:cd0cb71b69f2 111
daniQQue 21:9b8be3dff01a 112 else if(onoffsignal_rightarm==0)
daniQQue 21:9b8be3dff01a 113 {
daniQQue 21:9b8be3dff01a 114 richting_motor1 = -1; //motordirection (ccw)
daniQQue 21:9b8be3dff01a 115 pwm_motor1 = 0; //motorspeed 0
daniQQue 21:9b8be3dff01a 116
daniQQue 21:9b8be3dff01a 117 }
daniQQue 21:9b8be3dff01a 118
daniQQue 8:cd0cb71b69f2 119 }
daniQQue 0:34c739fcc3e0 120
daniQQue 0:34c739fcc3e0 121 }