PID con botones y modo incremental

Dependencies:   Debounced TextLCD mbed

Fork of pid_teclas by Gustavo Ramirez

Revision:
1:0be27050f046
Parent:
0:9aa80672eb3d
--- a/main.cpp	Wed Oct 16 17:10:11 2013 +0000
+++ b/main.cpp	Thu Apr 09 10:55:35 2015 +0000
@@ -2,18 +2,156 @@
 #include "DebouncedIn.h"
 #include "TextLCD.h"
 
-AnalogIn Vin(PTC2);
-TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7
+
+//Este código lo hicimos basado en el programa pid_teclas de TONY63
 
+TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7 son los puertos del módulo LCD 2x16
+AnalogIn y(PTB1);     //puerto de entrada analógica
+AnalogOut u(PTE30);    //puerto de salida analógica
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
 DigitalOut led3(LED3);
-DebouncedIn button1(PTC12);
-DebouncedIn button2(PTC13);
-DebouncedIn button3(PTC16);
+DebouncedIn button1(PTC12);   //botón aumentar
+DebouncedIn button2(PTC13);   //botón disminuir
+DebouncedIn button3(PTC16);   //botón cambiar menú
+DebouncedIn button4(PTC17);   //botón enter
+
+int C1=0x0F;  //muestra el cursor
+int C4=0x0C; // quito cursor bajo
+int sp=0,kp=0,kd=0,ki=0,p=1;
+int yr, ap, ai, ad, err, med, err_v, cycle, pid; 
+int b; 
+int pidn;
+Timer t;
 
-int C1=0x0F;
-int sp=0,kp=0,kd=0,ki=0,p=1;
+int incremento(int c,int f,int aux)    //función auxiliar para el botón de aumentar
+{
+int bandera=0,i=0;
+        
+ while(button1==0)   
+ {
+i=1;
+ wait (0.5);  
+             
+                        
+  if ((bandera==2)&(i==1)) // si el módulo de la constante dividido 100 es cero
+  {
+ aux = aux+100; 
+ if (aux>=999)
+{
+aux = 999;
+}
+                                          
+lcd.locate(c,f);
+lcd.printf("   ");
+lcd.locate(c,f);
+lcd.printf("%d", aux);
+i=0;
+                               
+ }
+if ((bandera==1)&(i==1))
+{
+aux=aux+10;
+if(aux%100==0){bandera=2;} // si el módulo de la constante dividido 100 es cero, la bandera es igual a 2
+if (aux>= 999)
+{
+aux = 999;
+}
+                          
+                                           
+lcd.locate(c,f);
+lcd.printf("   ");
+lcd.locate(c,f);
+lcd.printf("%d", aux);
+i=0;            }
+                
+if ((bandera==0)&(i==1))
+ {
+++aux;  
+if(aux%10==0){bandera=1;}
+if (aux>= 999)
+{
+aux = 999;
+}
+                          
+                                             
+lcd.locate(c,f);
+lcd.printf("   ");
+lcd.locate(c,f);
+lcd.printf("%d", aux);
+i=0; }
+                               
+                               
+                                
+}
+bandera=0;
+return aux;
+}
+    
+int decremento(int c,int f,int aux)     //función auxiliar para el botón disminuir
+{
+int bandera=0,i=0;
+        
+while(button2==0)   
+{
+                    
+i=1;
+wait (0.5);
+                        
+if ((bandera==2)&(i==1))
+{
+aux = aux-100; 
+if (aux<=0)
+{
+aux = 0;
+}
+                                          
+lcd.locate(c,f);
+lcd.printf("   ");
+lcd.locate(c,f);
+lcd.printf("%d", aux);
+i=0;
+                               
+ }
+if ((bandera==1)&(i==1))
+{
+aux=aux-10;
+if(aux%100==0){bandera=2;}
+if (aux<=0)
+{
+aux = 0;
+}
+                                           
+lcd.locate(c,f);
+lcd.printf("   ");
+lcd.locate(c,f);
+lcd.printf("%d", aux);
+i=0;            }
+                
+ if ((bandera==0)&(i==1))
+ {
+--aux;  
+if(aux%10==0){bandera=1;}
+if (aux<=0)
+{
+aux = 0;
+}
+                                             
+lcd.locate(c,f);
+lcd.printf("   ");
+lcd.locate(c,f);
+lcd.printf("%d", aux);
+i=0; }
+                               
+                               
+                                
+}
+            
+bandera=0;
+return aux;
+};
+
+
 int main()
 {
     lcd.cls();
@@ -35,34 +173,58 @@
             if (p==1)
             {
                 ++sp;
+                  if (sp>=999)//se limita hasta 999
+                    {
+                       sp= 999;
+                    }
                 lcd.locate(3,0);
                 lcd.printf("   ");
                 lcd.locate(3,0);
                 lcd.printf("%d", sp);
+                wait (0.5);
+                sp=incremento(3,0,sp);    //se llama la función auxiliar
             }
             else if (p==2)
             {
                 ++kp;
+                  if (kp>=999)//se limita hasta 999
+                    {
+                       kp= 999;
+                    }
                 lcd.locate(11,0);
                 lcd.printf("   ");
                 lcd.locate(11,0);
                 lcd.printf("%d", kp);
+                wait (0.5);
+                kp=incremento(11,0,kp);    //se llama la función auxiliar
             }
             else if (p==3)
             {
                 ++ki;
+                    if (ki>=999)
+                    {
+                       ki= 999;
+                    }
                 lcd.locate(3,1);
                 lcd.printf("   ");
                 lcd.locate(3,1);
                 lcd.printf("%d", ki);
+                wait (0.5);
+                ki =incremento(3,1,ki);    //se llama la función auxiliar
             }
             else if (p==4)
             {
                 ++kd;
+                if (kd>=999)
+                    {
+                       kd = 999;
+                    }
                 lcd.locate(11,1);
                 lcd.printf("   ");
                 lcd.locate(11,1);
                 lcd.printf("%d", kd);
+                wait (0.5);
+                kd =incremento(11,1,kd);    //se llama la función auxiliar
             }
         }
         if (button2.falling())
