Electronica / Mbed OS Proyecto2

Dependencies:   Adafruit_GFX

Revision:
3:3fbadf6c492f
Parent:
2:fd3b989cedcd
Child:
4:7f4d81ed3984
--- a/main.cpp	Wed Dec 15 18:57:42 2021 +0000
+++ b/main.cpp	Thu Dec 16 11:39:46 2021 +0000
@@ -30,17 +30,27 @@
 DigitalOut dir(A4);
 DigitalOut enableMotor(D11);
 DigitalIn boton_inicial(D3);
+DigitalIn boton_emergencia(D7);
 DigitalOut dirAMotor(D4);//M121
 PwmOut     dirBMotor(D5);//M121
 DigitalOut rele(D6);//Rcal
-
-//InterruptIn encoderA(A2);
+InterruptIn encoderA(A2);
 
 Timer t;
+Timer bot;
+//Ticker tickerVel;
+int velocidad=0;
 
 float temperatura;
+int contador=0;
+//int target=75; Para el control de velocidad
 
-enum estados {reset, apagado, encendido, motorpalante,atras, estatico };
+void encoderAIrq()// contador de vueltas
+{
+    contador++;
+}
+
+enum estados {reset, apagado, motorpalante,atras, estatico, emergencia };
 estados estado;
 
 //HCSR04  usensor(D7,D8); //(PinName TrigPin,PinName EchoPin):
@@ -58,6 +68,12 @@
         enableMotor=1;
         paso(0);
     } else if (final1 ==1) {
+
+        gOled.clearDisplay();
+        gOled.printf("La temperatura es de %.4f\n",temperatura);
+        gOled.printf("La velocidad en rpm es de %d\r\n",velocidad);
+        gOled.display();
+        gOled.setTextCursor(0,0);
         estado = apagado;
         enableMotor =0;
     }
@@ -65,74 +81,91 @@
 
 void estadoapagado()
 {
+    dirBMotor =0;
+
     if (boton_inicial==1) {
         enableMotor=1;
+        t.reset();
         estado= motorpalante;
-        t.reset();
     }
 }
 
-/*void estadoencendido()
-{
-    if (boton_inicial==0) {
-        paso(1);
-        estado = motorpalante;
-
-    }
-}*/
-
 void estadomotorpalante()
 {
-//if (final2)
+    if (boton_emergencia==1) {
+        estado=emergencia;
+    }
     if (boton_inicial==0&&final2==0) {
-        
         paso(1);
-
-        dirAMotor=1;
+        dirBMotor=1;
         rele =1;
 
     } else if (final2==1) {
         int tiempo=t.read();
-        
         //pc.printf("La temperatura es de %.4f\n",temperatura);
         gOled.clearDisplay();
         gOled.printf("La temperatura es de %.4f\n",temperatura);
+        gOled.printf("La velocidad en rpm es de %d\r\n",velocidad);
         gOled.display();
         gOled.setTextCursor(0,0);
-    
-       // pc.printf("El tiempo transcurrido es de %d\n",tiempo);
+
+        // pc.printf("El tiempo transcurrido es de %d\n",tiempo);
         //pc.printf("La temperatura es de %.4f\n",temperatura);
         //dirAMotor=0;
         wait(2.0);
         //enableMotor=0;
         //rele=0;
-
         //if (final2==1) {
-            estado=atras;
+        estado=atras;
         //}
         //Print en pantalla con el Oled.
+    } else if (final2!=1 && boton_inicial==1 && final1!=1) {
+        enableMotor=0;
+        rele=0;
+        dirBMotor=1;//Esto hay que cambiarlo
+        estado= estatico;
+        bot.reset();
     }
 }
 
 void estadoatras()
 {
+    if (boton_emergencia==1) {
+        estado=emergencia;
+    }
     if (final2!=1 && boton_inicial==1 && final1!=1) {
         enableMotor=0;
         rele=0;
-        dirAMotor=0;
+        dirBMotor=1;//Esto hay que cambiarlo
+        bot.reset();
         estado= estatico;
     } else if (final1 ==1) {
         estado = motorpalante;
     } else {
         paso(0);
     }
+
 }
+void estadoestatico()
+{
+    if (boton_emergencia==1) {
+        estado=emergencia;
+    }
+    if (boton_inicial ==0 ) {
+        wait(0.5);
+        //estado = apagado;
 
+    } else if (boton_inicial==1 && bot>1) {
+        dirBMotor=0;
+        estado = reset;
+    }
+}
 void muestraTemp()
 {
     pc.printf("La temperatura es de %.4f\n",temperatura);
     gOled.clearDisplay();
     gOled.printf("La temperatura es de %.4f\n",temperatura);
+    gOled.printf("%d\r\n",velocidad);
     gOled.display();
     gOled.setTextCursor(0,0);
 }
@@ -143,12 +176,32 @@
     float Rt= 100e3*((3.3-tension)/tension);
     temperatura = (3950/(log(Rt/100e3)+(3950/298)))-273.1;
 }
+
+void funcionVel()
+{
+    velocidad=contador*60;//esto es en rpm haciendo *60seg para pasar a min.
+    contador=0;
+    /*if(target-velocidad>0){
+        dirBMotor=dirBMotor+0.01;
+        }
+        else{
+        dirBMotor=dirBMotor-0.01;
+            }*/
+}
+
+void estadoemergencia()
+{
+    estado = apagado;
+}
+
 int main()
 {
     pc.baud(115200);
+    pc.printf("La velocidad es de %d\r rpm\n",velocidad);
     gOled.begin();
     gOled.clearDisplay();
-    gOled.printf("Hola");
+    gOled.printf("Hola, bienvenido\n");
+    gOled.printf("La velocidad en rpm es de %d\r\n",velocidad);
     gOled.display();
     gOled.setTextCursor(0,0);
     //visualTemp.attach(&muestraTemp,1.0);
@@ -158,6 +211,11 @@
     enableMotor =1;
     pc.printf("Estado cerrada\n");
     t.start();
+    bot.start();
+    encoderA.rise(&encoderAIrq);  // attach the address of the flip function to the rising edge
+    // tickerVel.attach(&funcionVel,0.1);
+    dirAMotor=0;
+    //dirBMotor=0.5;
     while(1) {
         medir();
         //distancia=usensor.get_dist_cm();
@@ -171,10 +229,6 @@
                 estadoapagado();
                 pc.printf("Estado apagado\n");
                 break;
-            /* case encendido:
-                 pc.printf("HOLI2");
-                 estadoencendido();
-                 break;*/
             case motorpalante:
                 estadomotorpalante();
                 pc.printf("Estado alante\n");
@@ -185,23 +239,12 @@
                 break;
             case estatico:
                 pc.printf("Estado estatico\n");
-//estadoestatico();
+                estadoestatico();
                 break;
-                /*case cerrada:
-                    estadoCerrada();
-                    break;
-                case abriendose:
-                    estadoAbriendose();
-                    break;
-                case abierta:
-                    estadoAbierta();
-                    break;
-                case cerrandose:
-                    estadoCerrandose();
-                    break;
-                default:
-                    break;
-                }*/
+            case emergencia:
+                pc.printf("Estado emergencia\n");
+                estadoemergencia();
+                break;
         }
     }
 }
\ No newline at end of file