PID_TECLAS

Dependencies:   DebouncedIn TextLCD mbed

Fork of PID_Encoder by Gustavo Ramirez

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