Remco Dasselaar / Mbed 2 deprecated TotalControlEmg2

Dependencies:   HIDScope MODSERIAL QEI TextLCD mbed

Fork of TotalControlEmg2 by Remco Dasselaar

Committer:
RemcoDas
Date:
Wed Oct 28 12:47:38 2015 +0000
Revision:
51:dcbfdf3b9468
Parent:
50:16314b798754
Child:
52:85ddaee35185
Faal

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RemcoDas 33:b4757132437e 1 //--------------------Include files and libraries-------
Bartvaart 0:557b1ff83a8a 2 #include "mbed.h"
RemcoDas 24:ddd69385b55f 3 #include "QEI.h"
RemcoDas 24:ddd69385b55f 4 #include "MODSERIAL.h"
RemcoDas 24:ddd69385b55f 5 #include "TextLCD.h"
Bartvaart 7:040591b3f019 6 #include "HIDScope.h"
Bartvaart 5:b400209df739 7 #include "Filterdesigns.h"
Bartvaart 17:cfe44346645c 8 #include "Kalibratie.h"
Bartvaart 18:eec0880fcded 9 #include "Mode.h"
RemcoDas 24:ddd69385b55f 10 //--------------------Classes------------------------
RemcoDas 33:b4757132437e 11 InterruptIn btnSet(PTC6); // Kalibration button
RemcoDas 46:ed8ada80ba17 12 InterruptIn emgSet(PTA4); // EMG on/off button
RemcoDas 24:ddd69385b55f 13 MODSERIAL pc(USBTX, USBRX); // Modserial voor Putty
RemcoDas 33:b4757132437e 14 TextLCD lcd(PTC5, PTC7, PTC0, PTC9, PTC8, PTC1); // LCD screen on inner row of K64F, rs, e, d4-d7
RemcoDas 24:ddd69385b55f 15 PwmOut servo(D3); // PwmOut servo
RemcoDas 33:b4757132437e 16 AnalogIn emgL(A0), emgR(A1); // Analog input of EMG, left and right
RemcoDas 33:b4757132437e 17 AnalogIn potL(A2), potR(A3); // Potential meter left and right
RemcoDas 44:97f5622db2c4 18 AnalogIn ksLeft(A5), ksRight(A4); // Killswitch left and right
RemcoDas 39:f0f9b3432f43 19 HIDScope scope(2); // Hidscope, amount of scopes
RemcoDas 43:929cab5358ee 20 Ticker EMGticker, tickerControl, tickerLcd; // Ticker for EMG, regulator and break
RemcoDas 33:b4757132437e 21 // QEI Encoder(port 1, port 2, ,counts/rev
RemcoDas 25:c97d079e07f3 22 QEI enc1 (D13, D12, NC, 64), enc2 (D11, D10, NC, 64);
RemcoDas 33:b4757132437e 23 // Motor1 met PWM power control and direction
RemcoDas 24:ddd69385b55f 24 PwmOut pwmM1(D6);
RemcoDas 24:ddd69385b55f 25 DigitalOut dirM1(D7);
RemcoDas 33:b4757132437e 26 // Motor2 met PWM power control and direction
RemcoDas 24:ddd69385b55f 27 PwmOut pwmM2(D5);
RemcoDas 24:ddd69385b55f 28 DigitalOut dirM2(D4);
RemcoDas 44:97f5622db2c4 29 enum spelfase {CALIBRATE_EMG, CALIBRATE_AIM, CALIBRATE_BEAM, AIM, BREAK, FIRE}; // Program states, ACKNOWLEDGEMENT switch: BMT groep 4 2014
RemcoDas 41:91c8c39d7a33 30 uint8_t state = CALIBRATE_EMG; // first state program
RemcoDas 33:b4757132437e 31 enum aimFase {OFF, CW, CCW}; // Aim motor possible states
RemcoDas 33:b4757132437e 32 uint8_t aimState = OFF; // first state aim motor
RemcoDas 25:c97d079e07f3 33 //-------------------------------Variables---------------------------------------------------------------------
RemcoDas 51:dcbfdf3b9468 34 int maxCounts = 0; // max richt motor counts Aim motor
RemcoDas 33:b4757132437e 35 int breakPerc = 0;
RemcoDas 47:959ef2792024 36 volatile int modeL = 1, modeR = 1;
RemcoDas 51:dcbfdf3b9468 37 const double wM1 = 0.3, wM2 = 0.25; // Power motors
RemcoDas 51:dcbfdf3b9468 38 const double servoBreak = 0.0016, servoFree = 0.002; // Range servo,between servoL en servoR (= pulsewidth pwm servo)
RemcoDas 51:dcbfdf3b9468 39 const double tControl = 0.1, tLcd = 2; // ticker time (sec)
RemcoDas 43:929cab5358ee 40 const double Fs = 200; //Sample frequency Hz
RemcoDas 43:929cab5358ee 41 double t = 1/ Fs; // time EMGticker
RemcoDas 27:f62e450bb411 42 double thresholdlowL= 0, thresholdmidL = 0, thresholdhighL= 0;
RemcoDas 27:f62e450bb411 43 double thresholdlowR= 0, thresholdmidR = 0, thresholdhighR= 0;
RemcoDas 33:b4757132437e 44 double yL = 0, yR = 0; // y values EMG left and right
RemcoDas 33:b4757132437e 45 volatile bool L = false, R = false; // Booleans for checking if mode. has been 1?
RemcoDas 33:b4757132437e 46 volatile bool btn = false; // Button is pressed?
RemcoDas 46:ed8ada80ba17 47 volatile bool controlFlag = false, btnFlag = false, emgFlag = false, lcdFlag = false, runEmg = true; // Go flags
RemcoDas 24:ddd69385b55f 48 //----------------------------Functions-----------------------------------------------------------------------
RemcoDas 33:b4757132437e 49 void PRINT(const char* text){
RemcoDas 25:c97d079e07f3 50 lcd.cls(); // clear LCD scherm
RemcoDas 33:b4757132437e 51 lcd.printf(text); // print text op lcd
RemcoDas 25:c97d079e07f3 52 }
RichardRoos 34:60391fb72629 53 void EMGkalibratieL(){ // Determine thresholds left
RichardRoos 34:60391fb72629 54 PRINT("EMG LEFT relax muscle");
RichardRoos 34:60391fb72629 55 double ymin = KalibratieMin(emgL, true); // Minimum value left EMG, boolean indicates left
RemcoDas 27:f62e450bb411 56 wait(1);
RichardRoos 34:60391fb72629 57 PRINT("EMG LEFT flex muscle"); // LCD
RichardRoos 34:60391fb72629 58 double ymax = KalibratieMax(emgL, true); // Maxium value left EMG, boolean indicates left
RichardRoos 34:60391fb72629 59 PRINT("EMG LEFT well done!"); // LCD
RemcoDas 27:f62e450bb411 60
RemcoDas 51:dcbfdf3b9468 61 if((ymax-ymin) < 0.05 || !runEmg){ // No EMG connected or button pushed
RemcoDas 27:f62e450bb411 62 ymin = 10;
RemcoDas 27:f62e450bb411 63 ymax = 10;
RemcoDas 27:f62e450bb411 64 }
RemcoDas 33:b4757132437e 65 thresholdlowL = 4 * ymin; // Lowest threshold
RemcoDas 33:b4757132437e 66 thresholdmidL = 0.5 * ymax; // Midi threshold
RemcoDas 33:b4757132437e 67 thresholdhighL = 0.8 * ymax; // Highest threshold
RemcoDas 27:f62e450bb411 68
RemcoDas 33:b4757132437e 69 pc.printf("ymaxL = %f en yminL = %f \r\n",ymax, ymin);
RemcoDas 51:dcbfdf3b9468 70 wait(0.5);
RemcoDas 27:f62e450bb411 71 }
RemcoDas 33:b4757132437e 72 void EMGkalibratieR(){ // Determine thresholds right EMG, same as left
RichardRoos 34:60391fb72629 73 PRINT("EMG RIGHT relax muscle");
RemcoDas 30:8ae855348d22 74 double ymin = KalibratieMin(emgR, false);
Bartvaart 19:6c0245063b96 75 wait(1);
RemcoDas 41:91c8c39d7a33 76 PRINT("EMG RIGHT flex muscle");
RemcoDas 30:8ae855348d22 77 double ymax = KalibratieMax(emgR, false);
RemcoDas 41:91c8c39d7a33 78 PRINT("EMG LEFT well done!");
RemcoDas 27:f62e450bb411 79
RemcoDas 50:16314b798754 80 if((ymax-ymin) < 0.05|| !runEmg){
RemcoDas 27:f62e450bb411 81 ymin = 10;
RemcoDas 27:f62e450bb411 82 ymax = 10;
RemcoDas 29:9610df479f89 83 }
RemcoDas 33:b4757132437e 84 thresholdlowR = 4 * ymin;
RemcoDas 33:b4757132437e 85 thresholdmidR = 0.5 * ymax;
RemcoDas 27:f62e450bb411 86 thresholdhighR = 0.8 * ymax;
Bartvaart 22:c1811e60bfce 87
RemcoDas 33:b4757132437e 88 pc.printf("ymaxR = %f en yminR = %f \r\n",ymax, ymin); // terminal
RemcoDas 51:dcbfdf3b9468 89 wait(0.5);
RemcoDas 24:ddd69385b55f 90 }
RemcoDas 27:f62e450bb411 91 int EMGfilter(AnalogIn& emg, bool side){
RemcoDas 33:b4757132437e 92 double u = emg.read(); // read emg signal (left or right EMG)
RemcoDas 27:f62e450bb411 93 int mode = 1;
RemcoDas 30:8ae855348d22 94 if(side){
RemcoDas 33:b4757132437e 95 double y = FilterdesignsLeft(u); // filter signal left EMG
RemcoDas 47:959ef2792024 96 mode = Mode(modeL, y, thresholdlowL, thresholdmidL, thresholdhighL); // Determine mode with thresholds (1, 2, 3)
RemcoDas 27:f62e450bb411 97 }
RemcoDas 30:8ae855348d22 98 else {
RemcoDas 33:b4757132437e 99 double y = FilterdesignsRight(u);
RemcoDas 47:959ef2792024 100 mode = Mode(modeR, y, thresholdlowR, thresholdmidR, thresholdhighR); // right EMG
RemcoDas 27:f62e450bb411 101 }
RemcoDas 25:c97d079e07f3 102 return mode;
RemcoDas 26:d9855716ced7 103 }
RemcoDas 33:b4757132437e 104 int PotReader(AnalogIn& pot){ // read potentialmeter and determine its mode (1 = default, 2, 3)
RemcoDas 26:d9855716ced7 105 double volt = pot.read();
RemcoDas 41:91c8c39d7a33 106 int mode = 1;
RemcoDas 26:d9855716ced7 107 if(volt > 0.8){
RemcoDas 26:d9855716ced7 108 mode = 3;
RemcoDas 26:d9855716ced7 109 }
RemcoDas 26:d9855716ced7 110 else if(volt>0.35 && volt<0.65){
RemcoDas 26:d9855716ced7 111 mode = 2;
RemcoDas 26:d9855716ced7 112 }
RemcoDas 26:d9855716ced7 113 return mode;
RemcoDas 26:d9855716ced7 114 }
RemcoDas 33:b4757132437e 115 int defMode(AnalogIn& emg, AnalogIn& pot, bool side){ // Determine mode both from EMG and Potentialmeter, ONE of them must be ONE!
RemcoDas 27:f62e450bb411 116 int emgMode = EMGfilter(emg, side);
RemcoDas 26:d9855716ced7 117 int potMode = PotReader(pot);
RemcoDas 26:d9855716ced7 118 int mode = 1;
RemcoDas 33:b4757132437e 119 if(!(emgMode==1) != !(potMode==1)){ // emgMode = 1 XOR potMode = 1
RemcoDas 33:b4757132437e 120 if(emgMode > potMode){ // maximum of emg and pot
RemcoDas 26:d9855716ced7 121 mode = emgMode;
RemcoDas 26:d9855716ced7 122 }
RemcoDas 26:d9855716ced7 123 else{
RemcoDas 26:d9855716ced7 124 mode = potMode;
RemcoDas 26:d9855716ced7 125 }
RemcoDas 48:07e3cf7dc819 126 }
RemcoDas 26:d9855716ced7 127 return mode;
RemcoDas 26:d9855716ced7 128 }
RemcoDas 33:b4757132437e 129 void setEmgFlag(){ // Goflag EMG
RemcoDas 25:c97d079e07f3 130 emgFlag = true;
RichardRoos 34:60391fb72629 131 }
RichardRoos 34:60391fb72629 132 void setLcdFlag(){ // Goflag EMG
RichardRoos 34:60391fb72629 133 lcdFlag = true;
RichardRoos 34:60391fb72629 134 }
RemcoDas 24:ddd69385b55f 135 void btnSetAction(){ // Set knop K64F
RemcoDas 24:ddd69385b55f 136 btn = true; // GoFlag setknop
RemcoDas 46:ed8ada80ba17 137 }
RemcoDas 46:ed8ada80ba17 138 void emgOnOff(){ // Set knop K64F, push
RemcoDas 46:ed8ada80ba17 139 runEmg = !runEmg; // switch enable emg
RemcoDas 50:16314b798754 140 pc.printf("EMG is enabled: %i\r\n", runEmg);
RemcoDas 26:d9855716ced7 141 }
RemcoDas 33:b4757132437e 142 void setControlFlag(){ // Go flag setButton
RemcoDas 33:b4757132437e 143 controlFlag = true;
RemcoDas 24:ddd69385b55f 144 }
RemcoDas 44:97f5622db2c4 145 void checkSide(AnalogIn& ks, int dir){
RemcoDas 45:520d86583ff6 146 if((dirM2.read() == dir) && (pwmM2.read()>0)){ // direction motor clashes with killswitch and motor is on
RemcoDas 51:dcbfdf3b9468 147 if(ks.read()>0.95){
RemcoDas 39:f0f9b3432f43 148 pwmM2.write(0); // Aim motor freeze
RemcoDas 39:f0f9b3432f43 149 pc.printf("BOEM! CRASH! KASTUK!\r\n"); // Terminal
RemcoDas 39:f0f9b3432f43 150 PRINT("BOEM! CRASH!"); // LCD
RemcoDas 39:f0f9b3432f43 151 }
RemcoDas 44:97f5622db2c4 152 }
RemcoDas 46:ed8ada80ba17 153 }
RemcoDas 45:520d86583ff6 154 void checkAim(){ // check if killswitch clashes with direction motor
RemcoDas 44:97f5622db2c4 155 checkSide(ksLeft, 1);
RemcoDas 44:97f5622db2c4 156 checkSide(ksRight, 0);
RemcoDas 24:ddd69385b55f 157 }
RemcoDas 33:b4757132437e 158 void motorAim(int dir){ // Rotate Aim motor with given direction
RemcoDas 33:b4757132437e 159 dirM2.write(dir);
RemcoDas 51:dcbfdf3b9468 160 pwmM2.write(wM2);
RemcoDas 24:ddd69385b55f 161 }
RemcoDas 33:b4757132437e 162 bool controlAim(){ // Function to control aim motor with modes
RemcoDas 33:b4757132437e 163 bool both = false; // boolean if both modes are 3
RemcoDas 47:959ef2792024 164 modeL = defMode(emgL, potL, true); // determine mode left
RemcoDas 47:959ef2792024 165 modeR = defMode(emgR, potR, false); // determine mode right
RemcoDas 27:f62e450bb411 166
RemcoDas 27:f62e450bb411 167 scope.set(0, modeL);
RemcoDas 27:f62e450bb411 168 scope.set(1, modeR);
RemcoDas 33:b4757132437e 169 scope.send(); //send values to HIDScope
RemcoDas 27:f62e450bb411 170
RemcoDas 33:b4757132437e 171 if(modeR < 2 && !R){ // control if mode has been 1
RemcoDas 27:f62e450bb411 172 R = true;
RemcoDas 27:f62e450bb411 173 }
RemcoDas 28:e6d2fe0e593e 174 if(modeL < 2 && !L){
RemcoDas 28:e6d2fe0e593e 175 L = true;
RemcoDas 24:ddd69385b55f 176 }
RemcoDas 28:e6d2fe0e593e 177
RemcoDas 50:16314b798754 178 if((modeL>2 && L) && (modeR >2 && R)) { // mode L and mode R both 3, and both has been 1
RemcoDas 26:d9855716ced7 179 both = true; // Return true
RemcoDas 33:b4757132437e 180 pwmM2.write(0); // Aim motor freeze
RemcoDas 33:b4757132437e 181 aimState = OFF; // next state
RemcoDas 28:e6d2fe0e593e 182 }
RemcoDas 45:520d86583ff6 183 else if((modeR == 2)) { // modeR ==2
RemcoDas 45:520d86583ff6 184 if(aimState != CCW){
RemcoDas 43:929cab5358ee 185 aimState = CCW; // Rotate CCW
RichardRoos 34:60391fb72629 186 pc.printf("Rotate -, CCW\r\n");
RemcoDas 44:97f5622db2c4 187 PRINT("Rotate CCW");
RemcoDas 45:520d86583ff6 188 motorAim(0);
RemcoDas 28:e6d2fe0e593e 189 }
RemcoDas 24:ddd69385b55f 190 }
RemcoDas 45:520d86583ff6 191 else if((modeL == 2)) { // modeL == 2
RemcoDas 45:520d86583ff6 192 if(aimState != CW){
RemcoDas 43:929cab5358ee 193 aimState = CW; // Rotate CW
RichardRoos 34:60391fb72629 194 pc.printf("Rotate +, CW\r\n");
RemcoDas 44:97f5622db2c4 195 PRINT("Rotate CW");
RemcoDas 28:e6d2fe0e593e 196 motorAim(1);
RemcoDas 28:e6d2fe0e593e 197 }
RemcoDas 45:520d86583ff6 198 }
RemcoDas 45:520d86583ff6 199 // modeR AND CCW OR modeL and CW
RemcoDas 45:520d86583ff6 200 else if((modeR == 1 && aimState == CCW) || (modeL == 1 && aimState == CW)) { // R=1 en CW
RemcoDas 45:520d86583ff6 201 pwmM2.write(0); // Aim motor freeze
RemcoDas 45:520d86583ff6 202 aimState = OFF;
RemcoDas 45:520d86583ff6 203 pc.printf("Freeze\r\n");
RemcoDas 45:520d86583ff6 204 PRINT("Freeze");
RemcoDas 45:520d86583ff6 205 }
RemcoDas 24:ddd69385b55f 206 return both;
RemcoDas 24:ddd69385b55f 207 }
RemcoDas 43:929cab5358ee 208 int main(){
RemcoDas 24:ddd69385b55f 209 btnSet.mode(PullUp); // Button mode
RemcoDas 46:ed8ada80ba17 210 btnSet.rise(&btnSetAction); // Connect button to function
RemcoDas 46:ed8ada80ba17 211 emgSet.mode(PullUp); // Button mode
RemcoDas 49:b571c822c3f9 212 emgSet.rise(&emgOnOff); // Connect button to function
RemcoDas 43:929cab5358ee 213 tickerControl.attach(&setControlFlag,tControl); // ticker control motor
RemcoDas 26:d9855716ced7 214 EMGticker.attach(&setEmgFlag, t); // ticker EMG, 500H
RichardRoos 34:60391fb72629 215 tickerLcd.attach(&setLcdFlag,tLcd); // ticker lcd
RemcoDas 25:c97d079e07f3 216
RemcoDas 33:b4757132437e 217 pc.printf("\n\n\n\n\n"); // Terminal
RichardRoos 34:60391fb72629 218 pc.printf("---NEW GAME---\r\n");
RemcoDas 33:b4757132437e 219 PRINT("--- NEW GAME ---"); // LCD
RemcoDas 33:b4757132437e 220 while(1){ // Run program
RemcoDas 24:ddd69385b55f 221 switch(state){
RichardRoos 34:60391fb72629 222 case CALIBRATE_EMG: {
RichardRoos 34:60391fb72629 223 EMGkalibratieL(); // calibrate left EMG, determine thresholds
RichardRoos 34:60391fb72629 224 EMGkalibratieR(); // calibrate right EMG, determine thresholds
RichardRoos 34:60391fb72629 225 state = CALIBRATE_AIM; // Next state
RemcoDas 24:ddd69385b55f 226 break;
RemcoDas 24:ddd69385b55f 227 }
RichardRoos 34:60391fb72629 228 case CALIBRATE_AIM: {
RemcoDas 33:b4757132437e 229 pwmM2.period(1/100000); // period motor 2
RemcoDas 51:dcbfdf3b9468 230 pc.printf("Position is kalibrating\r\n"); // terminal
RichardRoos 37:090ba5b1e655 231 PRINT("Wait a moment, please"); // LCD
RemcoDas 33:b4757132437e 232 dirM2.write(0); // direction aim motor
RemcoDas 51:dcbfdf3b9468 233 pwmM2.write(wM2); // percentage motor power
RemcoDas 51:dcbfdf3b9468 234 volatile bool calibratedR = false;
RemcoDas 51:dcbfdf3b9468 235 volatile bool calibratedL = false;
RemcoDas 38:08c8fd55a3bb 236 while(state==CALIBRATE_AIM){
RemcoDas 43:929cab5358ee 237 if(controlFlag){
RemcoDas 38:08c8fd55a3bb 238 controlFlag = false;
RemcoDas 51:dcbfdf3b9468 239 //pc.printf("ksleft: %f, ksRight: %f, boolL: %i, boolR: %i, counts: %i\r\n", ksLeft.read(), ksRight.read(), calibratedL, calibratedR, abs(enc2.getPulses()));
RemcoDas 51:dcbfdf3b9468 240 if(calibratedR == false){ // Not calibrated
RemcoDas 51:dcbfdf3b9468 241 if((ksRight.read()>0.95)){ // Killswitch?
RemcoDas 39:f0f9b3432f43 242 pwmM2.write(0); // Aim motor freeze
RemcoDas 51:dcbfdf3b9468 243 enc2.reset(); // Reset encoder
RemcoDas 51:dcbfdf3b9468 244 calibratedR = true;
RemcoDas 39:f0f9b3432f43 245 dirM2.write(1); // direction aim motor
RemcoDas 51:dcbfdf3b9468 246 pwmM2.write(wM2); // percentage motor power, turn it on
RemcoDas 51:dcbfdf3b9468 247 }
RemcoDas 51:dcbfdf3b9468 248 }
RemcoDas 51:dcbfdf3b9468 249 else if(calibratedL == false){ // Right switch kalibrated
RemcoDas 51:dcbfdf3b9468 250 if((ksLeft.read()>0.95)){
RemcoDas 51:dcbfdf3b9468 251 pwmM2.write(0); // Aim motor freeze
RemcoDas 51:dcbfdf3b9468 252 maxCounts = abs(enc2.getPulses());
RemcoDas 51:dcbfdf3b9468 253 calibratedL = true;
RemcoDas 51:dcbfdf3b9468 254 dirM2.write(0); // direction aim motor
RemcoDas 51:dcbfdf3b9468 255 pwmM2.write(0.2); // percentage motor power, turn it on
RemcoDas 39:f0f9b3432f43 256 }
RemcoDas 51:dcbfdf3b9468 257 }
RemcoDas 51:dcbfdf3b9468 258 else { // Botch sides calibrated
RemcoDas 51:dcbfdf3b9468 259 //checkAim(); // Check killswitches
RemcoDas 51:dcbfdf3b9468 260 if(abs(enc2.getPulses()) < (maxCounts/2) && (pwmM2.read()>0)){ // rotate aim motor to midi position
RemcoDas 39:f0f9b3432f43 261 pwmM2.write(0); // Aim motor freeze
RemcoDas 44:97f5622db2c4 262 state = CALIBRATE_BEAM; // next state
RemcoDas 51:dcbfdf3b9468 263 PRINT("Aim calibrated"); // LCD
RemcoDas 51:dcbfdf3b9468 264 pc.printf("Aim calibrated\r\n");// Terminal
RemcoDas 39:f0f9b3432f43 265 }
RemcoDas 51:dcbfdf3b9468 266 }
RemcoDas 39:f0f9b3432f43 267 }
RemcoDas 24:ddd69385b55f 268 }
RemcoDas 49:b571c822c3f9 269 lcd.cls();
RemcoDas 24:ddd69385b55f 270 break;
RemcoDas 24:ddd69385b55f 271 }
RemcoDas 44:97f5622db2c4 272 case CALIBRATE_BEAM: {
RemcoDas 39:f0f9b3432f43 273 pc.printf("Calibrate beam motor with setknop\r\n"); // Terminal
RemcoDas 39:f0f9b3432f43 274 dirM1.write(0); // direction beam motor
RichardRoos 34:60391fb72629 275 pwmM1.period(1/100000); // period beam motor
RemcoDas 33:b4757132437e 276 servo.period(0.02); // 20ms period servo
RichardRoos 34:60391fb72629 277 btn = false; // Button is unpressed
RemcoDas 46:ed8ada80ba17 278 PRINT("Calibrate beam to 10 o'clock");
RemcoDas 44:97f5622db2c4 279 while(state==CALIBRATE_BEAM){
RemcoDas 45:520d86583ff6 280 if(controlFlag){
RemcoDas 24:ddd69385b55f 281 pc.printf(""); // lege regel printen, anders doet setknop het niet
RemcoDas 45:520d86583ff6 282 controlFlag = false;
RemcoDas 45:520d86583ff6 283 if(btn){ // If setbutton is on or one mode is 3, and has been 1
RemcoDas 43:929cab5358ee 284 enc1.reset(); // encoder 1 reset
RichardRoos 34:60391fb72629 285 PRINT("Beam calibrated");
RemcoDas 38:08c8fd55a3bb 286 pc.printf("Beam calibrated\r\n");
RemcoDas 43:929cab5358ee 287 btn = false; // button op false
RemcoDas 43:929cab5358ee 288 state = AIM; // next state
RemcoDas 45:520d86583ff6 289 }
RemcoDas 24:ddd69385b55f 290 }
RemcoDas 24:ddd69385b55f 291 }
RichardRoos 35:012b2e045579 292 lcd.cls();
RemcoDas 24:ddd69385b55f 293 break;
RemcoDas 24:ddd69385b55f 294 }
RemcoDas 24:ddd69385b55f 295 case AIM: {
RichardRoos 34:60391fb72629 296 pc.printf("Aim with EMG\r\n"); // terminal
RemcoDas 39:f0f9b3432f43 297 int i = 0; // counter for lcd
RemcoDas 39:f0f9b3432f43 298 R = false;
RemcoDas 39:f0f9b3432f43 299 L = false;
RemcoDas 46:ed8ada80ba17 300 while(state == AIM){
RemcoDas 50:16314b798754 301 if(controlFlag){ // motor control on GoFlag
RemcoDas 46:ed8ada80ba17 302 controlFlag = false;
RemcoDas 50:16314b798754 303 checkAim(); // Check position
RemcoDas 46:ed8ada80ba17 304 }
RemcoDas 50:16314b798754 305 if(emgFlag){ // Go flag EMG sampel
RemcoDas 46:ed8ada80ba17 306 emgFlag = false;
RemcoDas 46:ed8ada80ba17 307 if(controlAim()){
RemcoDas 46:ed8ada80ba17 308 pc.printf("Position chosen\r\n"); // terminal
RemcoDas 46:ed8ada80ba17 309 state = BREAK; // Next state (BREAK)
RemcoDas 46:ed8ada80ba17 310 }
RemcoDas 46:ed8ada80ba17 311 }
RemcoDas 50:16314b798754 312 if(lcdFlag){ // LCD loop to project 3 texts on lcd
RichardRoos 35:012b2e045579 313 lcdFlag = false;
RichardRoos 34:60391fb72629 314 if(i%3 == 0){
RemcoDas 51:dcbfdf3b9468 315 PRINT("Flex L or R half to aim");
RichardRoos 34:60391fb72629 316 }
RichardRoos 34:60391fb72629 317 else if(i%3 == 1){
RemcoDas 44:97f5622db2c4 318 PRINT("Flex both half to freeze");
RichardRoos 34:60391fb72629 319 }
RichardRoos 34:60391fb72629 320 else {
RemcoDas 44:97f5622db2c4 321 PRINT("Flex both full to continue");
RichardRoos 34:60391fb72629 322 }
RichardRoos 34:60391fb72629 323 i++;
RemcoDas 24:ddd69385b55f 324 }
RichardRoos 35:012b2e045579 325 }
RichardRoos 35:012b2e045579 326 lcd.cls();
RemcoDas 24:ddd69385b55f 327 break;
RemcoDas 24:ddd69385b55f 328 }
RemcoDas 33:b4757132437e 329 case BREAK: {
RemcoDas 44:97f5622db2c4 330 pc.printf("Set break percentage, current is: %i\r\n", breakPerc);
RemcoDas 24:ddd69385b55f 331 L = false;
RemcoDas 50:16314b798754 332 R = false;
RemcoDas 44:97f5622db2c4 333 PRINT("L := -1, R := +1 L+R = continue");
RemcoDas 50:16314b798754 334
RemcoDas 44:97f5622db2c4 335 wait(1);
RemcoDas 44:97f5622db2c4 336 while(state == BREAK){
RemcoDas 46:ed8ada80ba17 337 if(emgFlag){ // Go flag EMG sampelen
RemcoDas 44:97f5622db2c4 338
RemcoDas 26:d9855716ced7 339 emgFlag = false;
RemcoDas 47:959ef2792024 340 modeL = defMode(emgL, potL, true);
RemcoDas 47:959ef2792024 341 modeR = defMode(emgR, potR, false);
RemcoDas 43:929cab5358ee 342
RemcoDas 44:97f5622db2c4 343 if((modeL > 1) && (modeR > 1) && L && R) { // both modes > 1
RemcoDas 44:97f5622db2c4 344 state = FIRE;
RemcoDas 41:91c8c39d7a33 345 }
RemcoDas 44:97f5622db2c4 346 else if((modeL > 2 && L)|| (modeR > 2 && R)) { // left of right mode 3 = fire
RemcoDas 41:91c8c39d7a33 347 state = FIRE;
RemcoDas 41:91c8c39d7a33 348 }
RemcoDas 43:929cab5358ee 349 else {
RemcoDas 43:929cab5358ee 350 if(modeR < 2) { // modeR is 1
RemcoDas 43:929cab5358ee 351 R = true;
RemcoDas 43:929cab5358ee 352 }
RemcoDas 50:16314b798754 353 if(modeL < 2) { // modeL is 1
RemcoDas 43:929cab5358ee 354 L = true;
RemcoDas 43:929cab5358ee 355 }
RemcoDas 50:16314b798754 356 if(L && R){ // L and R has been 1
RemcoDas 43:929cab5358ee 357 if((modeL == 2) && L) { // modeL = BREAK lower with one
RemcoDas 50:16314b798754 358 if(breakPerc > 0){
RemcoDas 50:16314b798754 359 breakPerc--;
RemcoDas 43:929cab5358ee 360 }
RemcoDas 43:929cab5358ee 361 L = false;
RemcoDas 43:929cab5358ee 362 }
RemcoDas 43:929cab5358ee 363 else if((modeR == 2) && R) { // modeR = Break +1
RemcoDas 50:16314b798754 364 if(breakPerc < 3){
RemcoDas 50:16314b798754 365 breakPerc ++;
RemcoDas 43:929cab5358ee 366 }
RemcoDas 43:929cab5358ee 367 R = false;
RemcoDas 43:929cab5358ee 368 }
RemcoDas 50:16314b798754 369 if(modeL > 1 || modeR > 1){ // Print BREAK scale if one of both modes > 1
RemcoDas 50:16314b798754 370 pc.printf("Current breaking scale: %i \r\n", breakPerc);
RemcoDas 43:929cab5358ee 371 }
RemcoDas 43:929cab5358ee 372 }
RemcoDas 46:ed8ada80ba17 373 }
RemcoDas 50:16314b798754 374 if(lcdFlag){ // every 2 seconds update lcd
RemcoDas 46:ed8ada80ba17 375 lcdFlag = false;
RemcoDas 46:ed8ada80ba17 376 lcd.cls();
RemcoDas 50:16314b798754 377 lcd.printf("Break scale 0 - 4: %i", breakPerc);
RemcoDas 26:d9855716ced7 378 }
RemcoDas 24:ddd69385b55f 379 }
RemcoDas 24:ddd69385b55f 380 }
RemcoDas 50:16314b798754 381 lcd.cls();
RemcoDas 50:16314b798754 382 lcd.printf("Break fixed on: %i", breakPerc); // lcd
RemcoDas 44:97f5622db2c4 383 pc.printf("Break fixed\r\n"); // terminal
RemcoDas 33:b4757132437e 384 L = false; // modeL must be one for another action can take place
RemcoDas 50:16314b798754 385 R = false; // modeR ""
RemcoDas 24:ddd69385b55f 386 break;
RemcoDas 24:ddd69385b55f 387 }
RemcoDas 24:ddd69385b55f 388 case FIRE: {
RemcoDas 43:929cab5358ee 389 pc.printf("Shooting\r\n"); // terminal
RemcoDas 43:929cab5358ee 390 PRINT("FIRING"); // lcd
RemcoDas 51:dcbfdf3b9468 391 pwmM1.write(wM1); // beam motor on
RemcoDas 50:16314b798754 392 bool breaking = false;
RemcoDas 50:16314b798754 393 int countBreak = 0;
RemcoDas 50:16314b798754 394 if(breakPerc > 0){
RemcoDas 46:ed8ada80ba17 395 servo.pulsewidth(servoBreak); // Servo to break position
RemcoDas 46:ed8ada80ba17 396 breaking = true; // boolean breaking?
RemcoDas 51:dcbfdf3b9468 397 countBreak = 2100*breakPerc/3; // Amount of counts where must be breaked
RemcoDas 50:16314b798754 398 }
RemcoDas 40:35c7c60d7262 399 while(state == FIRE){ // while in FIRE state
RemcoDas 43:929cab5358ee 400 if(controlFlag){ // BREAK goFlag half rotation beam = breaking
RemcoDas 41:91c8c39d7a33 401 controlFlag = false; // GoFlag
RemcoDas 46:ed8ada80ba17 402 int counts = abs(enc1.getPulses())+250; // counts encoder beam, +250 is to correct 10 to 12 o'clock // encoder is 0 on 100 counts before 12 o'clock
RemcoDas 50:16314b798754 403 if(breaking){
RemcoDas 50:16314b798754 404 if((counts+countBreak)%4200 < 100){ // calculate with remainder, half revolution
RemcoDas 43:929cab5358ee 405 servo.pulsewidth(servoFree); // Servo to free position
RemcoDas 40:35c7c60d7262 406 breaking = false;
RemcoDas 40:35c7c60d7262 407 }
RemcoDas 24:ddd69385b55f 408 }
RemcoDas 50:16314b798754 409 if(!breaking){
RemcoDas 50:16314b798754 410 if((counts-100)%4200 < 100){ // rotated 1 rev?
RemcoDas 41:91c8c39d7a33 411 pwmM1.write(0); // motor beam off
RemcoDas 41:91c8c39d7a33 412 pc.printf("Beam on startposition\r\n"); // terminal
RemcoDas 41:91c8c39d7a33 413 state = AIM; // Next stage
RemcoDas 40:35c7c60d7262 414 }
RemcoDas 24:ddd69385b55f 415 }
RemcoDas 24:ddd69385b55f 416 }
RichardRoos 36:549d1ce7b96b 417 }
RichardRoos 36:549d1ce7b96b 418 lcd.cls();
RemcoDas 24:ddd69385b55f 419 break;
RemcoDas 24:ddd69385b55f 420 }
Bartvaart 19:6c0245063b96 421 }
Bartvaart 17:cfe44346645c 422 }
RemcoDas 24:ddd69385b55f 423 }