Melissa Elorza / Mbed 2 deprecated PID_con_Encoder

Dependencies:   Debounced QEI 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 
00006 AnalogIn Vin(PTC2); // entrada analoga 
00007 AnalogOut Vout(PTE30); // salida analoga
00008 //voltaje de salida maximo= 3.3 V
00009 
00010 TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7
00011 
00012 QEI wheel (PTD5, PTD0, NC, 150);
00013 DigitalOut led1(LED1);
00014 DigitalOut led2(LED2);
00015 DigitalOut led3(LED3);
00016 DebouncedIn botonEncoder(PTC5);
00017 DebouncedIn button4(PTC17);
00018 
00019 
00020 //int C1=0x0E; // solo muestra el curzor
00021 int C2=0x18; // desplaza izquierda
00022 int C3=0x1A; // desplaza derecha
00023 int C4=0x0C; // quito cursor bajo
00024 int C1=0x0F;
00025 int sp=0,kp=0,kd=0,ki=0,p=1,bandera = 0;
00026 int i; // indice de la variable
00027 int j; //variable controla cambio 4 posiciones
00028 float err;
00029 float pwmset;
00030 float eInteg;
00031 float pGain;
00032 float ePrev ;
00033 float iGain;
00034 float dGain;
00035 float x;
00036 int main()
00037 {
00038     lcd.cls();
00039     lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD
00040     lcd.locate(8,0);
00041     lcd.printf("kp=%d",kp);
00042     lcd.locate(0,1);
00043     lcd.printf("Ki=%d", ki);
00044     lcd.locate(8,1);
00045     lcd.printf("Kd=%d", kd);
00046     lcd.locate(0,0);
00047     lcd.printf("Sp=%d", sp);
00048 
00049  while(1) {
00050     
00051               led3 =1;
00052            if (botonEncoder.falling()) {   //INCREMENTA POSICION DEL MENU CON BOTON 3 (Switche encoder)
00053               led3 =!led3;              
00054                ++j;
00055                }    
00056                                                 
00057            if (j==0){      
00058                sp=sp+wheel.getPulses();
00059                wheel.reset();
00060                  if (sp>5){
00061                        sp=5;
00062                             } 
00063                  if (sp<0){
00064                        sp=0;
00065                           }   
00066                lcd.locate(3,0);
00067                lcd.printf("     ",sp);
00068                lcd.locate(3,0);
00069                lcd.printf("%d",sp);
00070                wait(0.2);
00071                
00072                if(botonEncoder.falling()){
00073                  j=1;
00074                  led3=0;
00075                  wait(0.3);
00076                  wheel.reset();   
00077                                     }   
00078                    
00079                      }
00080               
00081            if (j==1) {
00082                kp=kp+wheel.getPulses();
00083                wheel.reset();
00084                  if (kp>999){
00085                      kp=999;
00086                           }              
00087                  if (kp<0){
00088                      kp=0;
00089                           }   
00090                lcd.locate(11,0);
00091                lcd.printf("     ");
00092                lcd.locate(11,0);
00093                lcd.printf("%d",kp);
00094                wait(0.2);
00095                
00096                if(botonEncoder.falling()){
00097                  j=2;
00098                  led3=0;
00099                  wait(0.3);
00100                  wheel.reset();    
00101                                     }
00102                                            
00103                       }
00104               
00105            if (j==2) {
00106                ki=ki+wheel.getPulses();
00107                wheel.reset();
00108                  if (ki>999){
00109                      ki=999;
00110                           }              
00111                  if (ki<0){
00112                      ki=0;
00113                           }   
00114                lcd.locate(3,1);
00115                lcd.printf("     ");
00116                lcd.locate(3,1);
00117                lcd.printf("%d",ki);
00118                wait(0.2);
00119                
00120                if(botonEncoder.falling()){
00121                  j=3;
00122                  led3=0;
00123                  wait(0.3);
00124                  wheel.reset();
00125                                     }
00126                                     
00127                      }
00128                      
00129            if (j==3) {
00130                kd=kd+wheel.getPulses();
00131                wheel.reset();
00132                  if (kd>999){
00133                      kd=999;
00134                             }              
00135                  if (kd<0){
00136                      kd=0;
00137                           }   
00138                lcd.locate(11,1);
00139                lcd.printf("     ");
00140                lcd.locate(11,1);
00141                lcd.printf("%d",kd);
00142                wait(0.2);
00143                
00144                if(botonEncoder.falling()){
00145                  j=0;
00146                  led3=0;
00147                  wait(0.3);
00148                  wheel.reset();
00149                                     }
00150                                             
00151                        } 
00152             
00153            if (j==4) {
00154                j=0;
00155                      }                          
00156                      
00157            if (!button4){
00158            break;        //sale del bucle si pisan suiche4
00159                         }                  
00160               }          //cierro while(1)
00161           
00162     //enter
00163     if(button4.falling())
00164     {
00165       lcd.cls(); // al undir el boton 4 se borra la pantalla
00166    //for (int h=0;h<100;h++)
00167    //Vout= Vout+0.01;
00168      while(bandera==0)
00169      {
00170      wait(0.2);
00171      x=Vout.read(); // lee el voltaje de salida que nuestra analoga pero en porcentaje
00172 
00173      //}
00174   //pid
00175     
00176     err = sp-x;         // Calcula el error 
00177    pwmset = kp* err+ki * eInteg +kd* (err - ePrev);    // ecuacion para el PID
00178    pwmset=(0.976801/5)*pwmset;            // como el voltaje calculado fue un porcentaje se realiza una conversion respectiva al valor de salida
00179     Vout=pwmset;
00180     eInteg =eInteg+ err;                  // integral 
00181     ePrev = err;
00182     lcd.locate(0,1);
00183     lcd.printf("error=%f",err);
00184     lcd.locate(1,0);
00185     lcd.printf("salida=%f",x);
00186     if (button4.falling())
00187     { bandera=1;
00188     lcd.cls();
00189     p=1;
00190      lcd.locate(8,0);
00191     lcd.printf("kp=%d",kp);
00192     lcd.locate(0,1);
00193     lcd.printf("Ki=%d", ki);
00194     lcd.locate(8,1);
00195     lcd.printf("Kd=%d", kd);
00196     lcd.locate(0,0);
00197     lcd.printf("Sp=%d", sp);
00198 
00199     }
00200 
00201      
00202    }
00203   bandera = 0;
00204       }
00205       
00206                             
00207                     
00208     }
00209 
00210