juan carlos cortes parra / Mbed 2 deprecated Lab4rampadesaceleracion

Dependencies:   mbed TextLCD

Revision:
1:fccdc54f1314
Parent:
0:888f499e0ce0
Child:
2:7ea6fb21e108
--- a/main.cpp	Fri Dec 04 04:28:34 2020 +0000
+++ b/main.cpp	Fri Dec 04 12:31:29 2020 +0000
@@ -12,6 +12,8 @@
 DigitalOut left(PTC0);
 DigitalIn start(PTC3),finish(PTC7);
 Timer Timer1;
+Ticker rampa; 
+BusOut step_secuence(PTB0,PTB1,PTB2,PTB3);
 int rea,ns;
 string cadena;
 using namespace std;
@@ -27,6 +29,9 @@
 bool antirebote=false;
 int recorrido=0,rpm1=0;
 char cade;
+bool bit=false;
+int velram;
+float tiempoe=0;
 //using namespace boost;
 float h;
 void inicio(){
@@ -34,15 +39,19 @@
         if(start==0){
             break;
         }
-        velocidad(2,0.5);
+        velocidad(2,0.05);
     }
     }
-void rampa (int sel){
-    for(float i=1;i<=10;i++){
-     velocidad(sel,i*10);
-     wait(0.01);
-    }
-    }
+void rampa1(){
+    lcd.cls();
+    lcd.locate(0,1); 
+    lcd.printf("millos");
+    if(bit){
+    velram=velram+1; 
+    if (velram >=20){
+        velram=20;
+        }
+    } }
 int main(){
     velocidad(0,0);
     inicio();
@@ -51,12 +60,19 @@
     int red=0;
     char buffer[12],aux[4];
     while(1){
+             if(second==0){
+                 rampa.attach(&rampa1,1*0.05);
+                 }else{
+                  rampa.attach(&rampa1,second*0.05);   
+                     }
+        
              
          if(pc.readable()){// captura string
          
           pc.gets(buffer,12);
           buffer[0]=' ';
           cade=buffer[1];
+             if (rpm1==0){
               aux[1]=buffer[1];
               aux[2]=buffer[2];
               aux[3]=buffer[3];      
@@ -69,17 +85,31 @@
               aux[2]=buffer[8];
               aux[3]=buffer[9];
               distancia=dist(aux);
-          
+              
+              }
+              }
         
-          if(red>0){
-              recorrido=red*6;
+          if(distancia>0){
+              bit=true;
+              velram=1;
+              distancia=distancia*46;
               }
+              
           //pc.printf("%s",buffer);
          // dist(buffer);
          //pc.printf("%s",cadena);
          pc.printf("%d",recorrido);
-         }
-        
+       
+         if(( distancia>0)&&(contador<=distancia)){
+            auxrpm=(60*20)/(rpm1*46*velram);
+              velocidad(selec,auxrpm);
+             
+        }if(contador==distancia){
+            contador=0;
+            distancia=0;
+            second=0;
+            
+            }
         if(cade=='d'){
             selec=1; 
             
@@ -92,18 +122,7 @@
         if(cade=='n'){selec=0;     
             }
             
-            
-            
-            
-          pc.printf("%d  ,%d  ,%d ",rpm1,distancia,second);
-               
-            
-          wait(0.01);
-         
-           // selec=1;
-          
-          
-          //velocidad(selec,cambio);
+           pc.printf("%d  ,%d  ,%d ",rpm1,distancia,second);
    
            
        
@@ -125,15 +144,25 @@
             break;
         case 1:
             
-            if(rpm<10){
-                pc.printf("00%.1f ,%d ",rpm,distancia);
-                }
-            if((rpm>10)&&(rpm<100)){
-                pc.printf("0%.1f ,%d ",rpm,distancia);
-                } 
-            if(rpm>100){
-                pc.printf("0%.1f ,%d ",rpm,distancia);
-                }  
+          for( int P=1;P<=4;P++){
+               contador=contador+1;
+               switch (P) {
+            case 0:
+                step_secuence=0b01;
+                break;
+            case 1:
+                step_secuence=0b10;
+                break;
+            case 2:
+                step_secuence=0b100;
+                break;
+            case 3:
+                step_secuence=0b1000;
+                break;
+                
+        }
+        wait(ad);
+         }    
            
             lcd.cls();
             lcd.locate(0,0);     
@@ -145,15 +174,27 @@
             l2=0;
             break;
         case 2:
-              if(rpm<10){
-                pc.printf("00%.1f ,%d ",rpm,distancia);
-                }
-            if((rpm>10)&&(rpm<100)){
-                pc.printf("0%.1f ,%d ",rpm,distancia);
-                } 
-            if(rpm>100){
-                pc.printf("0%.1f ,%d ",rpm,distancia);
-                }  
+                  
+          for( int b=4;b>=1;b--){
+               contador=contador+1;
+               switch (b) {
+            case 0:
+                step_secuence=0b01;
+                break;
+            case 1:
+                step_secuence=0b10;
+                break;
+            case 2:
+                step_secuence=0b100;
+                break;
+            case 3:
+                step_secuence=0b1000;
+                break;
+                
+        }
+        wait(ad);
+         }    
+           
             lcd.cls();   
             lcd.locate(0,0);     
             lcd.printf("%d mm",distancia);
@@ -171,7 +212,7 @@
 int dist(char datos[4]){
    int numero=0,contador=0;
    char c=' ';
-    for(int i = 3;i >= 0;i --){
+    for(int i = 4;i >= 0;i --){
        c=datos[i];
        //pc.putc(c);
        if(c=='0'){contador=contador+1;  }