Dit is alleen het EMG gedeelte

Dependencies:   mbed HIDScope biquadFilter MODSERIAL FXOS8700Q

main.cpp

Committer:
Jellehierck
Date:
2019-10-21
Revision:
9:f6a935be28e1
Parent:
8:ea3de43c9e8b

File content as of revision 9:f6a935be28e1:

//c++ script for filtering of measured EMG signals
#include "mbed.h" //Base library
#include "HIDScope.h" // to see if program is working and EMG is filtered properly
// #include "QEI.h"// is needed for the encoder
#include "MODSERIAL.h"// in order for connection with the pc
#include "BiQuad.h"
// #include "FastPWM.h"
// #include "Arduino.h" //misschien handig omdat we het EMG arduino board gebruiken (?)
// #include "EMGFilters.h"
#include <vector> // For easy array management
#include <numeric> // For manipulating array data

// PC serial connection
HIDScope        scope( 2 );
MODSERIAL pc(USBTX, USBRX);

//EMG inputs definieren
AnalogIn emg1_in (A1); //emg van rechterbicep, voor de x-richting
AnalogIn emg2_in (A2); //emg van linkerbicep, voor de y-richting
AnalogIn emg3_in (A3); //emg van een derde (nog te bepalen) spier, voor het vernaderen van de richting

// LED
DigitalOut      led_g(LED_GREEN);
DigitalOut      led_r(LED_RED);
DigitalOut      led_b(LED_BLUE);

// Buttons
InterruptIn button1(D11);
InterruptIn button2(D10);

//variablen voor EMG
volatile double emg1;
volatile double emg2;
volatile double emg3;

// Initialize tickers
Ticker tickSample;
Timeout timeoutCalibrationMVC;
Ticker tickSampleCalibration;

// Initialize values
const double Fs = 100; // sampling frequency (Hz)
const double Ts = 1/Fs; // sampling time (s)
const double T_cal = 5.0f; // Calibration duration (s)


int i_cal = 0; // Global iterator of calibration
const int n_cal = Fs * T_cal; // Number of elements of calibration
double emg1_cal[n_cal]; // Initialize calibration array

// Notch filter coefficients (iirnotch Q factor 35 @50Hz) from MATLAB in the following form:
// b01 b11 b21 a01 a11 a21
BiQuad bq_notch(0.995636295063941, -1.89829218816065,  0.995636295063941,  1,  -1.89829218816065,  0.991272590127882);

// Highpass filter coefficients (butter 4th order @10Hz cutoff) from MATLAB in the following form:
// b01 b11 b21 a01 a11 a21
// b02 b12 b22 a02 a12 a22
BiQuad bq_H1(0.922946103200875, -1.84589220640175,  0.922946103200875,  1,  -1.88920703055163,  0.892769008131025);
BiQuad bq_H2(1,                 -2,                 1,                  1,  -1.95046575793011,  0.954143234875078);
BiQuadChain bqc_notch_high; // Used to chain two 2nd other filters into a 4th order filter

// Lowpass filter coefficients (butter 4th order @5Hz cutoff) from MATLAB in the following form:
// b01 b11 b21 a01 a11 a21
// b02 b12 b22 a02 a12 a22
BiQuad bq_L1(5.32116245737504e-08,  1.06423249147501e-07,   5.32116245737504e-08,   1,  -1.94396715039462,  0.944882378004138);
BiQuad bq_L2(1,                     2,                      1,                      1,  -1.97586467534468,  0.976794920438162);
BiQuadChain bqc_low; // Used to chain two 2nd other filters into a 4th order filter

double getMean(double vect[n_cal])
{
    double sum = 0.0;
    int vect_n = n_cal;

    for ( int i = 0; i < vect_n; i++ ) {
        sum += vect[i];
    }
    return sum/vect_n;
}

double getStdev(double vect[n_cal], double vect_mean)
{
    double sum2 = 0.0;
    int vect_n = n_cal;

    for ( int i = 0; i < vect_n; i++ ) {
        sum2 += pow( vect[i] - vect_mean, 2 );
    }
    double output = sqrt( sum2 / vect_n );
    return output;
}

// Check if filters are stable
bool checkBQChainStable()
{
    bool n_hp_stable =  bqc_notch_high.stable();
    bool l_stable = bqc_low.stable();

    if (n_hp_stable && l_stable) {
        return true;
    } else {
        return false;
    }
}

/*
// Read samples, filter samples and output to HIDScope
void sample()
{
    // Read EMG inputs
    emg1 = emg1_in.read();
    emg2 = emg2_in.read();
    emg3 = emg3_in.read();

    // Output raw EMG input
    scope.set(0, emg1 );

    // Filter notch and highpass
    double emg1_n_hp = bqc_notch_high.step( emg1 );

    // Rectify
    double emg1_rectify = fabs( emg1_n_hp );

    // Filter lowpass (completes envelope)
    double emg1_env = bqc_low.step( emg1_rectify );

    // Output EMG after filters
    scope.set(1, emg1_env );
    scope.send();
}
*/

void calibrationMVCFinished()
{
    // pc.printf("Stop calibration");
    tickSampleCalibration.detach();
    double emg1_mean = getMean(emg1_cal);
    double emg1_stdev = getStdev(emg1_cal, emg1_mean);

    led_b = 1;

    // pc.printf("EMG Mean: %f   stdev: %f\r\n", emg1_mean, emg1_stdev);
}

void sampleCalibration()
{
    if (i_cal >= n_cal-1) {
        calibrationMVCFinished();
    }
    // Read EMG inputs
    emg1 = emg1_in.read();
    emg2 = emg2_in.read();
    emg3 = emg3_in.read();

    // Output raw EMG input
    scope.set(0, emg1 );

    double emg1_n_hp = bqc_notch_high.step( emg1 ); // Filter notch and highpass
    double emg1_rectify = fabs( emg1_n_hp ); // Filter lowpass (completes envelope)
    double emg1_env = bqc_low.step( emg1_rectify ); // Filter lowpass (completes envelope)

    // Output EMG after filters
    scope.set(1, emg1_env );
    scope.send();

    emg1_cal[i_cal] = emg1_env;
    i_cal++;

}

void calibrationMVC()
{
    
    tickSampleCalibration.attach( &sampleCalibration, Ts );
    led_b = 0;
}



void main()
{
    pc.baud(115200);
    pc.printf("Starting\r\n");

    // Initialize sample ticker
    // tickSample.attach(&sample, Ts);

    // Create BQ chains to reduce computations
    bqc_notch_high.add( &bq_notch ).add( &bq_H1 ).add( &bq_H2 );
    bqc_low.add( &bq_L1 ).add( &bq_L2 );

    led_b = 1; // Turn led off at startup
    led_g = 1;

    // If any filter chain is unstable, red led will light up
    if (checkBQChainStable) {
        led_r = 1; // LED off
    } else {
        led_r = 0; // LED on
    }

    button1.fall( &calibrationMVC );

    while(true) {

        // Show that system is running
        // led_g = !led_g;
        wait(0.5);
    }
}