Santiago Alvarez / Mbed 2 deprecated pid_2

Dependencies:   Debounced 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 AnalogIn Vin(PTC2);
00005 TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7  //documentacion de las librerias y disposicion de pines
00006 
00007 DigitalOut led1(LED1);
00008 DigitalOut led2(LED2);
00009 DigitalOut led3(LED3);
00010 DebouncedIn b1(PTC12);  //boton incrementa.
00011 DebouncedIn b2(PTC13);  //boton decrementa.
00012 DebouncedIn b3(PTB8);   //boton cambio menu.    
00013 DebouncedIn b4(PTB9);   //boton reset.
00014      
00015 
00016 int C1=0x0E; // solo muestra el cursor
00017 int C2=0x18; // desplaza el cursor a la  izquierda
00018 int C3=0x1A; // desplaza el cursor derecha
00019 int C4=0x0C; // quita el  cursor bajo
00020 int a; 
00021 int b;
00022 int kp, ki, kd, sp, err, med, pid, ap, ai, ad, err_v; //revisar antes de la primera tarear(PID)
00023 
00024 int main() {
00025     lcd.cls();
00026     lcd.printf("Sp%d",sp);      //ubicacion de las variables en la pantalla
00027     lcd.locate(8,0);
00028     lcd.printf("Kp%d",kp);
00029     lcd.locate(0,1);
00030     lcd.printf("Ki%d",ki);
00031     lcd.locate(8,1);
00032     lcd.printf("Kd%d",kd);
00033     lcd.writeCommand(C1);
00034     lcd.locate(0,0);
00035     lcd.printf("Sp%d",sp);
00036   
00037     while(1) {              //inicio cliclos del pid
00038         if (b1.falling()) { // incrementa
00039            led1 =!led1;
00040            ++a;
00041            lcd.locate(6,0);
00042            lcd.printf("%d", a);
00043         }
00044     if (b2.falling()) { //decrementa
00045            led2 =!led2;
00046            --a;
00047            lcd.locate(6,0);
00048            lcd.printf("%d", a);
00049             }
00050      if (b3.falling()) {      
00051                                             //botones
00052          if (b==0){
00053          lcd.locate(2,0);
00054          lcd.printf("%d",sp);
00055              if (b1.falling()) {                            
00056              ++sp;                
00057              }
00058              if (b2.falling()) {                            
00059              --sp;                
00060              } 
00061        }
00062         if (b==1) {                 
00063            lcd.locate(10,0);
00064            lcd.printf("%d",kp);
00065             if (b1.falling()){
00066             ++kp;
00067              }
00068              if (b2.falling()) {                            
00069              --kp;
00070               }
00071           }
00072           if (b==2) {
00073             lcd.locate(2,1);
00074              lcd.printf("%d",ki);
00075                   if (b1.falling()){
00076                   ++ki;
00077                   }
00078                   if (b2.falling()){                            
00079                   --ki;
00080                   }               
00081               }
00082            if (b==3) {
00083                lcd.locate(10,1);
00084                lcd.printf("%d",kd);
00085                   if (b1.falling()){
00086                   ++kd;
00087                   }
00088                   if (b2.falling()){                            
00089                   --kd
00090                   else(b==4) {
00091                      b=0;
00092                }                          
00093             }
00094            if (b4.falling()){
00095            break;                       
00096        }       }
00097   }                      
00098         
00099 
00100            lcd.writeCommand(C4);
00101            lcd.cls();      
00102            lcd.printf("    Guardando Datos!"); 
00103            wait(2);
00104            lcd.printf("     Espere....");
00105            wait(2);
00106            lcd.cls();
00107            lcd.printf("Iniciando PID");
00108            wait(2);
00109           
00110            lcd.cls();
00111            lcd.printf("Er%d",err);
00112            lcd.locate(8,0);
00113            lcd.printf("Me%d",med);
00114            lcd.locate(0,1);
00115            lcd.printf("Sp%d",sp);
00116            lcd.locate(8,1);
00117            lcd.printf("Co%d",pid);
00118            wait(5);
00119            
00120            void star_patch(void){  //funcion que imprime los caracteres que no van a variar en el display
00121             lcd.printf("SP=");
00122             lcd.locate(7,0);
00123             lcd.printf("Kp=");
00124             lcd.locate(0,1);
00125             lcd.printf("Ki=");
00126             lcd.locate(7,1);
00127             lcd.printf("Kd=");
00128             lcd.locate(3,0);
00129             }
00130 
00131         int main(){
00132         star_patch();
00133          while(1){
00134             if(k==3)k=0;
00135             if(salto.read()==0){    
00136              def_posicion(k);
00137               k++;
00138                  }
00139              else if(mas.read()==0){
00140              incremento(k); 
00141              }
00142              else if(menos.read()==0){
00143               decremento(k);
00144              }
00145         }
00146       }   
00147            
00148           //calculo del pid
00149            
00150            while(1) {
00151                               
00152            err = (sp-med);
00153            ap = kp*err;
00154            ai =(ki*err)+ai;    
00155            
00156            ad = kd*(err-err_v); 
00157            pid = (ap+ai+ad);
00158           
00159            if (pid > 999){
00160            pid=1000;
00161            } 
00162            
00163            wait(.5);
00164            lcd.locate(2,0);
00165            lcd.printf("%d",err);
00166            lcd.locate(10,0);
00167            lcd.printf("%d",med);
00168            lcd.locate(2,1);
00169            lcd.printf("%d",sp);
00170            lcd.locate(10,1);
00171            lcd.printf("%d",pid);
00172            err_v = err;           
00173           
00174            }
00175 }