De hele robot in 1 keer bam

Dependencies:   mbed QEI Servo HIDScope biquadFilter MODSERIAL FastPWM

main.cpp

Committer:
Jellehierck
Date:
2019-10-20
Revision:
3:c0ece64850db
Parent:
2:d3e9788ab1b3
Child:
4:09a01d2db8f7

File content as of revision 3:c0ece64850db:

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

// PC serial connection
// 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

//variablen voor EMG
double emg1;
double emg2;
double emg3;
double notch1;
double notch2;
double notch3;
double highpass1;
double highpass2;
double highpass3;
double lowpass1;
double lowpass2;
double lowpass3;
double rectify1;
double rectify2;
double rectify3;

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

void sample()
{
    emg1 = emg1_in.read();
    emg2 = emg2_in.read();
    emg3 = emg3_in.read();
}

//notch filter toepassen
notch1 = N1.step(emg1);
notch2 = N2.step(emg2);
notch3 = N3.step(emg3);

//high pass filter
high1 = H1.step(notch1);
high2 = H2.step(notch2);
high3 = H3.step(notch3);

//rectify toepassen, oftewel absolute waarde van EMG signaal nemen
absolute1 = fabs(high1);
absolute2 = fabs(high2);
absolute3 = fabs(high3);

//low pass filter
low1 = L1.step(absolute1);
low2 = L2.step(absolute2);
low3 = L3.step(absolute3);