For BIOROBOTICS PROJECT

Dependencies:   HIDScope MODSERIAL QEI mbed

main.cpp

Committer:
dbayuadi
Date:
2015-10-21
Revision:
1:3bae5ab25e20
Parent:
0:8ccd4c66e07f
Child:
2:ac98d055a6cd

File content as of revision 1:3bae5ab25e20:

#include "mbed.h"
#include "HIDScope.h"
#include "QEI.h"
AnalogIn    emg(A0);
HIDScope scope(5);
Ticker get;
AnalogIn pot(A1);
Serial pc(USBTX, USBRX); // tx, rx
const int N = 10;
volatile bool fn_go = false;
double y_notch_1;
double y_notch_2;
double y_high;
double y_low;
double y_rect;
const double Fs = 500;
const double Ts = 0.002;
double a;
const double gain_notch_1 = 0.912483;
const double b0_notch_1 = 1.0*gain_notch_1;
const double b1_notch_1 = -1.62829098849*gain_notch_1;
const double b2_notch_1 = 1.0*gain_notch_1;
const double a1_notch_1 = -1.49965530713;
const double a2_notch_1 =  0.90720693582;
double v1_notch_1 = 0, v2_notch_1 = 0;


const double gain_notch_2 = 1.000000;
const double b0_notch_2 = 1.0*gain_notch_2;
const double b1_notch_2 = -1.63220522905*gain_notch_2;
const double b2_notch_2 = 1.0*gain_notch_2;
const double a1_notch_2 = -1.61200955424;
const double a2_notch_2 =  0.91813588764;
double v1_notch_2 = 0, v2_notch_2 = 0;

const double gain_high = 0.981587;
const double b0_high = 1.0*gain_high;
const double b1_high = -1.99999992457*gain_high;
const double b2_high = 1.0*gain_high;
const double a1_high = -1.96306054568;
const double a2_high =  0.96374056522;
double v1_high = 0, v2_high = 0;

const double gain_low = 0.000561;
const double b0_low = 1.0*gain_low;
const double b1_low = 2.000*gain_low;
const double b2_low = 1.0*gain_low;
const double a1_low =   -1.93191036285;
const double a2_low =   0.93415335760;
double v1_low = 0, v2_low = 0;


double biquad(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 fn_activate()
{

    fn_go = true;
}

double sliding_average(double u,const int f_N)
{
    double f_x[f_N];
    double f_sum=0;
    f_x[1]=abs(u);
    for (int i=f_N; i>=1; i--){
        f_x[i]=f_x[i-1];
        }
        
      
    for (int i=f_N; i>=0; i--){
        f_sum=f_x[i]+f_sum;
       }
        a=f_sum/double(f_N);
        f_sum=0;
    return a;
}
double z1;
void scopeSend()
{
  y_high = biquad(emg.read(), v1_high, v1_high, a1_high, a2_high, b0_high, b1_high, b2_high); 
  y_notch_1 = biquad(y_high, v1_notch_1, v1_notch_1, a1_notch_1,a2_notch_1,b0_notch_1, b1_notch_1, b2_notch_1);
    y_notch_2 = biquad(y_notch_1, v1_notch_2, v1_notch_2, a1_notch_2, a2_notch_2, b0_notch_2, b1_notch_2, b2_notch_2);  
  y_rect = fabs(y_notch_2);             
            y_low = biquad(y_rect,  v1_low, v1_low, a1_low, a2_low, b0_low, b1_low, b2_low);
// z1 = 6*sliding_average(emg.read(), N); //moving average
scope.set(0,emg.read());
scope.set(1,y_high);
  scope.set(2,y_rect);
  scope.set(3,y_low);
 // scope.set(4,z1);

    scope.send();
}

int main()
{
get.attach(&fn_activate,Ts);

    while (true) {
        
        if(fn_go == true) {
            
            scopeSend();
            
           
            fn_go = false;
        }
    }
}