Dependencies:   mbed DS1820

Revision:
7:597508b756a0
Parent:
6:1e7fe07e6ab4
Child:
8:f5d80ad24de3
--- a/main.cpp	Tue Jun 11 19:07:40 2019 +0000
+++ b/main.cpp	Sat Jun 15 19:09:46 2019 +0000
@@ -22,7 +22,7 @@
 int tiempo_sensor_efecto_hall=0;//Variable de tiempo
 int tiempo_cooler=0; //Variable de tiempo
 int tiempo_informacion=0;//Variable de tiempo
-int tiempo_estabilizacion=0;
+int tiempo_estabilizacion=0;//Variable de tiempo
 
 void pulsadores(char pulsador);
 void timer();
@@ -163,7 +163,7 @@
             //Deshabilito despues de 2 seg y calculo las rpm
             if(tiempo_sensor_efecto_hall>=2000) {
                 habilitacion_pulsos=0;
-                velocidad_angular=cuenta_pulsos*30/2; //Nuestro cooler da 2 pulsos por vuelta 
+                velocidad_angular=cuenta_pulsos*30/2; //Nuestro cooler da 2 pulsos por vuelta
                 estado_interpretar_velocidad_angular=0;
                 velocidad_angular_leida=1;
             }
@@ -184,58 +184,61 @@
         default:
         case 0:
             //Ajusto el duty hasta un aproximado de 200rpm
-            if(velocidad_angular>=1000)
-                duty-=0.09;
-            if((velocidad_angular>=300)&&(velocidad_angular<1000))
-                duty-=0.01;
-            if(velocidad_angular<150)
-                duty+=0.05;
-            //Protejo contra overflow
-            if(duty<0)
-                duty=0;
-            if(duty>1)
-                duty=1;
-            estado_definir_200_rpm=1;
-           break;
-        case 1:
-            //Leo la nueva veloicidad angular
+            //Leo velocidad angular
             leer_velocidad_angular();
             if(velocidad_angular_leida) {
-                //Me fijo si estoy lo suficientemente cercano al valor deseado
                 velocidad_angular_leida=0;
-                if((velocidad_angular<300)&&(velocidad_angular>150)) {
-                    estado_definir_200_rpm=2;
+                //Hago ajuste grueso
+                if((velocidad_angular>300)||(velocidad_angular<150)) {
+                    if(velocidad_angular>=1000)
+                        duty-=0.09;
+                    if((velocidad_angular>=300)&&(velocidad_angular<1000))
+                        duty-=0.01;
+                    if(velocidad_angular<150)
+                        duty+=0.05;
+                    //Protejo contra overflow
+                    if(duty<0)
+                        duty=0;
+                    if(duty>1)
+                        duty=1;
+                    printf("Velocidad angular: %d, Duty: %4.1f\r\n",velocidad_angular,duty*100);
+                } //Si estoy muy proximo hago ajuste fino
+                else {
+                    estado_definir_200_rpm=1;
                     tiempo_estabilizacion=0;
                     printf("Ya casi termna el proceso...\r\n");
-                } else {
-                    //Si todavia falta variar sigo variando
-                    estado_definir_200_rpm=0;
-                    printf("Velocidad angular: %d, Duty: %4.1f\r\n",velocidad_angular,duty*100);
                 }
             }
             break;
-        case 2:
+        case 1:
             //Si estoy muy proximo al valor hago un ajuste fino
             //Dejo funcionar al cooler por 3 segundos asi verificar que
             //mas adelante no se frene o varie su velocidad en ese tiempo
             if(tiempo_estabilizacion>3000) {
                 leer_velocidad_angular();
                 if(velocidad_angular_leida) {
+                    //Me quedo con el duty que haga girar al cooler entre 180rpm y 220rpm
                     if((velocidad_angular<220)&&(velocidad_angular>180)) {
                         duty_definido=1;
                         duty_200_rpm=duty;
-                    } else {
+                    }
+                    //Corrijo la señal vaiando muy de a poco el duty
+                    else {
                         printf("Ajuste fino...\r\n");
                         if(velocidad_angular>200)
                             duty-=0.003;
                         if(velocidad_angular<200)
                             duty+=0.003;
-                        if(velocidad_angular==0)
+                        if(velocidad_angular<150)
                             duty+=0.05;
                     }
                     printf("Velocidad angular: %d, Duty: %4.1f\r\n",velocidad_angular,duty*100);
                     velocidad_angular_leida=0;
                     tiempo_estabilizacion=0;
+                    //Si ocurrio algun error y no estoy lo sufucientemente cerca vuelvo al ajuste grueso
+                    if((velocidad_angular>350)||(velocidad_angular==0)) {
+                        estado_definir_200_rpm=0;
+                    }
                 }
             }
             break;
@@ -311,8 +314,9 @@
             }
             break;
         case 1:
-            //Haciendo pruebas notamos que muchas veces el sensor mide mal entregando -1000° por lo que forzamos a que lea siempre bien con un "do while"
-            //El rango que consideramos correcto va de -5°C a 120°C
+            //Haciendo pruebas notamos que a veces el sensor mide mal entregando -1000°C
+            //Para corregir esto forzamos a que lea siempre bien con un "do while"
+            //El rango que consideramos correcto va de -5°C a 500°C
             do {
                 probe.convertTemperature(false, DS1820::all_devices);
                 temperatura=probe.temperature();
@@ -324,7 +328,7 @@
 }
 
 void pulsadores(char pulsador)
-// Leo el pulsador, le quito el rebote y guardo el dato ingresado en el vector "usuario". Además distingo entre ruido o una lectura valida
+// Leo el pulsador, le quito el rebote y modifico el modo en caso de ser necesario
 {
     static char estado_pulsadores;
     switch(estado_pulsadores) {
@@ -365,7 +369,7 @@
 }
 
 void timer()
-//Incremento contadores
+//Incremento contadores cada 1ms
 {
     tiempo_pulsadores++;
     tiempo_lazo_abierto++;