![](/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@12:70f0710400c2, 2019-10-21 (annotated)
- Committer:
- Jellehierck
- Date:
- Mon Oct 21 14:36:24 2019 +0000
- Revision:
- 12:70f0710400c2
- Parent:
- 11:042170a9b93a
- Child:
- 13:2724d2e747f1
- Child:
- 15:421d3d9c563b
EMG Calibration (almost) finished, working on making scale factors
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 | 2:d3e9788ab1b3 | 13 | // PC serial connection |
Jellehierck | 11:042170a9b93a | 14 | HIDScope scope( 3 ); |
Jellehierck | 8:ea3de43c9e8b | 15 | MODSERIAL pc(USBTX, USBRX); |
IsaRobin | 0:6972d0e91af1 | 16 | |
IsaRobin | 0:6972d0e91af1 | 17 | //EMG inputs definieren |
IsaRobin | 0:6972d0e91af1 | 18 | AnalogIn emg1_in (A1); //emg van rechterbicep, voor de x-richting |
IsaRobin | 0:6972d0e91af1 | 19 | AnalogIn emg2_in (A2); //emg van linkerbicep, voor de y-richting |
IsaRobin | 0:6972d0e91af1 | 20 | AnalogIn emg3_in (A3); //emg van een derde (nog te bepalen) spier, voor het vernaderen van de richting |
IsaRobin | 0:6972d0e91af1 | 21 | |
Jellehierck | 4:09a01d2db8f7 | 22 | // LED |
Jellehierck | 6:5437cc97e1e6 | 23 | DigitalOut led_g(LED_GREEN); |
Jellehierck | 6:5437cc97e1e6 | 24 | DigitalOut led_r(LED_RED); |
Jellehierck | 8:ea3de43c9e8b | 25 | DigitalOut led_b(LED_BLUE); |
Jellehierck | 8:ea3de43c9e8b | 26 | |
Jellehierck | 8:ea3de43c9e8b | 27 | // Buttons |
Jellehierck | 8:ea3de43c9e8b | 28 | InterruptIn button1(D11); |
Jellehierck | 8:ea3de43c9e8b | 29 | InterruptIn button2(D10); |
Jellehierck | 12:70f0710400c2 | 30 | InterruptIn button3(SW3); |
Jellehierck | 4:09a01d2db8f7 | 31 | |
IsaRobin | 0:6972d0e91af1 | 32 | //variablen voor EMG |
IsaRobin | 0:6972d0e91af1 | 33 | double emg1; |
IsaRobin | 0:6972d0e91af1 | 34 | double emg2; |
IsaRobin | 0:6972d0e91af1 | 35 | double emg3; |
Jellehierck | 12:70f0710400c2 | 36 | double emg1_MVC; |
Jellehierck | 12:70f0710400c2 | 37 | double emg1_MVC_stdev; |
Jellehierck | 12:70f0710400c2 | 38 | double emg1_rest; |
Jellehierck | 12:70f0710400c2 | 39 | double emg1_rest_stdev; |
Jellehierck | 7:7a088536f1c9 | 40 | |
Jellehierck | 7:7a088536f1c9 | 41 | vector<double> emg1_cal; |
Jellehierck | 7:7a088536f1c9 | 42 | |
IsaRobin | 0:6972d0e91af1 | 43 | |
Jellehierck | 4:09a01d2db8f7 | 44 | // Initialize tickers |
Jellehierck | 4:09a01d2db8f7 | 45 | Ticker tickSample; |
Jellehierck | 7:7a088536f1c9 | 46 | Timeout timeoutCalibrationMVC; |
Jellehierck | 12:70f0710400c2 | 47 | Timeout timeoutCalibrationRest; |
Jellehierck | 7:7a088536f1c9 | 48 | Ticker tickSampleCalibration; |
Jellehierck | 4:09a01d2db8f7 | 49 | |
Jellehierck | 4:09a01d2db8f7 | 50 | // Sample rate |
Jellehierck | 11:042170a9b93a | 51 | const double Fs = 500; // Sampling frequency (s) |
Jellehierck | 11:042170a9b93a | 52 | const double Ts = 1/Fs; // Sampling time (s) |
Jellehierck | 11:042170a9b93a | 53 | |
Jellehierck | 11:042170a9b93a | 54 | const double Tcal = 10.0f; // Calibration duration (s) |
Jellehierck | 11:042170a9b93a | 55 | |
Jellehierck | 11:042170a9b93a | 56 | int trim_cal = 1; // Trim the beginning of the calibration vector to reduce transient behaviour by X seconds |
Jellehierck | 11:042170a9b93a | 57 | int trim_cal_i = trim_cal * Fs - 1; |
Jellehierck | 4:09a01d2db8f7 | 58 | |
Jellehierck | 3:c0ece64850db | 59 | // Notch filter coefficients (iirnotch Q factor 35 @50Hz) from MATLAB in the following form: |
Jellehierck | 3:c0ece64850db | 60 | // b01 b11 b21 a01 a11 a21 |
Jellehierck | 11:042170a9b93a | 61 | BiQuad bq_notch( 0.995636295063941, -1.89829218816065, 0.995636295063941, 1, -1.89829218816065, 0.991272590127882); |
Jellehierck | 11:042170a9b93a | 62 | BiQuadChain bqc_notch; |
Jellehierck | 1:059cca298369 | 63 | |
Jellehierck | 3:c0ece64850db | 64 | // Highpass filter coefficients (butter 4th order @10Hz cutoff) from MATLAB in the following form: |
Jellehierck | 3:c0ece64850db | 65 | // b01 b11 b21 a01 a11 a21 |
Jellehierck | 3:c0ece64850db | 66 | // b02 b12 b22 a02 a12 a22 |
Jellehierck | 3:c0ece64850db | 67 | BiQuad bq_H1(0.922946103200875, -1.84589220640175, 0.922946103200875, 1, -1.88920703055163, 0.892769008131025); |
Jellehierck | 3:c0ece64850db | 68 | BiQuad bq_H2(1, -2, 1, 1, -1.95046575793011, 0.954143234875078); |
Jellehierck | 11:042170a9b93a | 69 | BiQuadChain bqc_high; // Used to chain two 2nd other filters into a 4th order filter |
IsaRobin | 0:6972d0e91af1 | 70 | |
Jellehierck | 3:c0ece64850db | 71 | // Lowpass filter coefficients (butter 4th order @5Hz cutoff) from MATLAB in the following form: |
Jellehierck | 3:c0ece64850db | 72 | // b01 b11 b21 a01 a11 a21 |
Jellehierck | 3:c0ece64850db | 73 | // b02 b12 b22 a02 a12 a22 |
Jellehierck | 3:c0ece64850db | 74 | BiQuad bq_L1(5.32116245737504e-08, 1.06423249147501e-07, 5.32116245737504e-08, 1, -1.94396715039462, 0.944882378004138); |
Jellehierck | 3:c0ece64850db | 75 | BiQuad bq_L2(1, 2, 1, 1, -1.97586467534468, 0.976794920438162); |
Jellehierck | 3:c0ece64850db | 76 | BiQuadChain bqc_low; // Used to chain two 2nd other filters into a 4th order filter |
Jellehierck | 2:d3e9788ab1b3 | 77 | |
Jellehierck | 8:ea3de43c9e8b | 78 | double getMean(const vector<double> &vect) |
Jellehierck | 7:7a088536f1c9 | 79 | { |
Jellehierck | 8:ea3de43c9e8b | 80 | double sum = 0.0; |
Jellehierck | 8:ea3de43c9e8b | 81 | int vect_n = vect.size(); |
Jellehierck | 8:ea3de43c9e8b | 82 | |
Jellehierck | 8:ea3de43c9e8b | 83 | for ( int i = 0; i < vect_n; i++ ) { |
Jellehierck | 8:ea3de43c9e8b | 84 | sum += vect[i]; |
Jellehierck | 8:ea3de43c9e8b | 85 | } |
Jellehierck | 8:ea3de43c9e8b | 86 | return sum/vect_n; |
Jellehierck | 8:ea3de43c9e8b | 87 | } |
Jellehierck | 8:ea3de43c9e8b | 88 | |
Jellehierck | 8:ea3de43c9e8b | 89 | double getStdev(const vector<double> &vect, const double vect_mean) |
Jellehierck | 8:ea3de43c9e8b | 90 | { |
Jellehierck | 8:ea3de43c9e8b | 91 | double sum2 = 0.0; |
Jellehierck | 8:ea3de43c9e8b | 92 | int vect_n = vect.size(); |
Jellehierck | 8:ea3de43c9e8b | 93 | |
Jellehierck | 8:ea3de43c9e8b | 94 | for ( int i = 0; i < vect_n; i++ ) { |
Jellehierck | 8:ea3de43c9e8b | 95 | sum2 += pow( vect[i] - vect_mean, 2 ); |
Jellehierck | 8:ea3de43c9e8b | 96 | } |
Jellehierck | 8:ea3de43c9e8b | 97 | double output = sqrt( sum2 / vect_n ); |
Jellehierck | 8:ea3de43c9e8b | 98 | return output; |
Jellehierck | 7:7a088536f1c9 | 99 | } |
Jellehierck | 7:7a088536f1c9 | 100 | |
Jellehierck | 6:5437cc97e1e6 | 101 | // Check if filters are stable |
Jellehierck | 6:5437cc97e1e6 | 102 | bool checkBQChainStable() |
Jellehierck | 6:5437cc97e1e6 | 103 | { |
Jellehierck | 11:042170a9b93a | 104 | bool n_stable = bqc_notch.stable(); |
Jellehierck | 11:042170a9b93a | 105 | bool hp_stable = bqc_high.stable(); |
Jellehierck | 6:5437cc97e1e6 | 106 | bool l_stable = bqc_low.stable(); |
Jellehierck | 6:5437cc97e1e6 | 107 | |
Jellehierck | 11:042170a9b93a | 108 | if (n_stable && hp_stable && l_stable) { |
Jellehierck | 6:5437cc97e1e6 | 109 | return true; |
Jellehierck | 6:5437cc97e1e6 | 110 | } else { |
Jellehierck | 6:5437cc97e1e6 | 111 | return false; |
Jellehierck | 6:5437cc97e1e6 | 112 | } |
Jellehierck | 6:5437cc97e1e6 | 113 | } |
Jellehierck | 6:5437cc97e1e6 | 114 | |
Jellehierck | 6:5437cc97e1e6 | 115 | |
Jellehierck | 11:042170a9b93a | 116 | /* |
Jellehierck | 6:5437cc97e1e6 | 117 | // Read samples, filter samples and output to HIDScope |
Jellehierck | 2:d3e9788ab1b3 | 118 | void sample() |
Jellehierck | 2:d3e9788ab1b3 | 119 | { |
Jellehierck | 4:09a01d2db8f7 | 120 | // Read EMG inputs |
Jellehierck | 2:d3e9788ab1b3 | 121 | emg1 = emg1_in.read(); |
Jellehierck | 2:d3e9788ab1b3 | 122 | emg2 = emg2_in.read(); |
Jellehierck | 2:d3e9788ab1b3 | 123 | emg3 = emg3_in.read(); |
Jellehierck | 4:09a01d2db8f7 | 124 | |
Jellehierck | 4:09a01d2db8f7 | 125 | // Output raw EMG input |
Jellehierck | 4:09a01d2db8f7 | 126 | scope.set(0, emg1 ); |
Jellehierck | 6:5437cc97e1e6 | 127 | |
Jellehierck | 5:3d65f89e3755 | 128 | // Filter notch and highpass |
Jellehierck | 5:3d65f89e3755 | 129 | double emg1_n_hp = bqc_notch_high.step( emg1 ); |
Jellehierck | 6:5437cc97e1e6 | 130 | |
Jellehierck | 5:3d65f89e3755 | 131 | // Rectify |
Jellehierck | 5:3d65f89e3755 | 132 | double emg1_rectify = fabs( emg1_n_hp ); |
Jellehierck | 6:5437cc97e1e6 | 133 | |
Jellehierck | 5:3d65f89e3755 | 134 | // Filter lowpass (completes envelope) |
Jellehierck | 5:3d65f89e3755 | 135 | double emg1_env = bqc_low.step( emg1_rectify ); |
Jellehierck | 4:09a01d2db8f7 | 136 | |
Jellehierck | 4:09a01d2db8f7 | 137 | // Output EMG after filters |
Jellehierck | 5:3d65f89e3755 | 138 | scope.set(1, emg1_env ); |
Jellehierck | 4:09a01d2db8f7 | 139 | scope.send(); |
Jellehierck | 2:d3e9788ab1b3 | 140 | } |
Jellehierck | 11:042170a9b93a | 141 | */ |
IsaRobin | 0:6972d0e91af1 | 142 | |
Jellehierck | 7:7a088536f1c9 | 143 | void sampleCalibration() |
Jellehierck | 7:7a088536f1c9 | 144 | { |
Jellehierck | 7:7a088536f1c9 | 145 | // Read EMG inputs |
Jellehierck | 7:7a088536f1c9 | 146 | emg1 = emg1_in.read(); |
Jellehierck | 7:7a088536f1c9 | 147 | emg2 = emg2_in.read(); |
Jellehierck | 7:7a088536f1c9 | 148 | emg3 = emg3_in.read(); |
Jellehierck | 7:7a088536f1c9 | 149 | |
Jellehierck | 7:7a088536f1c9 | 150 | // Output raw EMG input |
Jellehierck | 7:7a088536f1c9 | 151 | scope.set(0, emg1 ); |
Jellehierck | 10:97a79aa10a56 | 152 | |
Jellehierck | 10:97a79aa10a56 | 153 | double emg1_n = bqc_notch.step( emg1 ); // Filter notch |
Jellehierck | 11:042170a9b93a | 154 | scope.set(1, emg1_n); |
Jellehierck | 10:97a79aa10a56 | 155 | double emg1_hp = bqc_high.step( emg1_n ); // Filter highpass |
Jellehierck | 11:042170a9b93a | 156 | double emg1_rectify = fabs( emg1_hp ); // Rectify |
Jellehierck | 7:7a088536f1c9 | 157 | double emg1_env = bqc_low.step( emg1_rectify ); // Filter lowpass (completes envelope) |
Jellehierck | 7:7a088536f1c9 | 158 | |
Jellehierck | 7:7a088536f1c9 | 159 | // Output EMG after filters |
Jellehierck | 11:042170a9b93a | 160 | scope.set(2, emg1_env ); |
Jellehierck | 7:7a088536f1c9 | 161 | scope.send(); |
Jellehierck | 7:7a088536f1c9 | 162 | |
Jellehierck | 7:7a088536f1c9 | 163 | emg1_cal.push_back(emg1_env); |
Jellehierck | 7:7a088536f1c9 | 164 | } |
Jellehierck | 7:7a088536f1c9 | 165 | |
Jellehierck | 7:7a088536f1c9 | 166 | void calibrationMVCFinished() |
Jellehierck | 7:7a088536f1c9 | 167 | { |
Jellehierck | 7:7a088536f1c9 | 168 | tickSampleCalibration.detach(); |
Jellehierck | 11:042170a9b93a | 169 | |
Jellehierck | 12:70f0710400c2 | 170 | emg1_MVC = getMean(emg1_cal); |
Jellehierck | 12:70f0710400c2 | 171 | emg1_MVC_stdev = getStdev(emg1_cal, emg1_MVC); |
Jellehierck | 10:97a79aa10a56 | 172 | |
Jellehierck | 10:97a79aa10a56 | 173 | emg1_cal.clear(); |
Jellehierck | 8:ea3de43c9e8b | 174 | |
Jellehierck | 8:ea3de43c9e8b | 175 | led_b = 1; |
Jellehierck | 7:7a088536f1c9 | 176 | } |
Jellehierck | 7:7a088536f1c9 | 177 | |
Jellehierck | 7:7a088536f1c9 | 178 | void calibrationMVC() |
Jellehierck | 7:7a088536f1c9 | 179 | { |
Jellehierck | 11:042170a9b93a | 180 | timeoutCalibrationMVC.attach( &calibrationMVCFinished, Tcal); |
Jellehierck | 7:7a088536f1c9 | 181 | tickSampleCalibration.attach( &sampleCalibration, Ts ); |
Jellehierck | 8:ea3de43c9e8b | 182 | led_b = 0; |
Jellehierck | 7:7a088536f1c9 | 183 | } |
Jellehierck | 7:7a088536f1c9 | 184 | |
Jellehierck | 12:70f0710400c2 | 185 | void calibrationRestFinished() |
Jellehierck | 12:70f0710400c2 | 186 | { |
Jellehierck | 12:70f0710400c2 | 187 | tickSampleCalibration.detach(); |
Jellehierck | 12:70f0710400c2 | 188 | |
Jellehierck | 12:70f0710400c2 | 189 | emg1_rest = getMean(emg1_cal); |
Jellehierck | 12:70f0710400c2 | 190 | emg1_rest_stdev = getStdev(emg1_cal, emg1_rest); |
Jellehierck | 12:70f0710400c2 | 191 | |
Jellehierck | 12:70f0710400c2 | 192 | emg1_cal.clear(); |
Jellehierck | 7:7a088536f1c9 | 193 | |
Jellehierck | 12:70f0710400c2 | 194 | led_b = 1; |
Jellehierck | 12:70f0710400c2 | 195 | } |
Jellehierck | 12:70f0710400c2 | 196 | |
Jellehierck | 12:70f0710400c2 | 197 | void calibrationRest() |
Jellehierck | 12:70f0710400c2 | 198 | { |
Jellehierck | 12:70f0710400c2 | 199 | timeoutCalibrationRest.attach( &calibrationRestFinished, Tcal); |
Jellehierck | 12:70f0710400c2 | 200 | tickSampleCalibration.attach( &sampleCalibration, Ts ); |
Jellehierck | 12:70f0710400c2 | 201 | led_b = 0; |
Jellehierck | 12:70f0710400c2 | 202 | } |
Jellehierck | 12:70f0710400c2 | 203 | |
Jellehierck | 12:70f0710400c2 | 204 | void makeScale() |
Jellehierck | 12:70f0710400c2 | 205 | { |
Jellehierck | 12:70f0710400c2 | 206 | double margin_percentage = 10; |
Jellehierck | 12:70f0710400c2 | 207 | double factor1 = 1 / emg1_MVC; |
Jellehierck | 12:70f0710400c2 | 208 | double emg1_th = emg1_rest * factor1 + margin_percentage/100; |
Jellehierck | 12:70f0710400c2 | 209 | |
Jellehierck | 12:70f0710400c2 | 210 | pc.printf("Factor: %f TH: %f\r\n", factor1, emg1_th); |
Jellehierck | 12:70f0710400c2 | 211 | } |
Jellehierck | 7:7a088536f1c9 | 212 | |
Jellehierck | 5:3d65f89e3755 | 213 | void main() |
Jellehierck | 4:09a01d2db8f7 | 214 | { |
Jellehierck | 8:ea3de43c9e8b | 215 | pc.baud(115200); |
Jellehierck | 8:ea3de43c9e8b | 216 | pc.printf("Starting\r\n"); |
Jellehierck | 6:5437cc97e1e6 | 217 | // Initialize sample ticker |
Jellehierck | 8:ea3de43c9e8b | 218 | // tickSample.attach(&sample, Ts); |
Jellehierck | 6:5437cc97e1e6 | 219 | |
Jellehierck | 6:5437cc97e1e6 | 220 | // Create BQ chains to reduce computations |
Jellehierck | 10:97a79aa10a56 | 221 | bqc_notch.add( &bq_notch ); |
Jellehierck | 10:97a79aa10a56 | 222 | bqc_high.add( &bq_H1 ).add( &bq_H2 ); |
Jellehierck | 5:3d65f89e3755 | 223 | bqc_low.add( &bq_L1 ).add( &bq_L2 ); |
Jellehierck | 4:09a01d2db8f7 | 224 | |
Jellehierck | 8:ea3de43c9e8b | 225 | led_b = 1; // Turn led off at startup |
Jellehierck | 8:ea3de43c9e8b | 226 | led_g = 1; |
Jellehierck | 8:ea3de43c9e8b | 227 | |
Jellehierck | 6:5437cc97e1e6 | 228 | // If any filter chain is unstable, red led will light up |
Jellehierck | 6:5437cc97e1e6 | 229 | if (checkBQChainStable) { |
Jellehierck | 6:5437cc97e1e6 | 230 | led_r = 1; // LED off |
Jellehierck | 6:5437cc97e1e6 | 231 | } else { |
Jellehierck | 6:5437cc97e1e6 | 232 | led_r = 0; // LED on |
Jellehierck | 6:5437cc97e1e6 | 233 | } |
Jellehierck | 6:5437cc97e1e6 | 234 | |
Jellehierck | 8:ea3de43c9e8b | 235 | button1.fall( &calibrationMVC ); |
Jellehierck | 12:70f0710400c2 | 236 | button2.fall( &calibrationRest ); |
Jellehierck | 12:70f0710400c2 | 237 | button3.fall( &makeScale ); |
Jellehierck | 8:ea3de43c9e8b | 238 | |
Jellehierck | 4:09a01d2db8f7 | 239 | while(true) { |
Jellehierck | 7:7a088536f1c9 | 240 | |
Jellehierck | 6:5437cc97e1e6 | 241 | // Show that system is running |
Jellehierck | 8:ea3de43c9e8b | 242 | // led_g = !led_g; |
Jellehierck | 12:70f0710400c2 | 243 | pc.printf("EMG MVC: %f stdev: %f\r\nEMG Rest: %f stdev: %f\r\n", emg1_MVC, emg1_MVC_stdev, emg1_rest, emg1_rest_stdev); |
Jellehierck | 4:09a01d2db8f7 | 244 | wait(0.5); |
Jellehierck | 4:09a01d2db8f7 | 245 | } |
Jellehierck | 4:09a01d2db8f7 | 246 | } |