control movimiento motor con interfaz

Dependencies:   mbed TextLCD

Files at this revision

API Documentation at this revision

Comitter:
juankafil1
Date:
Sun Nov 22 23:40:35 2020 +0000
Parent:
1:98ce9ed49158
Commit message:
po

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Oct 26 19:25:19 2020 +0000
+++ b/main.cpp	Sun Nov 22 23:40:35 2020 +0000
@@ -1,7 +1,7 @@
 #include "string"
 #include "mbed.h"
 #include "sstream"
-
+#include <string>
 #include "TextLCD.h"
 
 TextLCD lcd(PTE20,PTE21,PTE22,PTE23,PTE29,PTE30, TextLCD::LCD16x2);// Rs, E, d4, d5, d6, d7, RW=GND
@@ -9,127 +9,154 @@
 DigitalIn pulso(PTC5);
 DigitalOut right(PTC7);
 DigitalOut left(PTC0);
+DigitalIn start(PTC3),finish(PTC7);
 Timer Timer1;
 int rea,ns;
 string cadena;
 using namespace std;
 void velocidad(int a,float ad);
+int dist(char datos[5]);
 PwmOut l1(PTB3),l2(PTC2);
 int selec;
 float cambio;
 float rpm=0,auxrpm=0;
 int contador=0;
-
+int distancia=0,disa=0;
 bool antirebote=false;
-
+int recorrido=0;
+char cade;
 //using namespace boost;
 float h;
