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