De hele robot in 1 keer bam

Dependencies:   mbed QEI Servo HIDScope biquadFilter MODSERIAL FastPWM

main.cpp

Committer:
Jellehierck
Date:
2019-10-25
Revision:
22:9079c6c0d898
Parent:
21:e4569b47945e
Child:
23:8a0a0b959af1

File content as of revision 22:9079c6c0d898:

//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

/*
------ DEFINE MBED CONNECTIONS ------
*/

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

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

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

// EMG Substates
enum EMG_States { emg_wait, emg_cal_MVC, emg_cal_rest, emg_check_cal, emg_make_scale, emg_operation }; // Define EMG substates
EMG_States emg_curr_state; // Initialize EMG substate variable
bool emg_state_changed;

// Global variables for EMG reading
AnalogIn emg1_in (A1); // Right biceps, x axis
AnalogIn emg2_in (A2); // Left biceps, y axis
AnalogIn emg3_in (A3); // Third muscle, TBD

double emg1;
double emg1_MVC;
double emg1_MVC_stdev;
double emg1_rest;
double emg1_rest_stdev;
vector<double> emg1_cal;

double emg2;
double emg2_MVC;
double emg2_MVC_stdev;
double emg2_rest;
double emg2_rest_stdev;
vector<double> emg2_cal;

double emg3;
double emg3_MVC;
double emg3_MVC_stdev;
double emg3_rest;
double emg3_rest_stdev;
vector<double> emg3_cal;

// Initialize tickers and timeouts
Ticker tickSample;
Ticker tickSampleCalibration;
Timer timerCalibration;
Timeout timeoutCalibrationMVC;
Timeout timeoutCalibrationRest;

/*
------ GLOBAL VARIABLES ------
*/
const double Fs = 500; // Sampling frequency (s)
const double Tcal = 10.0f; // Calibration duration (s)
int trim_cal = 1; // Trim transient behaviour of calibration (s)

// Calculate global variables
const double Ts = 1/Fs; // Sampling time (s)
int trim_cal_i = trim_cal * Fs - 1; // Determine iterator of transient behaviour trim

// Notch biquad filter coefficients (iirnotch Q factor 35 @50Hz) from MATLAB:
BiQuad bq1_notch( 0.995636295063941,  -1.89829218816065,   0.995636295063941,  1, -1.89829218816065,   0.991272590127882); // b01 b11 b21 a01 a11 a21
BiQuad bq2_notch = bq1_notch;
BiQuad bq3_notch = bq1_notch;
BiQuadChain bqc1_notch;
BiQuadChain bqc2_notch;
BiQuadChain bqc3_notch;

// Highpass biquad filter coefficients (butter 4th order @10Hz cutoff) from MATLAB
BiQuad bq1_H1(0.922946103200875, -1.84589220640175,  0.922946103200875,  1,  -1.88920703055163,  0.892769008131025); // b01 b11 b21 a01 a11 a21
BiQuad bq1_H2(1,                 -2,                 1,                  1,  -1.95046575793011,  0.954143234875078); // b02 b12 b22 a02 a12 a22
BiQuad bq2_H1 = bq1_H1;
BiQuad bq2_H2 = bq1_H2;
BiQuad bq3_H1 = bq1_H1;
BiQuad bq3_H2 = bq1_H2;
BiQuadChain bqc1_high;
BiQuadChain bqc2_high;
BiQuadChain bqc3_high;

// Lowpass biquad filter coefficients (butter 4th order @5Hz cutoff) from MATLAB:
BiQuad bq1_L1(5.32116245737504e-08,  1.06423249147501e-07,   5.32116245737504e-08,   1,  -1.94396715039462,  0.944882378004138); // b01 b11 b21 a01 a11 a21
BiQuad bq1_L2(1,                     2,                      1,                      1,  -1.97586467534468,  0.976794920438162); // b02 b12 b22 a02 a12 a22
BiQuad bq2_L1 = bq1_L1;
BiQuad bq2_L2 = bq1_L2;
BiQuad bq3_L1 = bq1_L1;
BiQuad bq3_L2 = bq1_L2;
BiQuadChain bqc1_low;
BiQuadChain bqc2_low;
BiQuadChain bqc3_low;