-
+void inicio(){
+    while(1){
+        if(start==0){
+            break;
+        }
+        velocidad(2,0.5);
+    }
+    }
+void rampa (int sel){
+    for(float i=1;i<=10;i++){
+     velocidad(sel,i*10);
+     wait(0.01);
+    }
+    }
 int main(){
     velocidad(0,0);
+    inicio();
+    velocidad(0,0);
+    lcd.printf("esta en los extremos");
+    int red=0;
+    char buffer[5];
     while(1){
         if((pulso==0)&&(antirebote==false)){
             contador++;
+            disa++;
             antirebote=true;
+            auxrpm=Timer1.read_ms();
+            Timer1.stop();
+            Timer1.reset();
+            rpm=10000/auxrpm;
+           
+            if(recorrido>0){
+            Timer1.start();}
             }
         if((pulso==1)&&(antirebote==true)){
             antirebote=false;
             }
-        if(contador==1){
-            Timer1.start();
-            }
-         if(contador==4){
-            auxrpm=Timer1.read_ms();
-            Timer1.stop();
-            Timer1.reset();
-            rpm=60000/auxrpm;
-            contador=0;
-            }   
-            
-        if(pc.readable()){
-        cadena=pc.getc();
-        }
-        if(cadena=="0"){
-            h=0.0;
-            
-            }
-       if (cadena=="1"){
-           
-            h=0.1;
+         
+         if(contador==6){
             
-            }
-        if (cadena=="2"){
-            h=0.2;
-             
-            }
-        if (cadena=="3"){
-            h=0.3;
-            
-            }
-        if (cadena=="4"){
-            h=0.4;
-            //if(m!=2){
-          //  m=1;
-            //
-                     
-            }
-        if (cadena=="5"){
-            h=0.5;
-            
-            }
-        if (cadena=="6"){
-            h=0.6;
-          
+            contador=0;     
+                distancia=distancia+1;
+               
+            //distancia=distancia+1;
             }
-        if (cadena=="7"){
-            h=0.7;
-             
-            }
-        if (cadena=="8"){
-            h=0.8;
-             
-            }
-        if (cadena=="9"){
-            h=0.9;
-            
+           
+            if(red>0){
+                if(selec==1){
+                    velocidad(selec,0.3);
+                    pc.printf("uuno");
+                    
+                    
+                    if(disa==2){velocidad(selec,0.4);}
+                    if(disa==3){velocidad(selec,0.6);}
+                    if(disa==4){velocidad(selec,0.8);}
+                    if((disa<recorrido)&&(disa>4)){
+                        
+                        velocidad(selec,1);   
+                    }  if( disa >= red ){
+                        distancia=0;
+                        disa=0;
+                        red=0;
+                        recorrido=0;
+                        velocidad(0,0);
+                    }
+                        
+                }if (selec==2){
+                    velocidad(selec,0.3);
+                    pc.printf("dos");
+                    
+                    if(disa==2){velocidad(selec,0.4);}
+                    if(disa==3){velocidad(selec,0.6);}
+                    if(disa==4){velocidad(selec,0.8);}
+                    if((disa<recorrido)&&(disa>4)){
+                        
+                        velocidad(selec,1);
+                    
+                    
+                    }if( disa >= red ){
+                        distancia=0;
+                        selec=0;
+                        disa=0;
+                        red=0;
+                        recorrido=0;
+                        velocidad(0,0);
+                    }
+                } 
             }
-        if (cadena=="10"){
-            h=1;
-             
-            }
-            if(cadena=="d"){
-                selec=1;
-                lcd.cls();
-                pc.printf("   derecha a velocidad de %.2f  rpm",rpm);
-                lcd.locate(0,0);     
-                lcd.printf("derecha");
-                lcd.locate(0,1);
-                lcd.printf("%.2f rpm",rpm);
+
+         if(pc.readable()){// captura string
+         
+          pc.gets(buffer,8);
+          buffer[0]=' ';
+          cade=buffer[1];
+          if(red==0){
+          red=dist(buffer);
+          }
+          if(red>0){
+              recorrido=red*6;
+              }
+          //pc.printf("%s",buffer);
+         // dist(buffer);
+         //pc.printf("%s",cadena);
+         pc.printf("%d",recorrido);
+         }
+        
+        if(cade=='d'){
+            selec=1; 
+            
             } 
-            if(cadena=="i"){
-                selec=2;
-                pc.printf("   izquierda a velocidad de %.2f  rpm",rpm);
-                lcd.locate(0,0);     
-                lcd.printf("izquierda");
-                lcd.locate(0,1);
-                lcd.printf("%.2f rpm",rpm);
+        if (cade=='i'){ 
+            selec=2; 
+            
+            
+            } 
+        if(cade=='n'){selec=0;     
             }
-            if(cadena=="n"){
-                selec=0;
-                
-                pc.printf("  sin sentido");
-                lcd.cls();
-                lcd.locate(0,0);     
-                lcd.printf("sin sentido");
-              
-                 
-                
-            }
+            
+            
+            
+            
+             
+               
+            
           wait(0.01);
+         
            // selec=1;
-          cambio=h;
+          
           
-          velocidad(selec,cambio);
+          //velocidad(selec,cambio);
    
            
        
@@ -140,14 +167,52 @@
 void velocidad(int a ,float ad){
     switch (a){
         case 0:
+            lcd.cls();
+            lcd.locate(0,0);     
+            lcd.printf("%d mm",distancia);
+            lcd.locate(0,1);
+            lcd.printf(" V :%.2f rpm",rpm);
             l1=0;
             l2=0;
+            
             break;
         case 1:
-            l1.write(ad);
+            
+            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);
+                }  
+           
+            lcd.cls();
+            lcd.locate(0,0);     
+            lcd.printf("%d mm",distancia);
+            lcd.locate(0,1);
+            lcd.printf(" V :%.2f rpm",rpm);
+            l1.write(ad); wait(0.02);
+            
             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);
+                }  
+            lcd.cls();   
+            lcd.locate(0,0);     
+            lcd.printf("%d mm",distancia);
+            lcd.locate(0,1);
+            lcd.printf("%.2f rpm",rpm);
+            wait(0.02);
             l2.write(ad);
             l1=0;
             break;
@@ -156,3 +221,68 @@
       
 }
 
+int dist(char datos[8]){
+   int numero=0,contador=0;
+   char c=' ';
+    for(int i = 8;i >= 0;i --){
+       c=datos[i];
+       //pc.putc(c);
+       if(c=='0'){contador=contador+1;  }
+       if(c=='1'){
+            contador=contador+1;
+            if(contador==1){numero=numero+1;}
+            if(contador==2){numero=numero+10;}
+            if(contador==3){numero=numero+100;} }
+        if(c=='2'){
+            contador=contador+1;
+            if(contador==1){numero=numero+2;}
+            if(contador==2){numero=numero+20;}
+            if(contador==3){numero=numero+200;} }
+        if(c=='3'){
+            contador=contador+1;
+            if(contador==1){numero=numero+3;}
+            if(contador==2){numero=numero+30;}
+            if(contador==3){numero=numero+300;} }
+        if(c=='4'){
+            contador=contador+1;
+            if(contador==1){numero=numero+4;}
+            if(contador==2){numero=numero+40;}
+            if(contador==3){numero=numero+400;} }
+        if(c=='5'){
+            contador=contador+1;
+            if(contador==1){numero=numero+5;}
+            if(contador==2){numero=numero+50;}
+            if(contador==3){numero=numero+500;} }
+        if(c=='6'){
+            contador=contador+1;
+            if(contador==1){numero=numero+6;}
+            if(contador==2){numero=numero+60;}
+            if(contador==3){numero=numero+600;} }
+        if(c=='7'){
+            contador=contador+1;
+            if(contador==1){numero=numero+7;}
+            if(contador==2){numero=numero+70;}
+            if(contador==3){numero=numero+700;} }
+        if(c=='8'){
+            contador=contador+1;
+            if(contador==1){numero=numero+8;}
+            if(contador==2){numero=numero+80;}
+            if(contador==3){numero=numero+800;} }
+        if(c=='9'){
+            contador=contador+1;
+            if(contador==1){numero=numero+9;}
+            if(contador==2){numero=numero+90;}
+            if(contador==3){numero=numero+900;} }
+       
+        }
+   
+    
+      
+    
+     
+   return numero;
+               
+               
+            
+    
+    }
\ No newline at end of file