EMG filtered

Dependencies:   HIDScope mbed

Fork of EMG by Tom Tom

main.cpp

Committer:
john111222333
Date:
2017-10-09
Revision:
21:173af3b4367d
Parent:
20:97059009a491
Child:
22:c7ccfe13d168

File content as of revision 21:173af3b4367d:

#include "mbed.h"
#include "HIDScope.h"

//Define objects
AnalogIn    EMGData1( A0 );
AnalogIn    emg1( A1 );

Ticker      sample_timer;
HIDScope    scope( 2 );
DigitalOut  led(LED1);
volatile float EMGData_f1=0;
double f_v1_EMG=0, f_v2_EMG=0; // set filter variables EMG 1
const double M1_F_A1_EMG=-1.561018075800718, M1_F_A2_EMG=0.641351538057563, M1_F_B0_EMG=0.020083365564211, 
M1_F_B1_EMG=0.040166731128423, M1_F_B2_EMG=0.020083365564211;// set EMG filter coefficients 

/** Sample function
 * this function samples the emg and sends it to HIDScope
 **/
 
//IIRFilter
double IIRFilter(double u, double &v1, double &v2, const double a1, 
const double a2, const double b0, const double b1, const double b2){
    double v = u-a1*v1-a2*v2;
    double y = b0*v + b1*v1 + b2*v2;
    v2=v1; v1=v;
    return y;
}


void sample()
{
    EMGData_f1 = IIRFilter(EMGData1, f_v1_EMG, f_v2_EMG, M1_F_A1_EMG, M1_F_A2_EMG, M1_F_B0_EMG, M1_F_B1_EMG, M1_F_B2_EMG);

    /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */
    scope.set(0, EMGData1);                            // set scope channel 0 EMG
    scope.set(1, EMGData_f1);                           // set scope channel 1 EMG filtered
    /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels) 
    *  Ensure that enough channels are available (HIDScope scope( 2 ))
    *  Finally, send all channels to the PC at once */
    scope.send();
    /* To indicate that the function is working, the LED is toggled */
    led = !led;
}

int main()
{   
    /**Attach the 'sample' function to the timer 'sample_timer'.
    * this ensures that 'sample' is executed every... 0.002 seconds = 500 Hz
    */
    sample_timer.attach(&sample, 0.005);

    /*empty loop, sample() is executed periodically*/
    while(1) {}
}