script zover

Dependencies:   HIDScope MODSERIAL mbed

main.cpp

Committer:
wiesdat
Date:
2014-10-17
Revision:
5:9415e5be8235
Parent:
2:620ff9f02d62
Child:
6:6f0bc2e465b0

File content as of revision 5:9415e5be8235:

#include "mbed.h"
#include "MODSERIAL.h"
#include "HIDScope.h"
#include <iostream>

#define A1LP    0.221199216928595
#define B1LP    -0.778800783071405

#define A1HP  -1.999801878951505
#define A0HP   0.999801878951505
#define B1HP   -1.971717601075000
#define B0HP  0.972111984032897

#define TSAMP 0.001

AnalogIn    emg0(PTB1);
MODSERIAL pc(USBTX,USBRX);
HIDScope scope(5);
Ticker timer;
float emg_value, ylp1, yhp1;
volatile bool looptimerflag;
float ysum = 0, yave=0 ;


float readEMG()
{

    emg_value=emg0.read();
    return emg_value;
}

float hpfilter1(float emg_value)
{
    static float y,x1=0,y1=0,x2=0, y2=0;
    yhp1 = y1*B1HP-y2*B0HP-x1*A1HP+x2*A0HP;
    x2 = x1;
    x1 = emg_value;
    y2 = y1;
    y1 = y;
    return yhp1;
}

float lpfilter1(float yhp1)
{
    static float y,x1=0,y1=0,x=0;
    ylp1 = A1LP*x1+y1*B1LP;
    y1 = y;
    x1 = x;
    x = yhp1;
    return ylp1;
}

void viewer()
{
    scope.set(0,emg_value);
    scope.set(1,yhp1);
    scope.set(2,ylp1);
    scope.set(3,yave);
    scope.set(4,ysum);
    scope.send();
}

void setlooptimerflag(void)
{
    looptimerflag = true;
}

int main()
{
    pc.baud(115200);
    timer.attach(setlooptimerflag,TSAMP);
    int n = 0;
    while(1) {

        while(!looptimerflag) /* do nothing */;
        looptimerflag = false;
        emg_value =  readEMG();
        yhp1 = hpfilter1(emg_value);  //function hpfilter
        ylp1 = lpfilter1(emg_value);       //function filter
        ysum = ysum+yhp1;
        n++;
       
        if(n==100) {
            yave = ysum/100;
            ysum = 0;
            n = 0;
            }

         viewer();                  //functie hidscope
        

    }
}