Dit is alleen het EMG gedeelte

Dependencies:   mbed HIDScope biquadFilter MODSERIAL FXOS8700Q

Committer:
Jellehierck
Date:
Mon Oct 21 12:52:47 2019 +0000
Revision:
10:97a79aa10a56
Parent:
8:ea3de43c9e8b
Child:
11:042170a9b93a
Working version, filter redesign

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IsaRobin 0:6972d0e91af1 1 //c++ script for filtering of measured EMG signals
IsaRobin 0:6972d0e91af1 2 #include "mbed.h" //Base library
IsaRobin 0:6972d0e91af1 3 #include "HIDScope.h" // to see if program is working and EMG is filtered properly
Jellehierck 2:d3e9788ab1b3 4 // #include "QEI.h"// is needed for the encoder
Jellehierck 8:ea3de43c9e8b 5 #include "MODSERIAL.h"// in order for connection with the pc
Jellehierck 2:d3e9788ab1b3 6 #include "BiQuad.h"
Jellehierck 2:d3e9788ab1b3 7 // #include "FastPWM.h"
Jellehierck 2:d3e9788ab1b3 8 // #include "Arduino.h" //misschien handig omdat we het EMG arduino board gebruiken (?)
Jellehierck 2:d3e9788ab1b3 9 // #include "EMGFilters.h"
IsaRobin 0:6972d0e91af1 10 #include <vector> // For easy array management
Jellehierck 7:7a088536f1c9 11 #include <numeric> // For manipulating array data
IsaRobin 0:6972d0e91af1 12
Jellehierck 2:d3e9788ab1b3 13 // PC serial connection
Jellehierck 4:09a01d2db8f7 14 HIDScope scope( 2 );
Jellehierck 8:ea3de43c9e8b 15 MODSERIAL pc(USBTX, USBRX);
IsaRobin 0:6972d0e91af1 16
IsaRobin 0:6972d0e91af1 17 //EMG inputs definieren
IsaRobin 0:6972d0e91af1 18 AnalogIn emg1_in (A1); //emg van rechterbicep, voor de x-richting
IsaRobin 0:6972d0e91af1 19 AnalogIn emg2_in (A2); //emg van linkerbicep, voor de y-richting
IsaRobin 0:6972d0e91af1 20 AnalogIn emg3_in (A3); //emg van een derde (nog te bepalen) spier, voor het vernaderen van de richting
IsaRobin 0:6972d0e91af1 21
Jellehierck 4:09a01d2db8f7 22 // LED
Jellehierck 6:5437cc97e1e6 23 DigitalOut led_g(LED_GREEN);
Jellehierck 6:5437cc97e1e6 24 DigitalOut led_r(LED_RED);
Jellehierck 8:ea3de43c9e8b 25 DigitalOut led_b(LED_BLUE);
Jellehierck 8:ea3de43c9e8b 26
Jellehierck 8:ea3de43c9e8b 27 // Buttons
Jellehierck 8:ea3de43c9e8b 28 InterruptIn button1(D11);
Jellehierck 8:ea3de43c9e8b 29 InterruptIn button2(D10);
Jellehierck 4:09a01d2db8f7 30
IsaRobin 0:6972d0e91af1 31 //variablen voor EMG
IsaRobin 0:6972d0e91af1 32 double emg1;
IsaRobin 0:6972d0e91af1 33 double emg2;
IsaRobin 0:6972d0e91af1 34 double emg3;
Jellehierck 10:97a79aa10a56 35 double emg1_mean;
Jellehierck 10:97a79aa10a56 36 double emg1_stdev;
Jellehierck 7:7a088536f1c9 37
Jellehierck 7:7a088536f1c9 38 vector<double> emg1_cal;
Jellehierck 7:7a088536f1c9 39
Jellehierck 7:7a088536f1c9 40 int i_cal;
IsaRobin 0:6972d0e91af1 41
Jellehierck 4:09a01d2db8f7 42 // Initialize tickers
Jellehierck 4:09a01d2db8f7 43 Ticker tickSample;
Jellehierck 7:7a088536f1c9 44 Timeout timeoutCalibrationMVC;
Jellehierck 7:7a088536f1c9 45 Ticker tickSampleCalibration;
Jellehierck 4:09a01d2db8f7 46
Jellehierck 4:09a01d2db8f7 47 // Sample rate
Jellehierck 4:09a01d2db8f7 48 const double Fs = 500; //Hz
Jellehierck 7:7a088536f1c9 49 const double Ts = 1/Fs; //sec
Jellehierck 4:09a01d2db8f7 50
Jellehierck 3:c0ece64850db 51 // Notch filter coefficients (iirnotch Q factor 35 @50Hz) from MATLAB in the following form:
Jellehierck 3:c0ece64850db 52 // b01 b11 b21 a01 a11 a21
Jellehierck 10:97a79aa10a56 53 // BiQuad bq_notch( 0.995636295063941, -1.89829218816065, 0.995636295063941, 1, -1.89829218816065, 0.991272590127882);
Jellehierck 10:97a79aa10a56 54 BiQuad bq_notch ( -1.1978e-16, 0.9561, 0.9780, -1.1978e-16, 0.9780 );
Jellehierck 1:059cca298369 55
Jellehierck 3:c0ece64850db 56 // Highpass filter coefficients (butter 4th order @10Hz cutoff) from MATLAB in the following form:
Jellehierck 3:c0ece64850db 57 // b01 b11 b21 a01 a11 a21
Jellehierck 3:c0ece64850db 58 // b02 b12 b22 a02 a12 a22
Jellehierck 3:c0ece64850db 59 BiQuad bq_H1(0.922946103200875, -1.84589220640175, 0.922946103200875, 1, -1.88920703055163, 0.892769008131025);
Jellehierck 3:c0ece64850db 60 BiQuad bq_H2(1, -2, 1, 1, -1.95046575793011, 0.954143234875078);
Jellehierck 4:09a01d2db8f7 61 BiQuadChain bqc_notch_high; // Used to chain two 2nd other filters into a 4th order filter
IsaRobin 0:6972d0e91af1 62
Jellehierck 3:c0ece64850db 63 // Lowpass filter coefficients (butter 4th order @5Hz cutoff) from MATLAB in the following form:
Jellehierck 3:c0ece64850db 64 // b01 b11 b21 a01 a11 a21
Jellehierck 3:c0ece64850db 65 // b02 b12 b22 a02 a12 a22
Jellehierck 3:c0ece64850db 66 BiQuad bq_L1(5.32116245737504e-08, 1.06423249147501e-07, 5.32116245737504e-08, 1, -1.94396715039462, 0.944882378004138);
Jellehierck 3:c0ece64850db 67 BiQuad bq_L2(1, 2, 1, 1, -1.97586467534468, 0.976794920438162);
Jellehierck 3:c0ece64850db 68 BiQuadChain bqc_low; // Used to chain two 2nd other filters into a 4th order filter
Jellehierck 2:d3e9788ab1b3 69
Jellehierck 8:ea3de43c9e8b 70 double getMean(const vector<double> &vect)
Jellehierck 7:7a088536f1c9 71 {
Jellehierck 8:ea3de43c9e8b 72 double sum = 0.0;
Jellehierck 8:ea3de43c9e8b 73 int vect_n = vect.size();
Jellehierck 8:ea3de43c9e8b 74
Jellehierck 8:ea3de43c9e8b 75 for ( int i = 0; i < vect_n; i++ ) {
Jellehierck 8:ea3de43c9e8b 76 sum += vect[i];
Jellehierck 8:ea3de43c9e8b 77 }
Jellehierck 8:ea3de43c9e8b 78 return sum/vect_n;
Jellehierck 8:ea3de43c9e8b 79 }
Jellehierck 8:ea3de43c9e8b 80
Jellehierck 8:ea3de43c9e8b 81 double getStdev(const vector<double> &vect, const double vect_mean)
Jellehierck 8:ea3de43c9e8b 82 {
Jellehierck 8:ea3de43c9e8b 83 double sum2 = 0.0;
Jellehierck 8:ea3de43c9e8b 84 int vect_n = vect.size();
Jellehierck 8:ea3de43c9e8b 85
Jellehierck 8:ea3de43c9e8b 86 for ( int i = 0; i < vect_n; i++ ) {
Jellehierck 8:ea3de43c9e8b 87 sum2 += pow( vect[i] - vect_mean, 2 );
Jellehierck 8:ea3de43c9e8b 88 }
Jellehierck 8:ea3de43c9e8b 89 double output = sqrt( sum2 / vect_n );
Jellehierck 8:ea3de43c9e8b 90 return output;
Jellehierck 7:7a088536f1c9 91 }
Jellehierck 7:7a088536f1c9 92
Jellehierck 6:5437cc97e1e6 93 // Check if filters are stable
Jellehierck 6:5437cc97e1e6 94 bool checkBQChainStable()
Jellehierck 6:5437cc97e1e6 95 {
Jellehierck 6:5437cc97e1e6 96 bool n_hp_stable = bqc_notch_high.stable();
Jellehierck 6:5437cc97e1e6 97 bool l_stable = bqc_low.stable();
Jellehierck 6:5437cc97e1e6 98
Jellehierck 6:5437cc97e1e6 99 if (n_hp_stable && l_stable) {
Jellehierck 6:5437cc97e1e6 100 return true;
Jellehierck 6:5437cc97e1e6 101 } else {
Jellehierck 6:5437cc97e1e6 102 return false;
Jellehierck 6:5437cc97e1e6 103 }
Jellehierck 6:5437cc97e1e6 104 }
Jellehierck 6:5437cc97e1e6 105
Jellehierck 6:5437cc97e1e6 106
Jellehierck 6:5437cc97e1e6 107 // Read samples, filter samples and output to HIDScope
Jellehierck 2:d3e9788ab1b3 108 void sample()
Jellehierck 2:d3e9788ab1b3 109 {
Jellehierck 4:09a01d2db8f7 110 // Read EMG inputs
Jellehierck 2:d3e9788ab1b3 111 emg1 = emg1_in.read();
Jellehierck 2:d3e9788ab1b3 112 emg2 = emg2_in.read();
Jellehierck 2:d3e9788ab1b3 113 emg3 = emg3_in.read();
Jellehierck 4:09a01d2db8f7 114
Jellehierck 4:09a01d2db8f7 115 // Output raw EMG input
Jellehierck 4:09a01d2db8f7 116 scope.set(0, emg1 );
Jellehierck 6:5437cc97e1e6 117
Jellehierck 5:3d65f89e3755 118 // Filter notch and highpass
Jellehierck 5:3d65f89e3755 119 double emg1_n_hp = bqc_notch_high.step( emg1 );
Jellehierck 6:5437cc97e1e6 120
Jellehierck 5:3d65f89e3755 121 // Rectify
Jellehierck 5:3d65f89e3755 122 double emg1_rectify = fabs( emg1_n_hp );
Jellehierck 6:5437cc97e1e6 123
Jellehierck 5:3d65f89e3755 124 // Filter lowpass (completes envelope)
Jellehierck 5:3d65f89e3755 125 double emg1_env = bqc_low.step( emg1_rectify );
Jellehierck 4:09a01d2db8f7 126
Jellehierck 4:09a01d2db8f7 127 // Output EMG after filters
Jellehierck 5:3d65f89e3755 128 scope.set(1, emg1_env );
Jellehierck 4:09a01d2db8f7 129 scope.send();
Jellehierck 7:7a088536f1c9 130
Jellehierck 7:7a088536f1c9 131
Jellehierck 2:d3e9788ab1b3 132 }
IsaRobin 0:6972d0e91af1 133
Jellehierck 7:7a088536f1c9 134 void sampleCalibration()
Jellehierck 7:7a088536f1c9 135 {
Jellehierck 7:7a088536f1c9 136 // Read EMG inputs
Jellehierck 7:7a088536f1c9 137 emg1 = emg1_in.read();
Jellehierck 7:7a088536f1c9 138 emg2 = emg2_in.read();
Jellehierck 7:7a088536f1c9 139 emg3 = emg3_in.read();
Jellehierck 7:7a088536f1c9 140
Jellehierck 7:7a088536f1c9 141 // Output raw EMG input
Jellehierck 7:7a088536f1c9 142 scope.set(0, emg1 );
Jellehierck 10:97a79aa10a56 143
Jellehierck 10:97a79aa10a56 144 double emg1_n = bqc_notch.step( emg1 ); // Filter notch
Jellehierck 10:97a79aa10a56 145 double emg1_hp = bqc_high.step( emg1_n ); // Filter highpass
Jellehierck 10:97a79aa10a56 146 double emg1_rectify = fabs( emg1_n_hp ); // Rectify
Jellehierck 7:7a088536f1c9 147 double emg1_env = bqc_low.step( emg1_rectify ); // Filter lowpass (completes envelope)
Jellehierck 7:7a088536f1c9 148
Jellehierck 7:7a088536f1c9 149 // Output EMG after filters
Jellehierck 7:7a088536f1c9 150 scope.set(1, emg1_env );
Jellehierck 7:7a088536f1c9 151 scope.send();
Jellehierck 7:7a088536f1c9 152
Jellehierck 7:7a088536f1c9 153 emg1_cal.push_back(emg1_env);
Jellehierck 7:7a088536f1c9 154 }
Jellehierck 7:7a088536f1c9 155
Jellehierck 7:7a088536f1c9 156 void calibrationMVCFinished()
Jellehierck 7:7a088536f1c9 157 {
Jellehierck 7:7a088536f1c9 158 tickSampleCalibration.detach();
Jellehierck 10:97a79aa10a56 159 emg1_mean = getMean(emg1_cal);
Jellehierck 10:97a79aa10a56 160 emg1_stdev = getStdev(emg1_cal, emg1_mean);
Jellehierck 10:97a79aa10a56 161
Jellehierck 10:97a79aa10a56 162 emg1_cal.clear();
Jellehierck 8:ea3de43c9e8b 163
Jellehierck 8:ea3de43c9e8b 164 led_b = 1;
Jellehierck 7:7a088536f1c9 165 }
Jellehierck 7:7a088536f1c9 166
Jellehierck 7:7a088536f1c9 167 void calibrationMVC()
Jellehierck 7:7a088536f1c9 168 {
Jellehierck 10:97a79aa10a56 169 timeoutCalibrationMVC.attach( &calibrationMVCFinished, 10.0f);
Jellehierck 7:7a088536f1c9 170 tickSampleCalibration.attach( &sampleCalibration, Ts );
Jellehierck 8:ea3de43c9e8b 171 led_b = 0;
Jellehierck 7:7a088536f1c9 172 }
Jellehierck 7:7a088536f1c9 173
Jellehierck 7:7a088536f1c9 174
Jellehierck 7:7a088536f1c9 175
Jellehierck 5:3d65f89e3755 176 void main()
Jellehierck 4:09a01d2db8f7 177 {
Jellehierck 8:ea3de43c9e8b 178 pc.baud(115200);
Jellehierck 8:ea3de43c9e8b 179 pc.printf("Starting\r\n");
Jellehierck 6:5437cc97e1e6 180 // Initialize sample ticker
Jellehierck 8:ea3de43c9e8b 181 // tickSample.attach(&sample, Ts);
Jellehierck 6:5437cc97e1e6 182
Jellehierck 6:5437cc97e1e6 183 // Create BQ chains to reduce computations
Jellehierck 10:97a79aa10a56 184 bqc_notch.add( &bq_notch );
Jellehierck 10:97a79aa10a56 185 bqc_high.add( &bq_H1 ).add( &bq_H2 );
Jellehierck 5:3d65f89e3755 186 bqc_low.add( &bq_L1 ).add( &bq_L2 );
Jellehierck 4:09a01d2db8f7 187
Jellehierck 8:ea3de43c9e8b 188 led_b = 1; // Turn led off at startup
Jellehierck 8:ea3de43c9e8b 189 led_g = 1;
Jellehierck 8:ea3de43c9e8b 190
Jellehierck 6:5437cc97e1e6 191 // If any filter chain is unstable, red led will light up
Jellehierck 6:5437cc97e1e6 192 if (checkBQChainStable) {
Jellehierck 6:5437cc97e1e6 193 led_r = 1; // LED off
Jellehierck 6:5437cc97e1e6 194 } else {
Jellehierck 6:5437cc97e1e6 195 led_r = 0; // LED on
Jellehierck 6:5437cc97e1e6 196 }
Jellehierck 6:5437cc97e1e6 197
Jellehierck 8:ea3de43c9e8b 198 button1.fall( &calibrationMVC );
Jellehierck 8:ea3de43c9e8b 199
Jellehierck 4:09a01d2db8f7 200 while(true) {
Jellehierck 7:7a088536f1c9 201
Jellehierck 6:5437cc97e1e6 202 // Show that system is running
Jellehierck 8:ea3de43c9e8b 203 // led_g = !led_g;
Jellehierck 10:97a79aa10a56 204 pc.printf("EMG Mean: %f stdev: %f\r\n", emg1_mean, emg1_stdev);
Jellehierck 4:09a01d2db8f7 205 wait(0.5);
Jellehierck 4:09a01d2db8f7 206 }
Jellehierck 4:09a01d2db8f7 207 }