Esteban Lage / Mbed 2 deprecated ELJPGTarea4

Dependencies:   Debounced Pulse1 QEI RTC-DS1307 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 #include "QEI.h"
00005 #include "Rtc_Ds1307.h"
00006 
00007 
00008 TextLCD lcd(PTB0, PTB1, PTB2, PTB3, PTC2, PTC1); // rs, e, d4, d5, d6, d7
00009 Rtc_Ds1307 rtc(PTE0, PTE1);
00010 QEI wheel (PTD7, PTD6, NC, 30);
00011 DebouncedIn button_enco(PTC5);
00012 PwmOut PIDWM(PTC9);
00013 PwmOut PIDANAL(PTE24);
00014 DigitalOut Sel(PTE31);
00015 AnalogIn y(PTE16);
00016 float periodo;
00017 float Frecuencia=200;
00018 int C1=0x0C;
00019 int m;
00020 int Conf=0;  // 
00021 int Lugar=0; // Definen el punto en el cual va el programa
00022 int i=0;
00023 int Ref = 0, Kp = 0, Ti = 0, Td = 0; 
00024 float t1 = 0, t2 = 0, t3 = 0, ai = 0, ap = 0, ad = 0, pid = 0, med = 0, RefPID = 0,Taza = 0, o = 0;
00025 float err = 0, err_v = 0;
00026 
00027 int main()
00028 {
00029     Rtc_Ds1307::Time_rtc tm = {};
00030     lcd.cls();
00031     lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD
00032     lcd.locate(0,0);
00033     lcd.printf("--ANALOG  --PWM");
00034     
00035     periodo=(1/Frecuencia);
00036     rtc.getTime(tm); //lee el tiempo del DS1307
00037     Sel = 1;
00038     while(1)
00039     {
00040         switch (Lugar)
00041         {
00042             case 0:
00043                 m=wheel.getPulses();  // m son los datos del encoder
00044                 if(m!=0 && Conf==0){
00045                     Conf=1;
00046                     wheel.reset();  
00047                     m=0;              
00048                 }
00049                 
00050                  if(m!=0 && Conf==1){
00051                     Conf=0;
00052                     wheel.reset();  
00053                     m=0;              
00054                 }
00055                 if(Conf==0){
00056                     
00057                     lcd.locate(11,0);
00058                     lcd.printf("-");                    
00059                     lcd.locate(1,0);
00060                     lcd.printf(">");
00061                     lcd.locate(0,1);                    
00062                     wait(0.1);
00063                     
00064                 }
00065                 
00066                  if(Conf==1){
00067                     
00068                     lcd.locate(1,0);
00069                     lcd.printf("-");                    
00070                     lcd.locate(11,0);
00071                     lcd.printf(">");
00072                     lcd.locate(0,1);                    
00073                     wait(0.1);
00074                 }
00075                 
00076                 if (button_enco.falling()){  //si se pulsa boton encoder
00077                     Lugar++;
00078                     m=0;       
00079                 }
00080             
00081             break;
00082             
00083             case 1:
00084                 switch(i)
00085                 {
00086                     case 0:
00087                     
00088                                               
00089                         if(Conf==0)
00090                         {
00091                             lcd.cls();
00092                             lcd.locate(0,0);
00093                             lcd.printf("Parametros");
00094                             wait(1);
00095                             i++;
00096                             m=0;
00097                             lcd.cls();
00098                             lcd.locate(0,0);
00099                             lcd.printf("Ref:     Kp:  ");
00100                             lcd.locate(0,1);
00101                             lcd.printf("Ti:      Td:  ");                             
00102                         }
00103                 
00104                         if(Conf==1)
00105                         {
00106                             lcd.cls();
00107                             lcd.locate(0,0);
00108                             lcd.printf("Parametros");
00109                             wait(1);
00110                             i++;
00111                             m=0;
00112                             lcd.cls();
00113                             lcd.locate(0,0);
00114                             lcd.printf("Ref:     Kp:  ");
00115                             lcd.locate(0,1);
00116                             lcd.printf("Ti:      Td:  ");  
00117                         }    
00118 
00119                     break;
00120                     
00121                     case 1:
00122                         m=wheel.getPulses();        
00123                         if (m!=0) 
00124                         {
00125                             Ref=Ref+m;
00126                             if(Ref>99)
00127                             {
00128                                 Ref=99; // Configuracion de la ref para analg
00129                             }
00130                             if(Ref<0)
00131                             {
00132                                 Ref=0;
00133                             }
00134                             
00135                             wheel.reset();  
00136                             m=0;
00137                         }
00138                         lcd.locate(5,0);
00139                         lcd.printf("%02d",Ref);
00140                         
00141                         if (button_enco.falling())
00142                         {
00143                             i++;
00144                             m=0;
00145                             lcd.locate(13,0);
00146                             //tm.mon=1;
00147                         }
00148                     
00149                     break;
00150                     
00151                     case 2:
00152                         m=wheel.getPulses();        
00153                         if (m!=0) 
00154                         {
00155                             Kp=Kp+m;
00156                             if(Kp>99)
00157                             {
00158                                 Kp=99;
00159                             }
00160                             if(Kp<0)  // Configuracion de Kp para analg
00161                             {
00162                                 Kp=0;
00163                             }
00164                             
00165                             wheel.reset();  
00166                             m=0;
00167                         }
00168                         lcd.locate(13,0);
00169                         lcd.printf("%02d",Kp);
00170                         
00171                         if (button_enco.falling())
00172                         {
00173                             i++;
00174                             m=0;
00175                             lcd.locate(4,1);                            
00176                         }
00177                     
00178                     break;
00179                     
00180                     case 3:
00181                         m=wheel.getPulses();        
00182                         if (m!=0) 
00183                         {
00184                             Ti=Ti+m; // Configuracion de Ti para analg
00185                             
00186                             if(Ti>99)
00187                             {
00188                                 Ti=99;
00189                             }
00190                             if(Ti<0)  
00191                             {
00192                                 Ti=0;
00193                             }
00194                             
00195                             wheel.reset();  
00196                             m=0;
00197                         }
00198                         lcd.locate(4,1);
00199                         lcd.printf("%02d",Ti);
00200                         
00201                         if (button_enco.falling())
00202                         {
00203                             i++;
00204                             m=0;
00205                             lcd.locate(13,1);
00206                             
00207                         }
00208                     
00209                     break;
00210                     
00211                      case 4:
00212                         m=wheel.getPulses();
00213                                 
00214                         if (m!=0) 
00215                         {
00216                             Td=Td+m; // Configuracion de Ti para analg
00217                             
00218                             if(Td>99)
00219                             {
00220                                 Td=99;
00221                             }
00222                             if(Td<0)  // Configuracion de Td para analg
00223                             {
00224                                 Td=0;
00225                             }
00226                             
00227                             wheel.reset();  
00228                             m=0;
00229                         }
00230                         lcd.locate(13,1);
00231                         lcd.printf("%02d",Td);
00232                         
00233                         if (button_enco.falling())
00234                         {
00235                             i++;
00236                             m=0;
00237                             lcd.cls();
00238                             lcd.locate(0,0);
00239                             lcd.printf("t1:     t2:  ");
00240                             lcd.locate(0,1);
00241                             lcd.printf("t3:  ");
00242                             lcd.locate(4,0);
00243                         }
00244                     break;
00245                     
00246                      case 5:
00247                         m=wheel.getPulses();
00248                         if (m!=0) 
00249                         {
00250                             t1=t1+m; // Configuracion de Ti para analg
00251                             if(t1>60)
00252                             {
00253                                 t1=60;
00254                             }
00255                             if(t1<0)  // Configuracion de Td para analg
00256                             {
00257                                 t1=0;
00258                             }
00259                             
00260                             wheel.reset();  
00261                             m=0;
00262                         }
00263                         lcd.locate(4,0);
00264                         lcd.printf("%.1f",t1);
00265                         
00266                         
00267                         if (button_enco.falling())
00268                         {
00269                             i++;
00270                             m=0;
00271                             lcd.locate(12,0);
00272                         }
00273                     
00274                     break;
00275                     
00276                      case 6:
00277                         m=wheel.getPulses();
00278                                 
00279                         if (m!=0) 
00280                         {
00281                             t2=t2+m; // Configuracion de Ti para anal
00282                             if(t2>60)
00283                             {
00284                                 t2=60;
00285                             }
00286                             if(t2<0)  // Configuracion de Td para analg
00287                             {
00288                                 t2=0;
00289                             }
00290                             
00291                             wheel.reset();  
00292                             m=0;
00293                         }
00294                         lcd.locate(12,0);
00295                         lcd.printf("%.1f",t2);
00296                         
00297                         
00298                         if (button_enco.falling())
00299                         {
00300                             i++;
00301                             m=0;
00302                             lcd.locate(4,1);
00303                         }
00304                     
00305                     break;
00306                     
00307                      case 7:
00308                         m=wheel.getPulses();
00309                                 
00310                         if (m!=0) 
00311                         {
00312                             t3=t3+m; // Configuracion de Ti para analg
00313                             if(t3>60)
00314                             {
00315                                 t3=60;
00316                             }
00317                             if(t3<0)  // Configuracion de Td para analg
00318                             {
00319                                 t3=0;
00320                             }
00321                             
00322                             wheel.reset();  
00323                             m=0;
00324                         }
00325                         lcd.locate(4,1);
00326                         lcd.printf("%.1f",t3);
00327     
00328                         if (button_enco.falling())
00329                         {
00330                             ///////////////////
00331                             if(Conf==0 || Conf==1)
00332                             {
00333                                 lcd.cls();
00334                                 lcd.locate(0,0);
00335                                 lcd.printf(" Parametros OK ");
00336                                 wait(1);
00337                                 i=0;
00338                                 m=0;
00339                                 Lugar++;
00340                                 lcd.cls();
00341                                 tm.sec = 0;
00342                                 tm.min = 0;
00343                                 tm.hour = 1;
00344                                 tm.date = 1;
00345                                 tm.mon = 1;
00346                                 tm.year = 2000;
00347                                 PIDWM.pulsewidth(0);
00348                                 rtc.setTime(tm,false,false);
00349                                 rtc.startClock();
00350                                                                  
00351                             }
00352                            ///////////////////
00353                         }
00354                         break;
00355                         }
00356                     break;
00357                     case 2:
00358                     if (Conf == 1 || Conf == 0){
00359                          
00360 lop1:                    med = y.read()*100;
00361                          rtc.getTime(tm);
00362                          if ((tm.sec)<t1)
00363                          {
00364                                 Taza = Ref/t1;
00365                                 RefPID = tm.sec*Taza;
00366                          }
00367                          if ((tm.sec)>=t1 && (tm.sec)<=(t2+t1)){
00368                                 RefPID = Ref;
00369                          }
00370                          if ((tm.sec)>(t2+t1)){
00371                                 if (RefPID<23){
00372                                     RefPID=23;
00373                                     }
00374                                     else if (RefPID>23){
00375                                 Taza = (23-Ref)/t3/60;
00376                                 RefPID = RefPID + tm.sec*Taza;
00377                                 }
00378                          }
00379                          if ((tm.sec)>(t1+t2+t3) && med < 23){
00380                                 RefPID = 23;  
00381                          }
00382                          err = (RefPID-med);  //se calcula el error
00383                          ap = Kp*err*0.01f;     //se calcula la accion proporcinal
00384                          ai =(Ti*err*0.01f)+ai;    //calculo de la integral del error
00385                          ad = Td*(err-err_v)*0.01f; //calculo de la accion derivativa
00386                          pid = (ap+ai+ad);
00387                          // se verifica que pid sea positivo **************************************
00388                          if(pid<=0)
00389                          {
00390                          pid=0;
00391                          }
00392  
00393                          // se verifica que pid sea menor o igual la valor maximo *****************
00394                          if (pid > 100)
00395                          {
00396                          pid=100;
00397                          }
00398  
00399        
00400                           //se muestran las variables******************************************
00401                          lcd.locate(1,0);
00402                          lcd.printf("err:     med:");
00403                          lcd.locate(4,0);
00404                          lcd.printf("%3.0f",err);
00405                          lcd.locate(13,0);
00406                          lcd.printf("%3.0f",med);
00407                          lcd.locate(1,1);
00408                          lcd.printf("Ref:     pid:");
00409                          lcd.locate(4,1);
00410                          lcd.printf("%3.0f",RefPID);
00411                          lcd.locate(13,1);
00412                          lcd.printf("%3.0f",pid);
00413            
00414             
00415         
00416  
00417                         //Normalizacion de la salida
00418                         // se actualizan las variables *******************************************
00419                          err_v = err;
00420                          o = pid/100;
00421                              PIDWM.pulsewidth(o*periodo);
00422                          //  se envia el valor pid a puerto analogico de salida (D/A) **************
00423         
00424                          //  se repite el ciclo
00425                          wait_ms(100);
00426                          goto lop1;
00427                         }
00428                     }
00429         }         
00430     }
00431