Mauricio Peña / Mbed 2 deprecated Encoder-PID-Tarea4

Dependencies:   Debounced QEI TextLCD11 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 #include "DebouncedIn.h"
00004 #include "QEI.h"
00005 
00006 TextLCD lcd(PTC10, PTC11, PTC12, PTC13, PTC16, PTC17);
00007 DebouncedIn next(PTE5);
00008 DebouncedIn menu(PTE4);
00009 int C1=0x0E;
00010 int C4=0x0C;
00011 AnalogOut Aout(PTE30);
00012 AnalogIn Ain(PTC2);
00013 QEI leftQei(PTD6, PTD7, NC, 624);
00014 
00015 int sp=0;
00016 int kp=0;
00017 int ki=0;
00018 int kd=0;
00019 
00020 int b;
00021 
00022 int c=0;
00023 int y=0;
00024 float i; // set point
00025 float j=0; // salida planta
00026 float ji=0; //variable proceso interno
00027 float spi=0; //variable proceso interno
00028 float h=0; // entrada planta
00029 float m=0; // Error
00030 float mv=0;
00031 float g=0;
00032 float x=0;
00033 float ap,ai,ad; 
00034 
00035 //NECESITO COMPARACIÓN Y YA 
00036 int pulsos (int x){
00037 
00038  
00039         if (leftQei.getPulses()>b){            
00040             x++;
00041             b=leftQei.getPulses();
00042              }      
00043         if (leftQei.getPulses()<b){            
00044             x--;
00045             b=leftQei.getPulses();
00046             if (x<0){
00047                x=0;
00048                     }
00049         }    
00050     return x;
00051 } // Fin Int main función pulsos
00052  
00053  int main() {
00054     lcd.cls();
00055     lcd.printf("Sp: %d",sp);
00056     lcd.locate(8,0);
00057     lcd.printf("Kp: %d",kp);
00058     lcd.locate(0,1);
00059     lcd.printf("Ki: %d",ki);
00060     lcd.locate(8,1);
00061     lcd.printf("Kd: %d",kd);
00062     lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD
00063     lcd.locate(0,0);
00064     lcd.printf("Sp: %d",sp);
00065  
00066 
00067 
00068  while(1) {
00069            if (next.falling()) {              
00070                ++c;
00071                }                                     //CAMBIO DE POSICIÓN ENCODER
00072            if (c==0){
00073                lcd.locate(3,0);
00074                lcd.putc(0xFE);           
00075                lcd.locate(4,0);
00076                lcd.printf("%d",sp);
00077                               
00078                sp=pulsos(sp);//revisar si puedo dejar sp
00079   
00080                //
00081            
00082                 if(sp>999){
00083                 sp=999;
00084                 }
00085                 
00086                 //NEW !! (not zeros in -- process)
00087                 if(sp<10){                
00088                 lcd.locate(5,0);
00089                 lcd.putc(0xFE);  
00090                         }
00091                         
00092                 if(sp<100){                
00093                 lcd.locate(6,0);
00094                 lcd.putc(0xFE);  
00095                         }                             
00096                 
00097                 lcd.locate(4,0);
00098                 lcd.printf("%d", sp);
00099                 //wait(0.1);  
00100                  
00101                //    
00102                   
00103               }
00104            if (c==1) {
00105                lcd.locate(11,0);
00106                lcd.putc(0xFE);           
00107                lcd.locate(12,0);
00108                lcd.printf("%d",kp);
00109                kp=pulsos(kp); //revisar si puedo dejar kp
00110                
00111                //
00112            
00113                 if(kp>999){
00114                 kp=999;
00115                 }
00116                 
00117                 //NEW !! (not zeros in -- process)
00118                 if(kp<10){                
00119                 lcd.locate(13,0);
00120                 lcd.putc(0xFE);  
00121                         }
00122                         
00123                 if(kp<100){                
00124                 lcd.locate(14,0);
00125                 lcd.putc(0xFE);  
00126                         }                             
00127                 
00128                 lcd.locate(12,0);
00129                 lcd.printf("%d", kp);
00130                 //wait(0.2);   
00131                  
00132                //    
00133 
00134               }
00135            if (c==2) {
00136                lcd.locate(3,1);
00137                lcd.putc(0xFE);               
00138                lcd.locate(4,1);
00139                lcd.printf("%d",ki);
00140                ki=pulsos(ki); //revisar si puedo dejar ki
00141                
00142                //
00143            
00144                 if(ki>999){
00145                 ki=999;
00146                 }
00147                 
00148                 //NEW !! (not zeros in -- process)
00149                 if(ki<10){                
00150                 lcd.locate(5,1);
00151                 lcd.putc(0xFE);  
00152                         }
00153                         
00154                 if(ki<100){                
00155                 lcd.locate(6,1);
00156                 lcd.putc(0xFE);  
00157                         }                             
00158                 
00159                 lcd.locate(4,1);
00160                 lcd.printf("%d", ki);
00161                 //wait(0.2);   
00162                  
00163                //    
00164               
00165               }
00166            if (c==3) {
00167                lcd.locate(11,1);
00168                lcd.putc(0xFE);           
00169                lcd.locate(12,1);
00170                lcd.printf("%d",kd);
00171                kd=pulsos(kd); //revisar si puedo dejar kd
00172                
00173                //
00174            
00175                 if(kd>999){
00176                 kd=999;
00177                 }
00178                 
00179                 //NEW !! (not zeros in -- process)
00180                 if(kd<10){                
00181                 lcd.locate(13,1);
00182                 lcd.putc(0xFE);  
00183                         }
00184                         
00185                 if(kd<100){                
00186                 lcd.locate(14,1);
00187                 lcd.putc(0xFE);  
00188                         }                             
00189                 
00190                 lcd.locate(12,1);
00191                 lcd.printf("%d", kd);
00192                 //wait(0.2);  
00193               } 
00194            if (c==4) {
00195                c=0;
00196                }                          
00197     
00198            if (menu.falling()){
00199                 break;    
00200                               }
00201           } //While    
00202 
00203            lcd.writeCommand(C4);//escribimos un comando segun el manual del modulo LCD para quitar cursor bajo
00204            lcd.cls(); //borra la pantalla
00205            lcd.printf("   GUARDADOS!"); 
00206            wait(2);
00207            lcd.cls();
00208            lcd.printf(" INICIA EL PID");
00209            wait(2);
00210            // se imprimen los parches del control  *****************************************
00211            
00212            i=sp/999;
00213            lcd.cls();
00214            lcd.printf("Er:%.2f",m);
00215            lcd.locate(8,0);
00216            lcd.printf("Me:%.2f",j);
00217            lcd.locate(0,1);
00218            lcd.printf("Sp:%d",sp);
00219            lcd.locate(8,1);
00220            lcd.printf("Co:%.1f",h);
00221            wait(2);
00222            
00223            //
00224         while(1) {
00225         
00226             spi=sp*(3.3/999);
00227             
00228         //wait(0.3);
00229               i=sp/999;
00230         if(i<=1){
00231        
00232         j=Ain;
00233         ji=j*3.3;
00234         m=(spi-ji);
00235         //n=m*100;
00236         ap=kp*m;
00237         ai=(ki*m)+ai;
00238         
00239         ad=kd*(m-mv);
00240         h=ap+ai+ad;
00241    
00242         if (h>999){
00243         h=999;
00244                    }
00245         if (h<0){
00246         h=0;
00247                    }
00248                               
00249         g=(h/999);
00250                                                        
00251         Aout=g;
00252         wait(0.22);
00253  
00254         
00255         lcd.cls();
00256         lcd.printf("Er:%.2f",m);
00257         lcd.locate(8,0);
00258         lcd.printf("Me:%.2f",j);
00259         lcd.locate(0,1);
00260         lcd.printf("Sp:%d",sp);
00261         lcd.locate(8,1);
00262         lcd.printf("Co:%.1f",h);
00263         }
00264 
00265 
00266         mv=m;
00267     } //While           
00268            //
00269 
00270           }//int main