EMG original converter code

Dependencies:   HIDScope MODSERIAL biquadFilter mbed

Fork of EMG_converter_code by K K

Committer:
huismaja
Date:
Wed Oct 26 08:13:32 2016 +0000
Revision:
9:07188eb1e8a1
Parent:
8:87b07f148124
Original EMG converter code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Technical_Muffin 0:1883d922ada8 1 #include "mbed.h"
huismaja 9:07188eb1e8a1 2 #include "HIDScope.h"
huismaja 9:07188eb1e8a1 3 #include "biquadFilter.h" // Require the HIDScope library
Technical_Muffin 3:a69f041108d4 4 #include "MODSERIAL.h"
huismaja 8:87b07f148124 5
Technical_Muffin 0:1883d922ada8 6 //Define objects
huismaja 9:07188eb1e8a1 7 AnalogIn emg(A0); //Analog of EMG input
Technical_Muffin 0:1883d922ada8 8 Ticker sample_timer;
Technical_Muffin 5:46e201518dd3 9 Ticker motor_timer;
Technical_Muffin 6:ec965bb75d40 10 Ticker cal_timer;
Technical_Muffin 0:1883d922ada8 11 HIDScope scope(2); // Instantize a 2-channel HIDScope object
huismaja 8:87b07f148124 12 DigitalIn button1(PTA4);//test button for starting motor 1
huismaja 8:87b07f148124 13 DigitalOut led1(LED_RED);
huismaja 8:87b07f148124 14 DigitalOut led2(LED_BLUE);
huismaja 8:87b07f148124 15 MODSERIAL pc(USBTX,USBRX);
huismaja 9:07188eb1e8a1 16
Technical_Muffin 0:1883d922ada8 17 /*The biquad filters required to transform the EMG signal into an usable signal*/
Technical_Muffin 0:1883d922ada8 18 biquadFilter filterhigh1(-1.1430, 0.4128, 0.6389, -1.2779, 0.6389);
Technical_Muffin 0:1883d922ada8 19 biquadFilter filterlow1(1.9556, 0.9565, 0.9780, 1.9561, 0.9780);
Technical_Muffin 0:1883d922ada8 20 biquadFilter notch(-1.1978e-16, 0.9561, 0.9780, -1.1978e-16, 0.9780);
Technical_Muffin 0:1883d922ada8 21 biquadFilter filterlow2(-1.9645, 0.9651, 1.5515e-4, 3.1030e-4, 1.5515e-4);
Technical_Muffin 1:9913e3886643 22 double emg_value;
Technical_Muffin 1:9913e3886643 23 double signalpart1;
Technical_Muffin 1:9913e3886643 24 double signalpart2;
Technical_Muffin 1:9913e3886643 25 double signalpart3;
Technical_Muffin 1:9913e3886643 26 double signalpart4;
Technical_Muffin 1:9913e3886643 27 double signalfinal;
Technical_Muffin 2:83659da3e5fe 28 double onoffsignal;
Technical_Muffin 4:fd29407c3115 29 double maxcal=0;
Technical_Muffin 6:ec965bb75d40 30 bool calyes=0;
Technical_Muffin 6:ec965bb75d40 31
Technical_Muffin 0:1883d922ada8 32 /*
Technical_Muffin 0:1883d922ada8 33 */
Technical_Muffin 2:83659da3e5fe 34 void filter(){
Technical_Muffin 6:ec965bb75d40 35 if(calyes==1){
Technical_Muffin 1:9913e3886643 36 emg_value = emg.read();//read the emg value from the elektrodes
Technical_Muffin 3:a69f041108d4 37 signalpart1 = notch.step(emg_value);//Highpass filter for removing offset and artifacts
Technical_Muffin 3:a69f041108d4 38 signalpart2 = filterhigh1.step(signalpart1);//rectify the filtered signal
Technical_Muffin 3:a69f041108d4 39 signalpart3 = abs(signalpart2);//low pass filter to envelope the emg
Technical_Muffin 3:a69f041108d4 40 signalpart4 = filterlow1.step(signalpart3);//notch filter to remove 50Hz signal
Technical_Muffin 1:9913e3886643 41 signalfinal = filterlow2.step(signalpart4);//2nd low pass filter to envelope the emg
Technical_Muffin 2:83659da3e5fe 42 onoffsignal=signalfinal/maxcal;//divide the emg signal by the max EMG to calibrate the signal per person
Technical_Muffin 1:9913e3886643 43 scope.set(0,emg_value);//set emg signal to scope in channel 1
Technical_Muffin 2:83659da3e5fe 44 scope.set(1,onoffsignal);//set filtered signal to scope in channel 2
huismaja 8:87b07f148124 45 scope.send();//send the signals to the scope
Technical_Muffin 6:ec965bb75d40 46 //pc.printf("emg signal %f, filtered signal %f \n",emg_value,onoffsignal);
Technical_Muffin 6:ec965bb75d40 47 }
Technical_Muffin 6:ec965bb75d40 48 }
Technical_Muffin 7:87d9904c1c19 49 void checkmotor(){//check the normalized signal and set actions if a threshold is passed
Technical_Muffin 6:ec965bb75d40 50 if(calyes==1){
Technical_Muffin 6:ec965bb75d40 51 if(onoffsignal<=0.25){
Technical_Muffin 6:ec965bb75d40 52 led1.write(1);
Technical_Muffin 6:ec965bb75d40 53 led2.write(0);
Technical_Muffin 6:ec965bb75d40 54 }
Technical_Muffin 6:ec965bb75d40 55 else if(onoffsignal >= 0.5){
Technical_Muffin 6:ec965bb75d40 56 led1.write(0);
Technical_Muffin 6:ec965bb75d40 57 led2.write(1);
Technical_Muffin 6:ec965bb75d40 58 }
Technical_Muffin 6:ec965bb75d40 59 }
Technical_Muffin 0:1883d922ada8 60 }
Technical_Muffin 5:46e201518dd3 61
Technical_Muffin 7:87d9904c1c19 62 void calibri(){//calibration function
Technical_Muffin 6:ec965bb75d40 63 if(button1.read()==false){
Technical_Muffin 7:87d9904c1c19 64 for(int n =0; n<5000;n++){//read for 5000 samples as calibration
Technical_Muffin 6:ec965bb75d40 65 emg_value = emg.read();//read the emg value from the elektrodes
Technical_Muffin 6:ec965bb75d40 66 signalpart1 = notch.step(emg_value);//Highpass filter for removing offset and artifacts
Technical_Muffin 6:ec965bb75d40 67 signalpart2 = filterhigh1.step(signalpart1);//rectify the filtered signal
Technical_Muffin 6:ec965bb75d40 68 signalpart3 = abs(signalpart2);//low pass filter to envelope the emg
Technical_Muffin 6:ec965bb75d40 69 signalpart4 = filterlow1.step(signalpart3);//notch filter to remove 50Hz signal
Technical_Muffin 6:ec965bb75d40 70 signalfinal = filterlow2.step(signalpart4);//2nd low pass filter to envelope the emg
Technical_Muffin 6:ec965bb75d40 71 double signalmeasure = signalfinal;
Technical_Muffin 7:87d9904c1c19 72 if (signalmeasure > maxcal){//determine what the highest reachable emg signal is
Technical_Muffin 6:ec965bb75d40 73 maxcal = signalmeasure;
Technical_Muffin 6:ec965bb75d40 74 }
Technical_Muffin 6:ec965bb75d40 75 }
Technical_Muffin 6:ec965bb75d40 76 calyes=1;
Technical_Muffin 6:ec965bb75d40 77 }
Technical_Muffin 6:ec965bb75d40 78 }
Technical_Muffin 0:1883d922ada8 79 int main()
Technical_Muffin 0:1883d922ada8 80 {
Technical_Muffin 4:fd29407c3115 81 pc.baud(115200);
huismaja 9:07188eb1e8a1 82 led1.write(0);
huismaja 9:07188eb1e8a1 83 led2.write(0);
Technical_Muffin 6:ec965bb75d40 84
huismaja 9:07188eb1e8a1 85 sample_timer.attach(&filter, 0.05);//continously execute the EMG reader and filter
huismaja 9:07188eb1e8a1 86 motor_timer.attach(&checkmotor, 0.05);//continously execute the motor controller
huismaja 9:07188eb1e8a1 87 cal_timer.attach(&calibri, 0.05);//ticker to check if motor is being calibrated
huismaja 9:07188eb1e8a1 88 //0.002
Technical_Muffin 5:46e201518dd3 89
Technical_Muffin 6:ec965bb75d40 90 while(1){
Technical_Muffin 7:87d9904c1c19 91 //random while loop to keep system going
Technical_Muffin 5:46e201518dd3 92 }
Technical_Muffin 0:1883d922ada8 93 }