K K / Mbed 2 deprecated EMG_converter_code

Dependencies:   HIDScope biquadFilter mbed MODSERIAL

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "HIDScope.h"
00003 #include "biquadFilter.h"        // Require the HIDScope library
00004 #include "MODSERIAL.h"
00005 //Define objects
00006 AnalogIn    emg(A0); //Analog of EMG input
00007 Ticker      sample_timer;
00008 Ticker      motor_timer;
00009 Ticker      cal_timer;
00010 HIDScope    scope(2);        // Instantize a 2-channel HIDScope object
00011 DigitalIn button1(PTA4);//test button for starting motor 1
00012 DigitalOut led1(LED_RED);
00013 DigitalOut led2(LED_BLUE);
00014 MODSERIAL pc(USBTX,USBRX);
00015 /*The biquad filters required to transform the EMG signal into an usable signal*/
00016 biquadFilter filterhigh1(-1.1430, 0.4128, 0.6389, -1.2779, 0.6389);
00017 biquadFilter filterlow1(1.9556, 0.9565, 0.9780, 1.9561, 0.9780);
00018 biquadFilter notch(-1.1978e-16, 0.9561, 0.9780, -1.1978e-16, 0.9780);
00019 biquadFilter filterlow2(-1.9645, 0.9651, 1.5515e-4, 3.1030e-4, 1.5515e-4);
00020 double emg_value;
00021 double signalpart1;
00022 double signalpart2;
00023 double signalpart3;
00024 double signalpart4;
00025 double signalfinal;
00026 double onoffsignal;
00027 double maxcal=0;        
00028 bool calyes=0;
00029 
00030 /* 
00031  */
00032 void filter(){
00033         if(calyes==1){
00034         emg_value = emg.read();//read the emg value from the elektrodes
00035         signalpart1 = notch.step(emg_value);//Highpass filter for removing offset and artifacts
00036         signalpart2 = filterhigh1.step(signalpart1);//rectify the filtered signal
00037         signalpart3 = abs(signalpart2);//low pass filter to envelope the emg
00038         signalpart4 = filterlow1.step(signalpart3);//notch filter to remove 50Hz signal
00039         signalfinal = filterlow2.step(signalpart4);//2nd low pass filter to envelope the emg
00040         onoffsignal=signalfinal/maxcal;//divide the emg signal by the max EMG to calibrate the signal per person
00041         scope.set(0,emg_value);//set emg signal to scope in channel 1
00042         scope.set(1,onoffsignal);//set filtered signal to scope in channel 2
00043     scope.send();//send the signals to the scope
00044         //pc.printf("emg signal %f, filtered signal %f \n",emg_value,onoffsignal);
00045 }
00046 }
00047 void checkmotor(){//check the normalized signal and set actions if a threshold is passed
00048     if(calyes==1){
00049     if(onoffsignal<=0.25){
00050              led1.write(1);
00051              led2.write(0);
00052              }
00053     else if(onoffsignal >= 0.5){
00054                  led1.write(0);
00055                  led2.write(1);
00056              }
00057              }
00058 }
00059 
00060 void calibri(){//calibration function
00061     if(button1.read()==false){
00062         for(int n =0; n<5000;n++){//read for 5000 samples as calibration
00063             emg_value = emg.read();//read the emg value from the elektrodes
00064             signalpart1 = notch.step(emg_value);//Highpass filter for removing offset and artifacts
00065             signalpart2 = filterhigh1.step(signalpart1);//rectify the filtered signal
00066             signalpart3 = abs(signalpart2);//low pass filter to envelope the emg
00067             signalpart4 = filterlow1.step(signalpart3);//notch filter to remove 50Hz signal
00068             signalfinal = filterlow2.step(signalpart4);//2nd low pass filter to envelope the emg
00069             double signalmeasure = signalfinal;
00070             if (signalmeasure > maxcal){//determine what the highest reachable emg signal is
00071                 maxcal = signalmeasure;
00072                 }
00073             }
00074         calyes=1;
00075     }  
00076     }
00077 int main()
00078 {
00079     pc.baud(115200);
00080     led1.write(1);
00081     led2.write(1);
00082    
00083         sample_timer.attach(&filter, 0.002);//continously execute the EMG reader and filter
00084         motor_timer.attach(&checkmotor, 0.002);//continously execute the motor controller
00085         cal_timer.attach(&calibri, 0.002);//ticker to check if motor is being calibrated
00086         
00087 
00088 while(1){
00089  //random while loop to keep system going                                                 
00090 }     
00091 }