Changed some stuff

Fork of EMG by Kevin Hetterscheid

emg.cpp

Committer:
AeroKev
Date:
2015-10-20
Revision:
23:8647e71ca713
Parent:
22:8c9dda710584
Child:
24:b7b3e87e0687

File content as of revision 23:8647e71ca713:

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

//Define objects
AnalogIn    emgIn0(A0); //Analog input
AnalogIn    emgIn1(A1); //Analog input
AnalogIn    emgIn2(A2); //Analog input

double highV[4];
double lowV[4];

double filter(double input, double coeff_input[], double coeff_output[], double prev_outputs[])
{
    double new_input = input;
    for(int i=1; i<5; i++) 
        new_input -= coeff_input[i] * prev_outputs[i-1];

    double new_output = coeff_output[0] * new_input;
    for(int i=1; i<5; i++) 
        new_output += coeff_output[i] * prev_outputs[i-1];

    // Set the new output as the first value of the 'recent outputs'
    for(int i = 3; i > 0; i--)
        prev_outputs[i] = prev_outputs[i-1];
    prev_outputs[0] = new_input;
    return new_output;
}

double fh_b[]= {0.7602, -3.0406, 4.5609, -3.0406, 0.7602};
double fh_a[]= {1, -3.4532, 4.5041, -2.6273, 0.5778};
double highpass_filter(double u)
{
    return filter(u, fh_a, fh_b, highV);
}

double fl_b[]= {0.00001329,    0.00005317,    0.00007976,   0.00005317,    0.00001329};
double fl_a[]= {1.0000,   -3.6717,    5.0680,   -3.1160,    0.7199};
double lowpass_filter(double u)
{
    return filter(u, fl_a, fl_b, lowV);
}

/** Sample function
 * this function samples the emg and sends it to HIDScope
 **/
double sample(int emgNum)
{
    double input = 0.0;
    if(emgNum == 1) input = emgIn1.read();
    if(emgNum == 2) input = emgIn2.read();
    else input = emgIn0.read();
    double output1 = highpass_filter(input);
    double output2 = fabs(output1);
    double output3 = lowpass_filter(output2);
    
    if(output3 >= 0 && output3 < 0.0507)
        output3 = 0.0;
    else if(output3 >= 0.0507 && output3 < 0.0966)
        output3 = 0.5;
    else if(output3 >= 0.0966 && output3 < 1)
        output3 = 1.0;
    
    return output3;
}