Dit is alleen het EMG gedeelte

Dependencies:   mbed HIDScope biquadFilter MODSERIAL FXOS8700Q

Committer:
Jellehierck
Date:
Tue Oct 22 08:34:25 2019 +0000
Revision:
14:3b6adb5000f1
Parent:
13:2724d2e747f1
(debug required) Version is not working due to memory overflow bug.

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 13:2724d2e747f1 13 /*
Jellehierck 13:2724d2e747f1 14 ------ DEFINE MBED CONNECTIONS ------
Jellehierck 13:2724d2e747f1 15 */
IsaRobin 0:6972d0e91af1 16
Jellehierck 13:2724d2e747f1 17 // PC serial connection
Jellehierck 14:3b6adb5000f1 18 HIDScope scope( 3 );
Jellehierck 13:2724d2e747f1 19 MODSERIAL pc(USBTX, USBRX);
IsaRobin 0:6972d0e91af1 20
Jellehierck 4:09a01d2db8f7 21 // LED
Jellehierck 6:5437cc97e1e6 22 DigitalOut led_g(LED_GREEN);
Jellehierck 6:5437cc97e1e6 23 DigitalOut led_r(LED_RED);
Jellehierck 8:ea3de43c9e8b 24 DigitalOut led_b(LED_BLUE);
Jellehierck 8:ea3de43c9e8b 25
Jellehierck 8:ea3de43c9e8b 26 // Buttons
Jellehierck 8:ea3de43c9e8b 27 InterruptIn button1(D11);
Jellehierck 8:ea3de43c9e8b 28 InterruptIn button2(D10);
Jellehierck 12:70f0710400c2 29 InterruptIn button3(SW3);
Jellehierck 4:09a01d2db8f7 30
Jellehierck 13:2724d2e747f1 31 // Global variables for EMG reading
Jellehierck 13:2724d2e747f1 32 AnalogIn emg1_in (A1); // Right biceps, x axis
Jellehierck 13:2724d2e747f1 33 AnalogIn emg2_in (A2); // Left biceps, y axis
Jellehierck 13:2724d2e747f1 34 AnalogIn emg3_in (A3); // Third muscle, TBD
Jellehierck 13:2724d2e747f1 35
IsaRobin 0:6972d0e91af1 36 double emg1;
Jellehierck 12:70f0710400c2 37 double emg1_MVC;
Jellehierck 12:70f0710400c2 38 double emg1_MVC_stdev;
Jellehierck 12:70f0710400c2 39 double emg1_rest;
Jellehierck 12:70f0710400c2 40 double emg1_rest_stdev;
Jellehierck 7:7a088536f1c9 41 vector<double> emg1_cal;
Jellehierck 7:7a088536f1c9 42
Jellehierck 14:3b6adb5000f1 43 /*
Jellehierck 13:2724d2e747f1 44 double emg2;
Jellehierck 13:2724d2e747f1 45 double emg2_MVC;
Jellehierck 13:2724d2e747f1 46 double emg2_MVC_stdev;
Jellehierck 13:2724d2e747f1 47 double emg2_rest;
Jellehierck 13:2724d2e747f1 48 double emg2_rest_stdev;
Jellehierck 14:3b6adb5000f1 49 //vector<double> emg2_cal;
IsaRobin 0:6972d0e91af1 50
Jellehierck 13:2724d2e747f1 51 double emg3;
Jellehierck 13:2724d2e747f1 52 double emg3_MVC;
Jellehierck 13:2724d2e747f1 53 double emg3_MVC_stdev;
Jellehierck 13:2724d2e747f1 54 double emg3_rest;
Jellehierck 13:2724d2e747f1 55 double emg3_rest_stdev;
Jellehierck 14:3b6adb5000f1 56 //vector<double> emg3_cal;
Jellehierck 14:3b6adb5000f1 57 */
Jellehierck 13:2724d2e747f1 58
Jellehierck 13:2724d2e747f1 59 // Initialize tickers and timeouts
Jellehierck 4:09a01d2db8f7 60 Ticker tickSample;
Jellehierck 13:2724d2e747f1 61 Ticker tickSampleCalibration;
Jellehierck 7:7a088536f1c9 62 Timeout timeoutCalibrationMVC;
Jellehierck 12:70f0710400c2 63 Timeout timeoutCalibrationRest;
Jellehierck 4:09a01d2db8f7 64
Jellehierck 13:2724d2e747f1 65 /*
Jellehierck 13:2724d2e747f1 66 ------ GLOBAL VARIABLES ------
Jellehierck 13:2724d2e747f1 67 */
Jellehierck 14:3b6adb5000f1 68 const int Fs = 10; // Sampling frequency (s)
Jellehierck 11:042170a9b93a 69 const double Tcal = 10.0f; // Calibration duration (s)
Jellehierck 13:2724d2e747f1 70 int trim_cal = 1; // Trim transient behaviour of calibration (s)
Jellehierck 4:09a01d2db8f7 71
Jellehierck 13:2724d2e747f1 72 // Calculate global variables
Jellehierck 13:2724d2e747f1 73 const double Ts = 1/Fs; // Sampling time (s)
Jellehierck 13:2724d2e747f1 74 int trim_cal_i = trim_cal * Fs - 1; // Determine iterator of transient behaviour trim
Jellehierck 13:2724d2e747f1 75
Jellehierck 13:2724d2e747f1 76 // Notch biquad filter coefficients (iirnotch Q factor 35 @50Hz) from MATLAB:
Jellehierck 13:2724d2e747f1 77 BiQuad bq_notch( 0.995636295063941, -1.89829218816065, 0.995636295063941, 1, -1.89829218816065, 0.991272590127882); // b01 b11 b21 a01 a11 a21
Jellehierck 11:042170a9b93a 78 BiQuadChain bqc_notch;
Jellehierck 1:059cca298369 79
Jellehierck 13:2724d2e747f1 80 // Highpass biquad filter coefficients (butter 4th order @10Hz cutoff) from MATLAB
Jellehierck 13:2724d2e747f1 81 BiQuad bq_H1(0.922946103200875, -1.84589220640175, 0.922946103200875, 1, -1.88920703055163, 0.892769008131025); // b01 b11 b21 a01 a11 a21
Jellehierck 13:2724d2e747f1 82 BiQuad bq_H2(1, -2, 1, 1, -1.95046575793011, 0.954143234875078); // b02 b12 b22 a02 a12 a22
Jellehierck 13:2724d2e747f1 83 BiQuadChain bqc_high;
IsaRobin 0:6972d0e91af1 84
Jellehierck 13:2724d2e747f1 85 // Lowpass biquad filter coefficients (butter 4th order @5Hz cutoff) from MATLAB:
Jellehierck 13:2724d2e747f1 86 BiQuad bq_L1(5.32116245737504e-08, 1.06423249147501e-07, 5.32116245737504e-08, 1, -1.94396715039462, 0.944882378004138); // b01 b11 b21 a01 a11 a21
Jellehierck 13:2724d2e747f1 87 BiQuad bq_L2(1, 2, 1, 1, -1.97586467534468, 0.976794920438162); // b02 b12 b22 a02 a12 a22
Jellehierck 13:2724d2e747f1 88 BiQuadChain bqc_low;
Jellehierck 2:d3e9788ab1b3 89
Jellehierck 13:2724d2e747f1 90 /*
Jellehierck 13:2724d2e747f1 91 ------ HELPER FUNCTIONS ------
Jellehierck 13:2724d2e747f1 92 */
Jellehierck 13:2724d2e747f1 93
Jellehierck 13:2724d2e747f1 94 // Return mean of vector
Jellehierck 8:ea3de43c9e8b 95 double getMean(const vector<double> &vect)
Jellehierck 7:7a088536f1c9 96 {
Jellehierck 8:ea3de43c9e8b 97 double sum = 0.0;
Jellehierck 8:ea3de43c9e8b 98 int vect_n = vect.size();
Jellehierck 8:ea3de43c9e8b 99
Jellehierck 8:ea3de43c9e8b 100 for ( int i = 0; i < vect_n; i++ ) {
Jellehierck 8:ea3de43c9e8b 101 sum += vect[i];
Jellehierck 8:ea3de43c9e8b 102 }
Jellehierck 8:ea3de43c9e8b 103 return sum/vect_n;
Jellehierck 8:ea3de43c9e8b 104 }
Jellehierck 8:ea3de43c9e8b 105
Jellehierck 13:2724d2e747f1 106 // Return standard deviation of vector
Jellehierck 8:ea3de43c9e8b 107 double getStdev(const vector<double> &vect, const double vect_mean)
Jellehierck 8:ea3de43c9e8b 108 {
Jellehierck 8:ea3de43c9e8b 109 double sum2 = 0.0;
Jellehierck 8:ea3de43c9e8b 110 int vect_n = vect.size();
Jellehierck 8:ea3de43c9e8b 111
Jellehierck 8:ea3de43c9e8b 112 for ( int i = 0; i < vect_n; i++ ) {
Jellehierck 8:ea3de43c9e8b 113 sum2 += pow( vect[i] - vect_mean, 2 );
Jellehierck 8:ea3de43c9e8b 114 }
Jellehierck 8:ea3de43c9e8b 115 double output = sqrt( sum2 / vect_n );
Jellehierck 8:ea3de43c9e8b 116 return output;
Jellehierck 7:7a088536f1c9 117 }
Jellehierck 7:7a088536f1c9 118
Jellehierck 13:2724d2e747f1 119 // Check filter stability
Jellehierck 6:5437cc97e1e6 120 bool checkBQChainStable()
Jellehierck 6:5437cc97e1e6 121 {
Jellehierck 11:042170a9b93a 122 bool n_stable = bqc_notch.stable();
Jellehierck 11:042170a9b93a 123 bool hp_stable = bqc_high.stable();
Jellehierck 6:5437cc97e1e6 124 bool l_stable = bqc_low.stable();
Jellehierck 6:5437cc97e1e6 125
Jellehierck 11:042170a9b93a 126 if (n_stable && hp_stable && l_stable) {
Jellehierck 6:5437cc97e1e6 127 return true;
Jellehierck 6:5437cc97e1e6 128 } else {
Jellehierck 6:5437cc97e1e6 129 return false;
Jellehierck 6:5437cc97e1e6 130 }
Jellehierck 6:5437cc97e1e6 131 }
Jellehierck 6:5437cc97e1e6 132
Jellehierck 13:2724d2e747f1 133 /*
Jellehierck 13:2724d2e747f1 134 ------ TICKER FUNCTIONS ------
Jellehierck 13:2724d2e747f1 135 */
Jellehierck 11:042170a9b93a 136 /*
Jellehierck 6:5437cc97e1e6 137 // Read samples, filter samples and output to HIDScope
Jellehierck 2:d3e9788ab1b3 138 void sample()
Jellehierck 2:d3e9788ab1b3 139 {
Jellehierck 4:09a01d2db8f7 140 // Read EMG inputs
Jellehierck 2:d3e9788ab1b3 141 emg1 = emg1_in.read();
Jellehierck 2:d3e9788ab1b3 142 emg2 = emg2_in.read();
Jellehierck 2:d3e9788ab1b3 143 emg3 = emg3_in.read();
Jellehierck 4:09a01d2db8f7 144
Jellehierck 4:09a01d2db8f7 145 // Output raw EMG input
Jellehierck 4:09a01d2db8f7 146 scope.set(0, emg1 );
Jellehierck 6:5437cc97e1e6 147
Jellehierck 5:3d65f89e3755 148 // Filter notch and highpass
Jellehierck 5:3d65f89e3755 149 double emg1_n_hp = bqc_notch_high.step( emg1 );
Jellehierck 6:5437cc97e1e6 150
Jellehierck 5:3d65f89e3755 151 // Rectify
Jellehierck 5:3d65f89e3755 152 double emg1_rectify = fabs( emg1_n_hp );
Jellehierck 6:5437cc97e1e6 153
Jellehierck 5:3d65f89e3755 154 // Filter lowpass (completes envelope)
Jellehierck 5:3d65f89e3755 155 double emg1_env = bqc_low.step( emg1_rectify );
Jellehierck 4:09a01d2db8f7 156
Jellehierck 4:09a01d2db8f7 157 // Output EMG after filters
Jellehierck 5:3d65f89e3755 158 scope.set(1, emg1_env );
Jellehierck 4:09a01d2db8f7 159 scope.send();
Jellehierck 2:d3e9788ab1b3 160 }
Jellehierck 11:042170a9b93a 161 */
IsaRobin 0:6972d0e91af1 162
Jellehierck 7:7a088536f1c9 163 void sampleCalibration()
Jellehierck 7:7a088536f1c9 164 {
Jellehierck 7:7a088536f1c9 165 // Read EMG inputs
Jellehierck 7:7a088536f1c9 166 emg1 = emg1_in.read();
Jellehierck 14:3b6adb5000f1 167 // emg2 = emg2_in.read();
Jellehierck 14:3b6adb5000f1 168 // emg3 = emg3_in.read();
Jellehierck 7:7a088536f1c9 169
Jellehierck 7:7a088536f1c9 170 // Output raw EMG input
Jellehierck 14:3b6adb5000f1 171 // scope.set(0, emg1 );
Jellehierck 10:97a79aa10a56 172
Jellehierck 10:97a79aa10a56 173 double emg1_n = bqc_notch.step( emg1 ); // Filter notch
Jellehierck 14:3b6adb5000f1 174 //scope.set(1, emg1_n);
Jellehierck 10:97a79aa10a56 175 double emg1_hp = bqc_high.step( emg1_n ); // Filter highpass
Jellehierck 14:3b6adb5000f1 176 // scope.set(1, emg1_hp);
Jellehierck 11:042170a9b93a 177 double emg1_rectify = fabs( emg1_hp ); // Rectify
Jellehierck 14:3b6adb5000f1 178 // scope.set(2, emg1_rectify);
Jellehierck 7:7a088536f1c9 179 double emg1_env = bqc_low.step( emg1_rectify ); // Filter lowpass (completes envelope)
Jellehierck 7:7a088536f1c9 180
Jellehierck 7:7a088536f1c9 181 // Output EMG after filters
Jellehierck 14:3b6adb5000f1 182 //scope.set(2, emg1_env );
Jellehierck 7:7a088536f1c9 183 scope.send();
Jellehierck 7:7a088536f1c9 184
Jellehierck 7:7a088536f1c9 185 emg1_cal.push_back(emg1_env);
Jellehierck 7:7a088536f1c9 186 }
Jellehierck 7:7a088536f1c9 187
Jellehierck 13:2724d2e747f1 188 /*
Jellehierck 13:2724d2e747f1 189 ------ EMG CALIBRATION FUNCTIONS ------
Jellehierck 13:2724d2e747f1 190 */
Jellehierck 13:2724d2e747f1 191
Jellehierck 13:2724d2e747f1 192 // Finish up calibration of MVC
Jellehierck 7:7a088536f1c9 193 void calibrationMVCFinished()
Jellehierck 7:7a088536f1c9 194 {
Jellehierck 13:2724d2e747f1 195 tickSampleCalibration.detach(); // Stop calibration ticker to remove interrupt
Jellehierck 13:2724d2e747f1 196 emg1_MVC = getMean(emg1_cal); // Store MVC globally
Jellehierck 13:2724d2e747f1 197 emg1_MVC_stdev = getStdev(emg1_cal, emg1_MVC); // Store MVC stde globally
Jellehierck 13:2724d2e747f1 198 emg1_cal.clear(); // Empty vector to prevent memory overflow
Jellehierck 13:2724d2e747f1 199 led_b = 1; // Turn off calibration led
Jellehierck 7:7a088536f1c9 200 }
Jellehierck 7:7a088536f1c9 201
Jellehierck 13:2724d2e747f1 202 // Run calibration of MVC
Jellehierck 7:7a088536f1c9 203 void calibrationMVC()
Jellehierck 7:7a088536f1c9 204 {
Jellehierck 13:2724d2e747f1 205 timeoutCalibrationMVC.attach( &calibrationMVCFinished, Tcal); // Stop MVC calibration after interval
Jellehierck 13:2724d2e747f1 206 tickSampleCalibration.attach( &sampleCalibration, Ts ); // Start sample ticker
Jellehierck 13:2724d2e747f1 207 led_b = 0; // Turn on calibration led
Jellehierck 7:7a088536f1c9 208 }
Jellehierck 7:7a088536f1c9 209
Jellehierck 13:2724d2e747f1 210 // Finish up calibration in rest
Jellehierck 12:70f0710400c2 211 void calibrationRestFinished()
Jellehierck 12:70f0710400c2 212 {
Jellehierck 13:2724d2e747f1 213 tickSampleCalibration.detach(); // Stop calibration ticker to remove interrupt
Jellehierck 13:2724d2e747f1 214 emg1_rest = getMean(emg1_cal); // Store rest globally
Jellehierck 13:2724d2e747f1 215 emg1_rest_stdev = getStdev(emg1_cal, emg1_rest);// Store rest stdev globally
Jellehierck 13:2724d2e747f1 216 emg1_cal.clear(); // Empty vector to prevent memory overflow
Jellehierck 13:2724d2e747f1 217 led_b = 1; // Turn off calibration led
Jellehierck 12:70f0710400c2 218 }
Jellehierck 12:70f0710400c2 219
Jellehierck 12:70f0710400c2 220 void calibrationRest()
Jellehierck 12:70f0710400c2 221 {
Jellehierck 13:2724d2e747f1 222 timeoutCalibrationRest.attach( &calibrationRestFinished, Tcal); // Stop rest calibration after interval
Jellehierck 13:2724d2e747f1 223 tickSampleCalibration.attach( &sampleCalibration, Ts ); // Start sample ticker
Jellehierck 13:2724d2e747f1 224 led_b = 0; // Turn on calibration led
Jellehierck 12:70f0710400c2 225 }
Jellehierck 12:70f0710400c2 226
Jellehierck 13:2724d2e747f1 227 // Determine scale factors for operation mode
Jellehierck 12:70f0710400c2 228 void makeScale()
Jellehierck 12:70f0710400c2 229 {
Jellehierck 13:2724d2e747f1 230 double margin_percentage = 10; // Set up % margin for rest
Jellehierck 13:2724d2e747f1 231 double factor1 = 1 / emg1_MVC; // Factor to normalize MVC
Jellehierck 13:2724d2e747f1 232 double emg1_th = emg1_rest * factor1 + margin_percentage/100; // Set normalized rest threshold
Jellehierck 12:70f0710400c2 233
Jellehierck 12:70f0710400c2 234 pc.printf("Factor: %f TH: %f\r\n", factor1, emg1_th);
Jellehierck 12:70f0710400c2 235 }
Jellehierck 7:7a088536f1c9 236
Jellehierck 5:3d65f89e3755 237 void main()
Jellehierck 4:09a01d2db8f7 238 {
Jellehierck 13:2724d2e747f1 239 pc.baud(115200); // MODSERIAL rate
Jellehierck 8:ea3de43c9e8b 240 pc.printf("Starting\r\n");
Jellehierck 13:2724d2e747f1 241
Jellehierck 13:2724d2e747f1 242 // tickSample.attach(&sample, Ts); // Initialize sample ticker
Jellehierck 6:5437cc97e1e6 243
Jellehierck 6:5437cc97e1e6 244 // Create BQ chains to reduce computations
Jellehierck 10:97a79aa10a56 245 bqc_notch.add( &bq_notch );
Jellehierck 10:97a79aa10a56 246 bqc_high.add( &bq_H1 ).add( &bq_H2 );
Jellehierck 5:3d65f89e3755 247 bqc_low.add( &bq_L1 ).add( &bq_L2 );
Jellehierck 4:09a01d2db8f7 248
Jellehierck 13:2724d2e747f1 249 led_b = 1; // Turn blue led off at startup
Jellehierck 13:2724d2e747f1 250 led_g = 1; // Turn green led off at startup
Jellehierck 13:2724d2e747f1 251 led_r = 1; // Turn red led off at startup
Jellehierck 8:ea3de43c9e8b 252
Jellehierck 6:5437cc97e1e6 253 // If any filter chain is unstable, red led will light up
Jellehierck 6:5437cc97e1e6 254 if (checkBQChainStable) {
Jellehierck 6:5437cc97e1e6 255 led_r = 1; // LED off
Jellehierck 6:5437cc97e1e6 256 } else {
Jellehierck 6:5437cc97e1e6 257 led_r = 0; // LED on
Jellehierck 6:5437cc97e1e6 258 }
Jellehierck 6:5437cc97e1e6 259
Jellehierck 13:2724d2e747f1 260 button1.fall( &calibrationMVC ); // Run MVC calibration on button press
Jellehierck 13:2724d2e747f1 261 button2.fall( &calibrationRest ); // Run rest calibration on button press
Jellehierck 13:2724d2e747f1 262 button3.fall( &makeScale ); // Create scale factors and close calibration at button press
Jellehierck 8:ea3de43c9e8b 263
Jellehierck 4:09a01d2db8f7 264 while(true) {
Jellehierck 7:7a088536f1c9 265
Jellehierck 6:5437cc97e1e6 266 // Show that system is running
Jellehierck 8:ea3de43c9e8b 267 // led_g = !led_g;
Jellehierck 12:70f0710400c2 268 pc.printf("EMG MVC: %f stdev: %f\r\nEMG Rest: %f stdev: %f\r\n", emg1_MVC, emg1_MVC_stdev, emg1_rest, emg1_rest_stdev);
Jellehierck 4:09a01d2db8f7 269 wait(0.5);
Jellehierck 4:09a01d2db8f7 270 }
Jellehierck 4:09a01d2db8f7 271 }