Programa para colocar las constantes de un PID con encoder y controlar una planta

Dependencies:   DebounceIn TextLCD mbed

Revision:
0:65c8fa8a589b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Oct 23 20:45:49 2015 +0000
@@ -0,0 +1,211 @@
+#include "mbed.h"
+#include "DebouncedIn.h"
+#include "TextLCD.h"
+#include "QEI.h" 
+ 
+AnalogIn Vin(PTC2); // entrada analoga 
+AnalogOut Vout(PTE30); // salida analoga
+//voltaje de salida maximo= 3.3 V
+ 
+TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7
+ 
+QEI wheel (PTD5, PTD0, NC, 150);
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DebouncedIn botonEncoder(PTC5);
+DebouncedIn button4(PTC17); // pulsador par dar "enter"
+ 
+ 
+//int C1=0x0E; //  muestra el cursor
+int C2=0x18; // desplaza izquierda
+int C3=0x1A; // desplaza derecha
+int C4=0x0C; // quito cursor bajo
+int C1=0x0F;
+int sp=0,kp=0,kd=0,ki=0,p=1,bandera = 0;
+int i; // indice de la variable
+int j; //variable controla cambio 4 posiciones
+float err;
+float pwmset;
+float eInteg;
+float pGain;
+float ePrev ;
+float iGain;
+float dGain;
+float x;
+int main()
+{
+    lcd.cls();
+    lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD
+    lcd.locate(8,0);
+    lcd.printf("kp=%d",kp);
+    lcd.locate(0,1);
+    lcd.printf("Ki=%d", ki);
+    lcd.locate(8,1);
+    lcd.printf("Kd=%d", kd);
+    lcd.locate(0,0);
+    lcd.printf("Sp=%d", sp);
+ 
+ while(1) {
+    
+              led3 =1;
+           if (botonEncoder.falling()) {   //INCREMENTA POSICION DEL MENU CON EL SWITCH DEL ENCODER
+              led3 =!led3;              
+               ++j;
+               }    
+                                                
+           if (j==0){      
+               sp=sp+wheel.getPulses();
+               wheel.reset();
+                 if (sp>5){
+                       sp=5;
+                            } 
+                 if (sp<0){
+                       sp=0;
+                          }   
+               lcd.locate(3,0);
+               lcd.printf("     ",sp);
+               lcd.locate(3,0);
+               lcd.printf("%d",sp);
+               wait(0.2);
+               
+               if(botonEncoder.falling()){
+                 j=1;
+                 led3=0;
+                 wait(0.3);
+                 wheel.reset();   
+                                    }   
+                   
+                     }
+              
+           if (j==1) {
+               kp=kp+wheel.getPulses();
+               wheel.reset();
+                 if (kp>999){
+                     kp=999;
+                          }              
+                 if (kp<0){
+                     kp=0;
+                          }   
+               lcd.locate(11,0);
+               lcd.printf("     ");
+               lcd.locate(11,0);
+               lcd.printf("%d",kp);
+               wait(0.2);
+               
+               if(botonEncoder.falling()){
+                 j=2;
+                 led3=0;
+                 wait(0.3);
+                 wheel.reset();    
+                                    }
+                                           
+                      }
+              
+           if (j==2) {
+               ki=ki+wheel.getPulses();
+               wheel.reset();
+                 if (ki>999){
+                     ki=999;
+                          }              
+                 if (ki<0){
+                     ki=0;
+                          }   
+               lcd.locate(3,1);
+               lcd.printf("     ");
+               lcd.locate(3,1);
+               lcd.printf("%d",ki);
+               wait(0.2);
+               
+               if(botonEncoder.falling()){
+                 j=3;
+                 led3=0;
+                 wait(0.3);
+                 wheel.reset();
+                                    }
+                                    
+                     }
+                     
+           if (j==3) {
+               kd=kd+wheel.getPulses();
+               wheel.reset();
+                 if (kd>999){
+                     kd=999;
+                            }              
+                 if (kd<0){
+                     kd=0;
+                          }   
+               lcd.locate(11,1);
+               lcd.printf("     ");
+               lcd.locate(11,1);
+               lcd.printf("%d",kd);
+               wait(0.2);
+               
+               if(botonEncoder.falling()){
+                 j=0;
+                 led3=0;
+                 wait(0.3);
+                 wheel.reset();
+                                    }
+                                            
+                       } 
+            
+           if (j==4) {
+               j=0;
+                     }                          
+                     
+           if (!button4){
+           break;        //sale del bucle si pisan suiche4
+                        }                  
+              }          //cierro while(1)
+          
+    //enter
+    if(button4.falling())
+    {
+      lcd.cls(); // al undir el boton 4 se borra la pantalla
+   //for (int h=0;h<100;h++)
+   //Vout= Vout+0.01;
+     while(bandera==0)
+     {
+     wait(0.2);
+     x=Vout.read(); // lee el voltaje de salida que nuestra analoga pero en porcentaje
+ 
+     //}
+  //pid
+    
+    err = sp-x;         // Calcula el error 
+   pwmset = kp* err+ki * eInteg +kd* (err - ePrev);    // ecuacion para el PID
+   pwmset=(0.976801/5)*pwmset;            // como el voltaje calculado fue un porcentaje se realiza una conversion respectiva al valor de salida
+    Vout=pwmset;
+    eInteg =eInteg+ err;                  // integral 
+    ePrev = err;
+    lcd.locate(0,1);
+    lcd.printf("error=%f",err);
+    lcd.locate(1,0);
+    lcd.printf("salida=%f",x);
+    if (button4.falling())
+    { bandera=1;
+    lcd.cls();
+    p=1;
+     lcd.locate(8,0);
+    lcd.printf("kp=%d",kp);
+    lcd.locate(0,1);
+    lcd.printf("Ki=%d", ki);
+    lcd.locate(8,1);
+    lcd.printf("Kd=%d", kd);
+    lcd.locate(0,0);
+    lcd.printf("Sp=%d", sp);
+ 
+    }
+ 
+     
+   }
+  bandera = 0;
+      }
+      
+                            
+                    
+    }
+ 
+ 
+