Initial version

Dependencies:   MODSERIAL mbed

main.cpp

Committer:
vsluiter
Date:
2013-10-17
Revision:
4:d0b4c806f4ea
Parent:
3:57c5633df1ca

File content as of revision 4:d0b4c806f4ea:

#include "mbed.h"
#include "MODSERIAL.h"

//Define objects
AnalogIn    emg_biceps(PTB0); //Analog input
PwmOut      red(LED_RED); // EMG meting
// PwmOut      blue(LED_BLUE); // uitgangssignaal controle
// PwmOut      green(LED_GREEN); 

Ticker timer;
MODSERIAL pc(USBTX,USBRX,64,1024);

#define GAIN_BICEPS 1
#define MAXCOUNT 50

#define NUM0 0.8841 // constante
#define NUM1 -3.53647 // z^-1
#define NUM2 5.3046 // z^-2etc.
#define NUM3 -3.5364
#define NUM4 0.8841

#define DEN0 1 // constante
#define DEN1 -3.7538
#define DEN2 5.2912
#define DEN3 -3.3189
#define DEN4 0.7816


/* hou in de gaten welke waarden globaal gedefinieerd moeten worden*/ 
float count =0;
float square_biceps = 0;
float sum_biceps = 0;
float mean_biceps = 0.2;

void looper()
{
    /*value0 is huidig, 1 is t-1, 2 is t-2 etc. Gebruik later aanduidingen ABCD. */   
    float mean;
    float sig_out_biceps;
    //static variables keep their values between function calls
    //the assignents are only executed the first iteration.
    static float in0 =0,in1 =0 ,in2 = 0,in3 = 0,in4 = 0;
    static float out0 = 0,out1 = 0 ,out2 = 0,out3 = 0,out4 = 0;
    float emg_abs; // square, mean en count eerder gedefinieerd
    //    initialisation of globals should be done at initialization of program
    //    count = 0; square_biceps = 0; sum_biceps = 0; mean_biceps = 0.2;
    in4 = in3; in3 = in4; in3 = in2; in2 = in1; in1 = in0;
    in0 = emg_biceps.read();
    red = in0;
    /* rode led aan voor meting emg*/ 
    out4 = out3; out3 = out2; out2 = out1; out1 = out0;
    out0 = (NUM0*in0 + NUM1*in1 + NUM2*in2 + NUM3*in3 + NUM4*in4 - DEN1*out1 - DEN2*out2 - DEN3*out3 - DEN4*out4 ) / DEN0;
       
    /*send value to PC. use 6 digits after decimal sign*/
    //if(pc.rxBufferGetSize(0)-pc.rxBufferGetCount() > 30) // ! Testen filter: gebruik de if om de serial optimaal te gebruiken.
        //pc.printf("%.6f\n",emg_out_biceps);
    /**When not using the LED, the above could also have been done this way:
    * pc.printf("%.6\n", emg0.read());
    */

    emg_abs = fabs(out0);
    mean = mean_biceps;
    sum_biceps += out0;
    square_biceps += (emg_abs - mean)*(emg_abs - mean); //neem absolute waarde, kwadrateer, voeg toe aan vorige square
    // voeg rest EMG's toe, variabelen alleen _spier geven als het nodig is.
    count += 1; // hou bij hoeveel squares er zijn opgeteld
    if (count >= MAXCOUNT)
        {   sig_out_biceps = sqrt(square_biceps/count);
            mean_biceps = sum_biceps/count;
            count= 0; square_biceps = 0; sum_biceps = 0; // en neem de RMS als er genoeg zijn geteld, stuur die door, en reset sqaure en count
            if(pc.txBufferGetSize(0)-pc.txBufferGetCount() > 30)
                pc.printf("%.6f\n",sig_out_biceps);
         }
}

int main()
{
    /*setup baudrate. Choose the same in your program on PC side*/
    pc.baud(115200);
    /*set the period for the PWM to the red LED*/
    red.period_ms(2); // periode pwm = 2*Fs , blijkbaar.
    // blue.period_ms(2);


    /**Here you attach the 'void looper(void)' function to the Ticker object0
    * The looper() function will be called every 0.001 seconds.
    * Please mind that the parentheses after looper are omitted when using attach.
    */ 
    timer.attach(looper, 0.001);
    while(1) // Loop
    {   
            // blue = sig_out_biceps;
        
    }
}