even versimpelen
Dependencies: HIDScope MODSERIAL PID QEI biquadFilter mbed
Fork of cpfromralph1 by
main.cpp@24:26659f1de039, 2016-11-03 (annotated)
- Committer:
- relvorelvo
- Date:
- Thu Nov 03 13:40:17 2016 +0000
- Revision:
- 24:26659f1de039
- Parent:
- 23:e5db011bd410
- Child:
- 25:07187cf76863
fine tuned and all, only boolean for right arm is still needed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:32bb76391d89 | 1 | #include "mbed.h" |
vsluiter | 11:ce72ec658a95 | 2 | #include "HIDScope.h" |
relvorelvo | 21:136a1ab8163c | 3 | #include "BiQuad.h" |
relvorelvo | 21:136a1ab8163c | 4 | #include "MODSERIAL.h" |
vsluiter | 0:32bb76391d89 | 5 | |
vsluiter | 4:8b298dfada81 | 6 | //Define objects |
relvorelvo | 23:e5db011bd410 | 7 | AnalogIn emg1_in( A0 ); |
relvorelvo | 23:e5db011bd410 | 8 | AnalogIn emg2_in( A1 ); |
relvorelvo | 23:e5db011bd410 | 9 | AnalogIn emg3_in( A2 ); |
relvorelvo | 23:e5db011bd410 | 10 | DigitalIn max_reader1( SW2 ); |
relvorelvo | 23:e5db011bd410 | 11 | DigitalIn max_reader3( SW3 ); |
relvorelvo | 23:e5db011bd410 | 12 | |
tomlankhorst | 19:2bf824669684 | 13 | |
relvorelvo | 23:e5db011bd410 | 14 | Ticker main_timer; |
relvorelvo | 23:e5db011bd410 | 15 | Ticker max_read1; |
relvorelvo | 23:e5db011bd410 | 16 | Ticker max_read3; |
relvorelvo | 23:e5db011bd410 | 17 | HIDScope scope( 5 ); |
relvorelvo | 23:e5db011bd410 | 18 | DigitalOut red(LED_RED); |
relvorelvo | 23:e5db011bd410 | 19 | DigitalOut blue(LED_BLUE); |
relvorelvo | 23:e5db011bd410 | 20 | DigitalOut green(LED_GREEN); |
relvorelvo | 23:e5db011bd410 | 21 | MODSERIAL pc(USBTX, USBRX); |
relvorelvo | 21:136a1ab8163c | 22 | |
relvorelvo | 21:136a1ab8163c | 23 | |
relvorelvo | 21:136a1ab8163c | 24 | // EMG variables |
relvorelvo | 23:e5db011bd410 | 25 | //Right Biceps |
relvorelvo | 23:e5db011bd410 | 26 | double emg1; |
relvorelvo | 23:e5db011bd410 | 27 | double emg1highfilter; |
relvorelvo | 23:e5db011bd410 | 28 | double emg1notchfilter; |
relvorelvo | 23:e5db011bd410 | 29 | double emg1abs; |
relvorelvo | 23:e5db011bd410 | 30 | double emg1lowfilter; |
relvorelvo | 23:e5db011bd410 | 31 | double emg1peak; |
relvorelvo | 24:26659f1de039 | 32 | double maxpart1; |
relvorelvo | 23:e5db011bd410 | 33 | // Left Biceps |
relvorelvo | 23:e5db011bd410 | 34 | double emg2; |
relvorelvo | 23:e5db011bd410 | 35 | double emg2highfilter; |
relvorelvo | 23:e5db011bd410 | 36 | double emg2notchfilter; |
relvorelvo | 23:e5db011bd410 | 37 | double emg2abs; |
relvorelvo | 23:e5db011bd410 | 38 | double emg2lowfilter; |
relvorelvo | 23:e5db011bd410 | 39 | double emg2peak; |
relvorelvo | 23:e5db011bd410 | 40 | double max1; |
relvorelvo | 24:26659f1de039 | 41 | double maxpart2; |
relvorelvo | 23:e5db011bd410 | 42 | // Left Lower Arm OR Triceps |
relvorelvo | 23:e5db011bd410 | 43 | double emg3; |
relvorelvo | 23:e5db011bd410 | 44 | double emg3highfilter; |
relvorelvo | 23:e5db011bd410 | 45 | double emg3notchfilter; |
relvorelvo | 23:e5db011bd410 | 46 | double emg3abs; |
relvorelvo | 23:e5db011bd410 | 47 | double emg3lowfilter; |
relvorelvo | 23:e5db011bd410 | 48 | double emg3peak; |
relvorelvo | 23:e5db011bd410 | 49 | double max3; |
relvorelvo | 23:e5db011bd410 | 50 | double maxpart3; |
vsluiter | 2:e314bb3b2d99 | 51 | |
relvorelvo | 21:136a1ab8163c | 52 | // Filter settings |
relvorelvo | 23:e5db011bd410 | 53 | // Right Biceps |
relvorelvo | 23:e5db011bd410 | 54 | BiQuad filterhigh1(9.704e-01,-1.9408,9.704e-01,-1.9389,9.427e-01); /* Filter at 10 Hz */ |
relvorelvo | 23:e5db011bd410 | 55 | BiQuad filternotch1(9.495e-01,-1.8062,9.495e-01,-1.8062,8.992e-01); /* Filter at 50 Hz */ |
relvorelvo | 24:26659f1de039 | 56 | BiQuad filterpeak1(1.0033,-1.984,9.852e-01,-1.9838,9.8855e-01); /* Gain peak at 11 Hz */ |
relvorelvo | 24:26659f1de039 | 57 | BiQuad filterlow1(1.368e-03,2.737e-03,1.369e-03,-1.9219,9.274e-01); /* Filter at 15 Hz */ |
relvorelvo | 23:e5db011bd410 | 58 | // Left Biceps |
relvorelvo | 23:e5db011bd410 | 59 | BiQuad filterhigh2(9.704e-01,-1.9408,9.704e-01,-1.9389,9.427e-01); |
relvorelvo | 23:e5db011bd410 | 60 | BiQuad filternotch2(9.495e-01,-1.8062,9.495e-01,-1.8062,8.992e-01); |
relvorelvo | 24:26659f1de039 | 61 | BiQuad filterpeak2(1.0033,-1.984,9.852e-01,-1.9838,9.8855e-01); |
relvorelvo | 24:26659f1de039 | 62 | BiQuad filterlow2(1.368e-03,2.737e-03,1.369e-03,-1.9219,9.274e-01); |
relvorelvo | 23:e5db011bd410 | 63 | // Left Lower Arm OR Triceps |
relvorelvo | 23:e5db011bd410 | 64 | BiQuad filterhigh3(9.704e-01,-1.9408,9.704e-01,-1.9389,9.427e-01); |
relvorelvo | 23:e5db011bd410 | 65 | BiQuad filternotch3(9.495e-01,-1.8062,9.495e-01,-1.8062,8.992e-01); |
relvorelvo | 24:26659f1de039 | 66 | BiQuad filterpeak3(1.0033,-1.984,9.852e-01,-1.9838,9.8855e-01); |
relvorelvo | 24:26659f1de039 | 67 | BiQuad filterlow3(1.368e-03,2.737e-03,1.369e-03,-1.9219,9.274e-01); |
relvorelvo | 23:e5db011bd410 | 68 | // |
relvorelvo | 21:136a1ab8163c | 69 | |
relvorelvo | 23:e5db011bd410 | 70 | // Finding max values for correct motor switch |
relvorelvo | 23:e5db011bd410 | 71 | void get_max1(){ |
relvorelvo | 23:e5db011bd410 | 72 | if (max_reader1==0){ |
relvorelvo | 23:e5db011bd410 | 73 | green = !green; |
relvorelvo | 23:e5db011bd410 | 74 | red = 1; |
relvorelvo | 23:e5db011bd410 | 75 | blue = 1; |
relvorelvo | 23:e5db011bd410 | 76 | for(int n=0;n<2000;n++){ |
relvorelvo | 23:e5db011bd410 | 77 | |
relvorelvo | 23:e5db011bd410 | 78 | emg1 = emg1_in.read(); |
relvorelvo | 23:e5db011bd410 | 79 | emg1highfilter = filterhigh1.step(emg1); |
relvorelvo | 23:e5db011bd410 | 80 | emg1notchfilter = filternotch1.step(emg1highfilter); |
relvorelvo | 23:e5db011bd410 | 81 | emg1abs = fabs(emg1notchfilter); |
relvorelvo | 23:e5db011bd410 | 82 | emg1lowfilter = filterlow1.step(emg1abs); |
relvorelvo | 23:e5db011bd410 | 83 | emg1peak = filterpeak1.step(emg1lowfilter); |
relvorelvo | 23:e5db011bd410 | 84 | |
relvorelvo | 23:e5db011bd410 | 85 | if (max1<emg1peak){ |
relvorelvo | 23:e5db011bd410 | 86 | max1 = emg1peak; |
relvorelvo | 23:e5db011bd410 | 87 | } |
relvorelvo | 23:e5db011bd410 | 88 | wait(0.001f); |
relvorelvo | 23:e5db011bd410 | 89 | } |
relvorelvo | 23:e5db011bd410 | 90 | wait(0.2f); |
relvorelvo | 23:e5db011bd410 | 91 | green = 1; |
relvorelvo | 23:e5db011bd410 | 92 | } |
relvorelvo | 24:26659f1de039 | 93 | maxpart1 = 0.28*max1; |
relvorelvo | 24:26659f1de039 | 94 | maxpart2 = 0.18*max1; |
relvorelvo | 23:e5db011bd410 | 95 | } |
relvorelvo | 21:136a1ab8163c | 96 | |
relvorelvo | 23:e5db011bd410 | 97 | void get_max3(){ |
relvorelvo | 23:e5db011bd410 | 98 | if (max_reader3==0){ |
relvorelvo | 23:e5db011bd410 | 99 | green = 1; |
relvorelvo | 23:e5db011bd410 | 100 | blue = 1; |
relvorelvo | 23:e5db011bd410 | 101 | red = !red; |
relvorelvo | 23:e5db011bd410 | 102 | for(int n=0;n<2000;n++){ |
relvorelvo | 23:e5db011bd410 | 103 | |
relvorelvo | 23:e5db011bd410 | 104 | emg3 = emg3_in.read(); |
relvorelvo | 23:e5db011bd410 | 105 | emg3highfilter = filterhigh3.step(emg3); |
relvorelvo | 23:e5db011bd410 | 106 | emg3notchfilter = filternotch3.step(emg3highfilter); |
relvorelvo | 23:e5db011bd410 | 107 | emg3abs = fabs(emg3notchfilter); |
relvorelvo | 23:e5db011bd410 | 108 | emg3lowfilter = filterlow3.step(emg3abs); |
relvorelvo | 23:e5db011bd410 | 109 | emg3peak = filterpeak3.step(emg3lowfilter); |
relvorelvo | 23:e5db011bd410 | 110 | |
relvorelvo | 23:e5db011bd410 | 111 | if (max3<emg3peak){ |
relvorelvo | 23:e5db011bd410 | 112 | max3 = emg3peak; |
relvorelvo | 23:e5db011bd410 | 113 | } |
relvorelvo | 23:e5db011bd410 | 114 | wait(0.001f); |
relvorelvo | 23:e5db011bd410 | 115 | } |
relvorelvo | 23:e5db011bd410 | 116 | wait(0.2f); |
relvorelvo | 23:e5db011bd410 | 117 | red = 1; |
relvorelvo | 23:e5db011bd410 | 118 | } |
relvorelvo | 24:26659f1de039 | 119 | maxpart3 = 0.28*max3; |
vsluiter | 2:e314bb3b2d99 | 120 | } |
vsluiter | 0:32bb76391d89 | 121 | |
relvorelvo | 23:e5db011bd410 | 122 | // Filtering & Scope |
relvorelvo | 23:e5db011bd410 | 123 | void filter() { |
relvorelvo | 23:e5db011bd410 | 124 | // Right Biceps |
relvorelvo | 23:e5db011bd410 | 125 | emg1 = emg1_in.read(); |
relvorelvo | 23:e5db011bd410 | 126 | emg1highfilter = filterhigh1.step(emg1); |
relvorelvo | 23:e5db011bd410 | 127 | emg1notchfilter = filternotch1.step(emg1highfilter); |
relvorelvo | 23:e5db011bd410 | 128 | emg1abs = fabs(emg1notchfilter); |
relvorelvo | 23:e5db011bd410 | 129 | emg1lowfilter = filterlow1.step(emg1abs); |
relvorelvo | 23:e5db011bd410 | 130 | emg1peak = filterpeak1.step(emg1lowfilter); |
relvorelvo | 23:e5db011bd410 | 131 | // Left Biceps |
relvorelvo | 23:e5db011bd410 | 132 | emg2 = emg2_in.read(); |
relvorelvo | 23:e5db011bd410 | 133 | emg2highfilter = filterhigh2.step(emg2); |
relvorelvo | 23:e5db011bd410 | 134 | emg2notchfilter = filternotch2.step(emg2highfilter); |
relvorelvo | 23:e5db011bd410 | 135 | emg2abs = fabs(emg2notchfilter); |
relvorelvo | 23:e5db011bd410 | 136 | emg2lowfilter = filterlow2.step(emg2abs); |
relvorelvo | 23:e5db011bd410 | 137 | emg2peak = filterpeak2.step(emg2lowfilter); |
relvorelvo | 23:e5db011bd410 | 138 | // Left Lower Arm OR Triceps |
relvorelvo | 23:e5db011bd410 | 139 | emg3 = emg3_in.read(); |
relvorelvo | 23:e5db011bd410 | 140 | emg3highfilter = filterhigh3.step(emg3); |
relvorelvo | 23:e5db011bd410 | 141 | emg3notchfilter = filternotch3.step(emg3highfilter); |
relvorelvo | 23:e5db011bd410 | 142 | emg3abs = fabs(emg3notchfilter); |
relvorelvo | 23:e5db011bd410 | 143 | emg3lowfilter = filterlow3.step(emg3abs); |
relvorelvo | 23:e5db011bd410 | 144 | emg3peak = filterpeak3.step(emg3lowfilter); |
relvorelvo | 23:e5db011bd410 | 145 | |
relvorelvo | 23:e5db011bd410 | 146 | |
relvorelvo | 23:e5db011bd410 | 147 | /* Compare measurement to the calibrated value to decide actions */ |
relvorelvo | 23:e5db011bd410 | 148 | if (maxpart1<emg1peak){ |
relvorelvo | 23:e5db011bd410 | 149 | red = 0; |
relvorelvo | 23:e5db011bd410 | 150 | blue = 1; |
relvorelvo | 23:e5db011bd410 | 151 | green = 1; |
relvorelvo | 23:e5db011bd410 | 152 | } |
relvorelvo | 23:e5db011bd410 | 153 | else { |
relvorelvo | 24:26659f1de039 | 154 | if (maxpart2<emg2peak){ |
relvorelvo | 23:e5db011bd410 | 155 | red = 1; |
relvorelvo | 23:e5db011bd410 | 156 | blue = 0; |
relvorelvo | 23:e5db011bd410 | 157 | green = 1; |
relvorelvo | 23:e5db011bd410 | 158 | } |
relvorelvo | 23:e5db011bd410 | 159 | |
relvorelvo | 23:e5db011bd410 | 160 | else { |
relvorelvo | 24:26659f1de039 | 161 | if (maxpart3<emg3peak){ |
relvorelvo | 23:e5db011bd410 | 162 | red = 1; |
relvorelvo | 23:e5db011bd410 | 163 | blue = 1; |
relvorelvo | 23:e5db011bd410 | 164 | green = 0; |
relvorelvo | 23:e5db011bd410 | 165 | } |
relvorelvo | 23:e5db011bd410 | 166 | |
relvorelvo | 23:e5db011bd410 | 167 | else { |
relvorelvo | 23:e5db011bd410 | 168 | red = 1; |
relvorelvo | 23:e5db011bd410 | 169 | blue = 1; |
relvorelvo | 23:e5db011bd410 | 170 | green = 1; |
relvorelvo | 23:e5db011bd410 | 171 | } |
relvorelvo | 23:e5db011bd410 | 172 | } |
relvorelvo | 23:e5db011bd410 | 173 | } |
relvorelvo | 23:e5db011bd410 | 174 | /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */ |
relvorelvo | 23:e5db011bd410 | 175 | scope.set(0, emg1peak ); |
relvorelvo | 23:e5db011bd410 | 176 | scope.set(1, emg2peak ); |
relvorelvo | 23:e5db011bd410 | 177 | scope.set(2, maxpart1 ); |
relvorelvo | 23:e5db011bd410 | 178 | scope.set(3, emg3peak ); |
relvorelvo | 23:e5db011bd410 | 179 | scope.set(4, maxpart3 ); |
relvorelvo | 23:e5db011bd410 | 180 | |
relvorelvo | 23:e5db011bd410 | 181 | scope.send(); |
relvorelvo | 23:e5db011bd410 | 182 | } |
tomlankhorst | 15:0da764eea774 | 183 | |
relvorelvo | 23:e5db011bd410 | 184 | int main(){ |
relvorelvo | 22:68ab712b62b2 | 185 | |
relvorelvo | 23:e5db011bd410 | 186 | main_timer.attach(&filter, 0.001); |
relvorelvo | 23:e5db011bd410 | 187 | max_read1.attach(&get_max1, 2); |
relvorelvo | 23:e5db011bd410 | 188 | max_read3.attach(&get_max3, 2); |
tomlankhorst | 15:0da764eea774 | 189 | while(1) {} |
vsluiter | 0:32bb76391d89 | 190 | } |