Grupo T / Mbed OS GRUPOT
Revision:
5:0cbf491f703d
Parent:
4:48717b42eab2
Child:
6:af32c68ff4cf
--- a/main.cpp	Wed May 02 19:56:10 2018 +0000
+++ b/main.cpp	Thu May 03 17:47:14 2018 +0000
@@ -1,5 +1,8 @@
 #include "mbed.h"
 
+// MACHINE SETUP ---------------------------------------------------------------------------
+
+// drivers output signal
 DigitalOut stepX(D2);
 DigitalOut dirX(D3);
 
@@ -9,12 +12,21 @@
 DigitalOut stepZ(D11);
 DigitalOut dirZ(D12);
 
+// hardware input signal
+// end-of-stroke sensors
+DigitalIn endX1(D5);
+DigitalIn endX2(D10);
+
+// IHM for development
 AnalogIn joyX(A0);
 AnalogIn joyY(A1);
 
 DigitalIn zUp(D6);
 DigitalIn zDwn(D7);
 
+// variables definition
+int modeStatus = 1;
+
 float valX;
 float valY;
 int valZUp;
@@ -24,82 +36,137 @@
 int activeY = 1;
 int activeZ = 1;
 
+int x_dir;
+int y_dir;
+int z_dir;
+
 int contador = 0;
 
-float pps = 800.0;
-float periodo = 1.0/pps;
-float tempo = periodo/2.0;
+float ppsMax = 600.0;
+
+void move(int pps, int x_dir, int y_dir, int z_dir, int x_step, int y_step, int z_step);
 
 int main(){
     printf("Starting...\r\n");
-    while(1){        
-        valX = joyX;
-        valY = joyY;
-        
-        valZUp = zUp;
-        valZDwn = zDwn;
-        
-        if(valX > 0.7){
-            if(dirX){
-                dirX = 0;    
+    
+    if(modeStatus){
+        //Código de JOG
+        printf("JOG Selected");
+        while(1){        
+            valX = joyX;
+            valY = joyY;
+            
+            valZUp = zUp;
+            valZDwn = zDwn;
+            
+            if(valX > 0.7){
+                if(x_dir){
+                    x_dir = 0;    
+                }
+                activeX = 1;    
+            }
+            else if(valX < 0.3){
+                if(!x_dir){
+                    x_dir = 1;    
+                }
+                activeX = 1;    
+            }
+            else{
+                activeX = 0;
             }
-            activeX = 1;    
-        }
-        else if(valX < 0.3){
-            if(!dirX){
-                dirX = 1;    
+            
+            //----------------------------------------------------------------------
+            
+            if(valY > 0.7){
+                if(!y_dir){
+                    y_dir = 1;    
+                }
+                activeY = 1;    
+            }
+            else if(valY < 0.3){
+                if(y_dir){
+                    y_dir = 0;    
+                }
+                activeY = 1;    
+            }
+            else{
+                activeY = 0;
             }
-            activeX = 1;    
-        }
-        else{
-            activeX = 0;
+            
+            //----------------------------------------------------------------------   
+            
+            if(!valZUp && valZDwn){
+                if(z_dir){
+                    z_dir = 0;    
+                }
+                activeZ = 1;    
+            }
+            else if(!valZDwn && valZUp){
+                if(!z_dir){
+                    z_dir = 1;    
+                }
+                activeZ = 1;    
+            }
+            else {
+                activeZ = 0;    
+            }
+            
+            //----------------------------------------------------------------------
+
+            move(ppsMax, x_dir, y_dir, z_dir, activeX, activeY, activeZ);            
+
         }
-        
-        //----------------------------------------------------------------------
-        
-        if(valY > 0.7){
-            if(!dirY){
-                dirY = 1;    
-            }
-            activeY = 1;    
-        }
-        else if(valY < 0.3){
-            if(dirY){
-                dirY = 0;    
-            }
-            activeY = 1;    
-        }
-        else{
-            activeY = 0;
-        }
-        
-        //----------------------------------------------------------------------   
-        
-        if(!valZUp && valZDwn){
-            if(dirZ){
-                dirZ = 0;    
-            }
-            activeZ = 1;    
-        }
-        else if(!valZDwn && valZUp){
-            if(!dirZ){
-                dirZ = 1;    
-            }
-            activeZ = 1;    
+    }
+    
+    else {
+        //Código de execução fora do JOG    
+    }
+    
+}
+
+void move(int pps, int x_dir, int y_dir, int z_dir, int x_step, int y_step, int z_step){
+    float time = 1.0/pps/2.0;
+    
+    dirX = x_dir;
+    dirY = y_dir;
+    dirZ = z_dir;
+    
+    int max_val;
+    
+    if(x_step > y_step){
+        if(x_step > z_step){
+            max_val = x_step;
         }
         else {
-            activeZ = 0;    
+            max_val = z_step;
         }
-        
-        //----------------------------------------------------------------------
+    } else if(y_step > z_step){
+        max_val = y_step;
+    }
+    else {
+        max_val = z_step;    
+    }
+    
+    for(int i = 0; i < max_val; i++){
+        if(i > x_step){
+            stepX = 0;
+        }
+        else { stepX = 1; }
         
-        stepX = activeX;
-        stepY = activeY;
-        stepZ = activeZ;
-        wait(tempo);
+        if(i > y_step){
+            stepY = 0;
+        }
+        else { stepY = 1; }
+        
+        if(i > z_step){
+            stepZ = 0;
+        }
+        else { stepZ = 1; }
+        
+        wait(time);
         stepX = 0;
         stepY = 0;
         stepZ = 0;
-        wait(tempo);
+        wait(time); 
     }
-}
\ No newline at end of file
+}