Dit is alleen het EMG gedeelte

Dependencies:   mbed HIDScope biquadFilter MODSERIAL FXOS8700Q

Committer:
Jellehierck
Date:
Mon Oct 21 14:07:30 2019 +0000
Revision:
11:042170a9b93a
Parent:
10:97a79aa10a56
Child:
12:70f0710400c2
Calibration is working;

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 11:042170a9b93a 14 HIDScope scope( 3 );
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
IsaRobin 0:6972d0e91af1 40
Jellehierck 4:09a01d2db8f7 41 // Initialize tickers
Jellehierck 4:09a01d2db8f7 42 Ticker tickSample;
Jellehierck 7:7a088536f1c9 43 Timeout timeoutCalibrationMVC;
Jellehierck 7:7a088536f1c9 44 Ticker tickSampleCalibration;
Jellehierck 4:09a01d2db8f7 45
Jellehierck 4:09a01d2db8f7 46 // Sample rate
Jellehierck 11:042170a9b93a 47 const double Fs = 500; // Sampling frequency (s)
Jellehierck 11:042170a9b93a 48 const double Ts = 1/Fs; // Sampling time (s)
Jellehierck 11:042170a9b93a 49
Jellehierck 11:042170a9b93a 50 const double Tcal = 10.0f; // Calibration duration (s)
Jellehierck 11:042170a9b93a 51
Jellehierck 11:042170a9b93a 52 int trim_cal = 1; // Trim the beginning of the calibration vector to reduce transient behaviour by X seconds
Jellehierck 11:042170a9b93a 53 int trim_cal_i = trim_cal * Fs - 1;
Jellehierck 4:09a01d2db8f7 54
Jellehierck 3:c0ece64850db 55 // Notch filter coefficients (iirnotch Q factor 35 @50Hz) from MATLAB in the following form:
Jellehierck 3:c0ece64850db 56 // b01 b11 b21 a01 a11 a21
Jellehierck 11:042170a9b93a 57 BiQuad bq_notch( 0.995636295063941, -1.89829218816065, 0.995636295063941, 1, -1.89829218816065, 0.991272590127882);
Jellehierck 11:042170a9b93a 58 BiQuadChain bqc_notch;
Jellehierck 1:059cca298369 59
Jellehierck 3:c0ece64850db 60 // Highpass filter coefficients (butter 4th order @10Hz cutoff) from MATLAB in the following form:
Jellehierck 3:c0ece64850db 61 // b01 b11 b21 a01 a11 a21
Jellehierck 3:c0ece64850db 62 // b02 b12 b22 a02 a12 a22
Jellehierck 3:c0ece64850db 63 BiQuad bq_H1(0.922946103200875, -1.84589220640175, 0.922946103200875, 1, -1.88920703055163, 0.892769008131025);
Jellehierck 3:c0ece64850db 64 BiQuad bq_H2(1, -2, 1, 1, -1.95046575793011, 0.954143234875078);
Jellehierck 11:042170a9b93a 65 BiQuadChain bqc_high; // Used to chain two 2nd other filters into a 4th order filter
IsaRobin 0:6972d0e91af1 66
Jellehierck 3:c0ece64850db 67 // Lowpass filter coefficients (butter 4th order @5Hz cutoff) from MATLAB in the following form:
Jellehierck 3:c0ece64850db 68 // b01 b11 b21 a01 a11 a21
Jellehierck 3:c0ece64850db 69 // b02 b12 b22 a02 a12 a22
Jellehierck 3:c0ece64850db 70 BiQuad bq_L1(5.32116245737504e-08, 1.06423249147501e-07, 5.32116245737504e-08, 1, -1.94396715039462, 0.944882378004138);
Jellehierck 3:c0ece64850db 71 BiQuad bq_L2(1, 2, 1, 1, -1.97586467534468, 0.976794920438162);
Jellehierck 3:c0ece64850db 72 BiQuadChain bqc_low; // Used to chain two 2nd other filters into a 4th order filter
Jellehierck 2:d3e9788ab1b3 73
Jellehierck 8:ea3de43c9e8b 74 double getMean(const vector<double> &vect)
Jellehierck 7:7a088536f1c9 75 {
Jellehierck 8:ea3de43c9e8b 76 double sum = 0.0;
Jellehierck 8:ea3de43c9e8b 77 int vect_n = vect.size();
Jellehierck 8:ea3de43c9e8b 78
Jellehierck 8:ea3de43c9e8b 79 for ( int i = 0; i < vect_n; i++ ) {
Jellehierck 8:ea3de43c9e8b 80 sum += vect[i];
Jellehierck 8:ea3de43c9e8b 81 }
Jellehierck 8:ea3de43c9e8b 82 return sum/vect_n;
Jellehierck 8:ea3de43c9e8b 83 }
Jellehierck 8:ea3de43c9e8b 84
Jellehierck 8:ea3de43c9e8b 85 double getStdev(const vector<double> &vect, const double vect_mean)
Jellehierck 8:ea3de43c9e8b 86 {
Jellehierck 8:ea3de43c9e8b 87 double sum2 = 0.0;
Jellehierck 8:ea3de43c9e8b 88 int vect_n = vect.size();
Jellehierck 8:ea3de43c9e8b 89
Jellehierck 8:ea3de43c9e8b 90 for ( int i = 0; i < vect_n; i++ ) {
Jellehierck 8:ea3de43c9e8b 91 sum2 += pow( vect[i] - vect_mean, 2 );
Jellehierck 8:ea3de43c9e8b 92 }
Jellehierck 8:ea3de43c9e8b 93 double output = sqrt( sum2 / vect_n );
Jellehierck 8:ea3de43c9e8b 94 return output;
Jellehierck 7:7a088536f1c9 95 }
Jellehierck 7:7a088536f1c9 96
Jellehierck 6:5437cc97e1e6 97 // Check if filters are stable
Jellehierck 6:5437cc97e1e6 98 bool checkBQChainStable()
Jellehierck 6:5437cc97e1e6 99 {
Jellehierck 11:042170a9b93a 100 bool n_stable = bqc_notch.stable();
Jellehierck 11:042170a9b93a 101 bool hp_stable = bqc_high.stable();
Jellehierck 6:5437cc97e1e6 102 bool l_stable = bqc_low.stable();
Jellehierck 6:5437cc97e1e6 103
Jellehierck 11:042170a9b93a 104 if (n_stable && hp_stable && l_stable) {
Jellehierck 6:5437cc97e1e6 105 return true;
Jellehierck 6:5437cc97e1e6 106 } else {
Jellehierck 6:5437cc97e1e6 107 return false;
Jellehierck 6:5437cc97e1e6 108 }
Jellehierck 6:5437cc97e1e6 109 }
Jellehierck 6:5437cc97e1e6 110
Jellehierck 6:5437cc97e1e6 111
Jellehierck 11:042170a9b93a 112 /*
Jellehierck 6:5437cc97e1e6 113 // Read samples, filter samples and output to HIDScope
Jellehierck 2:d3e9788ab1b3 114 void sample()
Jellehierck 2:d3e9788ab1b3 115 {
Jellehierck 4:09a01d2db8f7 116 // Read EMG inputs
Jellehierck 2:d3e9788ab1b3 117 emg1 = emg1_in.read();
Jellehierck 2:d3e9788ab1b3 118 emg2 = emg2_in.read();
Jellehierck 2:d3e9788ab1b3 119 emg3 = emg3_in.read();
Jellehierck 4:09a01d2db8f7 120
Jellehierck 4:09a01d2db8f7 121 // Output raw EMG input
Jellehierck 4:09a01d2db8f7 122 scope.set(0, emg1 );
Jellehierck 6:5437cc97e1e6 123
Jellehierck 5:3d65f89e3755 124 // Filter notch and highpass
Jellehierck 5:3d65f89e3755 125 double emg1_n_hp = bqc_notch_high.step( emg1 );
Jellehierck 6:5437cc97e1e6 126
Jellehierck 5:3d65f89e3755 127 // Rectify
Jellehierck 5:3d65f89e3755 128 double emg1_rectify = fabs( emg1_n_hp );
Jellehierck 6:5437cc97e1e6 129
Jellehierck 5:3d65f89e3755 130 // Filter lowpass (completes envelope)
Jellehierck 5:3d65f89e3755 131 double emg1_env = bqc_low.step( emg1_rectify );
Jellehierck 4:09a01d2db8f7 132
Jellehierck 4:09a01d2db8f7 133 // Output EMG after filters
Jellehierck 5:3d65f89e3755 134 scope.set(1, emg1_env );
Jellehierck 4:09a01d2db8f7 135 scope.send();
Jellehierck 2:d3e9788ab1b3 136 }
Jellehierck 11:042170a9b93a 137 */
IsaRobin 0:6972d0e91af1 138
Jellehierck 7:7a088536f1c9 139 void sampleCalibration()
Jellehierck 7:7a088536f1c9 140 {
Jellehierck 7:7a088536f1c9 141 // Read EMG inputs
Jellehierck 7:7a088536f1c9 142 emg1 = emg1_in.read();
Jellehierck 7:7a088536f1c9 143 emg2 = emg2_in.read();
Jellehierck 7:7a088536f1c9 144 emg3 = emg3_in.read();
Jellehierck 7:7a088536f1c9 145
Jellehierck 7:7a088536f1c9 146 // Output raw EMG input
Jellehierck 7:7a088536f1c9 147 scope.set(0, emg1 );
Jellehierck 10:97a79aa10a56 148
Jellehierck 10:97a79aa10a56 149 double emg1_n = bqc_notch.step( emg1 ); // Filter notch
Jellehierck 11:042170a9b93a 150 scope.set(1, emg1_n);
Jellehierck 10:97a79aa10a56 151 double emg1_hp = bqc_high.step( emg1_n ); // Filter highpass
Jellehierck 11:042170a9b93a 152 double emg1_rectify = fabs( emg1_hp ); // Rectify
Jellehierck 7:7a088536f1c9 153 double emg1_env = bqc_low.step( emg1_rectify ); // Filter lowpass (completes envelope)
Jellehierck 7:7a088536f1c9 154
Jellehierck 7:7a088536f1c9 155 // Output EMG after filters
Jellehierck 11:042170a9b93a 156 scope.set(2, emg1_env );
Jellehierck 7:7a088536f1c9 157 scope.send();
Jellehierck 7:7a088536f1c9 158
Jellehierck 7:7a088536f1c9 159 emg1_cal.push_back(emg1_env);
Jellehierck 7:7a088536f1c9 160 }
Jellehierck 7:7a088536f1c9 161
Jellehierck 7:7a088536f1c9 162 void calibrationMVCFinished()
Jellehierck 7:7a088536f1c9 163 {
Jellehierck 7:7a088536f1c9 164 tickSampleCalibration.detach();
Jellehierck 11:042170a9b93a 165
Jellehierck 10:97a79aa10a56 166 emg1_mean = getMean(emg1_cal);
Jellehierck 10:97a79aa10a56 167 emg1_stdev = getStdev(emg1_cal, emg1_mean);
Jellehierck 10:97a79aa10a56 168
Jellehierck 10:97a79aa10a56 169 emg1_cal.clear();
Jellehierck 8:ea3de43c9e8b 170
Jellehierck 8:ea3de43c9e8b 171 led_b = 1;
Jellehierck 7:7a088536f1c9 172 }
Jellehierck 7:7a088536f1c9 173
Jellehierck 7:7a088536f1c9 174 void calibrationMVC()
Jellehierck 7:7a088536f1c9 175 {
Jellehierck 11:042170a9b93a 176 timeoutCalibrationMVC.attach( &calibrationMVCFinished, Tcal);
Jellehierck 7:7a088536f1c9 177 tickSampleCalibration.attach( &sampleCalibration, Ts );
Jellehierck 8:ea3de43c9e8b 178 led_b = 0;
Jellehierck 7:7a088536f1c9 179 }
Jellehierck 7:7a088536f1c9 180
Jellehierck 7:7a088536f1c9 181
Jellehierck 7:7a088536f1c9 182
Jellehierck 5:3d65f89e3755 183 void main()
Jellehierck 4:09a01d2db8f7 184 {
Jellehierck 8:ea3de43c9e8b 185 pc.baud(115200);
Jellehierck 8:ea3de43c9e8b 186 pc.printf("Starting\r\n");
Jellehierck 6:5437cc97e1e6 187 // Initialize sample ticker
Jellehierck 8:ea3de43c9e8b 188 // tickSample.attach(&sample, Ts);
Jellehierck 6:5437cc97e1e6 189
Jellehierck 6:5437cc97e1e6 190 // Create BQ chains to reduce computations
Jellehierck 10:97a79aa10a56 191 bqc_notch.add( &bq_notch );
Jellehierck 10:97a79aa10a56 192 bqc_high.add( &bq_H1 ).add( &bq_H2 );
Jellehierck 5:3d65f89e3755 193 bqc_low.add( &bq_L1 ).add( &bq_L2 );
Jellehierck 4:09a01d2db8f7 194
Jellehierck 8:ea3de43c9e8b 195 led_b = 1; // Turn led off at startup
Jellehierck 8:ea3de43c9e8b 196 led_g = 1;
Jellehierck 8:ea3de43c9e8b 197
Jellehierck 6:5437cc97e1e6 198 // If any filter chain is unstable, red led will light up
Jellehierck 6:5437cc97e1e6 199 if (checkBQChainStable) {
Jellehierck 6:5437cc97e1e6 200 led_r = 1; // LED off
Jellehierck 6:5437cc97e1e6 201 } else {
Jellehierck 6:5437cc97e1e6 202 led_r = 0; // LED on
Jellehierck 6:5437cc97e1e6 203 }
Jellehierck 6:5437cc97e1e6 204
Jellehierck 8:ea3de43c9e8b 205 button1.fall( &calibrationMVC );
Jellehierck 8:ea3de43c9e8b 206
Jellehierck 4:09a01d2db8f7 207 while(true) {
Jellehierck 7:7a088536f1c9 208
Jellehierck 6:5437cc97e1e6 209 // Show that system is running
Jellehierck 8:ea3de43c9e8b 210 // led_g = !led_g;
Jellehierck 10:97a79aa10a56 211 pc.printf("EMG Mean: %f stdev: %f\r\n", emg1_mean, emg1_stdev);
Jellehierck 4:09a01d2db8f7 212 wait(0.5);
Jellehierck 4:09a01d2db8f7 213 }
Jellehierck 4:09a01d2db8f7 214 }