Remco Dasselaar / Mbed 2 deprecated TotalControlEmg2

Dependencies:   HIDScope MODSERIAL QEI TextLCD mbed

Fork of TotalControlEmg2 by Remco Dasselaar

Revision:
50:16314b798754
Parent:
49:b571c822c3f9
Child:
51:dcbfdf3b9468
--- a/main.cpp	Tue Oct 27 12:01:44 2015 +0000
+++ b/main.cpp	Wed Oct 28 09:28:48 2015 +0000
@@ -58,7 +58,7 @@
     double ymax = KalibratieMax(emgL, true);    // Maxium value left EMG, boolean indicates left
     PRINT("EMG LEFT         well done!");       // LCD
     
-    if((ymax-ymin) < 0.05 || !runEmg){                 // No EMG connected
+    if((ymax-ymin) < 0.05 || !runEmg){                 // No EMG connected or button pushed
         ymin = 10;
         ymax = 10;
         }    
@@ -76,7 +76,7 @@
     double ymax = KalibratieMax(emgR, false);
     PRINT("EMG LEFT         well done!");
     
-    if((ymax-ymin) < 0.05|| !runEmg){                 
+    if((ymax-ymin) < 0.05|| !runEmg){                     
         ymin = 10;
         ymax = 10;
         }        
@@ -135,7 +135,7 @@
     }    
 void emgOnOff(){                // Set knop K64F, push  
     runEmg = !runEmg;           // switch enable emg
-    pc.printf("EMG is enabled: &i\r\n", runEmg); 
+    pc.printf("EMG is enabled: %i\r\n", runEmg); 
     } 
 void setControlFlag(){             // Go flag setButton     
     controlFlag = true;
@@ -173,7 +173,7 @@
         L = true;        
         }
                 
-    if((modeL>2 && L) && (modeR >2 && R)) {             // mode L and mode R both 3, and both has been 1 herefore 
+    if((modeL>2 && L) && (modeR >2 && R)) {             // mode L and mode R both 3, and both has been 1 
         both = true;                                    // Return true
         pwmM2.write(0);                                 // Aim motor freeze
         aimState = OFF;                                 // next state
@@ -286,18 +286,18 @@
                 R = false;
                 L = false;      
                 while(state == AIM){                              
-                    if(controlFlag){                   // motor control on GoFlag
+                    if(controlFlag){                        // motor control on GoFlag
                         controlFlag = false;                  
-                        checkAim();                     // Check position                       
+                        checkAim();                         // Check position                       
                         }
-                    if(emgFlag){                        // Go flag EMG sampel
+                    if(emgFlag){                            // Go flag EMG sampel
                         emgFlag = false;                                                            
                         if(controlAim()){                                                                   
                             pc.printf("Position chosen\r\n");       // terminal                        
                             state = BREAK;                // Next state (BREAK)                       
                             }                      
                         }
-                    if(lcdFlag){                            // LCD loopje to project 3 texts on lcd
+                    if(lcdFlag){                            // LCD loop to project 3 texts on lcd
                         lcdFlag = false;
                         if(i%3 == 0){
                             PRINT("Flex left or    right half to aim");
@@ -317,9 +317,9 @@
             case BREAK: {                
                 pc.printf("Set break percentage, current is: %i\r\n", breakPerc);
                 L = false;
-                R = false;
-                int i = 0;                                  // counter for lcd 
+                R = false;                
                 PRINT("L := -1, R := +1 L+R = continue");
+                
                 wait(1);     
                 while(state == BREAK){                                                                                                 
                     if(emgFlag){            // Go flag EMG sampelen                        
@@ -338,64 +338,64 @@
                             if(modeR < 2) {           // modeR is 1
                                 R = true;
                                 }
-                            if(modeL < 2) {           // modeL is 1
+                            if(modeL < 2) {           // modeL is 1 
                                 L = true;
                                 }                            
-                            if(L && R){                        // L and R has been 1                            
+                            if(L && R){                             // L and R has been 1                            
                                 if((modeL == 2) && L) {             // modeL = BREAK lower with one
-                                    if(boolBrake){
-                                        boolBrake = false;              
+                                    if(breakPerc > 0){
+                                        breakPerc--;              
                                         }
                                     L = false;                                
                                     } 
                                 else if((modeR == 2) && R) {        // modeR = Break +1
-                                    if(!boolBrake){
-                                        boolBrake = true;              
+                                    if(breakPerc < 3){
+                                        breakPerc ++;              
                                         }
                                     R = false;                        
                                     }                         
-                                if(modeL > 1 || modeR > 1){               // Print BREAK scale if one of both modes > 1
-                                    pc.printf("Current breaking scale: %i \r\n", boolBrake);
-                                    
+                                if(modeL > 1 || modeR > 1){         // Print BREAK scale if one of both modes > 1
+                                    pc.printf("Current breaking scale: %i \r\n", breakPerc);                                    
                                     }                     
                                 }
                             }
-                        if(lcdFlag){                                    // every 2 seconds update lcd
+                        if(lcdFlag){                                // every 2 seconds update lcd
                             lcdFlag = false;
                             lcd.cls();
-                            lcd.printf("Break scale: %i", boolBrake);
+                            lcd.printf("Break scale 0 - 4: %i", breakPerc);
                             }                    
                         }
                     }
-                PRINT("Break fixed");           // lcd
+                lcd.cls();
+                lcd.printf("Break fixed on: %i", breakPerc);           // lcd
                 pc.printf("Break fixed\r\n");   // terminal
                 L = false;      // modeL must be one for another action can take place
-                R = false;      // modeR ""
-                lcd.cls();                            
+                R = false;      // modeR ""                                            
                 break;
                 }                    
             case FIRE: {            
                 pc.printf("Shooting\r\n");      // terminal
                 PRINT("FIRING");                // lcd        
                 pwmM1.write(0.3);               // beam motor on
-                bool breaking = false;                 
-                if(boolBrake){
+                bool breaking = false;
+                int countBreak = 0;                 
+                if(breakPerc > 0){              
                     servo.pulsewidth(servoBreak);   // Servo to break position
                     breaking = true;           // boolean breaking?
-                    }        
-                //int countBreak = 2100*abs(breakPerc)/10;    // Amount of counts where must be breaked                                             
+                    countBreak = 2100*breakPerc/4;      // Amount of counts where must be breaked
+                    }                                                  
                 while(state == FIRE){           // while in FIRE state                                      
                     if(controlFlag){            // BREAK goFlag half rotation beam = breaking
                         controlFlag = false;    // GoFlag
                         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                   
-                        if(breaking && boolBrake){             
-                            if((counts+2100)%4200 < 100){
+                        if(breaking){             
+                            if((counts+countBreak)%4200 < 100){     // calculate with remainder, half revolution
                                 servo.pulsewidth(servoFree);        // Servo to free position
                                 breaking = false;
                                 }
                             }
-                        if(!breaking || !boolBrake){          // Not breaking
-                            if((counts-100)%4200 < 100){                          // rotated 1 rev?
+                        if(!breaking){          
+                            if((counts-100)%4200 < 100){                    // rotated 1 rev?
                                 pwmM1.write(0);                             // motor beam off
                                 pc.printf("Beam on startposition\r\n");     // terminal
                                 state = AIM;                                // Next stage