This is with minumum of 10 milliseconds threshold delay 00:53
Dependencies: biquadFilter MODSERIAL QEI Servo mbed
Fork of StateMachine_EMg_RKI_PID_MOTOR_metklikenalles by
main.cpp@6:f55ab7e38a7f, 2018-11-01 (annotated)
- Committer:
- gastongab
- Date:
- Thu Nov 01 15:40:37 2018 +0000
- Revision:
- 6:f55ab7e38a7f
- Parent:
- 5:19f59a855475
- Child:
- 7:88fa84742b3c
EMG Is af;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aschut | 0:90750f158475 | 1 | //Voor het toevoegen van een button: |
aschut | 0:90750f158475 | 2 | #include "mbed.h" |
aschut | 0:90750f158475 | 3 | #include <iostream> |
gastongab | 2:0a8622662f6d | 4 | #include "BiQuad.h" |
gastongab | 2:0a8622662f6d | 5 | #include "BiQuadchains_zelfbeun.h" |
gastongab | 2:0a8622662f6d | 6 | #include "MODSERIAL.h" |
gastongab | 2:0a8622662f6d | 7 | |
gastongab | 2:0a8622662f6d | 8 | MODSERIAL pc(USBTX, USBRX); |
gastongab | 2:0a8622662f6d | 9 | |
aschut | 0:90750f158475 | 10 | DigitalOut gpo(D0); |
aschut | 0:90750f158475 | 11 | |
aschut | 0:90750f158475 | 12 | DigitalIn button2(SW3); |
aschut | 0:90750f158475 | 13 | DigitalIn button1(SW2); //or SW2 |
aschut | 0:90750f158475 | 14 | |
aschut | 0:90750f158475 | 15 | DigitalOut led1(LED_GREEN); |
aschut | 0:90750f158475 | 16 | DigitalOut led2(LED_RED); |
aschut | 0:90750f158475 | 17 | DigitalOut led3(LED_BLUE); |
aschut | 0:90750f158475 | 18 | |
gastongab | 2:0a8622662f6d | 19 | //EMG tickers, these tickers are called in the mainscript with fsample 500Hz, also sends to HIDscope with same fsample |
gastongab | 2:0a8622662f6d | 20 | Ticker sample_ticker; //ticker for filtering pref. with 1000Hz, define in tick.attach |
gastongab | 4:c7be673eb4a1 | 21 | Ticker threshold_check_ticker; |
gastongab | 2:0a8622662f6d | 22 | Timer t; //timer try out for Astrid |
gastongab | 2:0a8622662f6d | 23 | Timer timer_calibration; //timer for EMG calibration |
gastongab | 2:0a8622662f6d | 24 | |
gastongab | 2:0a8622662f6d | 25 | |
gastongab | 2:0a8622662f6d | 26 | |
gastongab | 2:0a8622662f6d | 27 | //Input system |
gastongab | 2:0a8622662f6d | 28 | AnalogIn emg1(A0); //right biceps |
gastongab | 2:0a8622662f6d | 29 | AnalogIn emg2(A1); //right triceps |
gastongab | 2:0a8622662f6d | 30 | AnalogIn emg3(A2); //left biceps |
gastongab | 2:0a8622662f6d | 31 | AnalogIn emg4(A3); //left triceps |
gastongab | 2:0a8622662f6d | 32 | |
gastongab | 2:0a8622662f6d | 33 | //Filtered EMG signals from the end of the chains |
gastongab | 4:c7be673eb4a1 | 34 | volatile double emg1_filtered, emg2_filtered, emg3_filtered, emg4_filtered; |
gastongab | 4:c7be673eb4a1 | 35 | int i = 0; |
gastongab | 2:0a8622662f6d | 36 | |
gastongab | 2:0a8622662f6d | 37 | void emgsample(){ |
gastongab | 2:0a8622662f6d | 38 | //All EMG signal through Highpass |
gastongab | 2:0a8622662f6d | 39 | double emgread1 = emg1.read(); |
gastongab | 2:0a8622662f6d | 40 | double emgread2 = emg2.read(); |
gastongab | 2:0a8622662f6d | 41 | double emgread3 = emg3.read(); |
gastongab | 2:0a8622662f6d | 42 | double emgread4 = emg4.read(); |
gastongab | 2:0a8622662f6d | 43 | |
gastongab | 2:0a8622662f6d | 44 | double emg1_highpassed = highp1.step(emgread1); |
gastongab | 2:0a8622662f6d | 45 | double emg2_highpassed = highp2.step(emgread2); |
gastongab | 2:0a8622662f6d | 46 | double emg3_highpassed = highp3.step(emgread3); |
gastongab | 2:0a8622662f6d | 47 | double emg4_highpassed = highp4.step(emgread4); |
gastongab | 2:0a8622662f6d | 48 | |
gastongab | 2:0a8622662f6d | 49 | //All EMG highpassed through Notch |
gastongab | 2:0a8622662f6d | 50 | double emg1_notched = notch1.step(emg1_highpassed); |
gastongab | 2:0a8622662f6d | 51 | double emg2_notched = notch2.step(emg2_highpassed); |
gastongab | 2:0a8622662f6d | 52 | double emg3_notched = notch3.step(emg3_highpassed); |
gastongab | 2:0a8622662f6d | 53 | double emg4_notched = notch4.step(emg4_highpassed); |
gastongab | 2:0a8622662f6d | 54 | |
gastongab | 2:0a8622662f6d | 55 | //All EMG notched rectify |
gastongab | 2:0a8622662f6d | 56 | double emg1_abs = abs(emg1_notched); |
gastongab | 2:0a8622662f6d | 57 | double emg2_abs = abs(emg2_notched); |
gastongab | 2:0a8622662f6d | 58 | double emg3_abs = abs(emg3_notched); |
gastongab | 2:0a8622662f6d | 59 | double emg4_abs = abs(emg4_notched); |
gastongab | 2:0a8622662f6d | 60 | |
gastongab | 2:0a8622662f6d | 61 | //All EMG abs into lowpass |
gastongab | 2:0a8622662f6d | 62 | emg1_filtered = lowp1.step(emg1_abs); |
gastongab | 2:0a8622662f6d | 63 | emg2_filtered = lowp2.step(emg2_abs); |
gastongab | 2:0a8622662f6d | 64 | emg3_filtered = lowp3.step(emg3_abs); |
gastongab | 2:0a8622662f6d | 65 | emg4_filtered = lowp4.step(emg4_abs); |
gastongab | 2:0a8622662f6d | 66 | |
gastongab | 2:0a8622662f6d | 67 | |
gastongab | 2:0a8622662f6d | 68 | } |
gastongab | 2:0a8622662f6d | 69 | |
gastongab | 2:0a8622662f6d | 70 | |
gastongab | 2:0a8622662f6d | 71 | //Define doubles for calibration and ticker |
gastongab | 2:0a8622662f6d | 72 | double ts = 0.001; //tijdsstap |
gastongab | 2:0a8622662f6d | 73 | double calibration_time = 55; //time EMG calibration should take |
gastongab | 2:0a8622662f6d | 74 | |
gastongab | 2:0a8622662f6d | 75 | volatile double temp_highest_emg1 = 0; //highest detected value right biceps |
gastongab | 2:0a8622662f6d | 76 | volatile double temp_highest_emg2 = 0; |
gastongab | 2:0a8622662f6d | 77 | volatile double temp_highest_emg3 = 0; |
gastongab | 2:0a8622662f6d | 78 | volatile double temp_highest_emg4 = 0; |
gastongab | 2:0a8622662f6d | 79 | |
gastongab | 2:0a8622662f6d | 80 | //Doubles for calculation threshold |
gastongab | 6:f55ab7e38a7f | 81 | double biceps_p_t = 0.4; //set threshold at percentage of highest value |
gastongab | 6:f55ab7e38a7f | 82 | double triceps_p_t = 0.5; //set threshold at percentage of highest value |
gastongab | 4:c7be673eb4a1 | 83 | volatile double threshold1; |
gastongab | 4:c7be673eb4a1 | 84 | volatile double threshold2; |
gastongab | 4:c7be673eb4a1 | 85 | volatile double threshold3; |
gastongab | 4:c7be673eb4a1 | 86 | volatile double threshold4; |
gastongab | 2:0a8622662f6d | 87 | |
gastongab | 2:0a8622662f6d | 88 | void CalibrationEMG() |
gastongab | 4:c7be673eb4a1 | 89 | { |
gastongab | 2:0a8622662f6d | 90 | //static float samples = calibration_time/ts; |
gastongab | 2:0a8622662f6d | 91 | while(timer_calibration<55){ |
gastongab | 2:0a8622662f6d | 92 | if(timer_calibration>0 && timer_calibration<10) |
gastongab | 4:c7be673eb4a1 | 93 | { |
gastongab | 2:0a8622662f6d | 94 | led1=!led1; |
gastongab | 2:0a8622662f6d | 95 | if(emg1_filtered>temp_highest_emg1) |
gastongab | 2:0a8622662f6d | 96 | { |
gastongab | 2:0a8622662f6d | 97 | temp_highest_emg1= emg1_filtered; |
gastongab | 5:19f59a855475 | 98 | pc.printf("Temp1 = %f \r\n",temp_highest_emg1); |
gastongab | 2:0a8622662f6d | 99 | } |
gastongab | 2:0a8622662f6d | 100 | } |
gastongab | 2:0a8622662f6d | 101 | if(timer_calibration>10 && timer_calibration<15) |
gastongab | 2:0a8622662f6d | 102 | { |
gastongab | 2:0a8622662f6d | 103 | led1=0; |
gastongab | 2:0a8622662f6d | 104 | led2=0; |
gastongab | 2:0a8622662f6d | 105 | led3=0; |
gastongab | 2:0a8622662f6d | 106 | } |
gastongab | 2:0a8622662f6d | 107 | if(timer_calibration>15 && timer_calibration<25) |
gastongab | 4:c7be673eb4a1 | 108 | { |
gastongab | 2:0a8622662f6d | 109 | led2=!led2; |
gastongab | 2:0a8622662f6d | 110 | if(emg2_filtered>temp_highest_emg2) |
gastongab | 2:0a8622662f6d | 111 | { |
gastongab | 2:0a8622662f6d | 112 | temp_highest_emg2= emg2_filtered; |
gastongab | 4:c7be673eb4a1 | 113 | pc.printf("Temp2 = %f \r\n",temp_highest_emg2); |
gastongab | 2:0a8622662f6d | 114 | } |
gastongab | 2:0a8622662f6d | 115 | } |
gastongab | 2:0a8622662f6d | 116 | if(timer_calibration>25 && timer_calibration<30) |
gastongab | 2:0a8622662f6d | 117 | { |
gastongab | 2:0a8622662f6d | 118 | led1=0; |
gastongab | 2:0a8622662f6d | 119 | led2=0; |
gastongab | 2:0a8622662f6d | 120 | led3=0; |
gastongab | 2:0a8622662f6d | 121 | } |
gastongab | 2:0a8622662f6d | 122 | if(timer_calibration>30 && timer_calibration<40) |
gastongab | 2:0a8622662f6d | 123 | { |
gastongab | 2:0a8622662f6d | 124 | led3=!led3; |
gastongab | 2:0a8622662f6d | 125 | if(emg3_filtered>temp_highest_emg3) |
gastongab | 2:0a8622662f6d | 126 | { |
gastongab | 2:0a8622662f6d | 127 | temp_highest_emg3= emg3_filtered; |
gastongab | 5:19f59a855475 | 128 | pc.printf("Temp3 = %f \r\n",temp_highest_emg3); |
gastongab | 2:0a8622662f6d | 129 | } |
gastongab | 2:0a8622662f6d | 130 | } |
gastongab | 2:0a8622662f6d | 131 | if(timer_calibration>40 && timer_calibration<45) |
gastongab | 2:0a8622662f6d | 132 | { |
gastongab | 2:0a8622662f6d | 133 | led1=0; |
gastongab | 2:0a8622662f6d | 134 | led2=0; |
gastongab | 2:0a8622662f6d | 135 | led3=0; |
gastongab | 2:0a8622662f6d | 136 | } |
gastongab | 2:0a8622662f6d | 137 | if(timer_calibration>45 && timer_calibration<55) |
gastongab | 2:0a8622662f6d | 138 | { |
gastongab | 2:0a8622662f6d | 139 | led2=!led2; |
gastongab | 2:0a8622662f6d | 140 | led3=!led3; |
gastongab | 5:19f59a855475 | 141 | if(emg4_filtered>temp_highest_emg4) |
gastongab | 2:0a8622662f6d | 142 | { |
gastongab | 5:19f59a855475 | 143 | temp_highest_emg4= emg4_filtered; |
gastongab | 5:19f59a855475 | 144 | pc.printf("Temp4 = %f \r\n",temp_highest_emg4); |
gastongab | 2:0a8622662f6d | 145 | } |
gastongab | 2:0a8622662f6d | 146 | } |
gastongab | 2:0a8622662f6d | 147 | led1=1; |
gastongab | 2:0a8622662f6d | 148 | led2=1; |
gastongab | 2:0a8622662f6d | 149 | led3=1; |
gastongab | 2:0a8622662f6d | 150 | |
gastongab | 2:0a8622662f6d | 151 | |
gastongab | 2:0a8622662f6d | 152 | } |
gastongab | 2:0a8622662f6d | 153 | |
gastongab | 2:0a8622662f6d | 154 | pc.printf("Highest value right biceps= %f \r\n", temp_highest_emg1); |
gastongab | 2:0a8622662f6d | 155 | pc.printf("Highest value right triceps= %f \r\n", temp_highest_emg2); |
gastongab | 2:0a8622662f6d | 156 | pc.printf("Highest value left biceps= %f \r\n", temp_highest_emg3); |
gastongab | 2:0a8622662f6d | 157 | pc.printf("Highest value left triceps= %f \r\n", temp_highest_emg4); |
gastongab | 2:0a8622662f6d | 158 | |
gastongab | 4:c7be673eb4a1 | 159 | |
gastongab | 6:f55ab7e38a7f | 160 | threshold1 = temp_highest_emg1*biceps_p_t; //Right Biceps |
gastongab | 6:f55ab7e38a7f | 161 | threshold2 = temp_highest_emg2*triceps_p_t; //Right Triceps |
gastongab | 6:f55ab7e38a7f | 162 | threshold3 = temp_highest_emg3*biceps_p_t; //Left Biceps |
gastongab | 6:f55ab7e38a7f | 163 | threshold4 = temp_highest_emg4*triceps_p_t; //Left Triceps |
gastongab | 2:0a8622662f6d | 164 | } |
gastongab | 2:0a8622662f6d | 165 | |
gastongab | 4:c7be673eb4a1 | 166 | //Check if emg_filtered has reached their threshold |
gastongab | 4:c7be673eb4a1 | 167 | int bicepsR; |
gastongab | 4:c7be673eb4a1 | 168 | int tricepsR; |
gastongab | 4:c7be673eb4a1 | 169 | int bicepsL; |
gastongab | 4:c7be673eb4a1 | 170 | int tricepsL; |
gastongab | 4:c7be673eb4a1 | 171 | |
gastongab | 2:0a8622662f6d | 172 | void threshold_check(){ |
gastongab | 4:c7be673eb4a1 | 173 | |
gastongab | 2:0a8622662f6d | 174 | //EMG1 threshold check |
gastongab | 2:0a8622662f6d | 175 | if(emg1_filtered>threshold1){ |
gastongab | 4:c7be673eb4a1 | 176 | bicepsR = 1; |
gastongab | 2:0a8622662f6d | 177 | } |
gastongab | 2:0a8622662f6d | 178 | else{ |
gastongab | 4:c7be673eb4a1 | 179 | bicepsR= 0; |
gastongab | 2:0a8622662f6d | 180 | } |
gastongab | 2:0a8622662f6d | 181 | //EMG2 threshold check |
gastongab | 2:0a8622662f6d | 182 | if(emg2_filtered>threshold2){ |
gastongab | 4:c7be673eb4a1 | 183 | tricepsR = 1; |
gastongab | 2:0a8622662f6d | 184 | } |
gastongab | 2:0a8622662f6d | 185 | else{ |
gastongab | 4:c7be673eb4a1 | 186 | tricepsR= 0; |
gastongab | 2:0a8622662f6d | 187 | } |
gastongab | 2:0a8622662f6d | 188 | //EMG3 threshold check |
gastongab | 2:0a8622662f6d | 189 | if(emg3_filtered>threshold3){ |
gastongab | 4:c7be673eb4a1 | 190 | bicepsL = 1; |
gastongab | 2:0a8622662f6d | 191 | } |
gastongab | 2:0a8622662f6d | 192 | else{ |
gastongab | 4:c7be673eb4a1 | 193 | bicepsL= 0; |
gastongab | 2:0a8622662f6d | 194 | } |
gastongab | 2:0a8622662f6d | 195 | //EMG4 threshold check |
gastongab | 2:0a8622662f6d | 196 | if(emg4_filtered>threshold4){ |
gastongab | 4:c7be673eb4a1 | 197 | tricepsL = 1; |
gastongab | 2:0a8622662f6d | 198 | } |
gastongab | 2:0a8622662f6d | 199 | else{ |
gastongab | 4:c7be673eb4a1 | 200 | tricepsL= 0; |
gastongab | 2:0a8622662f6d | 201 | } |
gastongab | 4:c7be673eb4a1 | 202 | |
gastongab | 4:c7be673eb4a1 | 203 | /* |
gastongab | 4:c7be673eb4a1 | 204 | pc.printf("Biceps Right = %i", bicepsR); |
gastongab | 4:c7be673eb4a1 | 205 | pc.printf("Triceps Right = %i",tricepsR); |
gastongab | 4:c7be673eb4a1 | 206 | pc.printf("Biceps Left = %i", bicepsL); |
gastongab | 4:c7be673eb4a1 | 207 | pc.printf("Triceps Left = %i", tricepsL); |
gastongab | 4:c7be673eb4a1 | 208 | */ |
gastongab | 4:c7be673eb4a1 | 209 | |
gastongab | 2:0a8622662f6d | 210 | |
gastongab | 2:0a8622662f6d | 211 | } |
aschut | 0:90750f158475 | 212 | |
gastongab | 4:c7be673eb4a1 | 213 | |
gastongab | 4:c7be673eb4a1 | 214 | //Activate ticker for Movement state, filtering and Threshold checking |
gastongab | 4:c7be673eb4a1 | 215 | void movement_ticker_activator(){ |
gastongab | 4:c7be673eb4a1 | 216 | sample_ticker.attach(&emgsample, ts); |
gastongab | 4:c7be673eb4a1 | 217 | threshold_check_ticker.attach(&threshold_check, ts); |
gastongab | 4:c7be673eb4a1 | 218 | } |
gastongab | 4:c7be673eb4a1 | 219 | void movement_ticker_deactivator(){ |
gastongab | 4:c7be673eb4a1 | 220 | sample_ticker.detach(); |
gastongab | 4:c7be673eb4a1 | 221 | threshold_check_ticker.detach(); |
gastongab | 4:c7be673eb4a1 | 222 | } |
gastongab | 4:c7be673eb4a1 | 223 | |
aschut | 0:90750f158475 | 224 | enum states {MOTORS_OFF,CALIBRATION,HOMING,DEMO,MOVEMENT,CLICK}; |
gastongab | 2:0a8622662f6d | 225 | states currentState = MOTORS_OFF; //Chosen startingposition for states |
aschut | 0:90750f158475 | 226 | bool stateChanged = true; // Make sure the initialization of first state is executed |
aschut | 0:90750f158475 | 227 | |
aschut | 0:90750f158475 | 228 | void ProcessStateMachine(void) |
aschut | 0:90750f158475 | 229 | { |
aschut | 0:90750f158475 | 230 | switch (currentState) |
aschut | 0:90750f158475 | 231 | { |
aschut | 0:90750f158475 | 232 | case MOTORS_OFF: |
aschut | 0:90750f158475 | 233 | // Actions |
aschut | 0:90750f158475 | 234 | if (stateChanged) |
aschut | 0:90750f158475 | 235 | { |
aschut | 0:90750f158475 | 236 | // state initialization: rood |
aschut | 0:90750f158475 | 237 | led1 = 1; |
aschut | 0:90750f158475 | 238 | led2 = 0; |
aschut | 0:90750f158475 | 239 | led3 = 1; |
aschut | 0:90750f158475 | 240 | wait (1); |
aschut | 0:90750f158475 | 241 | stateChanged = false; |
aschut | 0:90750f158475 | 242 | } |
aschut | 0:90750f158475 | 243 | |
aschut | 0:90750f158475 | 244 | // State transition logic: Als button 1 word ingedrukt --> calibratie, anders motor uithouden |
aschut | 0:90750f158475 | 245 | if (!button1) |
aschut | 0:90750f158475 | 246 | { |
aschut | 0:90750f158475 | 247 | currentState = CALIBRATION; |
aschut | 0:90750f158475 | 248 | stateChanged = true; |
aschut | 0:90750f158475 | 249 | } |
aschut | 0:90750f158475 | 250 | else if (!button2) |
aschut | 0:90750f158475 | 251 | { |
aschut | 0:90750f158475 | 252 | currentState = HOMING ; |
aschut | 0:90750f158475 | 253 | stateChanged = true; |
aschut | 0:90750f158475 | 254 | } |
aschut | 0:90750f158475 | 255 | else |
aschut | 0:90750f158475 | 256 | { |
aschut | 0:90750f158475 | 257 | currentState = MOTORS_OFF; |
aschut | 0:90750f158475 | 258 | stateChanged = true; |
aschut | 0:90750f158475 | 259 | } |
aschut | 0:90750f158475 | 260 | |
aschut | 0:90750f158475 | 261 | break; |
aschut | 0:90750f158475 | 262 | |
aschut | 0:90750f158475 | 263 | case CALIBRATION: |
aschut | 0:90750f158475 | 264 | // Actions |
aschut | 0:90750f158475 | 265 | if (stateChanged) |
aschut | 0:90750f158475 | 266 | { |
aschut | 0:90750f158475 | 267 | // state initialization: oranje |
gastongab | 2:0a8622662f6d | 268 | temp_highest_emg1 = 0; //highest detected value right biceps |
gastongab | 2:0a8622662f6d | 269 | temp_highest_emg2 = 0; |
gastongab | 2:0a8622662f6d | 270 | temp_highest_emg3 = 0; |
gastongab | 2:0a8622662f6d | 271 | temp_highest_emg4 = 0; |
gastongab | 2:0a8622662f6d | 272 | |
gastongab | 2:0a8622662f6d | 273 | timer_calibration.reset(); |
gastongab | 2:0a8622662f6d | 274 | timer_calibration.start(); |
gastongab | 4:c7be673eb4a1 | 275 | |
gastongab | 4:c7be673eb4a1 | 276 | sample_ticker.attach(&emgsample, ts); |
gastongab | 4:c7be673eb4a1 | 277 | CalibrationEMG(); |
gastongab | 4:c7be673eb4a1 | 278 | sample_ticker.detach(); |
gastongab | 4:c7be673eb4a1 | 279 | timer_calibration.stop(); |
gastongab | 4:c7be673eb4a1 | 280 | |
aschut | 0:90750f158475 | 281 | |
aschut | 0:90750f158475 | 282 | stateChanged = false; |
aschut | 0:90750f158475 | 283 | } |
aschut | 0:90750f158475 | 284 | |
aschut | 0:90750f158475 | 285 | // State transition logic: automatisch terug naar motors off. |
aschut | 0:90750f158475 | 286 | |
aschut | 0:90750f158475 | 287 | currentState = MOTORS_OFF; |
aschut | 0:90750f158475 | 288 | stateChanged = true; |
aschut | 0:90750f158475 | 289 | break; |
aschut | 0:90750f158475 | 290 | |
aschut | 0:90750f158475 | 291 | case HOMING: |
aschut | 0:90750f158475 | 292 | // Actions |
aschut | 0:90750f158475 | 293 | if (stateChanged) |
aschut | 0:90750f158475 | 294 | { |
aschut | 0:90750f158475 | 295 | // state initialization: green |
gastongab | 3:3a9fdac2ba69 | 296 | t.reset(); |
aschut | 0:90750f158475 | 297 | t.start(); |
aschut | 0:90750f158475 | 298 | led1 = 0; |
aschut | 0:90750f158475 | 299 | led2 = 1; |
aschut | 0:90750f158475 | 300 | led3 = 1; |
aschut | 0:90750f158475 | 301 | wait (1); |
aschut | 0:90750f158475 | 302 | |
aschut | 0:90750f158475 | 303 | stateChanged = false; |
aschut | 0:90750f158475 | 304 | } |
aschut | 0:90750f158475 | 305 | |
aschut | 0:90750f158475 | 306 | // State transition logic: naar DEMO (button1), naar MOVEMENT(button2) |
aschut | 0:90750f158475 | 307 | if (!button1) |
aschut | 0:90750f158475 | 308 | { |
aschut | 0:90750f158475 | 309 | currentState = DEMO; |
aschut | 0:90750f158475 | 310 | stateChanged = true; |
aschut | 0:90750f158475 | 311 | } |
aschut | 0:90750f158475 | 312 | else if (!button2) |
aschut | 0:90750f158475 | 313 | { |
aschut | 0:90750f158475 | 314 | currentState = MOVEMENT ; |
aschut | 0:90750f158475 | 315 | stateChanged = true; |
aschut | 0:90750f158475 | 316 | } |
aschut | 0:90750f158475 | 317 | else if (t>300) |
aschut | 0:90750f158475 | 318 | { |
aschut | 0:90750f158475 | 319 | t.stop(); |
aschut | 0:90750f158475 | 320 | t.reset(); |
aschut | 0:90750f158475 | 321 | currentState = MOTORS_OFF ; |
aschut | 0:90750f158475 | 322 | stateChanged = true; |
aschut | 0:90750f158475 | 323 | } |
aschut | 0:90750f158475 | 324 | else |
aschut | 0:90750f158475 | 325 | { |
aschut | 0:90750f158475 | 326 | currentState = HOMING ; |
aschut | 0:90750f158475 | 327 | stateChanged = true; |
aschut | 0:90750f158475 | 328 | } |
aschut | 0:90750f158475 | 329 | break; |
aschut | 0:90750f158475 | 330 | |
aschut | 0:90750f158475 | 331 | case DEMO: |
aschut | 0:90750f158475 | 332 | // Actions |
aschut | 0:90750f158475 | 333 | if (stateChanged) |
aschut | 0:90750f158475 | 334 | { |
aschut | 0:90750f158475 | 335 | // state initialization: light blue |
aschut | 0:90750f158475 | 336 | led1 = 0; |
aschut | 0:90750f158475 | 337 | led2 = 1; |
aschut | 0:90750f158475 | 338 | led3 = 0; |
aschut | 0:90750f158475 | 339 | wait (1); |
aschut | 0:90750f158475 | 340 | |
aschut | 0:90750f158475 | 341 | stateChanged = false; |
aschut | 0:90750f158475 | 342 | } |
aschut | 0:90750f158475 | 343 | |
aschut | 0:90750f158475 | 344 | // State transition logic: automatisch terug naar HOMING |
aschut | 0:90750f158475 | 345 | currentState = HOMING; |
aschut | 0:90750f158475 | 346 | stateChanged = true; |
aschut | 0:90750f158475 | 347 | break; |
aschut | 0:90750f158475 | 348 | |
aschut | 0:90750f158475 | 349 | case MOVEMENT: |
aschut | 0:90750f158475 | 350 | // Actions |
aschut | 0:90750f158475 | 351 | if (stateChanged) |
aschut | 0:90750f158475 | 352 | { |
aschut | 0:90750f158475 | 353 | // state initialization: purple |
gastongab | 4:c7be673eb4a1 | 354 | t.reset(); |
aschut | 0:90750f158475 | 355 | t.start(); |
gastongab | 4:c7be673eb4a1 | 356 | |
aschut | 0:90750f158475 | 357 | led1 = 1; |
aschut | 0:90750f158475 | 358 | led2 = 0; |
gastongab | 4:c7be673eb4a1 | 359 | led3 = 0; |
gastongab | 4:c7be673eb4a1 | 360 | wait(2); |
gastongab | 4:c7be673eb4a1 | 361 | |
gastongab | 4:c7be673eb4a1 | 362 | movement_ticker_activator(); |
gastongab | 3:3a9fdac2ba69 | 363 | |
gastongab | 4:c7be673eb4a1 | 364 | led1 = 0; |
gastongab | 4:c7be673eb4a1 | 365 | led2 = 0; |
gastongab | 4:c7be673eb4a1 | 366 | led3 = 0; |
gastongab | 4:c7be673eb4a1 | 367 | wait(2); |
gastongab | 4:c7be673eb4a1 | 368 | |
gastongab | 4:c7be673eb4a1 | 369 | |
aschut | 0:90750f158475 | 370 | stateChanged = false; |
aschut | 0:90750f158475 | 371 | } |
aschut | 0:90750f158475 | 372 | |
aschut | 0:90750f158475 | 373 | // State transition logic: naar CLICK (button1), naar MOTORS_OFF(button2) anders naar MOVEMENT |
aschut | 0:90750f158475 | 374 | if (!button1) |
aschut | 0:90750f158475 | 375 | { |
gastongab | 4:c7be673eb4a1 | 376 | movement_ticker_deactivator(); |
aschut | 0:90750f158475 | 377 | currentState = CLICK; |
aschut | 1:070092564648 | 378 | stateChanged = true; |
aschut | 0:90750f158475 | 379 | } |
aschut | 0:90750f158475 | 380 | else if (!button2) |
aschut | 0:90750f158475 | 381 | { |
gastongab | 4:c7be673eb4a1 | 382 | movement_ticker_deactivator(); |
aschut | 0:90750f158475 | 383 | currentState = MOTORS_OFF ; |
aschut | 0:90750f158475 | 384 | stateChanged = true; |
aschut | 0:90750f158475 | 385 | } |
aschut | 0:90750f158475 | 386 | else if (t>300) |
aschut | 0:90750f158475 | 387 | { |
gastongab | 4:c7be673eb4a1 | 388 | movement_ticker_deactivator(); |
aschut | 0:90750f158475 | 389 | t.stop(); |
aschut | 0:90750f158475 | 390 | t.reset(); |
aschut | 0:90750f158475 | 391 | currentState = HOMING ; |
aschut | 0:90750f158475 | 392 | stateChanged = true; |
aschut | 0:90750f158475 | 393 | } |
aschut | 0:90750f158475 | 394 | else |
gastongab | 4:c7be673eb4a1 | 395 | { |
gastongab | 4:c7be673eb4a1 | 396 | //For every muscle a different colour if threshold is passed |
gastongab | 4:c7be673eb4a1 | 397 | if(bicepsR==1){ |
gastongab | 4:c7be673eb4a1 | 398 | led1 = 0; |
gastongab | 4:c7be673eb4a1 | 399 | led2 = 1; |
gastongab | 4:c7be673eb4a1 | 400 | led3 = 1; |
gastongab | 4:c7be673eb4a1 | 401 | } |
gastongab | 4:c7be673eb4a1 | 402 | else if (bicepsR==0 && tricepsR==0 && bicepsL==0 && tricepsL==0 ){ |
gastongab | 4:c7be673eb4a1 | 403 | led1 = 1; |
gastongab | 4:c7be673eb4a1 | 404 | led2 = 1; |
gastongab | 4:c7be673eb4a1 | 405 | led3 = 1; |
gastongab | 4:c7be673eb4a1 | 406 | } |
gastongab | 4:c7be673eb4a1 | 407 | if(tricepsR==1){ |
gastongab | 4:c7be673eb4a1 | 408 | led1 = 1; |
gastongab | 4:c7be673eb4a1 | 409 | led2 = 0; |
gastongab | 4:c7be673eb4a1 | 410 | led3 = 1; |
gastongab | 4:c7be673eb4a1 | 411 | } |
gastongab | 4:c7be673eb4a1 | 412 | else if (bicepsR==0 && tricepsR==0 && bicepsL==0 && tricepsL==0 ){ |
gastongab | 4:c7be673eb4a1 | 413 | led1 = 1; |
gastongab | 4:c7be673eb4a1 | 414 | led2 = 1; |
gastongab | 4:c7be673eb4a1 | 415 | led3 = 1; |
gastongab | 4:c7be673eb4a1 | 416 | } |
gastongab | 4:c7be673eb4a1 | 417 | if(bicepsL==1){ |
gastongab | 4:c7be673eb4a1 | 418 | led1 = 1; |
gastongab | 4:c7be673eb4a1 | 419 | led2 = 1; |
gastongab | 4:c7be673eb4a1 | 420 | led3 = 0; |
gastongab | 4:c7be673eb4a1 | 421 | } |
gastongab | 4:c7be673eb4a1 | 422 | else if (bicepsR==0 && tricepsR==0 && bicepsL==0 && tricepsL==0 ){ |
gastongab | 4:c7be673eb4a1 | 423 | led1 = 1; |
gastongab | 4:c7be673eb4a1 | 424 | led2 = 1; |
gastongab | 4:c7be673eb4a1 | 425 | led3 = 1; |
gastongab | 4:c7be673eb4a1 | 426 | } |
gastongab | 4:c7be673eb4a1 | 427 | if(tricepsL==1){ |
gastongab | 4:c7be673eb4a1 | 428 | led1 = 1; |
gastongab | 4:c7be673eb4a1 | 429 | led2 = 0; |
gastongab | 4:c7be673eb4a1 | 430 | led3 = 0; |
gastongab | 4:c7be673eb4a1 | 431 | } |
gastongab | 4:c7be673eb4a1 | 432 | else if (bicepsR==0 && tricepsR==0 && bicepsL==0 && tricepsL==0 ){ |
gastongab | 4:c7be673eb4a1 | 433 | led1 = 1; |
gastongab | 4:c7be673eb4a1 | 434 | led2 = 1; |
gastongab | 4:c7be673eb4a1 | 435 | led3 = 1; |
gastongab | 4:c7be673eb4a1 | 436 | } |
aschut | 0:90750f158475 | 437 | currentState = MOVEMENT ; |
gastongab | 4:c7be673eb4a1 | 438 | stateChanged = false; |
aschut | 0:90750f158475 | 439 | } |
gastongab | 4:c7be673eb4a1 | 440 | |
aschut | 0:90750f158475 | 441 | break; |
aschut | 0:90750f158475 | 442 | |
aschut | 0:90750f158475 | 443 | case CLICK: |
aschut | 0:90750f158475 | 444 | // Actions |
aschut | 0:90750f158475 | 445 | if (stateChanged) |
aschut | 0:90750f158475 | 446 | { |
aschut | 0:90750f158475 | 447 | // state initialization: blue |
aschut | 0:90750f158475 | 448 | led1 = 1; |
aschut | 0:90750f158475 | 449 | led2 = 1; |
aschut | 0:90750f158475 | 450 | led3 = 0; |
aschut | 0:90750f158475 | 451 | wait (1); |
aschut | 0:90750f158475 | 452 | |
aschut | 0:90750f158475 | 453 | stateChanged = false; |
aschut | 0:90750f158475 | 454 | } |
aschut | 0:90750f158475 | 455 | |
aschut | 0:90750f158475 | 456 | // State transition logic: automatisch terug naar MOVEMENT. |
aschut | 0:90750f158475 | 457 | |
aschut | 0:90750f158475 | 458 | currentState = MOVEMENT; |
aschut | 0:90750f158475 | 459 | stateChanged = true; |
aschut | 0:90750f158475 | 460 | break; |
aschut | 0:90750f158475 | 461 | |
gastongab | 4:c7be673eb4a1 | 462 | } |
aschut | 0:90750f158475 | 463 | } |
aschut | 0:90750f158475 | 464 | |
aschut | 0:90750f158475 | 465 | int main() |
aschut | 0:90750f158475 | 466 | { |
gastongab | 4:c7be673eb4a1 | 467 | //BiQuad Chain add |
gastongab | 4:c7be673eb4a1 | 468 | highp1.add( &highp1_1 ).add( &highp1_2 ); |
gastongab | 4:c7be673eb4a1 | 469 | notch1.add( ¬ch1_1 ).add( ¬ch1_2 ); |
gastongab | 4:c7be673eb4a1 | 470 | lowp1.add( &lowp1_1 ).add(&lowp1_2); |
gastongab | 4:c7be673eb4a1 | 471 | |
gastongab | 4:c7be673eb4a1 | 472 | highp2.add( &highp2_1 ).add( &highp2_2 ); |
gastongab | 4:c7be673eb4a1 | 473 | notch2.add( ¬ch2_1 ).add( ¬ch2_2 ); |
gastongab | 4:c7be673eb4a1 | 474 | lowp2.add( &lowp2_1 ).add(&lowp2_2); |
gastongab | 4:c7be673eb4a1 | 475 | |
gastongab | 4:c7be673eb4a1 | 476 | highp3.add( &highp3_1 ).add( &highp3_2 ); |
gastongab | 4:c7be673eb4a1 | 477 | notch3.add( ¬ch3_1 ).add( ¬ch3_2 ); |
gastongab | 4:c7be673eb4a1 | 478 | lowp3.add( &lowp3_1 ).add(&lowp3_2); |
gastongab | 4:c7be673eb4a1 | 479 | |
gastongab | 4:c7be673eb4a1 | 480 | highp4.add( &highp4_1 ).add( &highp4_2 ); |
gastongab | 4:c7be673eb4a1 | 481 | notch4.add( ¬ch4_1 ).add( ¬ch4_2 ); |
gastongab | 4:c7be673eb4a1 | 482 | lowp4.add( &lowp4_1 ).add(&lowp4_2); |
gastongab | 4:c7be673eb4a1 | 483 | |
gastongab | 2:0a8622662f6d | 484 | pc.baud(115200); |
aschut | 0:90750f158475 | 485 | led1 = 1; |
aschut | 0:90750f158475 | 486 | led2 = 1; |
aschut | 0:90750f158475 | 487 | led3 = 1; |
gastongab | 4:c7be673eb4a1 | 488 | |
gastongab | 4:c7be673eb4a1 | 489 | while (true) |
gastongab | 4:c7be673eb4a1 | 490 | { |
aschut | 0:90750f158475 | 491 | ProcessStateMachine(); |
aschut | 0:90750f158475 | 492 | |
aschut | 0:90750f158475 | 493 | } |
aschut | 0:90750f158475 | 494 | |
aschut | 0:90750f158475 | 495 | } |
aschut | 0:90750f158475 | 496 | |
aschut | 0:90750f158475 | 497 | |
aschut | 0:90750f158475 | 498 | |
aschut | 0:90750f158475 | 499 | |
aschut | 0:90750f158475 | 500 | |
aschut | 0:90750f158475 | 501 |