/*
------ HELPER FUNCTIONS ------
*/

// Return mean of vector
double getMean(const vector<double> &vect)
{
    double sum = 0.0;
    int vect_n = vect.size();

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

// Return standard deviation of vector
double getStdev(const vector<double> &vect, const double vect_mean)
{
    double sum2 = 0.0;
    int vect_n = vect.size();

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

// Check filter stability
bool checkBQChainStable()
{
    bool n_stable = bqc1_notch.stable();
    bool hp_stable =  bqc1_high.stable();
    bool l_stable = bqc1_low.stable();

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

/*
------ TICKER FUNCTIONS ------
*/
/*
// 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 = bqc1_notch_high.step( emg1 );

    // Rectify
    double emg1_rectify = fabs( emg1_n_hp );

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

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

void sampleCalibration()
{
    // Read EMG inputs
    emg1 = emg1_in.read();
    emg2 = emg2_in.read();
    emg3 = emg3_in.read();

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

    double emg1_n = bqc1_notch.step( emg1 );         // Filter notch
    double emg1_hp = bqc1_high.step( emg1_n );       // Filter highpass
    double emg1_rectify = fabs( emg1_hp );           // Rectify
    double emg1_env = bqc1_low.step( emg1_rectify ); // Filter lowpass (completes envelope)

    double emg2_n = bqc2_notch.step( emg2 );         // Filter notch
    double emg2_hp = bqc2_high.step( emg2_n );       // Filter highpass
    double emg2_rectify = fabs( emg2_hp );           // Rectify
    double emg2_env = bqc2_low.step( emg2_rectify ); // Filter lowpass (completes envelope)

    scope.set(0, emg1_n);
    scope.set(1, emg2_n);

    scope.set(2, emg1_env );
    scope.set(3, emg2_env );
    scope.send();

    // IF STATEMENT TOEVOEGEN VOOR CALIBRATIE
    emg1_cal.push_back(emg1_env); // Add values to calibration vector
    emg2_cal.push_back(emg2_env); // Add values to calibration vector
}

/*
------ EMG CALIBRATION FUNCTIONS ------
*/

// Finish up calibration of MVC
void calibrationFinished()
{

    switch( emg_curr_state ) {
        case emg_cal_MVC:
            emg1_MVC = getMean(emg1_cal); // Store MVC globally
            emg1_MVC_stdev = getStdev(emg1_cal, emg1_MVC); // Store MVC stde globally
            vector<double>().swap(emg1_cal); // Empty vector to prevent memory overflow

            emg2_MVC = getMean(emg2_cal); // Store MVC globally
            emg2_MVC_stdev = getStdev(emg2_cal, emg2_MVC); // Store MVC stde globally
            vector<double>().swap(emg2_cal); // Empty vector to prevent memory overflow
            break;
        case emg_cal_rest:
            emg1_MVC = getMean(emg1_cal); // Store MVC globally
            emg1_MVC_stdev = getStdev(emg1_cal, emg1_MVC); // Store MVC stde globally
            vector<double>().swap(emg1_cal); // Empty vector to prevent memory overflow

            emg2_MVC = getMean(emg2_cal); // Store MVC globally
            emg2_MVC_stdev = getStdev(emg2_cal, emg2_MVC); // Store MVC stde globally
            vector<double>().swap(emg2_cal); // Empty vector to prevent memory overflow
            break;
    }

    led_b = 1; // Turn off calibration led
}

// Finish up calibration in rest
void calibrationRestFinished()
{
    tickSampleCalibration.detach(); // Stop calibration ticker to remove interrupt
    emg1_rest = getMean(emg1_cal); // Store rest globally
    emg1_rest_stdev = getStdev(emg1_cal, emg1_rest);// Store rest stdev globally
    emg1_cal.clear(); // Empty vector to prevent memory overflow


    emg2_rest = getMean(emg2_cal); // Store rest globally
    emg2_rest_stdev = getStdev(emg2_cal, emg2_rest);// Store rest stdev globally
    emg2_cal.clear(); // Empty vector to prevent memory overflow
    led_b = 1; // Turn off calibration led
}

// Run calibration of EMG
void do_emg_cal()
{
    if ( emg_state_changed == true ) {
        emg_state_changed == false;
        led_b = 0; // Turn on calibration led
        timerCalibration.reset();
        timerCalibration.start();

        switch( emg_curr_state ) {
            case emg_cal_MVC:
                timeoutCalibrationMVC.attach( &calibrationMVCFinished, Tcal); // Stop MVC calibration after interval
                tickSampleCalibration.attach( &sampleCalibration, Ts ); // Start sample ticker
                break;
            case emg_cal_rest:
                timeoutCalibrationRest.attach( &calibrationRestFinished, Tcal); // Stop rest calibration after interval
                tickSampleCalibration.attach( &sampleCalibration, Ts ); // Start sample ticker
                break;
        }

        // Allemaal dingen doen tot de end conditions true zijn

        if ( timerCalibration.read() >= Tcal ) {
            tickSampleCalibration.detach(); // Stop calibration ticker to remove interrupt

            calibrationFinished();

            emg_curr_state == emg_wait;
            stateChanged == true;

            pc.printf("Calibration step finished");
        }
    }

    /*
    // Run calibration in rest
    void calibrationRest()
    {
        timeoutCalibrationRest.attach( &calibrationRestFinished, Tcal); // Stop rest calibration after interval
        tickSampleCalibration.attach( &sampleCalibration, Ts ); // Start sample ticker
        led_b = 0; // Turn on calibration led
    }
    */

// Determine scale factors for operation mode
    void makeScale() {
        double margin_percentage = 10; // Set up % margin for rest
        double factor1 = 1 / emg1_MVC; // Factor to normalize MVC
        double emg1_th = emg1_rest * factor1 + margin_percentage/100; // Set normalized rest threshold

        pc.printf("Factor: %f   TH: %f\r\n", factor1, emg1_th);
    }

    /*
    ------ EMG SUBSTATE MACHINE ------
    */
    void emg_state_machine() {
        switch(emg_curr_state) {
            case emg_wait:
                //do_emg_wait();
                break;
            case emg_cal_MVC:
                do_emg_cal();
                break;
            case emg_cal_rest:
                do_emg_cal();
                break;
            case emg_check_cal:
                //do_emg_check_cal();
                break;
            case emg_make_scale:
                //do_make_scale();
                break;
            case emg_operation:
                //do_emg_operation();
                break;
        }
    }

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

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

        // Create BQ chains to reduce computations
        bqc1_notch.add( &bq1_notch );
        bqc1_high.add( &bq1_H1 ).add( &bq1_H2 );
        bqc1_low.add( &bq1_L1 ).add( &bq1_L2 );

        bqc2_notch.add( &bq2_notch );
        bqc2_high.add( &bq2_H1 ).add( &bq2_H2 );
        bqc2_low.add( &bq2_L1 ).add( &bq2_L2 );

        bqc3_notch.add( &bq3_notch );
        bqc3_high.add( &bq3_H1 ).add( &bq3_H2 );
        bqc3_low.add( &bq3_L1 ).add( &bq3_L2 );

        led_b = 1; // Turn blue led off at startup
        led_g = 1; // Turn green led off at startup
        led_r = 1; // Turn red led off at startup

        // 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 ); // Run MVC calibration on button press
        button2.fall( &calibrationRest ); // Run rest calibration on button press
        button3.fall( &makeScale ); // Create scale factors and close calibration at button press

        while(true) {

            // Show that system is running
            // led_g = !led_g;
            pc.printf("Vector emg1_cal: %i      vector emg2_cal: %i\r\n", emg1_cal.size(), emg2_cal.size());
            wait(1.0f);
        }
    }