Remco Dasselaar / Mbed 2 deprecated TotalControlEmg2

Dependencies:   HIDScope MODSERIAL QEI TextLCD mbed

Fork of TotalControlEmg2 by Remco Dasselaar

Committer:
RemcoDas
Date:
Wed Oct 28 13:22:20 2015 +0000
Revision:
54:062b9f36edf2
Parent:
53:4a0857fbbb34
Child:
55:a435e46299ea
Switch in calibrate aim, nog niet getest; ;

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