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