Dependencies:   mbed HIDScope biquadFilter

main.cpp

Committer:
hidde1104
Date:
2019-10-11
Revision:
22:65c90d816235
Parent:
21:48b40b82d195
Child:
23:4c66541f00d3

File content as of revision 22:65c90d816235:

#include "mbed.h"
#include "HIDScope.h"
#include "BiQuad.h"
InterruptIn but1(PTC6);

BiQuad bq1 (0.881889334678067,  -1.76377866935613,   0.8818893346780671,  -1.77069673005903,   0.797707797506027);

BiQuad bq2 (0.000198358203463849,    0.000396716406927699,    0.000198358203463849, -1.96262073248799,   0.963423352820821);


BiQuadChain bqc;


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

Ticker      sample_timer;
HIDScope    scope( 3 );
DigitalOut  led(LED2);

/** Sample function
 * this function samples the emg and sends it to HIDScope
 **/
 
 float sample_max = 0;
 float sample_min = 1;
 
     float emg0_value = emg0.read(); 
    float emg1_value = emg1.read();
 
    float filter_value = fabs(bq2.step(fabs(bq1.step(emg0_value - emg1_value))));

void calibrate() {

    filter_value = filter_value-sample_min;
    filter_value = filter_value/(sample_max-sample_min);
    } 
 
void sample()
{
    /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */
    float emg0_value = emg0.read(); 
    float emg1_value = emg1.read();
    
    //double filter_value = bqc.step(emg1_value);
    float filter_value = fabs(bq2.step(fabs(bq1.step(emg0_value - emg1_value))));
    if (filter_value > sample_max) {
        sample_max = filter_value;
        }
    if (sample_min > filter_value) {
        sample_min = filter_value;
        }
    
        but1.mode(PullUp); 
    but1.rise(&calibrate);
    
    scope.set(0, emg0.read() );
    scope.set(1, emg1.read() );
    //scope.set(2, filter_value);
    /* 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()
{   
    bqc.add( &bq1);
    /**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.002);
    /*empty loop, sample() is executed periodically*/
    while(1) {}
}