![](/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@26:7e81c7db6e7a, 2019-10-25 (annotated)
- Committer:
- Jellehierck
- Date:
- Fri Oct 25 14:32:35 2019 +0000
- Revision:
- 26:7e81c7db6e7a
- Parent:
- 25:a1be4cf2ab0b
- Child:
- 27:f18da01093c9
Maybe working version of makeScale and EMG operation mode in state machine. Needs testing
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 | 19:94dc52f8a59e | 183 | bool n_stable = bqc1_notch.stable(); |
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 | 25:a1be4cf2ab0b | 199 | if (sampleNow == true) { |
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 | 25:a1be4cf2ab0b | 221 | scope.set(0, emg1_n); |
Jellehierck | 25:a1be4cf2ab0b | 222 | scope.set(1, emg2_n); |
Jellehierck | 20:0e9218673aa8 | 223 | |
Jellehierck | 25:a1be4cf2ab0b | 224 | scope.set(2, emg1_env ); |
Jellehierck | 25:a1be4cf2ab0b | 225 | scope.set(3, emg2_env ); |
Jellehierck | 25:a1be4cf2ab0b | 226 | scope.send(); |
Jellehierck | 26:7e81c7db6e7a | 227 | |
Jellehierck | 25:a1be4cf2ab0b | 228 | if (calibrateNow) { |
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 | 25:a1be4cf2ab0b | 278 | emg_MVC_cal_done = true; |
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 | 25:a1be4cf2ab0b | 291 | emg_rest_cal_done = true; |
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 | // pc.printf("State: emg_wait\r\n"); |
Jellehierck | 25:a1be4cf2ab0b | 306 | } |
Jellehierck | 25:a1be4cf2ab0b | 307 | |
Jellehierck | 25:a1be4cf2ab0b | 308 | // Do stuff until end condition is met |
Jellehierck | 25:a1be4cf2ab0b | 309 | |
Jellehierck | 25:a1be4cf2ab0b | 310 | // State transition guard |
Jellehierck | 25:a1be4cf2ab0b | 311 | if ( button1_pressed ) { |
Jellehierck | 25:a1be4cf2ab0b | 312 | button1_pressed = false; |
Jellehierck | 25:a1be4cf2ab0b | 313 | emg_curr_state = emg_cal_MVC; |
Jellehierck | 25:a1be4cf2ab0b | 314 | emg_state_changed = true; |
Jellehierck | 25:a1be4cf2ab0b | 315 | } else if ( button2_pressed ) { |
Jellehierck | 25:a1be4cf2ab0b | 316 | button2_pressed = false; |
Jellehierck | 25:a1be4cf2ab0b | 317 | emg_curr_state = emg_cal_rest; |
Jellehierck | 25:a1be4cf2ab0b | 318 | emg_state_changed = true; |
Jellehierck | 25:a1be4cf2ab0b | 319 | } else if ( emg_MVC_cal_done && emg_rest_cal_done ) { |
Jellehierck | 26:7e81c7db6e7a | 320 | emg_curr_state = emg_operation; |
Jellehierck | 25:a1be4cf2ab0b | 321 | emg_state_changed = true; |
Jellehierck | 25:a1be4cf2ab0b | 322 | } |
Jellehierck | 7:7a088536f1c9 | 323 | } |
Jellehierck | 7:7a088536f1c9 | 324 | |
Jellehierck | 21:e4569b47945e | 325 | // Run calibration of EMG |
Jellehierck | 21:e4569b47945e | 326 | void do_emg_cal() |
Jellehierck | 21:e4569b47945e | 327 | { |
Jellehierck | 22:9079c6c0d898 | 328 | if ( emg_state_changed == true ) { |
Jellehierck | 25:a1be4cf2ab0b | 329 | emg_state_changed = false; |
Jellehierck | 25:a1be4cf2ab0b | 330 | // pc.printf("Starting calibration :"); |
Jellehierck | 21:e4569b47945e | 331 | led_b = 0; // Turn on calibration led |
Jellehierck | 22:9079c6c0d898 | 332 | timerCalibration.reset(); |
Jellehierck | 22:9079c6c0d898 | 333 | timerCalibration.start(); |
Jellehierck | 25:a1be4cf2ab0b | 334 | sampleNow = true; // Enable signal sampling in sampleSignal() |
Jellehierck | 25:a1be4cf2ab0b | 335 | calibrateNow = true; |
Jellehierck | 26:7e81c7db6e7a | 336 | |
Jellehierck | 25:a1be4cf2ab0b | 337 | emg1_cal.reserve(Fs * Tcal); |
Jellehierck | 25:a1be4cf2ab0b | 338 | emg2_cal.reserve(Fs * Tcal); |
Jellehierck | 25:a1be4cf2ab0b | 339 | emg3_cal.reserve(Fs * Tcal); |
Jellehierck | 22:9079c6c0d898 | 340 | |
Jellehierck | 22:9079c6c0d898 | 341 | switch( emg_curr_state ) { |
Jellehierck | 21:e4569b47945e | 342 | case emg_cal_MVC: |
Jellehierck | 25:a1be4cf2ab0b | 343 | // pc.printf("MVC\r\n"); |
Jellehierck | 21:e4569b47945e | 344 | break; |
Jellehierck | 21:e4569b47945e | 345 | case emg_cal_rest: |
Jellehierck | 25:a1be4cf2ab0b | 346 | // pc.printf("Rest\r\n"); |
Jellehierck | 21:e4569b47945e | 347 | break; |
Jellehierck | 21:e4569b47945e | 348 | } |
Jellehierck | 22:9079c6c0d898 | 349 | } |
Jellehierck | 7:7a088536f1c9 | 350 | |
Jellehierck | 23:8a0a0b959af1 | 351 | // Allemaal dingen doen tot de end conditions true zijn |
Jellehierck | 23:8a0a0b959af1 | 352 | |
Jellehierck | 24:540c284e881d | 353 | if ( timerCalibration.read() >= Tcal ) { // After interval Tcal the calibration step is finished |
Jellehierck | 25:a1be4cf2ab0b | 354 | sampleNow = false; // Disable signal sampling in sampleSignal() |
Jellehierck | 25:a1be4cf2ab0b | 355 | calibrateNow = false; // Disable calibration sampling |
Jellehierck | 23:8a0a0b959af1 | 356 | |
Jellehierck | 23:8a0a0b959af1 | 357 | calibrationFinished(); // Process calibration data |
Jellehierck | 23:8a0a0b959af1 | 358 | led_b = 1; // Turn off calibration led |
Jellehierck | 23:8a0a0b959af1 | 359 | |
Jellehierck | 25:a1be4cf2ab0b | 360 | emg_curr_state = emg_wait; // Set next state |
Jellehierck | 25:a1be4cf2ab0b | 361 | emg_state_changed = true; // State has changed (to run |
Jellehierck | 25:a1be4cf2ab0b | 362 | |
Jellehierck | 25:a1be4cf2ab0b | 363 | // pc.printf("Calibration step finished"); |
Jellehierck | 25:a1be4cf2ab0b | 364 | } |
Jellehierck | 25:a1be4cf2ab0b | 365 | } |
Jellehierck | 23:8a0a0b959af1 | 366 | |
Jellehierck | 26:7e81c7db6e7a | 367 | void do_emg_operation() |
Jellehierck | 26:7e81c7db6e7a | 368 | { |
Jellehierck | 25:a1be4cf2ab0b | 369 | // Entry function |
Jellehierck | 25:a1be4cf2ab0b | 370 | if ( emg_state_changed == true ) { |
Jellehierck | 26:7e81c7db6e7a | 371 | emg_state_changed = false; |
Jellehierck | 26:7e81c7db6e7a | 372 | double margin_percentage = 10; // Set up % margin for rest |
Jellehierck | 26:7e81c7db6e7a | 373 | |
Jellehierck | 26:7e81c7db6e7a | 374 | emg1_factor = 1 / emg1_MVC; // Factor to normalize MVC |
Jellehierck | 26:7e81c7db6e7a | 375 | emg1_th = emg1_rest * emg1_factor + margin_percentage/100; // Set normalized rest threshold |
Jellehierck | 26:7e81c7db6e7a | 376 | emg2_factor = 1 / emg2_MVC; // Factor to normalize MVC |
Jellehierck | 26:7e81c7db6e7a | 377 | emg2_th = emg2_rest * emg2_factor + margin_percentage/100; // Set normalized rest threshold |
Jellehierck | 26:7e81c7db6e7a | 378 | emg3_factor = 1 / emg3_MVC; // Factor to normalize MVC |
Jellehierck | 26:7e81c7db6e7a | 379 | emg3_th = emg3_rest * emg3_factor + margin_percentage/100; // Set normalized rest threshold |
Jellehierck | 25:a1be4cf2ab0b | 380 | } |
Jellehierck | 25:a1be4cf2ab0b | 381 | |
Jellehierck | 25:a1be4cf2ab0b | 382 | // Do stuff until end condition is met |
Jellehierck | 26:7e81c7db6e7a | 383 | |
Jellehierck | 26:7e81c7db6e7a | 384 | double emg1_norm = emg1_env * emg1_factor; |
Jellehierck | 26:7e81c7db6e7a | 385 | double emg2_norm = emg2_env * emg2_factor; |
Jellehierck | 26:7e81c7db6e7a | 386 | double emg3_norm = emg3_env * emg3_factor; |
Jellehierck | 26:7e81c7db6e7a | 387 | |
Jellehierck | 26:7e81c7db6e7a | 388 | if ( emg1_norm < emg1_th ) { |
Jellehierck | 26:7e81c7db6e7a | 389 | emg1_out = 0.0; |
Jellehierck | 26:7e81c7db6e7a | 390 | } else if ( emg1_norm > 1.0f ) { |
Jellehierck | 26:7e81c7db6e7a | 391 | emg1_out = 1.0; |
Jellehierck | 26:7e81c7db6e7a | 392 | } else { |
Jellehierck | 26:7e81c7db6e7a | 393 | emg1_out = rescale(emg1_norm, emg1_th, 1, 0, 1); |
Jellehierck | 26:7e81c7db6e7a | 394 | } |
Jellehierck | 26:7e81c7db6e7a | 395 | |
Jellehierck | 26:7e81c7db6e7a | 396 | if ( emg2_norm < emg2_th ) { |
Jellehierck | 26:7e81c7db6e7a | 397 | emg2_out = 0.0; |
Jellehierck | 26:7e81c7db6e7a | 398 | } else if ( emg2_norm > 1.0f ) { |
Jellehierck | 26:7e81c7db6e7a | 399 | emg2_out = 1.0; |
Jellehierck | 26:7e81c7db6e7a | 400 | } else { |
Jellehierck | 26:7e81c7db6e7a | 401 | emg2_out = rescale(emg2_norm, emg2_th, 1, 0, 1); |
Jellehierck | 26:7e81c7db6e7a | 402 | } |
Jellehierck | 26:7e81c7db6e7a | 403 | |
Jellehierck | 26:7e81c7db6e7a | 404 | if ( emg3_norm < emg3_th ) { |
Jellehierck | 26:7e81c7db6e7a | 405 | emg3_out = 0.0; |
Jellehierck | 26:7e81c7db6e7a | 406 | } else if ( emg3_norm > 1.0f ) { |
Jellehierck | 26:7e81c7db6e7a | 407 | emg3_out = 1.0; |
Jellehierck | 26:7e81c7db6e7a | 408 | } else { |
Jellehierck | 26:7e81c7db6e7a | 409 | emg3_out = rescale(emg3_norm, emg3_th, 1, 0, 1); |
Jellehierck | 26:7e81c7db6e7a | 410 | } |
Jellehierck | 25:a1be4cf2ab0b | 411 | |
Jellehierck | 25:a1be4cf2ab0b | 412 | // State transition guard |
Jellehierck | 26:7e81c7db6e7a | 413 | if ( false ) { |
Jellehierck | 26:7e81c7db6e7a | 414 | emg_curr_state = emg_wait; |
Jellehierck | 26:7e81c7db6e7a | 415 | emg_state_changed = true; |
Jellehierck | 25:a1be4cf2ab0b | 416 | // More functions |
Jellehierck | 23:8a0a0b959af1 | 417 | } |
Jellehierck | 23:8a0a0b959af1 | 418 | } |
Jellehierck | 23:8a0a0b959af1 | 419 | |
Jellehierck | 23:8a0a0b959af1 | 420 | /* |
Jellehierck | 23:8a0a0b959af1 | 421 | ------ EMG SUBSTATE MACHINE ------ |
Jellehierck | 23:8a0a0b959af1 | 422 | */ |
Jellehierck | 23:8a0a0b959af1 | 423 | void emg_state_machine() |
Jellehierck | 23:8a0a0b959af1 | 424 | { |
Jellehierck | 23:8a0a0b959af1 | 425 | switch(emg_curr_state) { |
Jellehierck | 23:8a0a0b959af1 | 426 | case emg_wait: |
Jellehierck | 25:a1be4cf2ab0b | 427 | do_emg_wait(); |
Jellehierck | 23:8a0a0b959af1 | 428 | break; |
Jellehierck | 23:8a0a0b959af1 | 429 | case emg_cal_MVC: |
Jellehierck | 23:8a0a0b959af1 | 430 | do_emg_cal(); |
Jellehierck | 23:8a0a0b959af1 | 431 | break; |
Jellehierck | 23:8a0a0b959af1 | 432 | case emg_cal_rest: |
Jellehierck | 23:8a0a0b959af1 | 433 | do_emg_cal(); |
Jellehierck | 23:8a0a0b959af1 | 434 | break; |
Jellehierck | 23:8a0a0b959af1 | 435 | case emg_operation: |
Jellehierck | 26:7e81c7db6e7a | 436 | do_emg_operation(); |
Jellehierck | 23:8a0a0b959af1 | 437 | break; |
Jellehierck | 23:8a0a0b959af1 | 438 | } |
Jellehierck | 23:8a0a0b959af1 | 439 | } |
Jellehierck | 23:8a0a0b959af1 | 440 | |
Jellehierck | 25:a1be4cf2ab0b | 441 | // Global loop of program |
Jellehierck | 25:a1be4cf2ab0b | 442 | void tickGlobalFunc() |
Jellehierck | 25:a1be4cf2ab0b | 443 | { |
Jellehierck | 25:a1be4cf2ab0b | 444 | sampleSignal(); |
Jellehierck | 25:a1be4cf2ab0b | 445 | emg_state_machine(); |
Jellehierck | 25:a1be4cf2ab0b | 446 | // controller(); |
Jellehierck | 25:a1be4cf2ab0b | 447 | // outputToMotors(); |
Jellehierck | 25:a1be4cf2ab0b | 448 | } |
Jellehierck | 25:a1be4cf2ab0b | 449 | |
Jellehierck | 23:8a0a0b959af1 | 450 | void main() |
Jellehierck | 23:8a0a0b959af1 | 451 | { |
Jellehierck | 23:8a0a0b959af1 | 452 | pc.baud(115200); // MODSERIAL rate |
Jellehierck | 23:8a0a0b959af1 | 453 | pc.printf("Starting\r\n"); |
Jellehierck | 23:8a0a0b959af1 | 454 | |
Jellehierck | 23:8a0a0b959af1 | 455 | // tickSample.attach(&sample, Ts); // Initialize sample ticker |
Jellehierck | 23:8a0a0b959af1 | 456 | |
Jellehierck | 23:8a0a0b959af1 | 457 | // Create BQ chains to reduce computations |
Jellehierck | 23:8a0a0b959af1 | 458 | bqc1_notch.add( &bq1_notch ); |
Jellehierck | 23:8a0a0b959af1 | 459 | bqc1_high.add( &bq1_H1 ).add( &bq1_H2 ); |
Jellehierck | 23:8a0a0b959af1 | 460 | bqc1_low.add( &bq1_L1 ).add( &bq1_L2 ); |
Jellehierck | 23:8a0a0b959af1 | 461 | |
Jellehierck | 23:8a0a0b959af1 | 462 | bqc2_notch.add( &bq2_notch ); |
Jellehierck | 23:8a0a0b959af1 | 463 | bqc2_high.add( &bq2_H1 ).add( &bq2_H2 ); |
Jellehierck | 23:8a0a0b959af1 | 464 | bqc2_low.add( &bq2_L1 ).add( &bq2_L2 ); |
Jellehierck | 23:8a0a0b959af1 | 465 | |
Jellehierck | 23:8a0a0b959af1 | 466 | bqc3_notch.add( &bq3_notch ); |
Jellehierck | 23:8a0a0b959af1 | 467 | bqc3_high.add( &bq3_H1 ).add( &bq3_H2 ); |
Jellehierck | 23:8a0a0b959af1 | 468 | bqc3_low.add( &bq3_L1 ).add( &bq3_L2 ); |
Jellehierck | 23:8a0a0b959af1 | 469 | |
Jellehierck | 23:8a0a0b959af1 | 470 | led_b = 1; // Turn blue led off at startup |
Jellehierck | 23:8a0a0b959af1 | 471 | led_g = 1; // Turn green led off at startup |
Jellehierck | 23:8a0a0b959af1 | 472 | led_r = 1; // Turn red led off at startup |
Jellehierck | 23:8a0a0b959af1 | 473 | |
Jellehierck | 23:8a0a0b959af1 | 474 | // If any filter chain is unstable, red led will light up |
Jellehierck | 25:a1be4cf2ab0b | 475 | if (checkBQChainStable()) { |
Jellehierck | 23:8a0a0b959af1 | 476 | led_r = 1; // LED off |
Jellehierck | 23:8a0a0b959af1 | 477 | } else { |
Jellehierck | 23:8a0a0b959af1 | 478 | led_r = 0; // LED on |
Jellehierck | 6:5437cc97e1e6 | 479 | } |
Jellehierck | 25:a1be4cf2ab0b | 480 | emg_curr_state = emg_wait; |
Jellehierck | 6:5437cc97e1e6 | 481 | |
Jellehierck | 25:a1be4cf2ab0b | 482 | tickGlobal.attach( &tickGlobalFunc, Ts ); |
Jellehierck | 25:a1be4cf2ab0b | 483 | |
Jellehierck | 25:a1be4cf2ab0b | 484 | button1.fall( &button1Press ); // Run MVC calibration on button press |
Jellehierck | 25:a1be4cf2ab0b | 485 | button2.fall( &button2Press ); // Run rest calibration on button press |
Jellehierck | 8:ea3de43c9e8b | 486 | |
Jellehierck | 23:8a0a0b959af1 | 487 | while(true) { |
Jellehierck | 7:7a088536f1c9 | 488 | |
Jellehierck | 23:8a0a0b959af1 | 489 | // Show that system is running |
Jellehierck | 23:8a0a0b959af1 | 490 | // led_g = !led_g; |
Jellehierck | 25:a1be4cf2ab0b | 491 | 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 | 492 | wait(1.0f); |
Jellehierck | 23:8a0a0b959af1 | 493 | } |
Jellehierck | 23:8a0a0b959af1 | 494 | } |