Remco Dasselaar / Mbed 2 deprecated TotalControlEmg2

Dependencies:   HIDScope MODSERIAL QEI TextLCD mbed

Fork of TotalControlEmg2 by Remco Dasselaar

Committer:
RemcoDas
Date:
Thu Oct 22 08:14:20 2015 +0000
Revision:
42:18cb904dab56
Parent:
41:91c8c39d7a33
Child:
43:929cab5358ee
Met killswitch als digitalIn, 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 33:b4757132437e 12 DigitalOut ledR(LED_RED), LedB(LED3); // Leds on K64F
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 42:18cb904dab56 18 DigitalIn ksLeft(PTE24), ksRight(PTE25); // Killswitch left and right
RemcoDas 39:f0f9b3432f43 19 HIDScope scope(2); // Hidscope, amount of scopes
RichardRoos 34:60391fb72629 20 Ticker EMGticker, tickerControl, tickerBreak, 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 41:91c8c39d7a33 29 enum spelfase {CALIBRATE_EMG, CALIBRATE_AIM, CALIBRATE_PEND, 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 33:b4757132437e 34 const int on = 0, off = 1; // on off
RichardRoos 34:60391fb72629 35 int maxCounts = 13000; // max richt motor counts Aim motor
RemcoDas 33:b4757132437e 36 int breakPerc = 0;
RemcoDas 40:35c7c60d7262 37 const double servoBreak = 0.0017, servoFree = 0.002; // Range servo,between servoL en servoR (= pulsewidth pwm servo)
RemcoDas 38:08c8fd55a3bb 38 const double tControl = 0.1, tBreak = 0.1, tLcd = 2; // tickers
RemcoDas 39:f0f9b3432f43 39 const double Fs = 200; //Sample frequency Hz
RemcoDas 33:b4757132437e 40 double t = 1/ Fs; // time EMGticker
RemcoDas 27:f62e450bb411 41 double thresholdlowL= 0, thresholdmidL = 0, thresholdhighL= 0;
RemcoDas 27:f62e450bb411 42 double thresholdlowR= 0, thresholdmidR = 0, thresholdhighR= 0;
RemcoDas 33:b4757132437e 43 double yL = 0, yR = 0; // y values EMG left and right
RemcoDas 33:b4757132437e 44 volatile bool L = false, R = false; // Booleans for checking if mode. has been 1?
RemcoDas 33:b4757132437e 45 volatile bool btn = false; // Button is pressed?
RichardRoos 34:60391fb72629 46 volatile bool controlFlag = false, btnFlag = false, breakFlag = false, emgFlag = false, lcdFlag = false; // Go flags
RemcoDas 24:ddd69385b55f 47 //----------------------------Functions-----------------------------------------------------------------------
RemcoDas 25:c97d079e07f3 48 void flipLed(DigitalOut& led){ //function to flip one LED
RemcoDas 25:c97d079e07f3 49 led.write(on);
RemcoDas 25:c97d079e07f3 50 wait(0.2);
RemcoDas 25:c97d079e07f3 51 led.write(off);
RemcoDas 25:c97d079e07f3 52 }
RemcoDas 33:b4757132437e 53 void PRINT(const char* text){
RemcoDas 25:c97d079e07f3 54 lcd.cls(); // clear LCD scherm
RemcoDas 33:b4757132437e 55 lcd.printf(text); // print text op lcd
RemcoDas 25:c97d079e07f3 56 }
RichardRoos 34:60391fb72629 57 void EMGkalibratieL(){ // Determine thresholds left
RichardRoos 34:60391fb72629 58 PRINT("EMG LEFT relax muscle");
RichardRoos 34:60391fb72629 59 double ymin = KalibratieMin(emgL, true); // Minimum value left EMG, boolean indicates left
RemcoDas 27:f62e450bb411 60 wait(1);
RichardRoos 34:60391fb72629 61 PRINT("EMG LEFT flex muscle"); // LCD
RichardRoos 34:60391fb72629 62 double ymax = KalibratieMax(emgL, true); // Maxium value left EMG, boolean indicates left
RichardRoos 34:60391fb72629 63 PRINT("EMG LEFT well done!"); // LCD
RemcoDas 27:f62e450bb411 64
RemcoDas 33:b4757132437e 65 if((ymax-ymin) < 0.05){ // No EMG connected
RemcoDas 27:f62e450bb411 66 ymin = 10;
RemcoDas 27:f62e450bb411 67 ymax = 10;
RemcoDas 27:f62e450bb411 68 }
RemcoDas 33:b4757132437e 69 thresholdlowL = 4 * ymin; // Lowest threshold
RemcoDas 33:b4757132437e 70 thresholdmidL = 0.5 * ymax; // Midi threshold
RemcoDas 33:b4757132437e 71 thresholdhighL = 0.8 * ymax; // Highest threshold
RemcoDas 27:f62e450bb411 72
RemcoDas 33:b4757132437e 73 pc.printf("ymaxL = %f en yminL = %f \r\n",ymax, ymin);
RemcoDas 27:f62e450bb411 74 }
RemcoDas 33:b4757132437e 75 void EMGkalibratieR(){ // Determine thresholds right EMG, same as left
RichardRoos 34:60391fb72629 76 PRINT("EMG RIGHT relax muscle");
RemcoDas 30:8ae855348d22 77 double ymin = KalibratieMin(emgR, false);
Bartvaart 19:6c0245063b96 78 wait(1);
RemcoDas 41:91c8c39d7a33 79 PRINT("EMG RIGHT flex muscle");
RemcoDas 30:8ae855348d22 80 double ymax = KalibratieMax(emgR, false);
RemcoDas 41:91c8c39d7a33 81 PRINT("EMG LEFT well done!");
RemcoDas 27:f62e450bb411 82
RemcoDas 33:b4757132437e 83 if((ymax-ymin) < 0.05){
RemcoDas 27:f62e450bb411 84 ymin = 10;
RemcoDas 27:f62e450bb411 85 ymax = 10;
RemcoDas 29:9610df479f89 86 }
RemcoDas 33:b4757132437e 87 thresholdlowR = 4 * ymin;
RemcoDas 33:b4757132437e 88 thresholdmidR = 0.5 * ymax;
RemcoDas 27:f62e450bb411 89 thresholdhighR = 0.8 * ymax;
Bartvaart 22:c1811e60bfce 90
RemcoDas 33:b4757132437e 91 pc.printf("ymaxR = %f en yminR = %f \r\n",ymax, ymin); // terminal
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 33:b4757132437e 98 mode = Mode(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 33:b4757132437e 102 mode = Mode(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 26:d9855716ced7 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 27:f62e450bb411 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 26:d9855716ced7 139 }
RemcoDas 33:b4757132437e 140 void setControlFlag(){ // Go flag setButton
RemcoDas 33:b4757132437e 141 controlFlag = true;
RemcoDas 24:ddd69385b55f 142 }
RemcoDas 33:b4757132437e 143 void setBreakFlag(){ // Go flag Break
RemcoDas 33:b4757132437e 144 breakFlag = true;
RemcoDas 24:ddd69385b55f 145 }
RemcoDas 41:91c8c39d7a33 146 void checkAim(){ // check if Killswitch is on or max counts is reached
RemcoDas 42:18cb904dab56 147 if((ksRight.read()) || (ksLeft.read())){
RemcoDas 41:91c8c39d7a33 148 if(pwmM1.read()>0){ // Aim motor is running
RemcoDas 39:f0f9b3432f43 149 pwmM2.write(0); // Aim motor freeze
RemcoDas 39:f0f9b3432f43 150 pc.printf("BOEM! CRASH! KASTUK!\r\n"); // Terminal
RemcoDas 39:f0f9b3432f43 151 PRINT("BOEM! CRASH!"); // LCD
RemcoDas 39:f0f9b3432f43 152 }
RemcoDas 33:b4757132437e 153 }
RemcoDas 24:ddd69385b55f 154 }
RemcoDas 33:b4757132437e 155 void motorAim(int dir){ // Rotate Aim motor with given direction
RemcoDas 33:b4757132437e 156 dirM2.write(dir);
RemcoDas 41:91c8c39d7a33 157 pwmM2.write(0.15);
RemcoDas 24:ddd69385b55f 158 }
RemcoDas 33:b4757132437e 159 bool controlAim(){ // Function to control aim motor with modes
RemcoDas 33:b4757132437e 160 bool both = false; // boolean if both modes are 3
RemcoDas 41:91c8c39d7a33 161 int modeL = defMode(emgL, potL, true); // determine mode left
RemcoDas 41:91c8c39d7a33 162 int modeR = defMode(emgR, potR, false); // determine mode right
RemcoDas 27:f62e450bb411 163
RemcoDas 27:f62e450bb411 164 scope.set(0, modeL);
RemcoDas 27:f62e450bb411 165 scope.set(1, modeR);
RemcoDas 33:b4757132437e 166 scope.send(); //send values to HIDScope
RemcoDas 27:f62e450bb411 167
RemcoDas 33:b4757132437e 168 if(modeR < 2 && !R){ // control if mode has been 1
RemcoDas 27:f62e450bb411 169 R = true;
RemcoDas 27:f62e450bb411 170 }
RemcoDas 28:e6d2fe0e593e 171 if(modeL < 2 && !L){
RemcoDas 28:e6d2fe0e593e 172 L = true;
RemcoDas 24:ddd69385b55f 173 }
RemcoDas 28:e6d2fe0e593e 174
RemcoDas 33:b4757132437e 175 if((modeL>2) && (modeR >2 && R && L)) { // mode L and mode R both 3, and both has been 1 herefore
RemcoDas 26:d9855716ced7 176 both = true; // Return true
RemcoDas 33:b4757132437e 177 pwmM2.write(0); // Aim motor freeze
RemcoDas 33:b4757132437e 178 aimState = OFF; // next state
RemcoDas 28:e6d2fe0e593e 179 }
RemcoDas 33:b4757132437e 180 else if((modeR == 2) && (modeL == 2)) { // modes are both 2
RemcoDas 33:b4757132437e 181 if(aimState!=OFF){ // only if aim motor is rotating
RemcoDas 33:b4757132437e 182 pwmM2.write(0); // Aim motor freeze
RemcoDas 33:b4757132437e 183 aimState = OFF; // motor state is off
RichardRoos 35:012b2e045579 184 pc.printf("Motor freeze\r\n"); // LCD
RemcoDas 33:b4757132437e 185 L = false; // Modes must be first 1 for another action
RemcoDas 33:b4757132437e 186 R = false; // ""
RemcoDas 28:e6d2fe0e593e 187 }
RemcoDas 24:ddd69385b55f 188 }
RemcoDas 33:b4757132437e 189 else if((modeL == 2) && (aimState != CCW && (modeR == 1))) { // modeL ==2 AND rotation is not CCW AND modeR has been 1
RemcoDas 28:e6d2fe0e593e 190 if(L){
RemcoDas 33:b4757132437e 191 aimState = CCW; // Rotate CCW
RichardRoos 34:60391fb72629 192 pc.printf("Rotate -, CCW\r\n");
RemcoDas 28:e6d2fe0e593e 193 motorAim(0);
RemcoDas 28:e6d2fe0e593e 194 }
RemcoDas 24:ddd69385b55f 195 }
RemcoDas 33:b4757132437e 196 else if((modeR == 2) && (aimState != CW && (modeL == 1))) { // modeR == 2 AND rotation is not CW AND modeL has been 1
RemcoDas 28:e6d2fe0e593e 197 if(R){
RemcoDas 33:b4757132437e 198 aimState = CW; // Rotate CW
RichardRoos 34:60391fb72629 199 pc.printf("Rotate +, CW\r\n");
RemcoDas 28:e6d2fe0e593e 200 motorAim(1);
RemcoDas 28:e6d2fe0e593e 201 }
RemcoDas 27:f62e450bb411 202 }
RemcoDas 24:ddd69385b55f 203 return both;
RemcoDas 24:ddd69385b55f 204 }
RemcoDas 24:ddd69385b55f 205 int main(){
RemcoDas 33:b4757132437e 206 flipLed(ledR); // test if code begins
RemcoDas 24:ddd69385b55f 207 btnSet.mode(PullUp); // Button mode
RemcoDas 33:b4757132437e 208 btnSet.rise(&btnSetAction); // Connect button to function
RemcoDas 33:b4757132437e 209 tickerControl.attach(&setControlFlag,tControl); // ticker control motor
RemcoDas 33:b4757132437e 210 tickerBreak.attach(&setBreakFlag,tBreak); // ticker break
RemcoDas 26:d9855716ced7 211 EMGticker.attach(&setEmgFlag, t); // ticker EMG, 500H
RichardRoos 34:60391fb72629 212 tickerLcd.attach(&setLcdFlag,tLcd); // ticker lcd
RemcoDas 25:c97d079e07f3 213
RemcoDas 33:b4757132437e 214 pc.printf("\n\n\n\n\n"); // Terminal
RichardRoos 34:60391fb72629 215 pc.printf("---NEW GAME---\r\n");
RemcoDas 33:b4757132437e 216 PRINT("--- NEW GAME ---"); // LCD
RemcoDas 33:b4757132437e 217 while(1){ // Run program
RemcoDas 24:ddd69385b55f 218 switch(state){
RichardRoos 34:60391fb72629 219 case CALIBRATE_EMG: {
RichardRoos 34:60391fb72629 220 EMGkalibratieL(); // calibrate left EMG, determine thresholds
RichardRoos 34:60391fb72629 221 EMGkalibratieR(); // calibrate right EMG, determine thresholds
RichardRoos 34:60391fb72629 222 state = CALIBRATE_AIM; // Next state
RemcoDas 24:ddd69385b55f 223 break;
RemcoDas 24:ddd69385b55f 224 }
RichardRoos 34:60391fb72629 225 case CALIBRATE_AIM: {
RemcoDas 33:b4757132437e 226 pwmM2.period(1/100000); // period motor 2
RemcoDas 33:b4757132437e 227 printf("Position is kalibrating\r\n"); // terminal
RichardRoos 37:090ba5b1e655 228 PRINT("Wait a moment, please"); // LCD
RemcoDas 33:b4757132437e 229 dirM2.write(0); // direction aim motor
RemcoDas 39:f0f9b3432f43 230 pwmM2.write(0.15); // percentage motor power
RichardRoos 34:60391fb72629 231 bool calibrated = false; //
RemcoDas 38:08c8fd55a3bb 232 while(state==CALIBRATE_AIM){
RemcoDas 38:08c8fd55a3bb 233 if(controlFlag){ // motor regelen op GoFlag
RemcoDas 38:08c8fd55a3bb 234 controlFlag = false;
RemcoDas 39:f0f9b3432f43 235 if(!calibrated){ // Not calibrated
RemcoDas 42:18cb904dab56 236 if((ksRight.read())){ // Killswitch?
RemcoDas 39:f0f9b3432f43 237 pwmM2.write(0); // Aim motor freeze
RemcoDas 39:f0f9b3432f43 238 enc2.reset(); // Reset encoder
RemcoDas 39:f0f9b3432f43 239 PRINT("Aim calibrated"); // LCD
RemcoDas 39:f0f9b3432f43 240 pc.printf("Aim calibrated\r\n");// Terminal
RemcoDas 39:f0f9b3432f43 241 calibrated = true; //
RemcoDas 39:f0f9b3432f43 242 //state = CALIBRATE_PEND; // next state
RemcoDas 39:f0f9b3432f43 243 wait(1);
RemcoDas 39:f0f9b3432f43 244 dirM2.write(1); // direction aim motor
RemcoDas 39:f0f9b3432f43 245 pwmM2.write(0.15); // percentage motor power, turn it on
RemcoDas 39:f0f9b3432f43 246 }
RemcoDas 39:f0f9b3432f43 247 }
RemcoDas 39:f0f9b3432f43 248 else { // Calibrated
RemcoDas 39:f0f9b3432f43 249 controlFlag = false;
RemcoDas 41:91c8c39d7a33 250 checkAim();
RemcoDas 41:91c8c39d7a33 251 pc.printf("aim counts: %i\r\n", abs(enc2.getPulses()));
RemcoDas 41:91c8c39d7a33 252 if(abs(enc2.getPulses()) > 600){ // rotate aim motor to midi position
RemcoDas 39:f0f9b3432f43 253 pwmM2.write(0); // Aim motor freeze
RemcoDas 39:f0f9b3432f43 254 state = CALIBRATE_PEND; // next state
RemcoDas 39:f0f9b3432f43 255 }
RemcoDas 39:f0f9b3432f43 256 }
RemcoDas 39:f0f9b3432f43 257 }
RemcoDas 24:ddd69385b55f 258 }
RemcoDas 24:ddd69385b55f 259 break;
RemcoDas 24:ddd69385b55f 260 }
RichardRoos 34:60391fb72629 261 case CALIBRATE_PEND: {
RemcoDas 39:f0f9b3432f43 262 pc.printf("Calibrate beam motor with setknop\r\n"); // Terminal
RemcoDas 39:f0f9b3432f43 263 dirM1.write(0); // direction beam motor
RichardRoos 34:60391fb72629 264 pwmM1.period(1/100000); // period beam motor
RemcoDas 33:b4757132437e 265 servo.period(0.02); // 20ms period servo
RemcoDas 39:f0f9b3432f43 266 //pwmM1.write(0.2); // Turn motor on, low power
RichardRoos 34:60391fb72629 267 btn = false; // Button is unpressed
RichardRoos 34:60391fb72629 268 R = false; // modeR must become 1
RichardRoos 34:60391fb72629 269 L = false; // modeL must become 1
RichardRoos 36:549d1ce7b96b 270 PRINT("Calibrate beam to 10 o'clock");
RichardRoos 36:549d1ce7b96b 271 wait(1);
RichardRoos 34:60391fb72629 272 PRINT("Flex right half to swing beam");
RichardRoos 34:60391fb72629 273 while(state==CALIBRATE_PEND){
RemcoDas 31:074b9d03d816 274 if(emgFlag){
RemcoDas 24:ddd69385b55f 275 pc.printf(""); // lege regel printen, anders doet setknop het niet
RemcoDas 31:074b9d03d816 276 emgFlag = false;
RemcoDas 30:8ae855348d22 277
RichardRoos 34:60391fb72629 278 int modeL = defMode(emgL, potL, true); // determine modeL
RichardRoos 34:60391fb72629 279 int modeR = defMode(emgR, potR, false); // determine modeR
RemcoDas 33:b4757132437e 280
RemcoDas 33:b4757132437e 281 if(modeR < 2) { // modeR == 1
RemcoDas 33:b4757132437e 282 R = true;
RemcoDas 33:b4757132437e 283 }
RemcoDas 33:b4757132437e 284 if(modeL < 2) { // modeL == 1
RemcoDas 33:b4757132437e 285 L = true;
RichardRoos 34:60391fb72629 286 }
RemcoDas 33:b4757132437e 287 if (btn || (modeL == 3 && L) || (modeR == 3 && R)){ // If setbutton is on or one mode is 3, and has been 1
RemcoDas 33:b4757132437e 288 pwmM1.write(0); // Motor 1 freeze
RemcoDas 33:b4757132437e 289 enc1.reset(); // encoder 1 reset
RichardRoos 34:60391fb72629 290 PRINT("Beam calibrated");
RemcoDas 38:08c8fd55a3bb 291 pc.printf("Beam calibrated\r\n");
RemcoDas 33:b4757132437e 292 btn = false; // button op false
RemcoDas 33:b4757132437e 293 state = AIM; // next state
RemcoDas 24:ddd69385b55f 294 }
RemcoDas 31:074b9d03d816 295 else if(modeL == 2){
RemcoDas 41:91c8c39d7a33 296 if(pwmM1.read()> 0){ // beam is moving
RemcoDas 41:91c8c39d7a33 297 pwmM1.write(0); // beam freeze
RemcoDas 41:91c8c39d7a33 298 PRINT("Flex both full to continue"); // LCD
RemcoDas 41:91c8c39d7a33 299 pc.printf("Beam freeze\r\n");
RemcoDas 41:91c8c39d7a33 300 }
RemcoDas 31:074b9d03d816 301 }
RichardRoos 34:60391fb72629 302 else if(modeR == 2){
RemcoDas 41:91c8c39d7a33 303 if(pwmM1.read()== 0){ // beam is freezed
RemcoDas 41:91c8c39d7a33 304 //pwmM1.write(0.2); // beam rotate
RemcoDas 41:91c8c39d7a33 305 PRINT("Flex left half to stop beam"); // LCD
RemcoDas 41:91c8c39d7a33 306 pc.printf("Beam move\r\n");
RemcoDas 41:91c8c39d7a33 307 }
RemcoDas 31:074b9d03d816 308 }
RemcoDas 24:ddd69385b55f 309 }
RemcoDas 24:ddd69385b55f 310 }
RichardRoos 35:012b2e045579 311 lcd.cls();
RemcoDas 24:ddd69385b55f 312 break;
RemcoDas 24:ddd69385b55f 313 }
RemcoDas 24:ddd69385b55f 314 case AIM: {
RichardRoos 34:60391fb72629 315 pc.printf("Aim with EMG\r\n"); // terminal
RemcoDas 39:f0f9b3432f43 316 int i = 0; // counter for lcd
RemcoDas 39:f0f9b3432f43 317 R = false;
RemcoDas 39:f0f9b3432f43 318 L = false;
RichardRoos 34:60391fb72629 319 while(state == AIM){
RichardRoos 36:549d1ce7b96b 320 if(lcdFlag){ // LCD loopje to project 3 texts on lcd
RichardRoos 35:012b2e045579 321 lcdFlag = false;
RichardRoos 34:60391fb72629 322 if(i%3 == 0){
RichardRoos 34:60391fb72629 323 PRINT("Flex left or right half to aim");
RichardRoos 34:60391fb72629 324 }
RichardRoos 34:60391fb72629 325 else if(i%3 == 1){
RichardRoos 34:60391fb72629 326 PRINT("Flex both half to freeze");
RichardRoos 34:60391fb72629 327 }
RichardRoos 34:60391fb72629 328 else {
RichardRoos 34:60391fb72629 329 PRINT("Flex both full to continue");
RichardRoos 34:60391fb72629 330 }
RichardRoos 34:60391fb72629 331 i++;
RichardRoos 34:60391fb72629 332 }
RichardRoos 34:60391fb72629 333
RemcoDas 33:b4757132437e 334 if(controlFlag){ // motor control on GoFlag
RemcoDas 33:b4757132437e 335 controlFlag = false;
RemcoDas 33:b4757132437e 336 checkAim(); // Check position
RemcoDas 24:ddd69385b55f 337 }
RemcoDas 33:b4757132437e 338 if(emgFlag){ // Go flag EMG sampel
RemcoDas 26:d9855716ced7 339 emgFlag = false;
RemcoDas 24:ddd69385b55f 340 if(controlAim()){
RichardRoos 35:012b2e045579 341 pc.printf("Position chosen\r\n"); // terminal
RichardRoos 34:60391fb72629 342 state = BREAK; // Next state (BREAK)
RemcoDas 24:ddd69385b55f 343 }
RemcoDas 24:ddd69385b55f 344 }
RichardRoos 35:012b2e045579 345 }
RichardRoos 35:012b2e045579 346 lcd.cls();
RemcoDas 24:ddd69385b55f 347 break;
RemcoDas 24:ddd69385b55f 348 }
RemcoDas 33:b4757132437e 349 case BREAK: {
RemcoDas 33:b4757132437e 350 pc.printf("Set break percentage, current is: %.0f\r\n", breakPerc);
RemcoDas 24:ddd69385b55f 351 L = false;
RemcoDas 24:ddd69385b55f 352 R = false;
RichardRoos 35:012b2e045579 353 int i = 0; // counter for lcd
RichardRoos 35:012b2e045579 354 while(state == BREAK){
RichardRoos 36:549d1ce7b96b 355 if(lcdFlag){ // LCD loop to project text on LCD
RichardRoos 35:012b2e045579 356 lcdFlag = false;
RichardRoos 35:012b2e045579 357 if(i%2 == 0){
RichardRoos 35:012b2e045579 358 PRINT("Flex L or R to adjust break");
RichardRoos 35:012b2e045579 359 }
RichardRoos 35:012b2e045579 360 else {
RichardRoos 35:012b2e045579 361 PRINT("Flex both to continue");
RichardRoos 35:012b2e045579 362 }
RichardRoos 35:012b2e045579 363 i++;
RichardRoos 35:012b2e045579 364 }
RemcoDas 26:d9855716ced7 365 if(emgFlag){ // Go flag EMG sampelen
RemcoDas 26:d9855716ced7 366 emgFlag = false;
RemcoDas 27:f62e450bb411 367 int modeL = defMode(emgL, potL, true);
RemcoDas 27:f62e450bb411 368 int modeR = defMode(emgR, potR, false);
RemcoDas 27:f62e450bb411 369
RemcoDas 33:b4757132437e 370 if(modeR < 2) { // modeR is 1
RemcoDas 24:ddd69385b55f 371 R = true;
RemcoDas 24:ddd69385b55f 372 }
RemcoDas 33:b4757132437e 373 if(modeL < 2) { // modeL is 1
RemcoDas 24:ddd69385b55f 374 L = true;
RemcoDas 24:ddd69385b55f 375 }
RemcoDas 39:f0f9b3432f43 376
RemcoDas 41:91c8c39d7a33 377 if((modeL > 1) && modeR > 1) { // both modes > 1
RemcoDas 41:91c8c39d7a33 378 state = FIRE;
RemcoDas 41:91c8c39d7a33 379 }
RemcoDas 41:91c8c39d7a33 380 else if(modeL > 2 || modeR > 2) { // left of right mode 3 = fire
RemcoDas 41:91c8c39d7a33 381 state = FIRE;
RemcoDas 41:91c8c39d7a33 382 }
RemcoDas 39:f0f9b3432f43 383 if(L && R){
RemcoDas 41:91c8c39d7a33 384 if((modeL == 2) && L) { // modeL = BREAK lower with one
RemcoDas 33:b4757132437e 385 if(breakPerc>1){
RemcoDas 33:b4757132437e 386 breakPerc--;
RemcoDas 24:ddd69385b55f 387 }
RemcoDas 26:d9855716ced7 388 L = false;
RemcoDas 24:ddd69385b55f 389 }
RemcoDas 33:b4757132437e 390 else if((modeR == 2) && R) { // modeR = Break +1
RemcoDas 33:b4757132437e 391 if(breakPerc<10){
RemcoDas 33:b4757132437e 392 breakPerc++;
RemcoDas 24:ddd69385b55f 393 }
RemcoDas 24:ddd69385b55f 394 R = false;
RemcoDas 26:d9855716ced7 395 }
RemcoDas 33:b4757132437e 396 if(modeL > 1 || modeR > 1){ // Print BREAK scale if one of both modes > 1
RemcoDas 33:b4757132437e 397 pc.printf("Current breaking scale: %i\r\n", breakPerc);
RichardRoos 34:60391fb72629 398 lcd.cls();
RichardRoos 36:549d1ce7b96b 399 lcd.printf("Break scale: %i", breakPerc);
RemcoDas 26:d9855716ced7 400 }
RemcoDas 26:d9855716ced7 401 }
RemcoDas 24:ddd69385b55f 402 }
RemcoDas 24:ddd69385b55f 403 }
RemcoDas 33:b4757132437e 404 L = false; // modeL must be one for another action can take place
RichardRoos 35:012b2e045579 405 R = false; // modeR ""
RichardRoos 35:012b2e045579 406 lcd.cls();
RemcoDas 24:ddd69385b55f 407 break;
RemcoDas 24:ddd69385b55f 408 }
RemcoDas 24:ddd69385b55f 409 case FIRE: {
RemcoDas 24:ddd69385b55f 410 pc.printf("Shooting\r\n");
RemcoDas 41:91c8c39d7a33 411 PRINT("FIRING");
RemcoDas 40:35c7c60d7262 412 pwmM1.write(0.25); // beam motor on
RemcoDas 40:35c7c60d7262 413 servo.pulsewidth(servoBreak); // Servo to break position
RemcoDas 41:91c8c39d7a33 414 bool breaking = true; // boolean breaking?
RemcoDas 41:91c8c39d7a33 415 int countBreak = 2100*abs(breakPerc)/10; // Amount of counts where must be breaked
RemcoDas 40:35c7c60d7262 416 while(state == FIRE){ // while in FIRE state
RemcoDas 41:91c8c39d7a33 417 if(controlFlag){ // BREAK goFlag half rotation beam = breaking
RemcoDas 41:91c8c39d7a33 418 controlFlag = false; // GoFlag
RemcoDas 41:91c8c39d7a33 419 int counts = abs(enc1.getPulses())+100;
RemcoDas 40:35c7c60d7262 420 if(breaking){ // encoder is 0 on 100 counts before 12 o'clock
RemcoDas 41:91c8c39d7a33 421 if((counts+countBreak)%4200 < 150){
RemcoDas 40:35c7c60d7262 422 servo.pulsewidth(servoFree); // Servo to free position
RemcoDas 40:35c7c60d7262 423 breaking = false;
RemcoDas 40:35c7c60d7262 424 }
RemcoDas 24:ddd69385b55f 425 }
RemcoDas 41:91c8c39d7a33 426 else{ // Not breaking
RemcoDas 41:91c8c39d7a33 427 if(counts%4200 < 400){ // rotated 1 rev?
RemcoDas 41:91c8c39d7a33 428 pwmM1.write(0); // motor beam off
RemcoDas 41:91c8c39d7a33 429 pc.printf("Beam on startposition\r\n"); // terminal
RemcoDas 41:91c8c39d7a33 430 state = AIM; // Next stage
RemcoDas 40:35c7c60d7262 431 }
RemcoDas 24:ddd69385b55f 432 }
RemcoDas 24:ddd69385b55f 433 }
RichardRoos 36:549d1ce7b96b 434 }
RichardRoos 36:549d1ce7b96b 435 lcd.cls();
RemcoDas 24:ddd69385b55f 436 break;
RemcoDas 24:ddd69385b55f 437 }
Bartvaart 19:6c0245063b96 438 }
Bartvaart 17:cfe44346645c 439 }
RemcoDas 24:ddd69385b55f 440 }