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