Katherine Ortega / Mbed 2 deprecated TareaPID

Dependencies:   DebouncedIn TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 #include "DebouncedIn.h"
00004 #include "TextLCD.h"
00005 
00006 AnalogIn Vin(PTC2);
00007 TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7
00008 
00009 AnalogIn y(PTB0);
00010 AnalogOut u(PTE30);
00011 
00012 DigitalOut l1(LED1);
00013 DigitalOut l2(LED2);
00014 DigitalOut l3(LED3);
00015 DigitalIn b1(PTC12);
00016 DigitalIn b2(PTC13);
00017 DigitalIn b3(PTC16);
00018 DigitalIn b4(PTC17);
00019 
00020 int spnum=0,kpnum=0,kinum=0,kdnum=0,k=1;
00021 int bandera;
00022 Timer t;
00023 float tiempo;
00024 
00025 //codigos movimiento del curzor
00026 
00027 //int C1=0x0E; // solo muestra el curzor
00028 int C2=0x18; // desplaza izquierda
00029 int C3=0x1A; // desplaza derecha
00030 int C4=0x0C; // quito cursor bajo
00031 
00032 int C1=0x0F;
00033 int err, med, yr, pid, ap, ai, ad, err_v, cycle;
00034 float pidn;
00035 int pos=1;
00036 //int spnum=500, kinum=2, kpnum=1, kdnum=0, pos=1;
00037 //int incremento=1,flagt=0;
00038 
00039 int main()
00040 {
00041   lcd.cls(); // Borrar Pantalla
00042     lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD
00043 
00044     lcd.locate(8,0);
00045     lcd.printf("Kp=%d",kpnum);
00046     lcd.locate(0,1);
00047     lcd.printf("Ki=%d",kinum);
00048     lcd.locate(8,1);
00049     lcd.printf("Kd=%d",kdnum);
00050     lcd.locate(0,0);
00051     lcd.printf("Sp=%d",spnum);
00052     
00053        while(1)
00054        {
00055         while(k==1)
00056         {
00057             if(b1 & spnum<=1000)
00058             {
00059                 l1 = !l1;
00060                 if(bandera==0)
00061                 {
00062                     t.start();
00063                     bandera=1;
00064                 }
00065                 wait(0.50);
00066                 if(b1 & spnum<=1000)
00067                 {
00068                     tiempo=t.read();
00069                     if(tiempo<5)
00070                     {
00071                         spnum++;
00072                         lcd.locate(3,0);
00073                         lcd.printf("%d",spnum);
00074                     }
00075 
00076                     if(tiempo>=5 & tiempo<10)
00077                     {
00078                         spnum+=10;
00079                         lcd.locate(3,0);
00080                         lcd.printf("%d",spnum);
00081                     }
00082 
00083                     if(tiempo>=10)
00084                     {
00085                         spnum+=100;
00086                         lcd.locate(3,0);
00087                         lcd.printf("%d",spnum);
00088                     }
00089                 }
00090                 else
00091                 {
00092                     t.start();
00093                     bandera=0;
00094                 }
00095             }
00096 
00097 
00098             if(b2 & spnum>=1)
00099             {
00100                 l2 = !l2;
00101 
00102                 if(bandera==0)
00103                 {
00104                     t.start();
00105                     bandera=1;
00106                 }
00107 
00108                 wait(0.50);
00109 
00110                 if(b2 & spnum>=1)
00111                 {
00112                     tiempo=t.read();
00113 
00114                     if(tiempo<5)
00115                     {
00116                         spnum--;
00117                         lcd.locate(3,0);
00118                         lcd.printf("%d",spnum);
00119                     }
00120 
00121                     if(tiempo>=5 & tiempo<10)
00122                     {
00123                         spnum-=10;
00124                         lcd.locate(3,0);
00125                         lcd.printf("%d",spnum);
00126                     }
00127 
00128                     if(tiempo>=10)
00129                     {
00130                         spnum-=100;
00131                         lcd.locate(3,0);
00132                         lcd.printf("%d",spnum);
00133                     }
00134                 }
00135                 else
00136                 {
00137                     t.start();
00138                     bandera=0;
00139                 }
00140             }
00141             if (b3)
00142             {
00143                 k+=1;
00144                 wait(0.25);
00145                 lcd.locate(12,0);lcd.printf("%d",kpnum);               
00146             }
00147 
00148         }
00149 
00150         while(k==2)
00151         {
00152             if(b1 & kpnum<=1000)
00153             {
00154                 l1 = !l1;
00155                 if(bandera==0)
00156                 {
00157                     t.start();
00158                     bandera=1;
00159                 }
00160                 wait(0.50);
00161 
00162                 if(b1 & kpnum<=1000)
00163                 {
00164                     tiempo=t.read();
00165 
00166                     if(tiempo<5)
00167                     {
00168                         kpnum++;
00169                         lcd.locate(12,0);
00170                         lcd.printf("%d",kpnum);
00171                     }
00172 
00173                     if(tiempo>=5 & tiempo<10)
00174                     {
00175                         kpnum+=10;
00176                         lcd.locate(12,0);
00177                         lcd.printf("%d",kpnum);
00178                     }
00179 
00180                     if(tiempo>=10)
00181                     {
00182                         kpnum+=100;
00183                         lcd.locate(12,0);
00184                         lcd.printf("%d",kpnum);
00185                     }
00186                 }
00187                 else
00188                 {
00189                     t.start();
00190                     bandera=0;
00191                 }
00192             }
00193             if(b2 & kpnum>=1)
00194             {
00195                 l2 = !l2;
00196 
00197                 if(bandera==0)
00198                 {
00199                     t.start();
00200                     bandera=1;
00201                 }
00202                 wait(0.50);
00203 
00204                 if(b2 & kpnum>=1)
00205                 {
00206                     tiempo=t.read();
00207 
00208                     if(tiempo<5)
00209                     {
00210                         kpnum--;
00211                         lcd.locate(12,0);
00212                         lcd.printf("%d",kpnum);
00213                     }
00214 
00215                     if(tiempo>=5 & tiempo<10)
00216                     {
00217                         kpnum-=10;
00218                         lcd.locate(12,0);
00219                         lcd.printf("%d",kpnum);
00220                     }
00221 
00222                     if(tiempo>=10)
00223                     {
00224                         kpnum-=100;
00225                         lcd.locate(12,0);
00226                         lcd.printf("%d",kpnum);
00227                     }
00228                 }
00229                 else
00230                 {
00231                     t.start();
00232                     bandera=0;
00233                 }
00234             }
00235             if (b3)
00236             {
00237                 k+=1;
00238                 wait(0.25);
00239                 lcd.locate(3,1); lcd.printf("%d",kinum);
00240             }
00241         }
00242 
00243 
00244         while(k==3)
00245         {
00246             if(b1 & kinum<=1000)
00247             {
00248                 l1 = !l1;
00249 
00250                 if(bandera==0)
00251                 {
00252                     t.start();
00253                     bandera=1;
00254                 }
00255                 wait(0.50);
00256 
00257                 if(b1 & kinum<=1000)
00258                 {
00259                     tiempo=t.read();
00260 
00261                     if(tiempo<5)
00262                     {
00263                         kinum++;
00264                         lcd.locate(3,1);
00265                         lcd.printf("%d",kinum);
00266                     }
00267 
00268                     if(tiempo>=5 & tiempo<10)
00269                     {
00270                         kinum+=10;
00271                         lcd.locate(3,1);
00272                         lcd.printf("%d",kinum);
00273                     }
00274 
00275                     if(tiempo>=10)
00276                     {
00277                         kinum+=100;
00278                         lcd.locate(3,1);
00279                         lcd.printf("%d",kinum);
00280                     }
00281                 }
00282 
00283                 else
00284                 {
00285                     t.start();
00286                     bandera=0;
00287                 }
00288             }
00289 
00290             if(b2 & kinum>=1)
00291             {
00292                 l2 = !l2;
00293                 if(bandera==0)
00294                 {
00295                     t.start();
00296                     bandera=1;
00297                 }
00298                 wait(0.50);
00299 
00300                 if(b2 & kinum>=1)
00301                 {
00302                     tiempo=t.read();
00303 
00304                     if(tiempo<5)
00305                     {
00306                         kinum--;
00307                         lcd.locate(3,1);
00308                         lcd.printf("%d",kinum);
00309                     }
00310 
00311                     if(tiempo>=5 & tiempo<10)
00312                     {
00313                         kinum-=10;
00314                         lcd.locate(3,1);
00315                         lcd.printf("%d",kinum);
00316                     }
00317 
00318                     if(tiempo>=10)
00319                     {
00320                         kinum-=100;
00321                         lcd.locate(3,1);
00322                         lcd.printf("%d",kinum);
00323                     }
00324                 }
00325                 else
00326                 {
00327                     t.start();
00328                     bandera=0;
00329                 }
00330             }
00331             if (b3)
00332             {
00333                 k+=1;
00334                 wait(0.25);
00335                 lcd.locate(12,1); lcd.printf("%d",kdnum);
00336             }
00337         }
00338 
00339         while(k==4)
00340         {
00341             if(b1 & kdnum<=1000)
00342             {
00343                 l1 = !l1;
00344                 if(bandera==0)
00345                 {
00346                     t.start();
00347                     bandera=1;
00348                 }
00349 
00350                 wait(0.50);
00351 
00352                 if(b1 & kdnum<=1000)
00353                 {
00354                     tiempo=t.read();
00355 
00356                     if(tiempo<5)
00357                     {
00358                         kdnum++;
00359                         lcd.locate(12,1);
00360                         lcd.printf("%d",kdnum);
00361                     }
00362 
00363                     if(tiempo>=5 & tiempo<10)
00364                     {
00365                         kdnum+=10;
00366                         lcd.locate(12,1);
00367                         lcd.printf("%d",kdnum);
00368                     }
00369 
00370                     if(tiempo>=10)
00371                     {
00372                         kdnum+=100;
00373                         lcd.locate(12,1);
00374                         lcd.printf("%d",kdnum);
00375                     }
00376                 }
00377                 else
00378                 {
00379                     t.start();
00380                     bandera=0;
00381                 }
00382             }
00383 
00384 
00385             if(b2 & kdnum>=1)
00386             {
00387                 l2 = !l2;
00388 
00389                 if(bandera==0)
00390                 {
00391                     t.start();
00392                     bandera=1;
00393                 }
00394                 wait(0.50);
00395 
00396                 if(b2 & kdnum>=1)
00397                 {
00398                     tiempo=t.read();
00399 
00400                     if(tiempo<5)
00401                     {
00402                         kdnum--;
00403                         lcd.locate(12,1);
00404                         lcd.printf("%d",kdnum);
00405                     }
00406 
00407                     if(tiempo>=5 & tiempo<10)
00408                     {
00409                         kdnum-=10;
00410                         lcd.locate(12,1);
00411                         lcd.printf("%d",kdnum);
00412                     }
00413 
00414                     if(tiempo>=10)
00415                     {
00416                         kdnum-=100;
00417                         lcd.locate(12,1);
00418                         lcd.printf("%d",kdnum);
00419                     }
00420                 }
00421                 else
00422                 {
00423                     t.start();
00424                     bandera=0;
00425                 }
00426             }
00427 
00428             if (b3)
00429             {
00430                 k=1;
00431                 wait(0.25);
00432                 lcd.locate(3,0);
00433                 lcd.printf("%d",spnum);
00434             }
00435 
00436         }
00437         
00438     
00439         if (b4)
00440         {
00441            break;     //sale del bucle si pisan suiche4
00442         }
00443         continue;
00444     }
00445 
00446 
00447     //Transicion
00448     
00449     lcd.writeCommand(C4);//escribimos un comando segun el manual del modulo LCD para quitar cursor bajo
00450     lcd.cls(); //borra la pantalla
00451     lcd.printf("GUARDADOS!");
00452     wait(1);
00453     lcd.cls();
00454     lcd.printf(" INICIA EL PID");
00455     wait(1);
00456     // se imprimen los parches del control  *****************************************
00457     lcd.cls();
00458     lcd.printf("Er=%d",err);
00459     lcd.locate(8,0);
00460     lcd.printf("Me=%d",med);
00461     lcd.locate(0,1);
00462     lcd.printf("Sp=%d",spnum);
00463     lcd.locate(8,1);
00464     lcd.printf("Co=%d",pid);
00465     wait(2);
00466     
00467     // CICLO PRINCIPAL CONTROLADOR PID
00468     bandera=0;
00469     while(1)
00470     {
00471         med=999*y.read();   //leer puerto analogo y asignar a med
00472         err = (spnum-med);  //se calcula el error
00473 
00474         ap = kpnum*err;     //se calcula la accion proporcinal
00475 
00476         // se verifica que la accion integral no sea muy grande
00477         if(ai<100)
00478         {
00479             ai =(kinum*err)+ai;    //calculo de la integral del error
00480         }
00481 
00482         ad = kdnum*(err-err_v); //calculo de la accion derivativa
00483 
00484         pid = (ap+ai+ad);
00485 
00486         // se verifica que pid sea positivo **************************************
00487         if(pid<=0)
00488         {
00489             pid=0;
00490         }
00491 
00492         // se verifica que pid sea menor o igual la valor maximo *****************
00493         if (pid > 999)
00494         {
00495             pid=999;
00496         }
00497 
00498         // se actualizan las variables *******************************************
00499         err_v = err;
00500 
00501         //se muestran las variables******************************************
00502         if(bandera==0)
00503         {
00504             t.start();
00505             bandera=1;
00506         }
00507         if(t>=0.3)
00508         {
00509             lcd.locate(3,0);lcd.printf("    ");
00510             lcd.locate(3,0);lcd.printf("%d",err);
00511             lcd.locate(11,0);lcd.printf("    ");
00512             lcd.locate(11,0);lcd.printf("%d",med);
00513             lcd.locate(3,1);lcd.printf("    ");
00514             lcd.locate(3,1);lcd.printf("%d",spnum);
00515             lcd.locate(11,1);lcd.printf("    ");
00516             lcd.locate(11,1);lcd.printf("%d",pid);
00517             bandera=0;
00518             t.reset();
00519         }
00520 
00521         //Normalizacion de la salida
00522         pidn=pid/999;
00523         //  se envia el valor pid a puerto analogico de salida (D/A) **************
00524         u.write(pidn);
00525         //  se repite el ciclo
00526         wait(0.005);
00527     }
00528 
00529 }