![](/media/cache/group/9e3cc099b3b04bca937a1cca1da81b19.jpg.50x50_q85.jpg)
De hele robot in 1 keer bam
Dependencies: mbed QEI Servo HIDScope biquadFilter MODSERIAL FastPWM
main.cpp@19:94dc52f8a59e, 2019-10-25 (annotated)
- Committer:
- Jellehierck
- Date:
- Fri Oct 25 08:29:50 2019 +0000
- Revision:
- 19:94dc52f8a59e
- Parent:
- 18:9f24792bb39a
- Child:
- 20:0e9218673aa8
Fixed filters for multiple channels
Who changed what in which revision?
User | Revision | Line number | New 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 | 15:421d3d9c563b | 13 | /* |
Jellehierck | 15:421d3d9c563b | 14 | ------ DEFINE MBED CONNECTIONS ------ |
Jellehierck | 15:421d3d9c563b | 15 | */ |
IsaRobin | 0:6972d0e91af1 | 16 | |
Jellehierck | 15:421d3d9c563b | 17 | // PC serial connection |
Jellehierck | 18:9f24792bb39a | 18 | HIDScope scope( 4 ); |
Jellehierck | 15:421d3d9c563b | 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 | 16:7acbcc4aa35c | 31 | // EMG Substates |
Jellehierck | 18:9f24792bb39a | 32 | enum EMG_States { emg_wait, emg_cal_MVC, emg_cal_rest, emg_scale, emg_operation }; // Define EMG substates |
Jellehierck | 16:7acbcc4aa35c | 33 | EMG_States emg_curr_state; // Initialize EMG substate variable |
Jellehierck | 16:7acbcc4aa35c | 34 | |
Jellehierck | 15:421d3d9c563b | 35 | // Global variables for EMG reading |
Jellehierck | 15:421d3d9c563b | 36 | AnalogIn emg1_in (A1); // Right biceps, x axis |
Jellehierck | 15:421d3d9c563b | 37 | AnalogIn emg2_in (A2); // Left biceps, y axis |
Jellehierck | 15:421d3d9c563b | 38 | AnalogIn emg3_in (A3); // Third muscle, TBD |
Jellehierck | 15:421d3d9c563b | 39 | |
IsaRobin | 0:6972d0e91af1 | 40 | double emg1; |
Jellehierck | 12:70f0710400c2 | 41 | double emg1_MVC; |
Jellehierck | 12:70f0710400c2 | 42 | double emg1_MVC_stdev; |
Jellehierck | 12:70f0710400c2 | 43 | double emg1_rest; |
Jellehierck | 12:70f0710400c2 | 44 | double emg1_rest_stdev; |
Jellehierck | 7:7a088536f1c9 | 45 | vector<double> emg1_cal; |
Jellehierck | 7:7a088536f1c9 | 46 | |
Jellehierck | 15:421d3d9c563b | 47 | double emg2; |
Jellehierck | 15:421d3d9c563b | 48 | double emg2_MVC; |
Jellehierck | 15:421d3d9c563b | 49 | double emg2_MVC_stdev; |
Jellehierck | 15:421d3d9c563b | 50 | double emg2_rest; |
Jellehierck | 15:421d3d9c563b | 51 | double emg2_rest_stdev; |
Jellehierck | 15:421d3d9c563b | 52 | vector<double> emg2_cal; |
IsaRobin | 0:6972d0e91af1 | 53 | |
Jellehierck | 15:421d3d9c563b | 54 | double emg3; |
Jellehierck | 15:421d3d9c563b | 55 | double emg3_MVC; |
Jellehierck | 15:421d3d9c563b | 56 | double emg3_MVC_stdev; |
Jellehierck | 15:421d3d9c563b | 57 | double emg3_rest; |
Jellehierck | 15:421d3d9c563b | 58 | double emg3_rest_stdev; |
Jellehierck | 15:421d3d9c563b | 59 | vector<double> emg3_cal; |
Jellehierck | 15:421d3d9c563b | 60 | |
Jellehierck | 15:421d3d9c563b | 61 | // Initialize tickers and timeouts |
Jellehierck | 4:09a01d2db8f7 | 62 | Ticker tickSample; |
Jellehierck | 15:421d3d9c563b | 63 | Ticker tickSampleCalibration; |
Jellehierck | 7:7a088536f1c9 | 64 | Timeout timeoutCalibrationMVC; |
Jellehierck | 12:70f0710400c2 | 65 | Timeout timeoutCalibrationRest; |
Jellehierck | 4:09a01d2db8f7 | 66 | |
Jellehierck | 15:421d3d9c563b | 67 | /* |
Jellehierck | 15:421d3d9c563b | 68 | ------ GLOBAL VARIABLES ------ |
Jellehierck | 15:421d3d9c563b | 69 | */ |
Jellehierck | 11:042170a9b93a | 70 | const double Fs = 500; // Sampling frequency (s) |
Jellehierck | 11:042170a9b93a | 71 | const double Tcal = 10.0f; // Calibration duration (s) |
Jellehierck | 15:421d3d9c563b | 72 | int trim_cal = 1; // Trim transient behaviour of calibration (s) |
Jellehierck | 4:09a01d2db8f7 | 73 | |
Jellehierck | 15:421d3d9c563b | 74 | // Calculate global variables |
Jellehierck | 15:421d3d9c563b | 75 | const double Ts = 1/Fs; // Sampling time (s) |
Jellehierck | 15:421d3d9c563b | 76 | int trim_cal_i = trim_cal * Fs - 1; // Determine iterator of transient behaviour trim |
Jellehierck | 15:421d3d9c563b | 77 | |
Jellehierck | 15:421d3d9c563b | 78 | // Notch biquad filter coefficients (iirnotch Q factor 35 @50Hz) from MATLAB: |
Jellehierck | 19:94dc52f8a59e | 79 | BiQuad bq1_notch( 0.995636295063941, -1.89829218816065, 0.995636295063941, 1, -1.89829218816065, 0.991272590127882); // b01 b11 b21 a01 a11 a21 |
Jellehierck | 19:94dc52f8a59e | 80 | BiQuad bq2_notch = bq1_notch; |
Jellehierck | 19:94dc52f8a59e | 81 | BiQuad bq3_notch = bq1_notch; |
Jellehierck | 19:94dc52f8a59e | 82 | BiQuadChain bqc1_notch; |
Jellehierck | 19:94dc52f8a59e | 83 | BiQuadChain bqc2_notch; |
Jellehierck | 19:94dc52f8a59e | 84 | BiQuadChain bqc3_notch; |
Jellehierck | 1:059cca298369 | 85 | |
Jellehierck | 15:421d3d9c563b | 86 | // Highpass biquad filter coefficients (butter 4th order @10Hz cutoff) from MATLAB |
Jellehierck | 19:94dc52f8a59e | 87 | BiQuad bq1_H1(0.922946103200875, -1.84589220640175, 0.922946103200875, 1, -1.88920703055163, 0.892769008131025); // b01 b11 b21 a01 a11 a21 |
Jellehierck | 19:94dc52f8a59e | 88 | BiQuad bq1_H2(1, -2, 1, 1, -1.95046575793011, 0.954143234875078); // b02 b12 b22 a02 a12 a22 |
Jellehierck | 19:94dc52f8a59e | 89 | BiQuad bq2_H1 = bq1_H1; |
Jellehierck | 19:94dc52f8a59e | 90 | BiQuad bq2_H2 = bq1_H2; |
Jellehierck | 19:94dc52f8a59e | 91 | BiQuad bq3_H1 = bq1_H1; |
Jellehierck | 19:94dc52f8a59e | 92 | BiQuad bq3_H2 = bq1_H2; |
Jellehierck | 19:94dc52f8a59e | 93 | BiQuadChain bqc1_high; |
Jellehierck | 19:94dc52f8a59e | 94 | BiQuadChain bqc2_high; |
Jellehierck | 19:94dc52f8a59e | 95 | BiQuadChain bqc3_high; |
IsaRobin | 0:6972d0e91af1 | 96 | |
Jellehierck | 15:421d3d9c563b | 97 | // Lowpass biquad filter coefficients (butter 4th order @5Hz cutoff) from MATLAB: |
Jellehierck | 19:94dc52f8a59e | 98 | BiQuad bq1_L1(5.32116245737504e-08, 1.06423249147501e-07, 5.32116245737504e-08, 1, -1.94396715039462, 0.944882378004138); // b01 b11 b21 a01 a11 a21 |
Jellehierck | 19:94dc52f8a59e | 99 | BiQuad bq1_L2(1, 2, 1, 1, -1.97586467534468, 0.976794920438162); // b02 b12 b22 a02 a12 a22 |
Jellehierck | 19:94dc52f8a59e | 100 | BiQuad bq2_L1 = bq1_L1; |
Jellehierck | 19:94dc52f8a59e | 101 | BiQuad bq2_L2 = bq1_L2; |
Jellehierck | 19:94dc52f8a59e | 102 | BiQuad bq3_L1 = bq1_L1; |
Jellehierck | 19:94dc52f8a59e | 103 | BiQuad bq3_L2 = bq1_L2; |
Jellehierck | 19:94dc52f8a59e | 104 | BiQuadChain bqc1_low; |
Jellehierck | 19:94dc52f8a59e | 105 | BiQuadChain bqc2_low; |
Jellehierck | 19:94dc52f8a59e | 106 | BiQuadChain bqc3_low; |
Jellehierck | 2:d3e9788ab1b3 | 107 | |
Jellehierck | 15:421d3d9c563b | 108 | /* |
Jellehierck | 15:421d3d9c563b | 109 | ------ HELPER FUNCTIONS ------ |
Jellehierck | 15:421d3d9c563b | 110 | */ |
Jellehierck | 15:421d3d9c563b | 111 | |
Jellehierck | 15:421d3d9c563b | 112 | // Return mean of vector |
Jellehierck | 8:ea3de43c9e8b | 113 | double getMean(const vector<double> &vect) |
Jellehierck | 7:7a088536f1c9 | 114 | { |
Jellehierck | 8:ea3de43c9e8b | 115 | double sum = 0.0; |
Jellehierck | 8:ea3de43c9e8b | 116 | int vect_n = vect.size(); |
Jellehierck | 8:ea3de43c9e8b | 117 | |
Jellehierck | 8:ea3de43c9e8b | 118 | for ( int i = 0; i < vect_n; i++ ) { |
Jellehierck | 8:ea3de43c9e8b | 119 | sum += vect[i]; |
Jellehierck | 8:ea3de43c9e8b | 120 | } |
Jellehierck | 8:ea3de43c9e8b | 121 | return sum/vect_n; |
Jellehierck | 8:ea3de43c9e8b | 122 | } |
Jellehierck | 8:ea3de43c9e8b | 123 | |
Jellehierck | 15:421d3d9c563b | 124 | // Return standard deviation of vector |
Jellehierck | 8:ea3de43c9e8b | 125 | double getStdev(const vector<double> &vect, const double vect_mean) |
Jellehierck | 8:ea3de43c9e8b | 126 | { |
Jellehierck | 8:ea3de43c9e8b | 127 | double sum2 = 0.0; |
Jellehierck | 8:ea3de43c9e8b | 128 | int vect_n = vect.size(); |
Jellehierck | 8:ea3de43c9e8b | 129 | |
Jellehierck | 8:ea3de43c9e8b | 130 | for ( int i = 0; i < vect_n; i++ ) { |
Jellehierck | 8:ea3de43c9e8b | 131 | sum2 += pow( vect[i] - vect_mean, 2 ); |
Jellehierck | 8:ea3de43c9e8b | 132 | } |
Jellehierck | 8:ea3de43c9e8b | 133 | double output = sqrt( sum2 / vect_n ); |
Jellehierck | 8:ea3de43c9e8b | 134 | return output; |
Jellehierck | 7:7a088536f1c9 | 135 | } |
Jellehierck | 7:7a088536f1c9 | 136 | |
Jellehierck | 15:421d3d9c563b | 137 | // Check filter stability |
Jellehierck | 6:5437cc97e1e6 | 138 | bool checkBQChainStable() |
Jellehierck | 6:5437cc97e1e6 | 139 | { |
Jellehierck | 19:94dc52f8a59e | 140 | bool n_stable = bqc1_notch.stable(); |
Jellehierck | 19:94dc52f8a59e | 141 | bool hp_stable = bqc1_high.stable(); |
Jellehierck | 19:94dc52f8a59e | 142 | bool l_stable = bqc1_low.stable(); |
Jellehierck | 6:5437cc97e1e6 | 143 | |
Jellehierck | 11:042170a9b93a | 144 | if (n_stable && hp_stable && l_stable) { |
Jellehierck | 6:5437cc97e1e6 | 145 | return true; |
Jellehierck | 6:5437cc97e1e6 | 146 | } else { |
Jellehierck | 6:5437cc97e1e6 | 147 | return false; |
Jellehierck | 6:5437cc97e1e6 | 148 | } |
Jellehierck | 6:5437cc97e1e6 | 149 | } |
Jellehierck | 6:5437cc97e1e6 | 150 | |
Jellehierck | 15:421d3d9c563b | 151 | /* |
Jellehierck | 15:421d3d9c563b | 152 | ------ TICKER FUNCTIONS ------ |
Jellehierck | 15:421d3d9c563b | 153 | */ |
Jellehierck | 11:042170a9b93a | 154 | /* |
Jellehierck | 6:5437cc97e1e6 | 155 | // Read samples, filter samples and output to HIDScope |
Jellehierck | 2:d3e9788ab1b3 | 156 | void sample() |
Jellehierck | 2:d3e9788ab1b3 | 157 | { |
Jellehierck | 4:09a01d2db8f7 | 158 | // Read EMG inputs |
Jellehierck | 2:d3e9788ab1b3 | 159 | emg1 = emg1_in.read(); |
Jellehierck | 2:d3e9788ab1b3 | 160 | emg2 = emg2_in.read(); |
Jellehierck | 2:d3e9788ab1b3 | 161 | emg3 = emg3_in.read(); |
Jellehierck | 4:09a01d2db8f7 | 162 | |
Jellehierck | 4:09a01d2db8f7 | 163 | // Output raw EMG input |
Jellehierck | 4:09a01d2db8f7 | 164 | scope.set(0, emg1 ); |
Jellehierck | 6:5437cc97e1e6 | 165 | |
Jellehierck | 5:3d65f89e3755 | 166 | // Filter notch and highpass |
Jellehierck | 19:94dc52f8a59e | 167 | double emg1_n_hp = bqc1_notch_high.step( emg1 ); |
Jellehierck | 6:5437cc97e1e6 | 168 | |
Jellehierck | 5:3d65f89e3755 | 169 | // Rectify |
Jellehierck | 5:3d65f89e3755 | 170 | double emg1_rectify = fabs( emg1_n_hp ); |
Jellehierck | 6:5437cc97e1e6 | 171 | |
Jellehierck | 5:3d65f89e3755 | 172 | // Filter lowpass (completes envelope) |
Jellehierck | 19:94dc52f8a59e | 173 | double emg1_env = bqc1_low.step( emg1_rectify ); |
Jellehierck | 4:09a01d2db8f7 | 174 | |
Jellehierck | 4:09a01d2db8f7 | 175 | // Output EMG after filters |
Jellehierck | 5:3d65f89e3755 | 176 | scope.set(1, emg1_env ); |
Jellehierck | 4:09a01d2db8f7 | 177 | scope.send(); |
Jellehierck | 2:d3e9788ab1b3 | 178 | } |
Jellehierck | 11:042170a9b93a | 179 | */ |
IsaRobin | 0:6972d0e91af1 | 180 | |
Jellehierck | 7:7a088536f1c9 | 181 | void sampleCalibration() |
Jellehierck | 7:7a088536f1c9 | 182 | { |
Jellehierck | 7:7a088536f1c9 | 183 | // Read EMG inputs |
Jellehierck | 7:7a088536f1c9 | 184 | emg1 = emg1_in.read(); |
Jellehierck | 7:7a088536f1c9 | 185 | emg2 = emg2_in.read(); |
Jellehierck | 7:7a088536f1c9 | 186 | emg3 = emg3_in.read(); |
Jellehierck | 7:7a088536f1c9 | 187 | |
Jellehierck | 7:7a088536f1c9 | 188 | // Output raw EMG input |
Jellehierck | 19:94dc52f8a59e | 189 | //scope.set(0, emg1 ); |
Jellehierck | 19:94dc52f8a59e | 190 | // scope.set(1, emg2 ); |
Jellehierck | 10:97a79aa10a56 | 191 | |
Jellehierck | 19:94dc52f8a59e | 192 | double emg1_n = bqc1_notch.step( emg1 ); // Filter notch |
Jellehierck | 19:94dc52f8a59e | 193 | double emg1_hp = bqc1_high.step( emg1_n ); // Filter highpass |
Jellehierck | 17:e4e7b1fbb263 | 194 | double emg1_rectify = fabs( emg1_hp ); // Rectify |
Jellehierck | 19:94dc52f8a59e | 195 | double emg1_env = bqc1_low.step( emg1_rectify ); // Filter lowpass (completes envelope) |
Jellehierck | 17:e4e7b1fbb263 | 196 | |
Jellehierck | 19:94dc52f8a59e | 197 | double emg2_n = bqc2_notch.step( emg2 ); // Filter notch |
Jellehierck | 19:94dc52f8a59e | 198 | double emg2_hp = bqc2_high.step( emg2_n ); // Filter highpass |
Jellehierck | 17:e4e7b1fbb263 | 199 | double emg2_rectify = fabs( emg2_hp ); // Rectify |
Jellehierck | 19:94dc52f8a59e | 200 | double emg2_env = bqc2_low.step( emg2_rectify ); // Filter lowpass (completes envelope) |
Jellehierck | 17:e4e7b1fbb263 | 201 | |
Jellehierck | 19:94dc52f8a59e | 202 | scope.set(0, emg1_n); |
Jellehierck | 19:94dc52f8a59e | 203 | scope.set(1, emg2_n); |
Jellehierck | 17:e4e7b1fbb263 | 204 | // scope.set(3, emg1_rectify); |
Jellehierck | 7:7a088536f1c9 | 205 | |
Jellehierck | 7:7a088536f1c9 | 206 | // Output EMG after filters |
Jellehierck | 17:e4e7b1fbb263 | 207 | scope.set(2, emg1_env ); |
Jellehierck | 17:e4e7b1fbb263 | 208 | scope.set(3, emg2_env ); |
Jellehierck | 7:7a088536f1c9 | 209 | scope.send(); |
Jellehierck | 7:7a088536f1c9 | 210 | |
Jellehierck | 17:e4e7b1fbb263 | 211 | // IF STATEMENT TOEVOEGEN VOOR CALIBRATIE |
Jellehierck | 17:e4e7b1fbb263 | 212 | emg1_cal.push_back(emg1_env); // Add values to calibration vector |
Jellehierck | 17:e4e7b1fbb263 | 213 | emg2_cal.push_back(emg2_env); // Add values to calibration vector |
Jellehierck | 7:7a088536f1c9 | 214 | } |
Jellehierck | 7:7a088536f1c9 | 215 | |
Jellehierck | 15:421d3d9c563b | 216 | /* |
Jellehierck | 15:421d3d9c563b | 217 | ------ EMG CALIBRATION FUNCTIONS ------ |
Jellehierck | 15:421d3d9c563b | 218 | */ |
Jellehierck | 15:421d3d9c563b | 219 | |
Jellehierck | 15:421d3d9c563b | 220 | // Finish up calibration of MVC |
Jellehierck | 7:7a088536f1c9 | 221 | void calibrationMVCFinished() |
Jellehierck | 7:7a088536f1c9 | 222 | { |
Jellehierck | 15:421d3d9c563b | 223 | tickSampleCalibration.detach(); // Stop calibration ticker to remove interrupt |
Jellehierck | 15:421d3d9c563b | 224 | emg1_MVC = getMean(emg1_cal); // Store MVC globally |
Jellehierck | 15:421d3d9c563b | 225 | emg1_MVC_stdev = getStdev(emg1_cal, emg1_MVC); // Store MVC stde globally |
Jellehierck | 15:421d3d9c563b | 226 | emg1_cal.clear(); // Empty vector to prevent memory overflow |
Jellehierck | 19:94dc52f8a59e | 227 | emg2_MVC = getMean(emg2_cal); // Store MVC globally |
Jellehierck | 19:94dc52f8a59e | 228 | emg2_MVC_stdev = getStdev(emg2_cal, emg2_MVC); // Store MVC stde globally |
Jellehierck | 19:94dc52f8a59e | 229 | emg2_cal.clear(); // Empty vector to prevent memory overflow |
Jellehierck | 15:421d3d9c563b | 230 | led_b = 1; // Turn off calibration led |
Jellehierck | 7:7a088536f1c9 | 231 | } |
Jellehierck | 7:7a088536f1c9 | 232 | |
Jellehierck | 15:421d3d9c563b | 233 | // Run calibration of MVC |
Jellehierck | 7:7a088536f1c9 | 234 | void calibrationMVC() |
Jellehierck | 7:7a088536f1c9 | 235 | { |
Jellehierck | 15:421d3d9c563b | 236 | timeoutCalibrationMVC.attach( &calibrationMVCFinished, Tcal); // Stop MVC calibration after interval |
Jellehierck | 15:421d3d9c563b | 237 | tickSampleCalibration.attach( &sampleCalibration, Ts ); // Start sample ticker |
Jellehierck | 15:421d3d9c563b | 238 | led_b = 0; // Turn on calibration led |
Jellehierck | 7:7a088536f1c9 | 239 | } |
Jellehierck | 7:7a088536f1c9 | 240 | |
Jellehierck | 15:421d3d9c563b | 241 | // Finish up calibration in rest |
Jellehierck | 12:70f0710400c2 | 242 | void calibrationRestFinished() |
Jellehierck | 12:70f0710400c2 | 243 | { |
Jellehierck | 15:421d3d9c563b | 244 | tickSampleCalibration.detach(); // Stop calibration ticker to remove interrupt |
Jellehierck | 15:421d3d9c563b | 245 | emg1_rest = getMean(emg1_cal); // Store rest globally |
Jellehierck | 15:421d3d9c563b | 246 | emg1_rest_stdev = getStdev(emg1_cal, emg1_rest);// Store rest stdev globally |
Jellehierck | 15:421d3d9c563b | 247 | emg1_cal.clear(); // Empty vector to prevent memory overflow |
Jellehierck | 19:94dc52f8a59e | 248 | emg2_rest = getMean(emg2_cal); // Store rest globally |
Jellehierck | 19:94dc52f8a59e | 249 | emg2_rest_stdev = getStdev(emg2_cal, emg2_rest);// Store rest stdev globally |
Jellehierck | 19:94dc52f8a59e | 250 | emg2_cal.clear(); // Empty vector to prevent memory overflow |
Jellehierck | 15:421d3d9c563b | 251 | led_b = 1; // Turn off calibration led |
Jellehierck | 12:70f0710400c2 | 252 | } |
Jellehierck | 12:70f0710400c2 | 253 | |
Jellehierck | 16:7acbcc4aa35c | 254 | // Run calibration in rest |
Jellehierck | 12:70f0710400c2 | 255 | void calibrationRest() |
Jellehierck | 12:70f0710400c2 | 256 | { |
Jellehierck | 15:421d3d9c563b | 257 | timeoutCalibrationRest.attach( &calibrationRestFinished, Tcal); // Stop rest calibration after interval |
Jellehierck | 15:421d3d9c563b | 258 | tickSampleCalibration.attach( &sampleCalibration, Ts ); // Start sample ticker |
Jellehierck | 15:421d3d9c563b | 259 | led_b = 0; // Turn on calibration led |
Jellehierck | 12:70f0710400c2 | 260 | } |
Jellehierck | 12:70f0710400c2 | 261 | |
Jellehierck | 15:421d3d9c563b | 262 | // Determine scale factors for operation mode |
Jellehierck | 12:70f0710400c2 | 263 | void makeScale() |
Jellehierck | 12:70f0710400c2 | 264 | { |
Jellehierck | 15:421d3d9c563b | 265 | double margin_percentage = 10; // Set up % margin for rest |
Jellehierck | 15:421d3d9c563b | 266 | double factor1 = 1 / emg1_MVC; // Factor to normalize MVC |
Jellehierck | 15:421d3d9c563b | 267 | double emg1_th = emg1_rest * factor1 + margin_percentage/100; // Set normalized rest threshold |
Jellehierck | 12:70f0710400c2 | 268 | |
Jellehierck | 12:70f0710400c2 | 269 | pc.printf("Factor: %f TH: %f\r\n", factor1, emg1_th); |
Jellehierck | 12:70f0710400c2 | 270 | } |
Jellehierck | 7:7a088536f1c9 | 271 | |
Jellehierck | 16:7acbcc4aa35c | 272 | /* |
Jellehierck | 16:7acbcc4aa35c | 273 | ------ EMG SUBSTATE MACHINE ------ |
Jellehierck | 16:7acbcc4aa35c | 274 | */ |
Jellehierck | 16:7acbcc4aa35c | 275 | |
Jellehierck | 16:7acbcc4aa35c | 276 | |
Jellehierck | 16:7acbcc4aa35c | 277 | |
Jellehierck | 5:3d65f89e3755 | 278 | void main() |
Jellehierck | 4:09a01d2db8f7 | 279 | { |
Jellehierck | 15:421d3d9c563b | 280 | pc.baud(115200); // MODSERIAL rate |
Jellehierck | 8:ea3de43c9e8b | 281 | pc.printf("Starting\r\n"); |
Jellehierck | 15:421d3d9c563b | 282 | |
Jellehierck | 15:421d3d9c563b | 283 | // tickSample.attach(&sample, Ts); // Initialize sample ticker |
Jellehierck | 6:5437cc97e1e6 | 284 | |
Jellehierck | 6:5437cc97e1e6 | 285 | // Create BQ chains to reduce computations |
Jellehierck | 19:94dc52f8a59e | 286 | bqc1_notch.add( &bq1_notch ); |
Jellehierck | 19:94dc52f8a59e | 287 | bqc1_high.add( &bq1_H1 ).add( &bq1_H2 ); |
Jellehierck | 19:94dc52f8a59e | 288 | bqc1_low.add( &bq1_L1 ).add( &bq1_L2 ); |
Jellehierck | 19:94dc52f8a59e | 289 | |
Jellehierck | 19:94dc52f8a59e | 290 | bqc2_notch.add( &bq2_notch ); |
Jellehierck | 19:94dc52f8a59e | 291 | bqc2_high.add( &bq2_H1 ).add( &bq2_H2 ); |
Jellehierck | 19:94dc52f8a59e | 292 | bqc2_low.add( &bq2_L1 ).add( &bq2_L2 ); |
Jellehierck | 19:94dc52f8a59e | 293 | |
Jellehierck | 19:94dc52f8a59e | 294 | bqc3_notch.add( &bq3_notch ); |
Jellehierck | 19:94dc52f8a59e | 295 | bqc3_high.add( &bq3_H1 ).add( &bq3_H2 ); |
Jellehierck | 19:94dc52f8a59e | 296 | bqc3_low.add( &bq3_L1 ).add( &bq3_L2 ); |
Jellehierck | 4:09a01d2db8f7 | 297 | |
Jellehierck | 15:421d3d9c563b | 298 | led_b = 1; // Turn blue led off at startup |
Jellehierck | 15:421d3d9c563b | 299 | led_g = 1; // Turn green led off at startup |
Jellehierck | 15:421d3d9c563b | 300 | led_r = 1; // Turn red led off at startup |
Jellehierck | 8:ea3de43c9e8b | 301 | |
Jellehierck | 6:5437cc97e1e6 | 302 | // If any filter chain is unstable, red led will light up |
Jellehierck | 6:5437cc97e1e6 | 303 | if (checkBQChainStable) { |
Jellehierck | 6:5437cc97e1e6 | 304 | led_r = 1; // LED off |
Jellehierck | 6:5437cc97e1e6 | 305 | } else { |
Jellehierck | 6:5437cc97e1e6 | 306 | led_r = 0; // LED on |
Jellehierck | 6:5437cc97e1e6 | 307 | } |
Jellehierck | 6:5437cc97e1e6 | 308 | |
Jellehierck | 15:421d3d9c563b | 309 | button1.fall( &calibrationMVC ); // Run MVC calibration on button press |
Jellehierck | 15:421d3d9c563b | 310 | button2.fall( &calibrationRest ); // Run rest calibration on button press |
Jellehierck | 15:421d3d9c563b | 311 | button3.fall( &makeScale ); // Create scale factors and close calibration at button press |
Jellehierck | 8:ea3de43c9e8b | 312 | |
Jellehierck | 4:09a01d2db8f7 | 313 | while(true) { |
Jellehierck | 7:7a088536f1c9 | 314 | |
Jellehierck | 6:5437cc97e1e6 | 315 | // Show that system is running |
Jellehierck | 8:ea3de43c9e8b | 316 | // led_g = !led_g; |
Jellehierck | 12:70f0710400c2 | 317 | 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 | 19:94dc52f8a59e | 318 | wait(1.0f); |
Jellehierck | 4:09a01d2db8f7 | 319 | } |
Jellehierck | 4:09a01d2db8f7 | 320 | } |