Controlador PID con lectura mediante encoder

Dependencies:   DebouncedIn QEI TextLCD_encoder mbed

Files at this revision

API Documentation at this revision

Comitter:
lcorralesc1
Date:
Sun Dec 15 15:57:03 2013 +0000
Parent:
2:fa1d90037a6c
Commit message:
This program makes a PID control and the values for control action are entered by quadrature encoder.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r fa1d90037a6c -r 9347b362d33c main.cpp
--- a/main.cpp	Wed Nov 13 01:54:13 2013 +0000
+++ b/main.cpp	Sun Dec 15 15:57:03 2013 +0000
@@ -8,7 +8,7 @@
 TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7
 QEI wheel (PTD5, PTD0, NC, 100);
 
-AnalogIn y(PTB0);
+AnalogIn y(PTB1);
 AnalogOut u(PTE30);
 
 DigitalOut led1(LED1);
@@ -31,8 +31,10 @@
 
 int i; // indice de la variable
 int j; //variable controla cambio 4 posiciones
-int kp, ki, kd, sp, err, med, yr, ap, ai, ad, err_v, cycle, pid; 
+int kp, ki, kd, sp, yr, ap, ai, ad, err, med, err_v, cycle, pid; 
+int b=0; 
 float pidn;
+Timer t;
 
 int main() {
     lcd.cls();
@@ -176,20 +178,21 @@
            lcd.printf("Sp%d",sp);
            lcd.locate(8,1);
            lcd.printf("Co%d",pid);
-           wait(5); 
+           wait(1); 
            
            
            // CICLO PRINCIPAL CONTROLADOR PID
-           
+           b=0;
            while(1) {
            med=999*y.read();                   //leer puerto analogo y asignar a med
            err = (sp-med);
            ap = kp*err;
            
            // se verifica que la accion integral no sea muy grande
-           //if(ai<500){
+           if(ai<100)
+           {
               ai =(ki*err)+ai;    //calculo de la integral del error
-               //      }
+           }
            //else{
                 //Dejo de sumar la accion integral
              //  }
@@ -197,24 +200,32 @@
            ad = kd*(err-err_v); //calculo de la accion derivativa
            pid = (ap+ai+ad);
            
-           // se actualizan las variables *******************************************
-          // err_v = err;      
+              
                 
            // se verifica que pid sea positivo **************************************
-           if(pid<=0){
+           if(pid<=0)
+           {
               pid=0;
-                     }
+           }
            // se verifica que pid sea menor o igual la valor maximo *****************
-           if (pid > 999){
-               pid=999;
-                         } 
+           if (pid > 5000)
+           {
+               pid=5000;
+           } 
                       
            // se actualizan las variables *******************************************
-           //err_v = err;           
+           err_v = err;         
            
            //se muestran las variables******************************************
            
-           wait(0.3);
+           if (b==0)
+           {
+            t.start();
+            b=1;
+           }
+        if(t>=0.3)
+        {
+           wait(0.2);
            lcd.locate(2,0);
            lcd.printf("    ");
            lcd.locate(2,0);
@@ -231,16 +242,18 @@
            lcd.printf("    ");
            lcd.locate(10,1);
            lcd.printf("%d",pid);
-           
+        }  
            
            //Normalizacion de la salida
            pidn=pid/999;
            //  se envia el valor pid a puerto analogico de salida (D/A) **************
            u.write(pidn);
-           err_v = err; 
+          
            //  se repite el ciclo
+           wait(1.5);
            }
            
            
               
-        }
\ No newline at end of file
+        }
+        
\ No newline at end of file