@@ -80,6 +242,8 @@
                     lcd.printf("   ");
                     lcd.locate(3,0);
                     lcd.printf("%d", sp);
+                    wait(0.5);
+                    sp =decremento(3,0,sp);    //se llama la función auxiliar
                 }
             }
             if (p==2)
@@ -94,6 +258,8 @@
                     lcd.printf("   ");
                     lcd.locate(11,0);
                     lcd.printf("%d", kp);
+                    wait(0.5);
+                    kp =decremento(11,0,kp);     //se llama la función auxiliar
                 }
             }
             if (p==3)
@@ -108,6 +274,8 @@
                     lcd.printf("   ");
                     lcd.locate(3,1);
                     lcd.printf("%d", ki);
+                    wait(0.5);
+                    ki =decremento(3,1,ki);     //se llama la función auxiliar
                 }
             }
             if (p==4)
@@ -122,6 +290,8 @@
                     lcd.printf("   ");
                     lcd.locate(11,1);
                     lcd.printf("%d", kd);
+                    wait(0.5);
+                    kd =decremento(11,1,kd);     //se llama la función auxiliar
                 }
             }
         }
@@ -161,5 +331,88 @@
                 
             }
         }
+        
+        if (button4.falling()){           //Botón Enter
+            
+         break;        //se sale del ciclo cuando se oprime el botón Enter
+        }
+        
     }
+    
+              lcd.writeCommand(C4);//escribimos un comando segun el manual del modulo LCD para quitar cursor bajo
+           lcd.cls(); //borra la pantalla
+           lcd.printf("VALORES \nGUARDADOS"); 
+           wait(2);
+           
+           // se imprimen los parches del control  *****************************************
+           lcd.cls();
+           lcd.printf("Er=%d",err);
+           lcd.locate(8,0);
+           lcd.printf("Me=%d",med);
+           lcd.locate(0,1);
+           lcd.printf("Sp=%d",sp);
+           lcd.locate(8,1);
+           lcd.printf("Co=%d",pid);
+           wait(2); 
+           
+           
+                      // CICLO PRINCIPAL CONTROLADOR PID
+           while(1) {
+           med=1000*y.read();                   //leer puerto analogo y asignar a med
+           err = (sp-med);
+           ap = kp*err;
+            ai =(ki*err)+ai;    //calculo de la integral del error     
+           ad = kd*(err-err_v); //calculo de la accion derivativa
+           pid = (ap+ai+ad);
+           
+              
+            if (pid > 999){
+            pid=1000;
+            } 
+            else if (pid<0){
+            pid=0;
+            }     
+           // se actualizan las variables *******************************************
+           err_v = err;         
+           
+           //se muestran las variables******************************************
+           
+           if (b==0)
+           {
+            t.start();
+            b=1;
+           }
+if (t>=0.4){
+wait(0.05);
+lcd.locate(3,0);
+lcd.printf("     ");
+lcd.locate(3,0);
+lcd.printf("%d",err);
+lcd.locate(11,0);
+lcd.printf("     ");
+lcd.locate(11,0);
+lcd.printf("%d",med);
+lcd.locate(3,1);
+lcd.printf("     ");
+lcd.locate(3,1);
+lcd.printf("%d",sp);
+lcd.locate(11,1);
+lcd.printf("     ");
+lcd.locate(11,1);
+lcd.printf("%d ",pid);
+t.reset();
+b=0;
 }
+           
+           //Normalizacion de la salida
+           pidn=pid/1000;
+           //  se envia el valor pid a puerto analogico de salida (D/A) **************
+           u.write(pidn);
+          
+           //  se repite el ciclo
+           wait(0.1);
+           }
+           
+                      
+        }
+