Alexander Vallejo / Mbed 2 deprecated Tarea3

Dependencies:   Debounced 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 AnalogIn Vin(PTC2);
00006 TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7
00007 
00008 AnalogIn y(PTB0);
00009 AnalogOut u(PTE30);
00010 
00011 DigitalOut led1(LED1);
00012 DigitalOut led2(LED2);
00013 DigitalOut led3(LED3);
00014 DigitalIn button1(PTC12);
00015 DigitalIn button2(PTC13);
00016 DigitalIn button3(PTC16);
00017 DigitalIn button4(PTC17);
00018 
00019 
00020 //codigos movimiento del curzor
00021 
00022 //int C1=0x0E; // solo muestra el curzor
00023 int C2=0x18; // desplaza izquierda
00024 int C3=0x1A; // desplaza derecha
00025 int C4=0x0C; // quito cursor bajo
00026 
00027 int C1=0x0F;
00028 int err, med, yr, pid, ap, ai, ad, err_v, cycle;
00029 float pidn;
00030 int spnum=0,kinum=0,kpnum=0,kdnum=0,pos=1;
00031 int incremento=1,flagt=0;
00032 Timer t;
00033 
00034 int main()
00035 {
00036     lcd.cls(); // Borrar Pantalla
00037     lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD
00038 
00039     lcd.locate(8,0);
00040     lcd.printf("Kp=%d",kpnum);
00041     lcd.locate(0,1);
00042     lcd.printf("Ki=%d",kinum);
00043     lcd.locate(8,1);
00044     lcd.printf("Kd=%d",kdnum);
00045     lcd.locate(0,0);
00046     lcd.printf("Sp=%d",spnum);
00047 
00048     while(1)
00049     {
00050         if (!button1)
00051         {
00052             if(flagt==0)
00053             {
00054                 t.start();
00055                 flagt=1;
00056             }
00057 
00058             led1 =!led1;
00059             wait(0.25);
00060             if(!button1)
00061             {
00062                 if(t.read()>5)
00063                 {
00064                     incremento=100;
00065                 }
00066                 else if(t.read()>2.75)
00067                 {
00068                     incremento=10;
00069                 }
00070 
00071                 if(pos==1)
00072                 {
00073                     if(spnum+incremento>=999)
00074                     {
00075                         spnum=999;
00076                         lcd.locate(3,0);
00077                         lcd.printf("    ");
00078                         lcd.locate(3,0);
00079                         lcd.printf("%d", spnum);
00080                     }
00081                     else
00082                     {
00083                         spnum+=incremento;
00084                         lcd.locate(3,0);
00085                         lcd.printf("%d", spnum);
00086                     }
00087                 }
00088                 else if(pos==2)
00089                 {
00090                     if(kpnum+incremento>=999)
00091                     {
00092                         kpnum=999;
00093                         lcd.locate(11,0);
00094                         lcd.printf("    ");
00095                         lcd.locate(11,0);
00096                         lcd.printf("%d", kpnum);
00097                     }
00098                     else
00099                     {
00100                         kpnum+=incremento;
00101                         lcd.locate(11,0);
00102                         lcd.printf("%d", kpnum);
00103                     }
00104                 }
00105                 else if(pos==3)
00106                 {
00107                     if(kinum+incremento>=999)
00108                     {
00109                         kinum=999;
00110                         lcd.locate(3,1);
00111                         lcd.printf("    ");
00112                         lcd.locate(3,1);
00113                         lcd.printf("%d", kinum);
00114                     }
00115                     else
00116                     {
00117                         kinum+=incremento;
00118                         lcd.locate(3,1);
00119                         lcd.printf("%d", kinum);
00120                     }
00121                 }
00122                 else if(pos==4)
00123                 {
00124                     if(kdnum+incremento>=999)
00125                     {
00126                         kdnum=999;
00127                         lcd.locate(11,1);
00128                         lcd.printf("    ");
00129                         lcd.locate(11,1);
00130                         lcd.printf("%d", kdnum);
00131                     }
00132                     else
00133                     {
00134                         kdnum+=incremento;
00135                         lcd.locate(11,1);
00136                         lcd.printf("%d", kdnum);
00137                     }
00138                 }
00139             }
00140         }
00141 
00142         if (!button2)
00143         {
00144             if(flagt==0)
00145             {
00146                 //t.reset();
00147                 t.start();
00148                 flagt=1;
00149             }
00150 
00151             led1 =!led1;
00152             wait(0.25);
00153             if(!button2)
00154             {
00155                 if(t.read()>5)
00156                 {
00157                     incremento=100;
00158                 }
00159                 else if(t.read()>2.75)
00160                 {
00161                     incremento=10;
00162                 }
00163 
00164                 led2 =!led2;
00165                 if(pos==1)
00166                 {
00167                     if(spnum-incremento<0)
00168                     {
00169                         //No ocurre nada
00170                     }
00171                     else
00172                     {
00173                         spnum-=incremento;
00174                         lcd.locate(3,0);
00175                         lcd.printf("    ");
00176                         lcd.locate(3,0);
00177                         lcd.printf("%d", spnum);
00178                     }
00179                 }
00180                 else if(pos==2)
00181                 {
00182                     if(kpnum-incremento<0)
00183                     {
00184                         //No ocurre nada
00185                     }
00186                     else
00187                     {
00188                         kpnum-=incremento;
00189                         lcd.locate(11,0);
00190                         lcd.printf("    ");
00191                         lcd.locate(11,0);
00192                         lcd.printf("%d", kpnum);
00193                     }
00194                 }
00195                 else if(pos==3)
00196                 {
00197                     if(kinum-incremento<0)
00198                     {
00199                         //No ocurre nada
00200                     }
00201                     else
00202                     {
00203                         kinum-=incremento;
00204                         lcd.locate(3,1);
00205                         lcd.printf("    ");
00206                         lcd.locate(3,1);
00207                         lcd.printf("%d", kinum);
00208                     }
00209                 }
00210                 else if(pos==4)
00211                 {
00212                     if(kdnum-incremento<0)
00213                     {
00214                         //No ocurre nada
00215                     }
00216                     else
00217                     {
00218                         kdnum-=incremento;
00219                         lcd.locate(11,1);
00220                         lcd.printf("    ");
00221                         lcd.locate(11,1);
00222                         lcd.printf("%d", kdnum);
00223                     }
00224                 }
00225             }
00226         }
00227 
00228         if (!button3)
00229         {
00230             led3 =!led3;
00231             if(pos==4)
00232             {
00233                 pos=1;
00234                 lcd.locate(3,0);
00235                 lcd.printf("%d", spnum);
00236             }
00237             else if (pos==1)
00238             {
00239                 pos++;
00240                 lcd.locate(11,0);
00241                 lcd.printf("%d", kpnum);
00242             }
00243             else if(pos==2)
00244             {
00245                 pos++;
00246                 lcd.locate(3,1);
00247                 lcd.printf("%d", kinum);
00248             }
00249             else if(pos==3)
00250             {
00251                 pos++;
00252                 lcd.locate(11,1);
00253                 lcd.printf("%d", kdnum);
00254             }
00255             wait(0.25);
00256 
00257         }
00258 
00259         if (button1 && button2)
00260         {
00261             flagt=0;
00262             t.reset();
00263             incremento=1;
00264         }
00265         if (!button4)
00266         {
00267             break;     //sale del bucle si pisan suiche4
00268         }
00269     }
00270 
00271 
00272     //Transicion
00273     lcd.writeCommand(C4);//escribimos un comando segun el manual del modulo LCD para quitar cursor bajo
00274     lcd.cls(); //borra la pantalla
00275     lcd.printf("   GUARDADOS!");
00276     wait(1);
00277     lcd.cls();
00278     lcd.printf(" INICIA EL PID");
00279     wait(1);
00280     // se imprimen los parches del control  *****************************************
00281     lcd.cls();
00282     lcd.printf("Er=%d",err);
00283     lcd.locate(8,0);
00284     lcd.printf("Me=%d",med);
00285     lcd.locate(0,1);
00286     lcd.printf("Sp=%d",spnum);
00287     lcd.locate(8,1);
00288     lcd.printf("Co=%d",pid);
00289     wait(2);
00290 
00291     // CICLO PRINCIPAL CONTROLADOR PID
00292     flagt=0;
00293     while(1)
00294     {
00295         med=999*y.read();   //leer puerto analogo y asignar a med
00296         err = (spnum-med);  //se calcula el error
00297 
00298         ap = kpnum*err;     //se calcula la accion proporcinal
00299 
00300         // se verifica que la accion integral no sea muy grande
00301         if(ai<100)
00302         {
00303             ai =(kinum*err)+ai;    //calculo de la integral del error
00304         }
00305 
00306         ad = kdnum*(err-err_v); //calculo de la accion derivativa
00307 
00308         pid = (ap+ai+ad);
00309 
00310         // se verifica que pid sea positivo **************************************
00311         if(pid<=0)
00312         {
00313             pid=0;
00314         }
00315 
00316         // se verifica que pid sea menor o igual la valor maximo *****************
00317         if (pid > 999)
00318         {
00319             pid=999;
00320         }
00321 
00322         // se actualizan las variables *******************************************
00323         err_v = err;
00324 
00325         //se muestran las variables******************************************
00326         if(flagt==0)
00327         {
00328             t.start();
00329             flagt=1;
00330         }
00331         if(t>=0.3)
00332         {
00333             lcd.locate(3,0);lcd.printf("    ");
00334             lcd.locate(3,0);lcd.printf("%d",err);
00335             lcd.locate(11,0);lcd.printf("    ");
00336             lcd.locate(11,0);lcd.printf("%d",med);
00337             lcd.locate(3,1);lcd.printf("    ");
00338             lcd.locate(3,1);lcd.printf("%d",spnum);
00339             lcd.locate(11,1);lcd.printf("    ");
00340             lcd.locate(11,1);lcd.printf("%d",pid);
00341             flagt=0;
00342             t.reset();
00343         }
00344 
00345         //Normalizacion de la salida
00346         pidn=pid/999;
00347         //  se envia el valor pid a puerto analogico de salida (D/A) **************
00348         u.write(pidn);
00349         //  se repite el ciclo
00350         wait(0.005);
00351     }
00352 
00353 }