Grupo T / Mbed OS GRUPOT
Revision:
20:1d98a6f51dee
Parent:
19:e69ff16f35aa
Child:
21:c6c33381fc5f
diff -r e69ff16f35aa -r 1d98a6f51dee main.cpp
--- a/main.cpp	Tue May 08 16:41:37 2018 +0000
+++ b/main.cpp	Wed May 09 11:53:46 2018 +0000
@@ -27,7 +27,7 @@
 DigitalIn zUp(D15);
 DigitalIn zDwn(D14);
 
-InterruptIn saveBtn(D13);
+InterruptIn saveBtn(A5);
 InterruptIn startBtn(D12);
 
 DigitalIn botao(D11);
@@ -103,6 +103,9 @@
 int pontos_restantes = 0;
 
 int startSaved = 0;
+int autoReload = 0;
+int exitSavedPointsLoop = 0;
+int keepSavedPointsReload = 0;
 
 // prototype functions
 void setupPins();
@@ -150,192 +153,194 @@
     
     saveBtn.rise(&savePoint);
     startBtn.rise(&startSavedPoints);
-    
-    if(modeStatus){
-        //Código de JOG
-        printf("JOG Selected\n\r");
-        while(1){        
-            valX = joyX;
-            valY = joyY;
+        
+    while(1){
+        if(modeStatus){
+            autoReload = 0;
+            saved = 0;
+            startSaved = 0;
             
-            valZUp = zUp;
-            valZDwn = zDwn;
-            
-            if(valX > 0.7){
-                if(x_dir != x_plus){
-                    x_dir = x_plus;
+            //Código de JOG
+            printf("JOG Selected\n\r");
+            while(1){        
+                valX = joyX;
+                valY = joyY;
+                
+                valZUp = zUp;
+                valZDwn = zDwn;
+                
+                if(valX > 0.7){
+                    if(x_dir != x_plus){
+                        x_dir = x_plus;
+                    }
+                    activeX = 1;     
                 }
-                activeX = 1; 
-                //totalX+=1;    
-            }
-            else if(valX < 0.3){
-                if(x_dir != x_minus){
-                    x_dir = x_minus;   
+                else if(valX < 0.3){
+                    if(x_dir != x_minus){
+                        x_dir = x_minus;   
+                    }
+                    activeX = 1;  
+                }
+                else{
+                    activeX = 0;
                 }
-                activeX = 1;   
-                //totalX-=1;  
-            }
-            else{
-                activeX = 0;
-            }
-            
-            //----------------------------------------------------------------------
-            
-            if(valY > 0.7){
-                if(y_dir != y_plus){
-                    y_dir = y_plus;   
+                
+                //----------------------------------------------------------------------
+                
+                if(valY > 0.7){
+                    if(y_dir != y_plus){
+                        y_dir = y_plus;   
+                    }
+                    activeY = 1;  
                 }
-                activeY = 1;  
-                //totalY+=1;   
-            }
-            else if(valY < 0.3){
-                if(y_dir != y_minus){
-                    y_dir = y_minus;    
+                else if(valY < 0.3){
+                    if(y_dir != y_minus){
+                        y_dir = y_minus;    
+                    }
+                    activeY = 1;  
+                }
+                else{
+                    activeY = 0;
                 }
-                activeY = 1; 
-                //totalY-=1;   
-            }
-            else{
-                activeY = 0;
-            }
-            
-            //----------------------------------------------------------------------   
-            
-            if(!valZUp && valZDwn){
-                if(z_dir != z_minus){
-                    z_dir = z_minus;    
+                
+                //----------------------------------------------------------------------   
+                
+                if(!valZUp && valZDwn){
+                    if(z_dir != z_minus){
+                        z_dir = z_minus;    
+                    }
+                    activeZ = 1;   
                 }
-                activeZ = 1;  
-                //totalZ-=1;  
-            }
-            else if(!valZDwn && valZUp){
-                if(z_dir != z_plus){
-                    z_dir = z_plus;   
+                else if(!valZDwn && valZUp){
+                    if(z_dir != z_plus){
+                        z_dir = z_plus;   
+                    }
+                    activeZ = 1;   
+                }
+                else {
+                    activeZ = 0;    
                 }
-                activeZ = 1; 
-                //totalZ+=1;   
-            }
-            else {
-                activeZ = 0;    
-            }
-            
-            //----------------------------------------------------------------------
-
-            move(ppsMax, x_dir, y_dir, z_dir, activeX, activeY, activeZ);  
-            X = steps_to_distance(totalX, xPitch);
-            Y = steps_to_distance(totalY, yPitch);
-            Z = steps_to_distance(totalZ, zPitch);
-            
-            if(contador > 10000){
-                printf("X: %.2f     Y: %.2f     Z: %.2f\r\n", X, Y, Z);
-                contador = 0;
+                
+                //----------------------------------------------------------------------
+    
+                move(ppsMax, x_dir, y_dir, z_dir, activeX, activeY, activeZ);  
+                X = steps_to_distance(totalX, xPitch);
+                Y = steps_to_distance(totalY, yPitch);
+                Z = steps_to_distance(totalZ, zPitch);
+                
+                if(contador > 10000){
+                    printf("X: %.2f     Y: %.2f     Z: %.2f\r\n", X, Y, Z);
+                    contador = 0;
+                }
+                
+                contador+=1;
+                
+                if(startSaved){
+                    break;
+                }
             }
             
-            contador+=1;
-            
-            if(startSaved){
-                break;
-            }
-        }
-        
-        printf("\nStarting saved points\n\r");
-        
-        x_distance_to_run = points[0][0] - points[0][saved-1];
-        y_distance_to_run = points[1][0] - points[1][saved-1];
-        z_distance_to_run = points[2][0] - points[2][saved-1]; 
-        
-        printf("\nGoing to first point\n\r");
-        
-        if(x_distance_to_run < 0){
-                x_dir = x_minus;
-                x_distance_to_run = -1.0*x_distance_to_run;
-            } else { x_dir = x_plus; }
-            
-            if(y_distance_to_run < 0){
-                y_dir = y_minus;
-                y_distance_to_run = -1.0*y_distance_to_run;
-            } else { y_dir = y_plus; }
-            
-            if(z_distance_to_run < 0){
-                z_dir = z_minus;
-                z_distance_to_run = -1.0*z_distance_to_run;
-            } else { z_dir = z_plus; }
-            
-        x_steps_to_run = distance_to_steps(x_distance_to_run, xPitch); 
-        y_steps_to_run = distance_to_steps(y_distance_to_run, yPitch); 
-        z_steps_to_run = distance_to_steps(z_distance_to_run, zPitch);
-        
-        printf("deltaX: %.2f        deltaY: %.2f            deltaZ: %.2f\n\r", x_distance_to_run, y_distance_to_run, z_distance_to_run);
-        
-        move(ppsMax, x_dir, y_dir, z_dir, x_steps_to_run, 0, 0);
-        move(ppsMax, x_dir, y_dir, z_dir, 0, y_steps_to_run, 0);
-        move(ppsMax, x_dir, y_dir, z_dir, 0, 0, z_steps_to_run);
-        
-        wait(2);
-        
-        printf("\nSaved points\n\r");
-        
-        for(int i = 1; i < saved; i++){
-            x_distance_to_run = points[0][i]-points[0][i-1];
-            y_distance_to_run = points[1][i]-points[1][i-1];
-            z_distance_to_run = points[2][i]-points[2][i-1];
-            
-            printf("deltaX: %.2f        deltaY: %.2f            deltaZ: %.2f\n\r", x_distance_to_run, y_distance_to_run, z_distance_to_run);
+            while(1){
+                
+                printf("\nStarting saved points\n\r");
+                
+                // home
+                x_distance_to_run = points[0][0] - points[0][saved-1];
+                y_distance_to_run = points[1][0] - points[1][saved-1];
+                z_distance_to_run = points[2][0] - points[2][saved-1]; 
+                
+                printf("\nGoing to first point\n\r");
+                
+                if(x_distance_to_run < 0){
+                        x_dir = x_minus;
+                        x_distance_to_run = -1.0*x_distance_to_run;
+                    } else { x_dir = x_plus; }
+                    
+                    if(y_distance_to_run < 0){
+                        y_dir = y_minus;
+                        y_distance_to_run = -1.0*y_distance_to_run;
+                    } else { y_dir = y_plus; }
+                    
+                    if(z_distance_to_run < 0){
+                        z_dir = z_minus;
+                        z_distance_to_run = -1.0*z_distance_to_run;
+                    } else { z_dir = z_plus; }
+                    
+                x_steps_to_run = distance_to_steps(x_distance_to_run, xPitch); 
+                y_steps_to_run = distance_to_steps(y_distance_to_run, yPitch); 
+                z_steps_to_run = distance_to_steps(z_distance_to_run, zPitch);
+                
+                printf("deltaX: %.2f        deltaY: %.2f            deltaZ: %.2f\n\r", x_distance_to_run, y_distance_to_run, z_distance_to_run);
+                
+                move(ppsMax, x_dir, y_dir, z_dir, x_steps_to_run, 0, 0);
+                move(ppsMax, x_dir, y_dir, z_dir, 0, y_steps_to_run, 0);
+                move(ppsMax, x_dir, y_dir, z_dir, 0, 0, z_steps_to_run);
+                
+                wait(2);
+                
+                printf("\nSaved points\n\r");
+                
+                // routine
+                
+                for(int i = 1; i < saved; i++){
+                    x_distance_to_run = points[0][i]-points[0][i-1];
+                    y_distance_to_run = points[1][i]-points[1][i-1];
+                    z_distance_to_run = points[2][i]-points[2][i-1];
+                    
+                    printf("deltaX: %.2f        deltaY: %.2f            deltaZ: %.2f\n\r", x_distance_to_run, y_distance_to_run, z_distance_to_run);
+                    
+                    if(x_distance_to_run < 0){
+                        x_dir = x_minus;
+                        x_distance_to_run = -1.0*x_distance_to_run;
+                    } else { x_dir = x_plus; }
+                    
+                    if(y_distance_to_run < 0){
+                        y_dir = y_minus;
+                        y_distance_to_run = -1.0*y_distance_to_run;
+                    } else { y_dir = y_plus; }
+                    
+                    if(z_distance_to_run < 0){
+                        z_dir = z_minus;
+                        z_distance_to_run = -1.0*z_distance_to_run;
+                    } else { z_dir = z_plus; }
+                    
+                    x_steps_to_run = distance_to_steps(x_distance_to_run, xPitch); 
+                    y_steps_to_run = distance_to_steps(y_distance_to_run, yPitch); 
+                    z_steps_to_run = distance_to_steps(z_distance_to_run, zPitch);
+                    
+                    move(ppsMax, x_dir, y_dir, z_dir, x_steps_to_run, 0, 0);
+                    move(ppsMax, x_dir, y_dir, z_dir, 0, y_steps_to_run, 0);
+                    move(ppsMax, x_dir, y_dir, z_dir, 0, 0, z_steps_to_run);
+                }
+                
+                printf("Done\n\r");
+                
+                printf("\nTo restart press blue, to return to JOG press yellow.\n\r");
+                
+                autoReload = 1;
+                exitSavedPointsLoop = 0;
+                keepSavedPointsReload = 0;
+                
+                while(1){
+                    if(exitSavedPointsLoop){
+                        printf("Exiting...");
+                        break;
+                    } else if(keepSavedPointsReload){
+                        printf("Restarting...\n\r");
+                        exitSavedPointsLoop = 0;
+                        break;    
+                    }
+                
+                    wait_ms(10);
+                }
+                if(exitSavedPointsLoop){
+                    break;
+                }                
+            }  
+        } else {
             
-            if(x_distance_to_run < 0){
-                x_dir = x_minus;
-                x_distance_to_run = -1.0*x_distance_to_run;
-            } else { x_dir = x_plus; }
-            
-            if(y_distance_to_run < 0){
-                y_dir = y_minus;
-                y_distance_to_run = -1.0*y_distance_to_run;
-            } else { y_dir = y_plus; }
-            
-            if(z_distance_to_run < 0){
-                z_dir = z_minus;
-                z_distance_to_run = -1.0*z_distance_to_run;
-            } else { z_dir = z_plus; }
-            
-            x_steps_to_run = distance_to_steps(x_distance_to_run, xPitch); 
-            y_steps_to_run = distance_to_steps(y_distance_to_run, yPitch); 
-            z_steps_to_run = distance_to_steps(z_distance_to_run, zPitch);
-            
-            move(ppsMax, x_dir, y_dir, z_dir, x_steps_to_run, 0, 0);
-            move(ppsMax, x_dir, y_dir, z_dir, 0, y_steps_to_run, 0);
-            move(ppsMax, x_dir, y_dir, z_dir, 0, 0, z_steps_to_run);
         }
-        
-        printf("Done\n\r");
     }
-    
-    else {
-        printf("Routine Selected\n\r");   
-        
-        /*
-        z_steps_to_run = distance_to_steps(20.0, zPitch); 
-        move(ppsMax, x_plus, x_plus, z_minus, 0, 0, z_steps_to_run);
-        
-        y_steps_to_run = distance_to_steps(50, yPitch);
-        move(ppsMax, x_plus, y_minus, z_plus, 0, y_steps_to_run, 0);
-        
-        x_steps_to_run = distance_to_steps(50, xPitch);
-        move(ppsMax, x_plus, y_plus, z_plus, x_steps_to_run, 0, 0); 
-        
-        y_steps_to_run = distance_to_steps(50, yPitch);
-        move(ppsMax, x_plus, y_plus, z_plus, 0, y_steps_to_run, 0);
-        
-        x_steps_to_run = distance_to_steps(50, xPitch);
-        move(ppsMax, x_minus, y_plus, z_plus, x_steps_to_run, 0, 0);   
-        
-        move(ppsMax, x_plus, x_plus, z_plus, 0, 0, z_steps_to_run);   */ 
-        
-        
-            move(ppsMax, x_plus, y_plus, z_plus, 400, 0, 0); 
-        
-        
-        printf("Done\n\r");
-    }  
 }  
 
 // FUNCTIONS ----------------------------------------------------------------
@@ -525,26 +530,41 @@
 }
 
 void savePoint(void){
-    printf("\nSave selected\n\r");
-    if(saved >= maxPoints){
-        printf("Max points reached\n\r");
-    } else{
-        X = steps_to_distance(totalX, xPitch);
-        Y = steps_to_distance(totalY, yPitch);
-        Z = steps_to_distance(totalZ, zPitch);
-        
-        points[0][saved] = X;
-        points[1][saved] = Y;
-        points[2][saved] = Z;
-        
-        saved+=1;
-    }
-    // save code
-    printf("Saved --> X: %.2f     Y: %.2f     Z: %.2f\r\n", X, Y, Z);
-    pontos_restantes = maxPoints-saved;
-    printf("Pontos restantes: %d\n\n\r", pontos_restantes);     
+    if(!autoReload){
+        keepSavedPointsReload = 0;
+        exitSavedPointsLoop = 0;
+        printf("\nSave selected\n\r");
+        if(saved >= maxPoints){
+            printf("Max points reached\n\r");
+        } else{
+            X = steps_to_distance(totalX, xPitch);
+            Y = steps_to_distance(totalY, yPitch);
+            Z = steps_to_distance(totalZ, zPitch);
+            
+            points[0][saved] = X;
+            points[1][saved] = Y;
+            points[2][saved] = Z;
+            
+            saved+=1;
+        }
+        // save code
+        printf("Saved --> X: %.2f     Y: %.2f     Z: %.2f\r\n", X, Y, Z);
+        pontos_restantes = maxPoints-saved;
+        printf("Pontos restantes: %d\n\n\r", pontos_restantes);
+    }  
+    else { 
+        exitSavedPointsLoop = 1;
+        keepSavedPointsReload = 0;
+    }   
 }
 
 void startSavedPoints(void){
-    startSaved = 1;    
+    if(!autoReload){
+        startSaved = 1; 
+        keepSavedPointsReload = 0;
+        exitSavedPointsLoop = 0;
+    } else {
+        keepSavedPointsReload = 1;
+        exitSavedPointsLoop = 0;    
+    } 
 }
\ No newline at end of file