![](/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@29:f51683a6cbbf, 2019-10-28 (annotated)
- Committer:
- Jellehierck
- Date:
- Mon Oct 28 14:40:43 2019 +0000
- Revision:
- 29:f51683a6cbbf
- Parent:
- 28:59e8266f4633
- Child:
- 30:bac3b60d6283
Added even more comments
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 | 26:7e81c7db6e7a | 32 | enum EMG_States { emg_wait, emg_cal_MVC, emg_cal_rest, emg_operation }; // Define EMG substates |
Jellehierck | 16:7acbcc4aa35c | 33 | EMG_States emg_curr_state; // Initialize EMG substate variable |
Jellehierck | 25:a1be4cf2ab0b | 34 | bool emg_state_changed = true; |
Jellehierck | 25:a1be4cf2ab0b | 35 | |
Jellehierck | 25:a1be4cf2ab0b | 36 | bool sampleNow = false; |
Jellehierck | 25:a1be4cf2ab0b | 37 | bool calibrateNow = false; |
Jellehierck | 25:a1be4cf2ab0b | 38 | bool emg_MVC_cal_done = false; |
Jellehierck | 25:a1be4cf2ab0b | 39 | bool emg_rest_cal_done = false; |
Jellehierck | 25:a1be4cf2ab0b | 40 | |
Jellehierck | 25:a1be4cf2ab0b | 41 | bool button1_pressed = false; |
Jellehierck | 25:a1be4cf2ab0b | 42 | bool button2_pressed = false; |
Jellehierck | 16:7acbcc4aa35c | 43 | |
Jellehierck | 15:421d3d9c563b | 44 | // Global variables for EMG reading |
Jellehierck | 15:421d3d9c563b | 45 | AnalogIn emg1_in (A1); // Right biceps, x axis |
Jellehierck | 15:421d3d9c563b | 46 | AnalogIn emg2_in (A2); // Left biceps, y axis |
Jellehierck | 15:421d3d9c563b | 47 | AnalogIn emg3_in (A3); // Third muscle, TBD |
Jellehierck | 15:421d3d9c563b | 48 | |
IsaRobin | 0:6972d0e91af1 | 49 | double emg1; |
Jellehierck | 26:7e81c7db6e7a | 50 | double emg1_env; |
Jellehierck | 12:70f0710400c2 | 51 | double emg1_MVC; |
Jellehierck | 12:70f0710400c2 | 52 | double emg1_MVC_stdev; |
Jellehierck | 12:70f0710400c2 | 53 | double emg1_rest; |
Jellehierck | 12:70f0710400c2 | 54 | double emg1_rest_stdev; |
Jellehierck | 26:7e81c7db6e7a | 55 | double emg1_factor; |
Jellehierck | 26:7e81c7db6e7a | 56 | double emg1_th; |
Jellehierck | 26:7e81c7db6e7a | 57 | double emg1_out; |
Jellehierck | 7:7a088536f1c9 | 58 | vector<double> emg1_cal; |
Jellehierck | 25:a1be4cf2ab0b | 59 | int emg1_cal_size; |
Jellehierck | 7:7a088536f1c9 | 60 | |
Jellehierck | 15:421d3d9c563b | 61 | double emg2; |
Jellehierck | 26:7e81c7db6e7a | 62 | double emg2_env; |
Jellehierck | 15:421d3d9c563b | 63 | double emg2_MVC; |
Jellehierck | 15:421d3d9c563b | 64 | double emg2_MVC_stdev; |
Jellehierck | 15:421d3d9c563b | 65 | double emg2_rest; |
Jellehierck | 15:421d3d9c563b | 66 | double emg2_rest_stdev; |
Jellehierck | 26:7e81c7db6e7a | 67 | double emg2_factor; |
Jellehierck | 26:7e81c7db6e7a | 68 | double emg2_th; |
Jellehierck | 26:7e81c7db6e7a | 69 | double emg2_out; |
Jellehierck | 15:421d3d9c563b | 70 | vector<double> emg2_cal; |
Jellehierck | 25:a1be4cf2ab0b | 71 | int emg2_cal_size; |
IsaRobin | 0:6972d0e91af1 | 72 | |
Jellehierck | 15:421d3d9c563b | 73 | double emg3; |
Jellehierck | 26:7e81c7db6e7a | 74 | double emg3_env; |
Jellehierck | 15:421d3d9c563b | 75 | double emg3_MVC; |
Jellehierck | 15:421d3d9c563b | 76 | double emg3_MVC_stdev; |
Jellehierck | 15:421d3d9c563b | 77 | double emg3_rest; |
Jellehierck | 15:421d3d9c563b | 78 | double emg3_rest_stdev; |
Jellehierck | 26:7e81c7db6e7a | 79 | double emg3_factor; |
Jellehierck | 26:7e81c7db6e7a | 80 | double emg3_th; |
Jellehierck | 26:7e81c7db6e7a | 81 | double emg3_out; |
Jellehierck | 15:421d3d9c563b | 82 | vector<double> emg3_cal; |
Jellehierck | 25:a1be4cf2ab0b | 83 | int emg3_cal_size; |
Jellehierck | 15:421d3d9c563b | 84 | |
Jellehierck | 15:421d3d9c563b | 85 | // Initialize tickers and timeouts |
Jellehierck | 4:09a01d2db8f7 | 86 | Ticker tickSample; |
Jellehierck | 15:421d3d9c563b | 87 | Ticker tickSampleCalibration; |
Jellehierck | 24:540c284e881d | 88 | Ticker tickGlobal; // Set global ticker |
Jellehierck | 22:9079c6c0d898 | 89 | Timer timerCalibration; |
Jellehierck | 4:09a01d2db8f7 | 90 | |
Jellehierck | 15:421d3d9c563b | 91 | /* |
Jellehierck | 15:421d3d9c563b | 92 | ------ GLOBAL VARIABLES ------ |
Jellehierck | 15:421d3d9c563b | 93 | */ |
Jellehierck | 11:042170a9b93a | 94 | const double Fs = 500; // Sampling frequency (s) |
Jellehierck | 11:042170a9b93a | 95 | const double Tcal = 10.0f; // Calibration duration (s) |
Jellehierck | 15:421d3d9c563b | 96 | int trim_cal = 1; // Trim transient behaviour of calibration (s) |
Jellehierck | 4:09a01d2db8f7 | 97 | |
Jellehierck | 15:421d3d9c563b | 98 | // Calculate global variables |
Jellehierck | 15:421d3d9c563b | 99 | const double Ts = 1/Fs; // Sampling time (s) |
Jellehierck | 15:421d3d9c563b | 100 | int trim_cal_i = trim_cal * Fs - 1; // Determine iterator of transient behaviour trim |
Jellehierck | 15:421d3d9c563b | 101 | |
Jellehierck | 15:421d3d9c563b | 102 | // Notch biquad filter coefficients (iirnotch Q factor 35 @50Hz) from MATLAB: |
Jellehierck | 19:94dc52f8a59e | 103 | BiQuad bq1_notch( 0.995636295063941, -1.89829218816065, 0.995636295063941, 1, -1.89829218816065, 0.991272590127882); // b01 b11 b21 a01 a11 a21 |
Jellehierck | 19:94dc52f8a59e | 104 | BiQuad bq2_notch = bq1_notch; |
Jellehierck | 19:94dc52f8a59e | 105 | BiQuad bq3_notch = bq1_notch; |
Jellehierck | 19:94dc52f8a59e | 106 | BiQuadChain bqc1_notch; |
Jellehierck | 19:94dc52f8a59e | 107 | BiQuadChain bqc2_notch; |
Jellehierck | 19:94dc52f8a59e | 108 | BiQuadChain bqc3_notch; |
Jellehierck | 1:059cca298369 | 109 | |
Jellehierck | 15:421d3d9c563b | 110 | // Highpass biquad filter coefficients (butter 4th order @10Hz cutoff) from MATLAB |
Jellehierck | 19:94dc52f8a59e | 111 | BiQuad bq1_H1(0.922946103200875, -1.84589220640175, 0.922946103200875, 1, -1.88920703055163, 0.892769008131025); // b01 b11 b21 a01 a11 a21 |
Jellehierck | 19:94dc52f8a59e | 112 | BiQuad bq1_H2(1, -2, 1, 1, -1.95046575793011, 0.954143234875078); // b02 b12 b22 a02 a12 a22 |
Jellehierck | 19:94dc52f8a59e | 113 | BiQuad bq2_H1 = bq1_H1; |
Jellehierck | 19:94dc52f8a59e | 114 | BiQuad bq2_H2 = bq1_H2; |
Jellehierck | 19:94dc52f8a59e | 115 | BiQuad bq3_H1 = bq1_H1; |
Jellehierck | 19:94dc52f8a59e | 116 | BiQuad bq3_H2 = bq1_H2; |
Jellehierck | 20:0e9218673aa8 | 117 | BiQuadChain bqc1_high; |
Jellehierck | 19:94dc52f8a59e | 118 | BiQuadChain bqc2_high; |
Jellehierck | 19:94dc52f8a59e | 119 | BiQuadChain bqc3_high; |
IsaRobin | 0:6972d0e91af1 | 120 | |
Jellehierck | 15:421d3d9c563b | 121 | // Lowpass biquad filter coefficients (butter 4th order @5Hz cutoff) from MATLAB: |
Jellehierck | 19:94dc52f8a59e | 122 | 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 | 123 | BiQuad bq1_L2(1, 2, 1, 1, -1.97586467534468, 0.976794920438162); // b02 b12 b22 a02 a12 a22 |
Jellehierck | 19:94dc52f8a59e | 124 | BiQuad bq2_L1 = bq1_L1; |
Jellehierck | 19:94dc52f8a59e | 125 | BiQuad bq2_L2 = bq1_L2; |
Jellehierck | 19:94dc52f8a59e | 126 | BiQuad bq3_L1 = bq1_L1; |
Jellehierck | 19:94dc52f8a59e | 127 | BiQuad bq3_L2 = bq1_L2; |
Jellehierck | 19:94dc52f8a59e | 128 | BiQuadChain bqc1_low; |
Jellehierck | 19:94dc52f8a59e | 129 | BiQuadChain bqc2_low; |
Jellehierck | 19:94dc52f8a59e | 130 | BiQuadChain bqc3_low; |
Jellehierck | 2:d3e9788ab1b3 | 131 | |
Jellehierck | 15:421d3d9c563b | 132 | /* |
Jellehierck | 15:421d3d9c563b | 133 | ------ HELPER FUNCTIONS ------ |
Jellehierck | 15:421d3d9c563b | 134 | */ |
Jellehierck | 15:421d3d9c563b | 135 | |
Jellehierck | 15:421d3d9c563b | 136 | // Return mean of vector |
Jellehierck | 8:ea3de43c9e8b | 137 | double getMean(const vector<double> &vect) |
Jellehierck | 7:7a088536f1c9 | 138 | { |
Jellehierck | 8:ea3de43c9e8b | 139 | double sum = 0.0; |
Jellehierck | 8:ea3de43c9e8b | 140 | int vect_n = vect.size(); |
Jellehierck | 8:ea3de43c9e8b | 141 | |
Jellehierck | 8:ea3de43c9e8b | 142 | for ( int i = 0; i < vect_n; i++ ) { |
Jellehierck | 8:ea3de43c9e8b | 143 | sum += vect[i]; |
Jellehierck | 8:ea3de43c9e8b | 144 | } |
Jellehierck | 8:ea3de43c9e8b | 145 | return sum/vect_n; |
Jellehierck | 8:ea3de43c9e8b | 146 | } |
Jellehierck | 8:ea3de43c9e8b | 147 | |
Jellehierck | 15:421d3d9c563b | 148 | // Return standard deviation of vector |
Jellehierck | 8:ea3de43c9e8b | 149 | double getStdev(const vector<double> &vect, const double vect_mean) |
Jellehierck | 8:ea3de43c9e8b | 150 | { |
Jellehierck | 8:ea3de43c9e8b | 151 | double sum2 = 0.0; |
Jellehierck | 8:ea3de43c9e8b | 152 | int vect_n = vect.size(); |
Jellehierck | 8:ea3de43c9e8b | 153 | |
Jellehierck | 8:ea3de43c9e8b | 154 | for ( int i = 0; i < vect_n; i++ ) { |
Jellehierck | 8:ea3de43c9e8b | 155 | sum2 += pow( vect[i] - vect_mean, 2 ); |
Jellehierck | 8:ea3de43c9e8b | 156 | } |
Jellehierck | 8:ea3de43c9e8b | 157 | double output = sqrt( sum2 / vect_n ); |
Jellehierck | 8:ea3de43c9e8b | 158 | return output; |
Jellehierck | 7:7a088536f1c9 | 159 | } |
Jellehierck | 7:7a088536f1c9 | 160 | |
Jellehierck | 26:7e81c7db6e7a | 161 | // Rescale values to certain range |
Jellehierck | 26:7e81c7db6e7a | 162 | double rescale(double input, double out_min, double out_max, double in_min, double in_max) |
Jellehierck | 26:7e81c7db6e7a | 163 | { |
Jellehierck | 26:7e81c7db6e7a | 164 | double output = out_min + ((input-in_min)/(in_max-in_min))*(out_max-out_min); // Based on MATLAB rescale function |
Jellehierck | 26:7e81c7db6e7a | 165 | return output; |
Jellehierck | 26:7e81c7db6e7a | 166 | } |
Jellehierck | 26:7e81c7db6e7a | 167 | |
Jellehierck | 25:a1be4cf2ab0b | 168 | // Handle button press |
Jellehierck | 25:a1be4cf2ab0b | 169 | void button1Press() |
Jellehierck | 25:a1be4cf2ab0b | 170 | { |
Jellehierck | 25:a1be4cf2ab0b | 171 | button1_pressed = true; |
Jellehierck | 25:a1be4cf2ab0b | 172 | } |
Jellehierck | 25:a1be4cf2ab0b | 173 | |
Jellehierck | 25:a1be4cf2ab0b | 174 | // Handle button press |
Jellehierck | 25:a1be4cf2ab0b | 175 | void button2Press() |
Jellehierck | 25:a1be4cf2ab0b | 176 | { |
Jellehierck | 25:a1be4cf2ab0b | 177 | button2_pressed = true; |
Jellehierck | 25:a1be4cf2ab0b | 178 | } |
Jellehierck | 25:a1be4cf2ab0b | 179 | |
Jellehierck | 15:421d3d9c563b | 180 | // Check filter stability |
Jellehierck | 6:5437cc97e1e6 | 181 | bool checkBQChainStable() |
Jellehierck | 6:5437cc97e1e6 | 182 | { |
Jellehierck | 29:f51683a6cbbf | 183 | bool n_stable = bqc1_notch.stable(); // Check stability of all BQ Chains |
Jellehierck | 19:94dc52f8a59e | 184 | bool hp_stable = bqc1_high.stable(); |
Jellehierck | 19:94dc52f8a59e | 185 | bool l_stable = bqc1_low.stable(); |
Jellehierck | 6:5437cc97e1e6 | 186 | |
Jellehierck | 11:042170a9b93a | 187 | if (n_stable && hp_stable && l_stable) { |
Jellehierck | 6:5437cc97e1e6 | 188 | return true; |
Jellehierck | 6:5437cc97e1e6 | 189 | } else { |
Jellehierck | 6:5437cc97e1e6 | 190 | return false; |
Jellehierck | 6:5437cc97e1e6 | 191 | } |
Jellehierck | 6:5437cc97e1e6 | 192 | } |
Jellehierck | 6:5437cc97e1e6 | 193 | |
Jellehierck | 15:421d3d9c563b | 194 | /* |
Jellehierck | 15:421d3d9c563b | 195 | ------ TICKER FUNCTIONS ------ |
Jellehierck | 15:421d3d9c563b | 196 | */ |
Jellehierck | 25:a1be4cf2ab0b | 197 | void sampleSignal() |
Jellehierck | 7:7a088536f1c9 | 198 | { |
Jellehierck | 29:f51683a6cbbf | 199 | if (sampleNow == true) { // This ticker only samples if the sample flag is true, to prevent unnecessary computations |
Jellehierck | 25:a1be4cf2ab0b | 200 | // Read EMG inputs |
Jellehierck | 25:a1be4cf2ab0b | 201 | emg1 = emg1_in.read(); |
Jellehierck | 25:a1be4cf2ab0b | 202 | emg2 = emg2_in.read(); |
Jellehierck | 25:a1be4cf2ab0b | 203 | emg3 = emg3_in.read(); |
Jellehierck | 25:a1be4cf2ab0b | 204 | |
Jellehierck | 7:7a088536f1c9 | 205 | |
Jellehierck | 25:a1be4cf2ab0b | 206 | double emg1_n = bqc1_notch.step( emg1 ); // Filter notch |
Jellehierck | 25:a1be4cf2ab0b | 207 | double emg1_hp = bqc1_high.step( emg1_n ); // Filter highpass |
Jellehierck | 25:a1be4cf2ab0b | 208 | double emg1_rectify = fabs( emg1_hp ); // Rectify |
Jellehierck | 26:7e81c7db6e7a | 209 | emg1_env = bqc1_low.step( emg1_rectify ); // Filter lowpass (completes envelope) |
Jellehierck | 25:a1be4cf2ab0b | 210 | |
Jellehierck | 25:a1be4cf2ab0b | 211 | double emg2_n = bqc2_notch.step( emg2 ); // Filter notch |
Jellehierck | 25:a1be4cf2ab0b | 212 | double emg2_hp = bqc2_high.step( emg2_n ); // Filter highpass |
Jellehierck | 25:a1be4cf2ab0b | 213 | double emg2_rectify = fabs( emg2_hp ); // Rectify |
Jellehierck | 26:7e81c7db6e7a | 214 | emg2_env = bqc2_low.step( emg2_rectify ); // Filter lowpass (completes envelope) |
Jellehierck | 20:0e9218673aa8 | 215 | |
Jellehierck | 25:a1be4cf2ab0b | 216 | double emg3_n = bqc3_notch.step( emg3 ); // Filter notch |
Jellehierck | 25:a1be4cf2ab0b | 217 | double emg3_hp = bqc3_high.step( emg3_n ); // Filter highpass |
Jellehierck | 25:a1be4cf2ab0b | 218 | double emg3_rectify = fabs( emg3_hp ); // Rectify |
Jellehierck | 26:7e81c7db6e7a | 219 | emg3_env = bqc3_low.step( emg3_rectify ); // Filter lowpass (completes envelope) |
Jellehierck | 20:0e9218673aa8 | 220 | |
Jellehierck | 28:59e8266f4633 | 221 | // Set HIDScope outputs |
Jellehierck | 28:59e8266f4633 | 222 | scope.set(0, emg1_n ); // One notch filtered EMG to check 50Hz disturbance |
Jellehierck | 28:59e8266f4633 | 223 | scope.set(1, emg1_env ); |
Jellehierck | 28:59e8266f4633 | 224 | scope.set(2, emg2_env ); |
Jellehierck | 25:a1be4cf2ab0b | 225 | scope.set(3, emg2_env ); |
Jellehierck | 25:a1be4cf2ab0b | 226 | scope.send(); |
Jellehierck | 26:7e81c7db6e7a | 227 | |
Jellehierck | 29:f51683a6cbbf | 228 | if (calibrateNow == true) { // Only add values to EMG vectors if calibration flag is true |
Jellehierck | 25:a1be4cf2ab0b | 229 | emg1_cal.push_back(emg1_env); // Add values to calibration vector |
Jellehierck | 25:a1be4cf2ab0b | 230 | emg1_cal_size = emg1_cal.size(); |
Jellehierck | 25:a1be4cf2ab0b | 231 | emg2_cal.push_back(emg2_env); // Add values to calibration vector |
Jellehierck | 25:a1be4cf2ab0b | 232 | emg2_cal_size = emg1_cal.size(); |
Jellehierck | 25:a1be4cf2ab0b | 233 | emg3_cal.push_back(emg3_env); // Add values to calibration vector |
Jellehierck | 25:a1be4cf2ab0b | 234 | emg3_cal_size = emg1_cal.size(); |
Jellehierck | 25:a1be4cf2ab0b | 235 | } |
Jellehierck | 25:a1be4cf2ab0b | 236 | } |
Jellehierck | 7:7a088536f1c9 | 237 | } |
Jellehierck | 7:7a088536f1c9 | 238 | |
Jellehierck | 15:421d3d9c563b | 239 | /* |
Jellehierck | 25:a1be4cf2ab0b | 240 | ------ EMG CALIBRATION STATES ------ |
Jellehierck | 15:421d3d9c563b | 241 | */ |
Jellehierck | 15:421d3d9c563b | 242 | |
Jellehierck | 25:a1be4cf2ab0b | 243 | /* ALL STATES HAVE THE FOLLOWING FORM: |
Jellehierck | 25:a1be4cf2ab0b | 244 | void do_state_function() { |
Jellehierck | 25:a1be4cf2ab0b | 245 | // Entry function |
Jellehierck | 25:a1be4cf2ab0b | 246 | if ( emg_state_changed == true ) { |
Jellehierck | 25:a1be4cf2ab0b | 247 | emg_state_changed == false; |
Jellehierck | 25:a1be4cf2ab0b | 248 | // More functions |
Jellehierck | 25:a1be4cf2ab0b | 249 | } |
Jellehierck | 25:a1be4cf2ab0b | 250 | |
Jellehierck | 25:a1be4cf2ab0b | 251 | // Do stuff until end condition is met |
Jellehierck | 25:a1be4cf2ab0b | 252 | doStuff(); |
Jellehierck | 25:a1be4cf2ab0b | 253 | |
Jellehierck | 25:a1be4cf2ab0b | 254 | // State transition guard |
Jellehierck | 25:a1be4cf2ab0b | 255 | if ( endCondition == true ) { |
Jellehierck | 25:a1be4cf2ab0b | 256 | emg_curr_state == next_state; |
Jellehierck | 25:a1be4cf2ab0b | 257 | emg_state_changed == true; |
Jellehierck | 25:a1be4cf2ab0b | 258 | // More functions |
Jellehierck | 25:a1be4cf2ab0b | 259 | } |
Jellehierck | 25:a1be4cf2ab0b | 260 | } |
Jellehierck | 25:a1be4cf2ab0b | 261 | */ |
Jellehierck | 25:a1be4cf2ab0b | 262 | |
Jellehierck | 25:a1be4cf2ab0b | 263 | // Finish up calibration |
Jellehierck | 22:9079c6c0d898 | 264 | void calibrationFinished() |
Jellehierck | 7:7a088536f1c9 | 265 | { |
Jellehierck | 22:9079c6c0d898 | 266 | switch( emg_curr_state ) { |
Jellehierck | 22:9079c6c0d898 | 267 | case emg_cal_MVC: |
Jellehierck | 22:9079c6c0d898 | 268 | emg1_MVC = getMean(emg1_cal); // Store MVC globally |
Jellehierck | 23:8a0a0b959af1 | 269 | emg1_MVC_stdev = getStdev(emg1_cal, emg1_MVC); // Store MVC stdev globally |
Jellehierck | 21:e4569b47945e | 270 | |
Jellehierck | 22:9079c6c0d898 | 271 | emg2_MVC = getMean(emg2_cal); // Store MVC globally |
Jellehierck | 23:8a0a0b959af1 | 272 | emg2_MVC_stdev = getStdev(emg2_cal, emg2_MVC); // Store MVC stdev globally |
Jellehierck | 25:a1be4cf2ab0b | 273 | |
Jellehierck | 25:a1be4cf2ab0b | 274 | emg3_MVC = getMean(emg3_cal); // Store MVC globally |
Jellehierck | 25:a1be4cf2ab0b | 275 | emg3_MVC_stdev = getStdev(emg3_cal, emg3_MVC); // Store MVC stdev globally |
Jellehierck | 25:a1be4cf2ab0b | 276 | |
Jellehierck | 25:a1be4cf2ab0b | 277 | |
Jellehierck | 29:f51683a6cbbf | 278 | emg_MVC_cal_done = true; // To set up transition guard to operation mode |
Jellehierck | 22:9079c6c0d898 | 279 | break; |
Jellehierck | 22:9079c6c0d898 | 280 | case emg_cal_rest: |
Jellehierck | 23:8a0a0b959af1 | 281 | emg1_rest = getMean(emg1_cal); // Store rest EMG globally |
Jellehierck | 23:8a0a0b959af1 | 282 | emg1_rest_stdev = getStdev(emg1_cal, emg1_rest); // Store rest stdev globally |
Jellehierck | 20:0e9218673aa8 | 283 | |
Jellehierck | 23:8a0a0b959af1 | 284 | emg2_rest = getMean(emg2_cal); // Store rest EMG globally |
Jellehierck | 23:8a0a0b959af1 | 285 | emg2_rest_stdev = getStdev(emg2_cal, emg2_rest); // Store MVC stdev globally |
Jellehierck | 25:a1be4cf2ab0b | 286 | |
Jellehierck | 25:a1be4cf2ab0b | 287 | emg3_rest = getMean(emg3_cal); // Store rest EMG globally |
Jellehierck | 25:a1be4cf2ab0b | 288 | emg3_rest_stdev = getStdev(emg3_cal, emg3_rest); // Store MVC stdev globally |
Jellehierck | 25:a1be4cf2ab0b | 289 | |
Jellehierck | 25:a1be4cf2ab0b | 290 | |
Jellehierck | 29:f51683a6cbbf | 291 | emg_rest_cal_done = true; // To set up transition guard to operation mode |
Jellehierck | 22:9079c6c0d898 | 292 | break; |
Jellehierck | 22:9079c6c0d898 | 293 | } |
Jellehierck | 23:8a0a0b959af1 | 294 | vector<double>().swap(emg1_cal); // Empty vector to prevent memory overflow |
Jellehierck | 23:8a0a0b959af1 | 295 | vector<double>().swap(emg2_cal); // Empty vector to prevent memory overflow |
Jellehierck | 25:a1be4cf2ab0b | 296 | vector<double>().swap(emg3_cal); // Empty vector to prevent memory overflow |
Jellehierck | 25:a1be4cf2ab0b | 297 | } |
Jellehierck | 25:a1be4cf2ab0b | 298 | |
Jellehierck | 25:a1be4cf2ab0b | 299 | // EMG Waiting state |
Jellehierck | 25:a1be4cf2ab0b | 300 | void do_emg_wait() |
Jellehierck | 25:a1be4cf2ab0b | 301 | { |
Jellehierck | 25:a1be4cf2ab0b | 302 | // Entry function |
Jellehierck | 25:a1be4cf2ab0b | 303 | if ( emg_state_changed == true ) { |
Jellehierck | 25:a1be4cf2ab0b | 304 | emg_state_changed = false; |
Jellehierck | 25:a1be4cf2ab0b | 305 | } |
Jellehierck | 25:a1be4cf2ab0b | 306 | |
Jellehierck | 27:f18da01093c9 | 307 | // Do nothing until end condition is met |
Jellehierck | 25:a1be4cf2ab0b | 308 | |
Jellehierck | 29:f51683a6cbbf | 309 | // State transition guard. Possible next states: |
Jellehierck | 29:f51683a6cbbf | 310 | // 1. emg_cal_MVC (button1 pressed) |
Jellehierck | 29:f51683a6cbbf | 311 | // 2. emg_cal_rest (button2 pressed) |
Jellehierck | 29:f51683a6cbbf | 312 | // 3. emg_operation (both calibrations have run) |
Jellehierck | 25:a1be4cf2ab0b | 313 | if ( button1_pressed ) { |
Jellehierck | 29:f51683a6cbbf | 314 | button1_pressed = false; // Disable button pressed function until next button press |
Jellehierck | 29:f51683a6cbbf | 315 | emg_curr_state = emg_cal_MVC; // Set next state |
Jellehierck | 29:f51683a6cbbf | 316 | emg_state_changed = true; // Enable entry functions |
Jellehierck | 29:f51683a6cbbf | 317 | |
Jellehierck | 25:a1be4cf2ab0b | 318 | } else if ( button2_pressed ) { |
Jellehierck | 29:f51683a6cbbf | 319 | button2_pressed = false; // Disable button pressed function until next button press |
Jellehierck | 29:f51683a6cbbf | 320 | emg_curr_state = emg_cal_rest; // Set next state |
Jellehierck | 29:f51683a6cbbf | 321 | emg_state_changed = true; // Enable entry functions |
Jellehierck | 29:f51683a6cbbf | 322 | |
Jellehierck | 25:a1be4cf2ab0b | 323 | } else if ( emg_MVC_cal_done && emg_rest_cal_done ) { |
Jellehierck | 29:f51683a6cbbf | 324 | emg_curr_state = emg_operation; // Set next state |
Jellehierck | 29:f51683a6cbbf | 325 | emg_state_changed = true; // Enable entry functions |
Jellehierck | 25:a1be4cf2ab0b | 326 | } |
Jellehierck | 7:7a088536f1c9 | 327 | } |
Jellehierck | 7:7a088536f1c9 | 328 | |
Jellehierck | 21:e4569b47945e | 329 | // Run calibration of EMG |
Jellehierck | 21:e4569b47945e | 330 | void do_emg_cal() |
Jellehierck | 21:e4569b47945e | 331 | { |
Jellehierck | 28:59e8266f4633 | 332 | // Entry functions |
Jellehierck | 22:9079c6c0d898 | 333 | if ( emg_state_changed == true ) { |
Jellehierck | 28:59e8266f4633 | 334 | emg_state_changed = false; // Disable entry functions |
Jellehierck | 21:e4569b47945e | 335 | led_b = 0; // Turn on calibration led |
Jellehierck | 28:59e8266f4633 | 336 | |
Jellehierck | 22:9079c6c0d898 | 337 | timerCalibration.reset(); |
Jellehierck | 28:59e8266f4633 | 338 | timerCalibration.start(); // Sets up timer to stop calibration after Tcal seconds |
Jellehierck | 25:a1be4cf2ab0b | 339 | sampleNow = true; // Enable signal sampling in sampleSignal() |
Jellehierck | 28:59e8266f4633 | 340 | calibrateNow = true; // Enable calibration vector functionality in sampleSignal() |
Jellehierck | 26:7e81c7db6e7a | 341 | |
Jellehierck | 28:59e8266f4633 | 342 | emg1_cal.reserve(Fs * Tcal); // Initialize vector lengths to prevent memory overflow |
Jellehierck | 25:a1be4cf2ab0b | 343 | emg2_cal.reserve(Fs * Tcal); |
Jellehierck | 25:a1be4cf2ab0b | 344 | emg3_cal.reserve(Fs * Tcal); |
Jellehierck | 22:9079c6c0d898 | 345 | } |
Jellehierck | 7:7a088536f1c9 | 346 | |
Jellehierck | 29:f51683a6cbbf | 347 | // Do nothing until end condition is met |
Jellehierck | 29:f51683a6cbbf | 348 | |
Jellehierck | 29:f51683a6cbbf | 349 | // State transition guard |
Jellehierck | 24:540c284e881d | 350 | if ( timerCalibration.read() >= Tcal ) { // After interval Tcal the calibration step is finished |
Jellehierck | 25:a1be4cf2ab0b | 351 | sampleNow = false; // Disable signal sampling in sampleSignal() |
Jellehierck | 25:a1be4cf2ab0b | 352 | calibrateNow = false; // Disable calibration sampling |
Jellehierck | 23:8a0a0b959af1 | 353 | |
Jellehierck | 23:8a0a0b959af1 | 354 | calibrationFinished(); // Process calibration data |
Jellehierck | 23:8a0a0b959af1 | 355 | led_b = 1; // Turn off calibration led |
Jellehierck | 23:8a0a0b959af1 | 356 | |
Jellehierck | 25:a1be4cf2ab0b | 357 | emg_curr_state = emg_wait; // Set next state |
Jellehierck | 25:a1be4cf2ab0b | 358 | emg_state_changed = true; // State has changed (to run |
Jellehierck | 25:a1be4cf2ab0b | 359 | } |
Jellehierck | 25:a1be4cf2ab0b | 360 | } |
Jellehierck | 23:8a0a0b959af1 | 361 | |
Jellehierck | 26:7e81c7db6e7a | 362 | void do_emg_operation() |
Jellehierck | 26:7e81c7db6e7a | 363 | { |
Jellehierck | 25:a1be4cf2ab0b | 364 | // Entry function |
Jellehierck | 25:a1be4cf2ab0b | 365 | if ( emg_state_changed == true ) { |
Jellehierck | 28:59e8266f4633 | 366 | emg_state_changed = false; // Disable entry functions |
Jellehierck | 26:7e81c7db6e7a | 367 | double margin_percentage = 10; // Set up % margin for rest |
Jellehierck | 28:59e8266f4633 | 368 | |
Jellehierck | 26:7e81c7db6e7a | 369 | emg1_factor = 1 / emg1_MVC; // Factor to normalize MVC |
Jellehierck | 26:7e81c7db6e7a | 370 | emg1_th = emg1_rest * emg1_factor + margin_percentage/100; // Set normalized rest threshold |
Jellehierck | 26:7e81c7db6e7a | 371 | emg2_factor = 1 / emg2_MVC; // Factor to normalize MVC |
Jellehierck | 26:7e81c7db6e7a | 372 | emg2_th = emg2_rest * emg2_factor + margin_percentage/100; // Set normalized rest threshold |
Jellehierck | 26:7e81c7db6e7a | 373 | emg3_factor = 1 / emg3_MVC; // Factor to normalize MVC |
Jellehierck | 26:7e81c7db6e7a | 374 | emg3_th = emg3_rest * emg3_factor + margin_percentage/100; // Set normalized rest threshold |
Jellehierck | 25:a1be4cf2ab0b | 375 | } |
Jellehierck | 25:a1be4cf2ab0b | 376 | |
Jellehierck | 25:a1be4cf2ab0b | 377 | // Do stuff until end condition is met |
Jellehierck | 28:59e8266f4633 | 378 | double emg1_norm = emg1_env * emg1_factor; // Normalize EMG signal with calibrated factor |
Jellehierck | 29:f51683a6cbbf | 379 | double emg2_norm = emg2_env * emg2_factor; // Idem |
Jellehierck | 29:f51683a6cbbf | 380 | double emg3_norm = emg3_env * emg3_factor; // Idem |
Jellehierck | 28:59e8266f4633 | 381 | |
Jellehierck | 28:59e8266f4633 | 382 | // Set normalized EMG output signal (CAN BE MOVED TO EXTERNAL FUNCTION BECAUSE IT IS REPEATED 3 TIMES) |
Jellehierck | 28:59e8266f4633 | 383 | if ( emg1_norm < emg1_th ) { // If below threshold, emg_out = 0 (ignored) |
Jellehierck | 26:7e81c7db6e7a | 384 | emg1_out = 0.0; |
Jellehierck | 28:59e8266f4633 | 385 | } else if ( emg1_norm > 1.0f ) { // If above MVC (due to filtering perhaps), emg_out = 1 (max value) |
Jellehierck | 26:7e81c7db6e7a | 386 | emg1_out = 1.0; |
Jellehierck | 28:59e8266f4633 | 387 | } else { // If in between threshold and MVC, scale EMG signal accordingly |
Jellehierck | 28:59e8266f4633 | 388 | // Inputs may be in range [emg_th, 1] |
Jellehierck | 28:59e8266f4633 | 389 | // Outputs are scaled to range [0, 1] |
Jellehierck | 26:7e81c7db6e7a | 390 | emg1_out = rescale(emg1_norm, emg1_th, 1, 0, 1); |
Jellehierck | 26:7e81c7db6e7a | 391 | } |
Jellehierck | 28:59e8266f4633 | 392 | |
Jellehierck | 28:59e8266f4633 | 393 | // Idem for emg2 |
Jellehierck | 26:7e81c7db6e7a | 394 | if ( emg2_norm < emg2_th ) { |
Jellehierck | 26:7e81c7db6e7a | 395 | emg2_out = 0.0; |
Jellehierck | 26:7e81c7db6e7a | 396 | } else if ( emg2_norm > 1.0f ) { |
Jellehierck | 26:7e81c7db6e7a | 397 | emg2_out = 1.0; |
Jellehierck | 26:7e81c7db6e7a | 398 | } else { |
Jellehierck | 26:7e81c7db6e7a | 399 | emg2_out = rescale(emg2_norm, emg2_th, 1, 0, 1); |
Jellehierck | 26:7e81c7db6e7a | 400 | } |
Jellehierck | 28:59e8266f4633 | 401 | |
Jellehierck | 28:59e8266f4633 | 402 | // Idem for emg3 |
Jellehierck | 26:7e81c7db6e7a | 403 | if ( emg3_norm < emg3_th ) { |
Jellehierck | 26:7e81c7db6e7a | 404 | emg3_out = 0.0; |
Jellehierck | 26:7e81c7db6e7a | 405 | } else if ( emg3_norm > 1.0f ) { |
Jellehierck | 26:7e81c7db6e7a | 406 | emg3_out = 1.0; |
Jellehierck | 26:7e81c7db6e7a | 407 | } else { |
Jellehierck | 26:7e81c7db6e7a | 408 | emg3_out = rescale(emg3_norm, emg3_th, 1, 0, 1); |
Jellehierck | 26:7e81c7db6e7a | 409 | } |
Jellehierck | 25:a1be4cf2ab0b | 410 | |
Jellehierck | 28:59e8266f4633 | 411 | // Set HIDScope outputs |
Jellehierck | 28:59e8266f4633 | 412 | scope.set(0, emg1_env ); // One envelope for reference |
Jellehierck | 28:59e8266f4633 | 413 | scope.set(1, emg1_out ); |
Jellehierck | 28:59e8266f4633 | 414 | scope.set(2, emg2_out ); |
Jellehierck | 28:59e8266f4633 | 415 | scope.set(3, emg3_out ); |
Jellehierck | 28:59e8266f4633 | 416 | scope.send(); |
Jellehierck | 28:59e8266f4633 | 417 | |
Jellehierck | 28:59e8266f4633 | 418 | |
Jellehierck | 25:a1be4cf2ab0b | 419 | // State transition guard |
Jellehierck | 26:7e81c7db6e7a | 420 | if ( false ) { |
Jellehierck | 28:59e8266f4633 | 421 | emg_curr_state = emg_wait; // Set next state |
Jellehierck | 28:59e8266f4633 | 422 | emg_state_changed = true; // Enable entry function |
Jellehierck | 23:8a0a0b959af1 | 423 | } |
Jellehierck | 23:8a0a0b959af1 | 424 | } |
Jellehierck | 23:8a0a0b959af1 | 425 | |
Jellehierck | 23:8a0a0b959af1 | 426 | /* |
Jellehierck | 23:8a0a0b959af1 | 427 | ------ EMG SUBSTATE MACHINE ------ |
Jellehierck | 23:8a0a0b959af1 | 428 | */ |
Jellehierck | 23:8a0a0b959af1 | 429 | void emg_state_machine() |
Jellehierck | 23:8a0a0b959af1 | 430 | { |
Jellehierck | 23:8a0a0b959af1 | 431 | switch(emg_curr_state) { |
Jellehierck | 23:8a0a0b959af1 | 432 | case emg_wait: |
Jellehierck | 25:a1be4cf2ab0b | 433 | do_emg_wait(); |
Jellehierck | 23:8a0a0b959af1 | 434 | break; |
Jellehierck | 23:8a0a0b959af1 | 435 | case emg_cal_MVC: |
Jellehierck | 23:8a0a0b959af1 | 436 | do_emg_cal(); |
Jellehierck | 23:8a0a0b959af1 | 437 | break; |
Jellehierck | 23:8a0a0b959af1 | 438 | case emg_cal_rest: |
Jellehierck | 23:8a0a0b959af1 | 439 | do_emg_cal(); |
Jellehierck | 23:8a0a0b959af1 | 440 | break; |
Jellehierck | 23:8a0a0b959af1 | 441 | case emg_operation: |
Jellehierck | 26:7e81c7db6e7a | 442 | do_emg_operation(); |
Jellehierck | 23:8a0a0b959af1 | 443 | break; |
Jellehierck | 23:8a0a0b959af1 | 444 | } |
Jellehierck | 23:8a0a0b959af1 | 445 | } |
Jellehierck | 23:8a0a0b959af1 | 446 | |
Jellehierck | 25:a1be4cf2ab0b | 447 | // Global loop of program |
Jellehierck | 25:a1be4cf2ab0b | 448 | void tickGlobalFunc() |
Jellehierck | 25:a1be4cf2ab0b | 449 | { |
Jellehierck | 25:a1be4cf2ab0b | 450 | sampleSignal(); |
Jellehierck | 25:a1be4cf2ab0b | 451 | emg_state_machine(); |
Jellehierck | 25:a1be4cf2ab0b | 452 | // controller(); |
Jellehierck | 25:a1be4cf2ab0b | 453 | // outputToMotors(); |
Jellehierck | 25:a1be4cf2ab0b | 454 | } |
Jellehierck | 25:a1be4cf2ab0b | 455 | |
Jellehierck | 23:8a0a0b959af1 | 456 | void main() |
Jellehierck | 23:8a0a0b959af1 | 457 | { |
Jellehierck | 23:8a0a0b959af1 | 458 | pc.baud(115200); // MODSERIAL rate |
Jellehierck | 23:8a0a0b959af1 | 459 | pc.printf("Starting\r\n"); |
Jellehierck | 23:8a0a0b959af1 | 460 | |
Jellehierck | 23:8a0a0b959af1 | 461 | // tickSample.attach(&sample, Ts); // Initialize sample ticker |
Jellehierck | 23:8a0a0b959af1 | 462 | |
Jellehierck | 23:8a0a0b959af1 | 463 | // Create BQ chains to reduce computations |
Jellehierck | 23:8a0a0b959af1 | 464 | bqc1_notch.add( &bq1_notch ); |
Jellehierck | 23:8a0a0b959af1 | 465 | bqc1_high.add( &bq1_H1 ).add( &bq1_H2 ); |
Jellehierck | 23:8a0a0b959af1 | 466 | bqc1_low.add( &bq1_L1 ).add( &bq1_L2 ); |
Jellehierck | 23:8a0a0b959af1 | 467 | |
Jellehierck | 23:8a0a0b959af1 | 468 | bqc2_notch.add( &bq2_notch ); |
Jellehierck | 23:8a0a0b959af1 | 469 | bqc2_high.add( &bq2_H1 ).add( &bq2_H2 ); |
Jellehierck | 23:8a0a0b959af1 | 470 | bqc2_low.add( &bq2_L1 ).add( &bq2_L2 ); |
Jellehierck | 23:8a0a0b959af1 | 471 | |
Jellehierck | 23:8a0a0b959af1 | 472 | bqc3_notch.add( &bq3_notch ); |
Jellehierck | 23:8a0a0b959af1 | 473 | bqc3_high.add( &bq3_H1 ).add( &bq3_H2 ); |
Jellehierck | 23:8a0a0b959af1 | 474 | bqc3_low.add( &bq3_L1 ).add( &bq3_L2 ); |
Jellehierck | 23:8a0a0b959af1 | 475 | |
Jellehierck | 23:8a0a0b959af1 | 476 | led_b = 1; // Turn blue led off at startup |
Jellehierck | 23:8a0a0b959af1 | 477 | led_g = 1; // Turn green led off at startup |
Jellehierck | 23:8a0a0b959af1 | 478 | led_r = 1; // Turn red led off at startup |
Jellehierck | 23:8a0a0b959af1 | 479 | |
Jellehierck | 23:8a0a0b959af1 | 480 | // If any filter chain is unstable, red led will light up |
Jellehierck | 25:a1be4cf2ab0b | 481 | if (checkBQChainStable()) { |
Jellehierck | 23:8a0a0b959af1 | 482 | led_r = 1; // LED off |
Jellehierck | 23:8a0a0b959af1 | 483 | } else { |
Jellehierck | 23:8a0a0b959af1 | 484 | led_r = 0; // LED on |
Jellehierck | 6:5437cc97e1e6 | 485 | } |
Jellehierck | 29:f51683a6cbbf | 486 | emg_curr_state = emg_wait; // Start off in EMG Wait state |
Jellehierck | 29:f51683a6cbbf | 487 | button1.fall( &button1Press ); // Change to state MVC calibration on button press |
Jellehierck | 29:f51683a6cbbf | 488 | button2.fall( &button2Press ); // Change to state rest calibration on button press |
Jellehierck | 6:5437cc97e1e6 | 489 | |
Jellehierck | 25:a1be4cf2ab0b | 490 | tickGlobal.attach( &tickGlobalFunc, Ts ); |
Jellehierck | 25:a1be4cf2ab0b | 491 | |
Jellehierck | 29:f51683a6cbbf | 492 | |
Jellehierck | 8:ea3de43c9e8b | 493 | |
Jellehierck | 23:8a0a0b959af1 | 494 | while(true) { |
Jellehierck | 7:7a088536f1c9 | 495 | |
Jellehierck | 23:8a0a0b959af1 | 496 | // Show that system is running |
Jellehierck | 23:8a0a0b959af1 | 497 | // led_g = !led_g; |
Jellehierck | 25:a1be4cf2ab0b | 498 | pc.printf("currentState: %i vectors size: %i %i %i\r\n", emg_curr_state, emg1_cal_size, emg2_cal_size, emg3_cal_size); |
Jellehierck | 23:8a0a0b959af1 | 499 | wait(1.0f); |
Jellehierck | 23:8a0a0b959af1 | 500 | } |
Jellehierck | 23:8a0a0b959af1 | 501 | } |