EMG signals all around zero, not showing differences anymore in hidscope

Dependencies:   HIDScope MODSERIAL biquadFilter mbed

Fork of a_check_emg_filtered_without_cal by Daniqe Kottelenberg

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //libraries
00002 #include "mbed.h"
00003 #include "HIDScope.h"
00004 #include "BiQuad.h"  
00005 #include "MODSERIAL.h"
00006 
00007 //Define objects
00008 AnalogIn    emg_biceps_right_in( A0 );              //analog in to get EMG in to c++
00009 Ticker      sample_timer;                           //ticker
00010 HIDScope    scope( 3);                              //open 3 channels in hidscope
00011 DigitalOut richting_motor1(D4);     //motor1 direction output    
00012 PwmOut pwm_motor1(D5);              //motor1 velocity  output
00013 MODSERIAL pc(USBTX,USBRX);          //connection with computer
00014 DigitalOut  led(LED_GREEN);         //include led
00015 DigitalIn   button (D9);            //button 
00016 
00017 //define variables
00018 
00019 double emg_biceps_right;
00020 double emg_filtered_high_biceps_right;
00021 double emg_abs_biceps_right;
00022 double emg_filtered_biceps_right;
00023 int    onoffsignal=0;
00024 double cut_off_value=0.20;
00025 double max_right_biceps; 
00026 
00027 BiQuad filterhigh(9.5654e-01,-1.9131e+00,9.5654e-01,-1.9112e+00,9.1498e-01);
00028 BiQuad filterlow (6.2942e-06, 1.2588e-05,6.2942e-06,-1.9929e+00,9.9292e-01);
00029 
00030 //functions which are called in ticker
00031 void filter(){
00032         emg_biceps_right                = emg_biceps_right_in.read();                   //read the emg value from the electrodes
00033         emg_filtered_high_biceps_right  = filterhigh.step(emg_biceps_right);
00034         emg_abs_biceps_right            =fabs(emg_filtered_high_biceps_right);          //fabs because float
00035         emg_filtered_biceps_right       =filterlow.step(emg_abs_biceps_right);          //gefilterd met high, abs, low
00036         led=!led;
00037         
00038         if (emg_filtered_biceps_right>cut_off_value)
00039         {onoffsignal=1;}
00040           
00041         else 
00042         {onoffsignal=0;}
00043                       
00044         //send signals  to scope
00045         scope.set(0, emg_biceps_right );           //set emg signal to scope in channel 1
00046         scope.set(1, emg_filtered_biceps_right);    
00047         scope.set(2, onoffsignal);
00048         scope.send();                       //send all the signals to the scope
00049                 }
00050                 
00051 void calibration(){
00052             if(button==0)
00053             {
00054             for(int n =0; n<2000;n++)                                                  //read for 5000 samples as calibration
00055                 {
00056             emg_biceps_right=emg_biceps_right_in.read();                            //read the emg value from the elektrodes
00057             emg_filtered_high_biceps_right= filterhigh.step(emg_biceps_right);      //highpass
00058             emg_abs_biceps_right=fabs(emg_filtered_high_biceps_right);              //fabs because float
00059             emg_filtered_biceps_right=filterlow.step(emg_abs_biceps_right);         //lowpass to envelope
00060                         
00061             if (emg_filtered_biceps_right > max_right_biceps)                    //determine what the highest reachable emg signal is
00062                 {
00063             max_right_biceps = emg_filtered_biceps_right;
00064                 }
00065                 }
00066             cut_off_value=0.2*max_right_biceps; 
00067             pc.printf(" change of cv %f ",cut_off_value );   
00068             }
00069                 }
00070 //program
00071 
00072 int main()
00073 {
00074 pc.baud(115200); //start pc connection baudrate 115200
00075 
00076 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
00077 sample_timer.attach (&calibration,0.001);    //continously execute callibration, only affects it when button is pressed for a while. 
00078 //endless loop
00079     while(1) {
00080                 
00081         if (onoffsignal==1)
00082         {
00083         richting_motor1 = 0;    //motordirection (ccw)
00084         pwm_motor1 = 1;         //motorspeed 1  
00085         
00086         }
00087         else if(onoffsignal==0)
00088         {
00089         richting_motor1 = 0;    //motordirection (ccw)
00090         pwm_motor1 = 0;         //motorspeed 0
00091         }
00092         
00093     }        
00094 }