23:00

Dependencies:   biquadFilter MODSERIAL QEI Servo mbed

Fork of StateMachine_EMg_RKI_PID_MOTOR_DEMO_CLICK by Gaston Gabriël

Committer:
gastongab
Date:
Thu Nov 01 15:08:05 2018 +0000
Revision:
4:c7be673eb4a1
Parent:
3:3a9fdac2ba69
Child:
5:19f59a855475
16:08

Who changed what in which revision?

UserRevisionLine numberNew 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 //Send data to HIDScope
gastongab 2:0a8622662f6d 69 //scope.set(0,emg1_filtered ); ONLY FOR VISUALIZATION
gastongab 2:0a8622662f6d 70 //scope.set(1,emg2_filtered);
gastongab 2:0a8622662f6d 71 //scope.set(2,emg3_filtered);
gastongab 2:0a8622662f6d 72 //scope.set(3,emg4_filtered);
gastongab 2:0a8622662f6d 73 //scope.send();
gastongab 2:0a8622662f6d 74
gastongab 2:0a8622662f6d 75 }
gastongab 2:0a8622662f6d 76
gastongab 2:0a8622662f6d 77
gastongab 2:0a8622662f6d 78 //Define doubles for calibration and ticker
gastongab 2:0a8622662f6d 79 double ts = 0.001; //tijdsstap
gastongab 2:0a8622662f6d 80 double calibration_time = 55; //time EMG calibration should take
gastongab 2:0a8622662f6d 81
gastongab 2:0a8622662f6d 82 volatile double temp_highest_emg1 = 0; //highest detected value right biceps
gastongab 2:0a8622662f6d 83 volatile double temp_highest_emg2 = 0;
gastongab 2:0a8622662f6d 84 volatile double temp_highest_emg3 = 0;
gastongab 2:0a8622662f6d 85 volatile double temp_highest_emg4 = 0;
gastongab 2:0a8622662f6d 86
gastongab 2:0a8622662f6d 87 //Doubles for calculation threshold
gastongab 4:c7be673eb4a1 88 double p_t = 0.4; //set threshold at percentage of highest value
gastongab 4:c7be673eb4a1 89 volatile double threshold1;
gastongab 4:c7be673eb4a1 90 volatile double threshold2;
gastongab 4:c7be673eb4a1 91 volatile double threshold3;
gastongab 4:c7be673eb4a1 92 volatile double threshold4;
gastongab 2:0a8622662f6d 93
gastongab 2:0a8622662f6d 94 void CalibrationEMG()
gastongab 4:c7be673eb4a1 95 {
gastongab 2:0a8622662f6d 96 //static float samples = calibration_time/ts;
gastongab 2:0a8622662f6d 97 while(timer_calibration<55){
gastongab 2:0a8622662f6d 98 if(timer_calibration>0 && timer_calibration<10)
gastongab 4:c7be673eb4a1 99 {
gastongab 2:0a8622662f6d 100 led1=!led1;
gastongab 2:0a8622662f6d 101 if(emg1_filtered>temp_highest_emg1)
gastongab 2:0a8622662f6d 102 {
gastongab 2:0a8622662f6d 103 temp_highest_emg1= emg1_filtered;
gastongab 2:0a8622662f6d 104 }
gastongab 2:0a8622662f6d 105 }
gastongab 2:0a8622662f6d 106 if(timer_calibration>10 && timer_calibration<15)
gastongab 2:0a8622662f6d 107 {
gastongab 2:0a8622662f6d 108 led1=0;
gastongab 2:0a8622662f6d 109 led2=0;
gastongab 2:0a8622662f6d 110 led3=0;
gastongab 2:0a8622662f6d 111 }
gastongab 2:0a8622662f6d 112 if(timer_calibration>15 && timer_calibration<25)
gastongab 4:c7be673eb4a1 113 {
gastongab 2:0a8622662f6d 114 led2=!led2;
gastongab 2:0a8622662f6d 115 if(emg2_filtered>temp_highest_emg2)
gastongab 2:0a8622662f6d 116 {
gastongab 2:0a8622662f6d 117 temp_highest_emg2= emg2_filtered;
gastongab 4:c7be673eb4a1 118 pc.printf("Temp2 = %f \r\n",temp_highest_emg2);
gastongab 2:0a8622662f6d 119 }
gastongab 2:0a8622662f6d 120 }
gastongab 2:0a8622662f6d 121 if(timer_calibration>25 && timer_calibration<30)
gastongab 2:0a8622662f6d 122 {
gastongab 2:0a8622662f6d 123 led1=0;
gastongab 2:0a8622662f6d 124 led2=0;
gastongab 2:0a8622662f6d 125 led3=0;
gastongab 2:0a8622662f6d 126 }
gastongab 2:0a8622662f6d 127 if(timer_calibration>30 && timer_calibration<40)
gastongab 2:0a8622662f6d 128 {
gastongab 2:0a8622662f6d 129 led3=!led3;
gastongab 2:0a8622662f6d 130 if(emg3_filtered>temp_highest_emg3)
gastongab 2:0a8622662f6d 131 {
gastongab 2:0a8622662f6d 132 temp_highest_emg3= emg3_filtered;
gastongab 2:0a8622662f6d 133 }
gastongab 2:0a8622662f6d 134 }
gastongab 2:0a8622662f6d 135 if(timer_calibration>40 && timer_calibration<45)
gastongab 2:0a8622662f6d 136 {
gastongab 2:0a8622662f6d 137 led1=0;
gastongab 2:0a8622662f6d 138 led2=0;
gastongab 2:0a8622662f6d 139 led3=0;
gastongab 2:0a8622662f6d 140 }
gastongab 2:0a8622662f6d 141 if(timer_calibration>45 && timer_calibration<55)
gastongab 2:0a8622662f6d 142 {
gastongab 2:0a8622662f6d 143 led2=!led2;
gastongab 2:0a8622662f6d 144 led3=!led3;
gastongab 2:0a8622662f6d 145 if(emg3_filtered>temp_highest_emg3)
gastongab 2:0a8622662f6d 146 {
gastongab 2:0a8622662f6d 147 temp_highest_emg3= emg3_filtered;
gastongab 2:0a8622662f6d 148 }
gastongab 2:0a8622662f6d 149 }
gastongab 2:0a8622662f6d 150 led1=1;
gastongab 2:0a8622662f6d 151 led2=1;
gastongab 2:0a8622662f6d 152 led3=1;
gastongab 2:0a8622662f6d 153
gastongab 2:0a8622662f6d 154
gastongab 2:0a8622662f6d 155 }
gastongab 2:0a8622662f6d 156
gastongab 2:0a8622662f6d 157 pc.printf("Highest value right biceps= %f \r\n", temp_highest_emg1);
gastongab 2:0a8622662f6d 158 pc.printf("Highest value right triceps= %f \r\n", temp_highest_emg2);
gastongab 2:0a8622662f6d 159 pc.printf("Highest value left biceps= %f \r\n", temp_highest_emg3);
gastongab 2:0a8622662f6d 160 pc.printf("Highest value left triceps= %f \r\n", temp_highest_emg4);
gastongab 2:0a8622662f6d 161
gastongab 4:c7be673eb4a1 162
gastongab 2:0a8622662f6d 163 threshold1 = temp_highest_emg1*p_t;
gastongab 2:0a8622662f6d 164 threshold2 = temp_highest_emg2*p_t;
gastongab 2:0a8622662f6d 165 threshold3 = temp_highest_emg3*p_t;
gastongab 2:0a8622662f6d 166 threshold4 = temp_highest_emg4*p_t;
gastongab 2:0a8622662f6d 167 }
gastongab 2:0a8622662f6d 168
gastongab 4:c7be673eb4a1 169 //Check if emg_filtered has reached their threshold
gastongab 4:c7be673eb4a1 170 int bicepsR;
gastongab 4:c7be673eb4a1 171 int tricepsR;
gastongab 4:c7be673eb4a1 172 int bicepsL;
gastongab 4:c7be673eb4a1 173 int tricepsL;
gastongab 4:c7be673eb4a1 174
gastongab 2:0a8622662f6d 175 void threshold_check(){
gastongab 4:c7be673eb4a1 176
gastongab 2:0a8622662f6d 177 //EMG1 threshold check
gastongab 2:0a8622662f6d 178 if(emg1_filtered>threshold1){
gastongab 4:c7be673eb4a1 179 bicepsR = 1;
gastongab 2:0a8622662f6d 180 }
gastongab 2:0a8622662f6d 181 else{
gastongab 4:c7be673eb4a1 182 bicepsR= 0;
gastongab 2:0a8622662f6d 183 }
gastongab 2:0a8622662f6d 184 //EMG2 threshold check
gastongab 2:0a8622662f6d 185 if(emg2_filtered>threshold2){
gastongab 4:c7be673eb4a1 186 tricepsR = 1;
gastongab 2:0a8622662f6d 187 }
gastongab 2:0a8622662f6d 188 else{
gastongab 4:c7be673eb4a1 189 tricepsR= 0;
gastongab 2:0a8622662f6d 190 }
gastongab 2:0a8622662f6d 191 //EMG3 threshold check
gastongab 2:0a8622662f6d 192 if(emg3_filtered>threshold3){
gastongab 4:c7be673eb4a1 193 bicepsL = 1;
gastongab 2:0a8622662f6d 194 }
gastongab 2:0a8622662f6d 195 else{
gastongab 4:c7be673eb4a1 196 bicepsL= 0;
gastongab 2:0a8622662f6d 197 }
gastongab 2:0a8622662f6d 198 //EMG4 threshold check
gastongab 2:0a8622662f6d 199 if(emg4_filtered>threshold4){
gastongab 4:c7be673eb4a1 200 tricepsL = 1;
gastongab 2:0a8622662f6d 201 }
gastongab 2:0a8622662f6d 202 else{
gastongab 4:c7be673eb4a1 203 tricepsL= 0;
gastongab 2:0a8622662f6d 204 }
gastongab 4:c7be673eb4a1 205
gastongab 4:c7be673eb4a1 206 /*
gastongab 4:c7be673eb4a1 207 pc.printf("Biceps Right = %i", bicepsR);
gastongab 4:c7be673eb4a1 208 pc.printf("Triceps Right = %i",tricepsR);
gastongab 4:c7be673eb4a1 209 pc.printf("Biceps Left = %i", bicepsL);
gastongab 4:c7be673eb4a1 210 pc.printf("Triceps Left = %i", tricepsL);
gastongab 4:c7be673eb4a1 211 */
gastongab 4:c7be673eb4a1 212
gastongab 2:0a8622662f6d 213
gastongab 2:0a8622662f6d 214 }
aschut 0:90750f158475 215
gastongab 4:c7be673eb4a1 216
gastongab 4:c7be673eb4a1 217 //Activate ticker for Movement state, filtering and Threshold checking
gastongab 4:c7be673eb4a1 218 void movement_ticker_activator(){
gastongab 4:c7be673eb4a1 219 sample_ticker.attach(&emgsample, ts);
gastongab 4:c7be673eb4a1 220 threshold_check_ticker.attach(&threshold_check, ts);
gastongab 4:c7be673eb4a1 221 }
gastongab 4:c7be673eb4a1 222 void movement_ticker_deactivator(){
gastongab 4:c7be673eb4a1 223 sample_ticker.detach();
gastongab 4:c7be673eb4a1 224 threshold_check_ticker.detach();
gastongab 4:c7be673eb4a1 225 }
gastongab 4:c7be673eb4a1 226
aschut 0:90750f158475 227 enum states {MOTORS_OFF,CALIBRATION,HOMING,DEMO,MOVEMENT,CLICK};
gastongab 2:0a8622662f6d 228 states currentState = MOTORS_OFF; //Chosen startingposition for states
aschut 0:90750f158475 229 bool stateChanged = true; // Make sure the initialization of first state is executed
aschut 0:90750f158475 230
aschut 0:90750f158475 231 void ProcessStateMachine(void)
aschut 0:90750f158475 232 {
aschut 0:90750f158475 233 switch (currentState)
aschut 0:90750f158475 234 {
aschut 0:90750f158475 235 case MOTORS_OFF:
aschut 0:90750f158475 236 // Actions
aschut 0:90750f158475 237 if (stateChanged)
aschut 0:90750f158475 238 {
aschut 0:90750f158475 239 // state initialization: rood
aschut 0:90750f158475 240 led1 = 1;
aschut 0:90750f158475 241 led2 = 0;
aschut 0:90750f158475 242 led3 = 1;
aschut 0:90750f158475 243 wait (1);
aschut 0:90750f158475 244 stateChanged = false;
aschut 0:90750f158475 245 }
aschut 0:90750f158475 246
aschut 0:90750f158475 247 // State transition logic: Als button 1 word ingedrukt --> calibratie, anders motor uithouden
aschut 0:90750f158475 248 if (!button1)
aschut 0:90750f158475 249 {
aschut 0:90750f158475 250 currentState = CALIBRATION;
aschut 0:90750f158475 251 stateChanged = true;
aschut 0:90750f158475 252 }
aschut 0:90750f158475 253 else if (!button2)
aschut 0:90750f158475 254 {
aschut 0:90750f158475 255 currentState = HOMING ;
aschut 0:90750f158475 256 stateChanged = true;
aschut 0:90750f158475 257 }
aschut 0:90750f158475 258 else
aschut 0:90750f158475 259 {
aschut 0:90750f158475 260 currentState = MOTORS_OFF;
aschut 0:90750f158475 261 stateChanged = true;
aschut 0:90750f158475 262 }
aschut 0:90750f158475 263
aschut 0:90750f158475 264 break;
aschut 0:90750f158475 265
aschut 0:90750f158475 266 case CALIBRATION:
aschut 0:90750f158475 267 // Actions
aschut 0:90750f158475 268 if (stateChanged)
aschut 0:90750f158475 269 {
aschut 0:90750f158475 270 // state initialization: oranje
gastongab 2:0a8622662f6d 271 temp_highest_emg1 = 0; //highest detected value right biceps
gastongab 2:0a8622662f6d 272 temp_highest_emg2 = 0;
gastongab 2:0a8622662f6d 273 temp_highest_emg3 = 0;
gastongab 2:0a8622662f6d 274 temp_highest_emg4 = 0;
gastongab 2:0a8622662f6d 275
gastongab 2:0a8622662f6d 276 timer_calibration.reset();
gastongab 2:0a8622662f6d 277 timer_calibration.start();
gastongab 4:c7be673eb4a1 278
gastongab 4:c7be673eb4a1 279 sample_ticker.attach(&emgsample, ts);
gastongab 4:c7be673eb4a1 280 CalibrationEMG();
gastongab 4:c7be673eb4a1 281 sample_ticker.detach();
gastongab 4:c7be673eb4a1 282 timer_calibration.stop();
gastongab 4:c7be673eb4a1 283
aschut 0:90750f158475 284
aschut 0:90750f158475 285 stateChanged = false;
aschut 0:90750f158475 286 }
aschut 0:90750f158475 287
aschut 0:90750f158475 288 // State transition logic: automatisch terug naar motors off.
aschut 0:90750f158475 289
aschut 0:90750f158475 290 currentState = MOTORS_OFF;
aschut 0:90750f158475 291 stateChanged = true;
aschut 0:90750f158475 292 break;
aschut 0:90750f158475 293
aschut 0:90750f158475 294 case HOMING:
aschut 0:90750f158475 295 // Actions
aschut 0:90750f158475 296 if (stateChanged)
aschut 0:90750f158475 297 {
aschut 0:90750f158475 298 // state initialization: green
gastongab 3:3a9fdac2ba69 299 t.reset();
aschut 0:90750f158475 300 t.start();
aschut 0:90750f158475 301 led1 = 0;
aschut 0:90750f158475 302 led2 = 1;
aschut 0:90750f158475 303 led3 = 1;
aschut 0:90750f158475 304 wait (1);
aschut 0:90750f158475 305
aschut 0:90750f158475 306 stateChanged = false;
aschut 0:90750f158475 307 }
aschut 0:90750f158475 308
aschut 0:90750f158475 309 // State transition logic: naar DEMO (button1), naar MOVEMENT(button2)
aschut 0:90750f158475 310 if (!button1)
aschut 0:90750f158475 311 {
aschut 0:90750f158475 312 currentState = DEMO;
aschut 0:90750f158475 313 stateChanged = true;
aschut 0:90750f158475 314 }
aschut 0:90750f158475 315 else if (!button2)
aschut 0:90750f158475 316 {
aschut 0:90750f158475 317 currentState = MOVEMENT ;
aschut 0:90750f158475 318 stateChanged = true;
aschut 0:90750f158475 319 }
aschut 0:90750f158475 320 else if (t>300)
aschut 0:90750f158475 321 {
aschut 0:90750f158475 322 t.stop();
aschut 0:90750f158475 323 t.reset();
aschut 0:90750f158475 324 currentState = MOTORS_OFF ;
aschut 0:90750f158475 325 stateChanged = true;
aschut 0:90750f158475 326 }
aschut 0:90750f158475 327 else
aschut 0:90750f158475 328 {
aschut 0:90750f158475 329 currentState = HOMING ;
aschut 0:90750f158475 330 stateChanged = true;
aschut 0:90750f158475 331 }
aschut 0:90750f158475 332 break;
aschut 0:90750f158475 333
aschut 0:90750f158475 334 case DEMO:
aschut 0:90750f158475 335 // Actions
aschut 0:90750f158475 336 if (stateChanged)
aschut 0:90750f158475 337 {
aschut 0:90750f158475 338 // state initialization: light blue
aschut 0:90750f158475 339 led1 = 0;
aschut 0:90750f158475 340 led2 = 1;
aschut 0:90750f158475 341 led3 = 0;
aschut 0:90750f158475 342 wait (1);
aschut 0:90750f158475 343
aschut 0:90750f158475 344 stateChanged = false;
aschut 0:90750f158475 345 }
aschut 0:90750f158475 346
aschut 0:90750f158475 347 // State transition logic: automatisch terug naar HOMING
aschut 0:90750f158475 348 currentState = HOMING;
aschut 0:90750f158475 349 stateChanged = true;
aschut 0:90750f158475 350 break;
aschut 0:90750f158475 351
aschut 0:90750f158475 352 case MOVEMENT:
aschut 0:90750f158475 353 // Actions
aschut 0:90750f158475 354 if (stateChanged)
aschut 0:90750f158475 355 {
aschut 0:90750f158475 356 // state initialization: purple
gastongab 4:c7be673eb4a1 357 t.reset();
aschut 0:90750f158475 358 t.start();
gastongab 4:c7be673eb4a1 359
aschut 0:90750f158475 360 led1 = 1;
aschut 0:90750f158475 361 led2 = 0;
gastongab 4:c7be673eb4a1 362 led3 = 0;
gastongab 4:c7be673eb4a1 363 wait(2);
gastongab 4:c7be673eb4a1 364
gastongab 4:c7be673eb4a1 365 movement_ticker_activator();
gastongab 3:3a9fdac2ba69 366
gastongab 4:c7be673eb4a1 367 led1 = 0;
gastongab 4:c7be673eb4a1 368 led2 = 0;
gastongab 4:c7be673eb4a1 369 led3 = 0;
gastongab 4:c7be673eb4a1 370 wait(2);
gastongab 4:c7be673eb4a1 371
gastongab 4:c7be673eb4a1 372
aschut 0:90750f158475 373 stateChanged = false;
aschut 0:90750f158475 374 }
aschut 0:90750f158475 375
aschut 0:90750f158475 376 // State transition logic: naar CLICK (button1), naar MOTORS_OFF(button2) anders naar MOVEMENT
aschut 0:90750f158475 377 if (!button1)
aschut 0:90750f158475 378 {
gastongab 4:c7be673eb4a1 379 movement_ticker_deactivator();
aschut 0:90750f158475 380 currentState = CLICK;
aschut 1:070092564648 381 stateChanged = true;
aschut 0:90750f158475 382 }
aschut 0:90750f158475 383 else if (!button2)
aschut 0:90750f158475 384 {
gastongab 4:c7be673eb4a1 385 movement_ticker_deactivator();
aschut 0:90750f158475 386 currentState = MOTORS_OFF ;
aschut 0:90750f158475 387 stateChanged = true;
aschut 0:90750f158475 388 }
aschut 0:90750f158475 389 else if (t>300)
aschut 0:90750f158475 390 {
gastongab 4:c7be673eb4a1 391 movement_ticker_deactivator();
aschut 0:90750f158475 392 t.stop();
aschut 0:90750f158475 393 t.reset();
aschut 0:90750f158475 394 currentState = HOMING ;
aschut 0:90750f158475 395 stateChanged = true;
aschut 0:90750f158475 396 }
aschut 0:90750f158475 397 else
gastongab 4:c7be673eb4a1 398 {
gastongab 4:c7be673eb4a1 399 //For every muscle a different colour if threshold is passed
gastongab 4:c7be673eb4a1 400 if(bicepsR==1){
gastongab 4:c7be673eb4a1 401 led1 = 0;
gastongab 4:c7be673eb4a1 402 led2 = 1;
gastongab 4:c7be673eb4a1 403 led3 = 1;
gastongab 4:c7be673eb4a1 404 }
gastongab 4:c7be673eb4a1 405 else if (bicepsR==0 && tricepsR==0 && bicepsL==0 && tricepsL==0 ){
gastongab 4:c7be673eb4a1 406 led1 = 1;
gastongab 4:c7be673eb4a1 407 led2 = 1;
gastongab 4:c7be673eb4a1 408 led3 = 1;
gastongab 4:c7be673eb4a1 409 }
gastongab 4:c7be673eb4a1 410 if(tricepsR==1){
gastongab 4:c7be673eb4a1 411 led1 = 1;
gastongab 4:c7be673eb4a1 412 led2 = 0;
gastongab 4:c7be673eb4a1 413 led3 = 1;
gastongab 4:c7be673eb4a1 414 }
gastongab 4:c7be673eb4a1 415 else if (bicepsR==0 && tricepsR==0 && bicepsL==0 && tricepsL==0 ){
gastongab 4:c7be673eb4a1 416 led1 = 1;
gastongab 4:c7be673eb4a1 417 led2 = 1;
gastongab 4:c7be673eb4a1 418 led3 = 1;
gastongab 4:c7be673eb4a1 419 }
gastongab 4:c7be673eb4a1 420 if(bicepsL==1){
gastongab 4:c7be673eb4a1 421 led1 = 1;
gastongab 4:c7be673eb4a1 422 led2 = 1;
gastongab 4:c7be673eb4a1 423 led3 = 0;
gastongab 4:c7be673eb4a1 424 }
gastongab 4:c7be673eb4a1 425 else if (bicepsR==0 && tricepsR==0 && bicepsL==0 && tricepsL==0 ){
gastongab 4:c7be673eb4a1 426 led1 = 1;
gastongab 4:c7be673eb4a1 427 led2 = 1;
gastongab 4:c7be673eb4a1 428 led3 = 1;
gastongab 4:c7be673eb4a1 429 }
gastongab 4:c7be673eb4a1 430 if(tricepsL==1){
gastongab 4:c7be673eb4a1 431 led1 = 1;
gastongab 4:c7be673eb4a1 432 led2 = 0;
gastongab 4:c7be673eb4a1 433 led3 = 0;
gastongab 4:c7be673eb4a1 434 }
gastongab 4:c7be673eb4a1 435 else if (bicepsR==0 && tricepsR==0 && bicepsL==0 && tricepsL==0 ){
gastongab 4:c7be673eb4a1 436 led1 = 1;
gastongab 4:c7be673eb4a1 437 led2 = 1;
gastongab 4:c7be673eb4a1 438 led3 = 1;
gastongab 4:c7be673eb4a1 439 }
aschut 0:90750f158475 440 currentState = MOVEMENT ;
gastongab 4:c7be673eb4a1 441 stateChanged = false;
aschut 0:90750f158475 442 }
gastongab 4:c7be673eb4a1 443
aschut 0:90750f158475 444 break;
aschut 0:90750f158475 445
aschut 0:90750f158475 446 case CLICK:
aschut 0:90750f158475 447 // Actions
aschut 0:90750f158475 448 if (stateChanged)
aschut 0:90750f158475 449 {
aschut 0:90750f158475 450 // state initialization: blue
aschut 0:90750f158475 451 led1 = 1;
aschut 0:90750f158475 452 led2 = 1;
aschut 0:90750f158475 453 led3 = 0;
aschut 0:90750f158475 454 wait (1);
aschut 0:90750f158475 455
aschut 0:90750f158475 456 stateChanged = false;
aschut 0:90750f158475 457 }
aschut 0:90750f158475 458
aschut 0:90750f158475 459 // State transition logic: automatisch terug naar MOVEMENT.
aschut 0:90750f158475 460
aschut 0:90750f158475 461 currentState = MOVEMENT;
aschut 0:90750f158475 462 stateChanged = true;
aschut 0:90750f158475 463 break;
aschut 0:90750f158475 464
gastongab 4:c7be673eb4a1 465 }
aschut 0:90750f158475 466 }
aschut 0:90750f158475 467
aschut 0:90750f158475 468 int main()
aschut 0:90750f158475 469 {
gastongab 4:c7be673eb4a1 470 //BiQuad Chain add
gastongab 4:c7be673eb4a1 471 highp1.add( &highp1_1 ).add( &highp1_2 );
gastongab 4:c7be673eb4a1 472 notch1.add( &notch1_1 ).add( &notch1_2 );
gastongab 4:c7be673eb4a1 473 lowp1.add( &lowp1_1 ).add(&lowp1_2);
gastongab 4:c7be673eb4a1 474
gastongab 4:c7be673eb4a1 475 highp2.add( &highp2_1 ).add( &highp2_2 );
gastongab 4:c7be673eb4a1 476 notch2.add( &notch2_1 ).add( &notch2_2 );
gastongab 4:c7be673eb4a1 477 lowp2.add( &lowp2_1 ).add(&lowp2_2);
gastongab 4:c7be673eb4a1 478
gastongab 4:c7be673eb4a1 479 highp3.add( &highp3_1 ).add( &highp3_2 );
gastongab 4:c7be673eb4a1 480 notch3.add( &notch3_1 ).add( &notch3_2 );
gastongab 4:c7be673eb4a1 481 lowp3.add( &lowp3_1 ).add(&lowp3_2);
gastongab 4:c7be673eb4a1 482
gastongab 4:c7be673eb4a1 483 highp4.add( &highp4_1 ).add( &highp4_2 );
gastongab 4:c7be673eb4a1 484 notch4.add( &notch4_1 ).add( &notch4_2 );
gastongab 4:c7be673eb4a1 485 lowp4.add( &lowp4_1 ).add(&lowp4_2);
gastongab 4:c7be673eb4a1 486
gastongab 2:0a8622662f6d 487 pc.baud(115200);
aschut 0:90750f158475 488 led1 = 1;
aschut 0:90750f158475 489 led2 = 1;
aschut 0:90750f158475 490 led3 = 1;
gastongab 4:c7be673eb4a1 491
gastongab 4:c7be673eb4a1 492 while (true)
gastongab 4:c7be673eb4a1 493 {
aschut 0:90750f158475 494 ProcessStateMachine();
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
aschut 0:90750f158475 502
aschut 0:90750f158475 503
aschut 0:90750f158475 504