EMG filtered

Dependencies:   HIDScope mbed

Fork of EMG by Tom Tom

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 
00004 //Define objects
00005 AnalogIn    EMGData_1( A0 );
00006 AnalogIn    emg1( A1 );
00007 
00008 Ticker      sample_timer;
00009 HIDScope    scope( 2 );
00010 DigitalOut  led(LED1);
00011 
00012 volatile float EMGData_1_HiFil=0;   //EMG Hi Filtered
00013 volatile float EMGData_1_HiFilAbs=0;    //EMG Hi Filtered abs
00014 volatile float EMGData_1_HiLoFil=0;   //EMG HiLo Filtered
00015 
00016 double f_v1_EMG_1_Hi=0, f_v2_EMG_1_Hi=0; // set Hi filter variables EMG 1
00017 const double M1_F_A1_EMG_Hi=-1.561018075800718, M1_F_A2_EMG_Hi=0.641351538057563, M1_F_B0_EMG_Hi=0.800592403464570, 
00018 M1_F_B1_EMG_Hi=-1.601184806929141, M1_F_B2_EMG_Hi=0.800592403464570;// set EMG filter coefficients 
00019 
00020 double f_v1_EMG_1_Lo=0, f_v2_EMG_1_Lo=0; // set Lo filter variables EMG 1
00021 const double M1_F_A1_EMG_Lo=-1.561018075800718, M1_F_A2_EMG_Lo=0.641351538057563, M1_F_B0_EMG_Lo=0.800592403464570, 
00022 M1_F_B1_EMG_Lo=-1.601184806929141, M1_F_B2_EMG_Lo=0.800592403464570;    // set EMG filter coefficients 
00023 
00024 /** Sample function
00025  * this function samples the emg and sends it to HIDScope
00026  **/
00027  
00028 //IIRFilter
00029 double IIRFilter(double u, double &v1, double &v2, const double a1, 
00030 const double a2, const double b0, const double b1, const double b2){
00031     double v = u-a1*v1-a2*v2;
00032     double y = b0*v + b1*v1 + b2*v2;
00033     v2=v1; v1=v;
00034     return y;
00035 }
00036 
00037 
00038 void sample()
00039 {
00040     EMGData_1_HiFil = IIRFilter(EMGData_1, f_v1_EMG_1_Hi, f_v2_EMG_1_Hi, M1_F_A1_EMG_Hi, M1_F_A2_EMG_Hi, M1_F_B0_EMG_Hi, M1_F_B1_EMG_Hi, M1_F_B2_EMG_Hi);
00041     EMGData_1_HiFilAbs = abs(EMGData_1_HiFil);
00042     EMGData_1_HiLoFil = IIRFilter(EMGData_1_HiFilAbs, f_v1_EMG_1_Lo, f_v2_EMG_1_Lo, M1_F_A1_EMG_Lo, M1_F_A2_EMG_Lo, M1_F_B0_EMG_Lo, M1_F_B1_EMG_Lo, M1_F_B2_EMG_Lo);
00043     /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */
00044     scope.set(0, EMGData_1);                            // set scope channel 0 EMG
00045     scope.set(1, EMGData_1_HiLoFil);                           // set scope channel 1 EMG filtered
00046     /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels) 
00047     *  Ensure that enough channels are available (HIDScope scope( 2 ))
00048     *  Finally, send all channels to the PC at once */
00049     scope.send();
00050     /* To indicate that the function is working, the LED is toggled */
00051     led = !led;
00052 }
00053 
00054 int main()
00055 {   
00056     /**Attach the 'sample' function to the timer 'sample_timer'.
00057     * this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz
00058     */
00059     sample_timer.attach(&sample, 0.005);
00060 
00061     /*empty loop, sample() is executed periodically*/
00062     while(1) {}
00063 }