Alejandro Marin / Mbed 2 deprecated Tarea3PID

Dependencies:   TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "DebouncedIn.h"
00003 #include "TextLCD.h"
00004 
00005 
00006 AnalogIn Vin(PTB3);
00007 AnalogOut Vout(PTE30);
00008 
00009 TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7
00010  
00011 DigitalOut led1(LED1);
00012 DigitalOut led2(LED2);
00013 DebouncedIn button1(PTC12);
00014 DebouncedIn button2(PTC13);
00015 DebouncedIn button3(PTC16);
00016 DebouncedIn button4(PTC17);
00017  
00018 /*    codigos movimiento del curzor
00019       18 para izquierda
00020       1A para derecha
00021 */      
00022  
00023  
00024 int C1=0x0E; // solo muestra el curzor
00025 int C2=0x18; // desplaza izquierda
00026 int C3=0x1A; // desplaza derecha
00027 int C4=0x0C; // quito cursor bajo
00028 int i; // indice de la variable
00029 int j;
00030 int Kp, Ki, Kd, Sp,  yr, cycle, med2 , c; 
00031 float med,pid,ap, err, ai, ad,err_v;
00032 int main() {
00033     
00034     lcd.writeCommand(0x0E);
00035     lcd.printf("Sp=     Kp=");
00036     lcd.locate(0,1);
00037     lcd.printf("Ki=     Kd=");
00038     lcd.locate(2,0);
00039     lcd.printf("=");
00040 
00041     while(1) {
00042       
00043     
00044      if(button1.falling()) {
00045                                         
00046             switch(i) {
00047                 case 0:
00048                     lcd.locate(2,0);
00049                     lcd.printf("=     ");
00050                     lcd.locate(3,0);
00051                     lcd.printf("%d",++Sp);
00052                     
00053                     break;
00054                 case 1:
00055                     lcd.locate(10,0);
00056                     lcd.printf("=     ");
00057                     lcd.locate(11,0);
00058                     lcd.printf("%d",++Kp);
00059                     break;
00060                 case 2:
00061                     
00062                     lcd.locate(2,1);
00063                     lcd.printf("=     ");
00064                     lcd.locate(3,1);
00065                     lcd.printf("%d",++Ki);
00066                     break;
00067                 case 3:
00068                     lcd.locate(10,1);
00069                     lcd.printf("=     ");
00070                     lcd.locate(11,1);
00071                     lcd.printf("%d",++Kd);
00072                     break;
00073             }
00074         }
00075         if(button2.falling()) {
00076             switch(i) {
00077                 case 0:
00078                     if(Sp<0) {
00079                         Sp=0;
00080                     }
00081                     lcd.locate(2,0);
00082                     lcd.printf("=     ");
00083                     lcd.locate(3,0);
00084                     lcd.printf("%d",Sp--);
00085                     break;
00086                 case 1:
00087                     if(Kp<0) {
00088                         Kp=0;
00089                     }
00090                     lcd.locate(10,0);
00091                     lcd.printf("=     ");
00092                     lcd.locate(11,0);
00093                     lcd.printf("%d",Kp--);
00094                     break;
00095                 case 2:
00096                    
00097                     if(Ki<0) {
00098                         Ki=0;
00099                     }
00100                     lcd.locate(2,1);
00101                     lcd.printf("=     ");
00102                     lcd.locate(3,1);
00103                     lcd.printf("%d",Ki--);
00104                     break;
00105                 case 3:
00106                    
00107                     if(Kd<0) {
00108                         Kd=0;
00109                     }
00110                     lcd.locate(10,1);
00111                     lcd.printf("=     ");
00112                     lcd.locate(11,1);
00113                     lcd.printf("%d",Kd--);
00114                     break;
00115             }
00116         }
00117         if(button3.falling()) {
00118             i++;
00119             if(i>3) {
00120                 i=0;
00121             }
00122             switch (i) {
00123                 case 0:
00124                     lcd.locate(2,0);
00125                     lcd.printf("=");
00126                     break;
00127                 case 1:
00128                     lcd.locate(10,0);
00129                     lcd.printf("=");
00130                     break;
00131                 case 2:
00132                     lcd.locate(2,1);
00133                     lcd.printf("=");
00134                     break;
00135                 case 3:
00136                     lcd.locate(10,1);
00137                     lcd.printf("=");
00138                     break;
00139             }
00140         }       
00141     
00142               
00143            if (button4.falling()){
00144            break;     //sale del bucle si pisan suiche4
00145                }
00146                         
00147                                         }
00148            lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD para quitar cursor bajo
00149            lcd.cls(); //borra la pantalla
00150            lcd.printf("   GUARDADOS!"); 
00151            wait(2);
00152            lcd.cls();
00153            lcd.printf(" INICIA EL PID");
00154            wait(2);
00155            // se imprimen los parches del control  *****************************************
00156            lcd.cls();
00157          
00158            
00159            lcd.printf("Er%d",err);
00160            lcd.locate(8,0);
00161            lcd.printf("Me%d",med2);
00162            lcd.locate(0,1);
00163            lcd.printf("Sp%d",Sp);
00164            lcd.locate(8,1);
00165            lcd.printf("Pid%d",pid);
00166            //wait(5);
00167            
00168            // CICLO PRINCIPAL CONTROLADOR PID
00169          
00170            while(1) {
00171                               //leer puerto analogo y asignar a med
00172            med=Vin.read();
00173            med2=med*100;
00174            err = (Sp-med2);
00175            float kp2;
00176            kp2=Kp*0.001;
00177            ap = kp2*err;
00178            float ki2;
00179            ki2=Ki*0.001;           
00180            ai =(ki2*err)+ai;    //calculo de la integral del error
00181            float kd2;
00182            kd2=Kd*0.0001;
00183            ad = kd2*(err-err_v); //calculo de la accion derivativa
00184            err_v=err;          //guarda el error
00185            pid = (ap+ai+ad);
00186            
00187               // se verifica que pid sea menor o igual la valor maximo *****************
00188             if (pid > .99999){
00189            pid=1;
00190            } 
00191            
00192            // se verifica que pid sea positivo **************************************
00193            if (pid <0){
00194            pid=0;
00195            } 
00196            
00197             //wait(.5);
00198             
00199            
00200            
00201            // se verifica que la accion integral no sea muy grande
00202            if (ai > 999){
00203            ai=1000;
00204            } 
00205           
00206            
00207            Vout=pid;
00208           
00209                      
00210            //****se muestran las variables******************************************
00211           if(c>600){           
00212            lcd.locate(2,0);
00213            lcd.printf("      "); 
00214            lcd.locate(0,0);
00215            lcd.printf("Er%2.2f",err);
00216            lcd.locate(10,0);
00217            lcd.printf("      ");
00218            lcd.locate(8,0);
00219            lcd.printf("Me%d",med2);
00220            lcd.locate(2,1);
00221            lcd.printf("      ");
00222            lcd.locate(0,1);
00223            lcd.printf("Sp%d",Sp);
00224            lcd.locate(10,1);
00225            lcd.printf("      ");
00226            lcd.locate(8,1);
00227            lcd.printf("Pid%4.3f",pid);
00228            c=0;
00229            }
00230            else 
00231            c++;
00232           
00233                            
00234            
00235             
00236            
00237            
00238            
00239            //  se envia el valor pid a puerto analogico de salida (D/A) **************
00240            //  se repite el ciclo
00241            }
00242 }