Jose Ruiz / Mbed 2 deprecated Encoder

Dependencies:   QEI TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Encoder.cpp Source File

Encoder.cpp

00001 // JOSE RUIZ // SEBASTIAN PELAEZ 
00002 // TAREA 3 PROCESADORES
00003 // ENCODER
00004 
00005 #include "mbed.h"
00006 #include "QEI.h"
00007 #include "TextLCD.h"
00008 
00009 
00010 TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7
00011 QEI encoder (PTD6, PTD7, PTA17, 624);// (Pines al encoder de derecha a izquierda 3,1,4,2(gnd))
00012 
00013 // PARA SONIDO
00014 AnalogOut Vout(PTE30);
00015 DigitalIn button3(PTA17);
00016 int i;
00017 
00018 
00019 int C2=0x18; 
00020 int C3=0x1A; 
00021 int C4=0x0C; 
00022 int C1=0x0F;
00023 int err, med, yr, pid, ap, ai, ad, err_v, cambio=0, diferencia=0;
00024 int spnum=0,kinum=0,kpnum=0,kdnum=0,pos=1;
00025 int flagt=0;
00026 Timer t;
00027 
00028 int main()
00029 {
00030     lcd.cls(); 
00031     lcd.writeCommand(C1);
00032 
00033     lcd.locate(8,0);
00034     lcd.printf("Kp=%d",kpnum);
00035     lcd.locate(0,1);
00036     lcd.printf("Ki=%d",kinum);
00037     lcd.locate(8,1);
00038     lcd.printf("Kd=%d",kdnum);
00039     lcd.locate(0,0);
00040     lcd.printf("Sp=%d",spnum);
00041 
00042     while(1)
00043     {
00044         diferencia=encoder.getPulses()-cambio;
00045         cambio=encoder.getPulses();
00046 
00047         if (diferencia==0)
00048         {
00049             }
00050         else if(diferencia>0)
00051         {
00052             if(pos==1)
00053             {
00054                 if(spnum+diferencia>=999)
00055                 {
00056                     spnum=999;
00057                     lcd.locate(3,0);
00058                     lcd.printf("    ");
00059                     lcd.locate(3,0);
00060                     lcd.printf("%d", spnum);
00061                 }
00062                 else
00063                 {
00064                     spnum+=diferencia;
00065                     lcd.locate(3,0);
00066                     lcd.printf("%d", spnum);
00067                 }
00068             }
00069             else if(pos==2)
00070             {
00071                 if(kpnum+diferencia>=999)
00072                 {
00073                     kpnum=999;
00074                     lcd.locate(11,0);
00075                     lcd.printf("    ");
00076                     lcd.locate(11,0);
00077                     lcd.printf("%d", kpnum);
00078                 }
00079                 else
00080                 {
00081                     kpnum+=diferencia;
00082                     lcd.locate(11,0);
00083                     lcd.printf("%d", kpnum);
00084                 }
00085             }
00086             else if(pos==3)
00087             {
00088                 if(kinum+diferencia>=999)
00089                 {
00090                     kinum=999;
00091                     lcd.locate(3,1);
00092                     lcd.printf("    ");
00093                     lcd.locate(3,1);
00094                     lcd.printf("%d", kinum);
00095                 }
00096                 else
00097                 {
00098                     kinum+=diferencia;
00099                     lcd.locate(3,1);
00100                     lcd.printf("%d", kinum);
00101                 }
00102             }
00103             else if(pos==4)
00104             {
00105                 if(kdnum+diferencia>=999)
00106                 {
00107                     kdnum=999;
00108                     lcd.locate(11,1);
00109                     lcd.printf("    ");
00110                     lcd.locate(11,1);
00111                     lcd.printf("%d", kdnum);
00112                 }
00113                 else
00114                 {
00115                     kdnum+=diferencia;
00116                     lcd.locate(11,1);
00117                     lcd.printf("%d", kdnum);
00118                 }
00119             }
00120         }
00121         
00122         else if(diferencia<0)
00123         {
00124             if(pos==1)
00125             {
00126                 if(spnum+diferencia<0)
00127                 {
00128                 }
00129                 else
00130                 {
00131                     spnum+=diferencia;
00132                     lcd.locate(3,0);
00133                     lcd.printf("    ");
00134                     lcd.locate(3,0);
00135                     lcd.printf("%d", spnum);
00136                 }
00137             }
00138             else if(pos==2)
00139             {
00140                 if(kpnum+diferencia<0)
00141                 {
00142                 }
00143                 else
00144                 {
00145                     kpnum+=diferencia;
00146                     lcd.locate(11,0);
00147                     lcd.printf("    ");
00148                     lcd.locate(11,0);
00149                     lcd.printf("%d", kpnum);
00150                 }
00151             }
00152             else if(pos==3)
00153             {
00154                 if(kinum+diferencia<0)
00155                 {
00156                 }
00157                 else
00158                 {
00159                     kinum+=diferencia;
00160                     lcd.locate(3,1);
00161                     lcd.printf("    ");
00162                     lcd.locate(3,1);
00163                     lcd.printf("%d", kinum);
00164                 }
00165             }
00166             else if(pos==4)
00167             {
00168                 if(kdnum+diferencia<0)
00169                 {
00170                 }
00171                 else
00172                 {
00173                     kdnum+=diferencia;
00174                     lcd.locate(11,1);
00175                     lcd.printf("    ");
00176                     lcd.locate(11,1);
00177                     lcd.printf("%d", kdnum);
00178                 }
00179             }
00180         }
00181 
00182         if (!button3)
00183         { 
00184             {
00185                    Vout=2.5;
00186                     wait(0.1);
00187                     Vout=0;
00188             }
00189             if(pos==4)
00190             {
00191                 pos=1;
00192                 lcd.locate(3,0);
00193                 lcd.printf("%d", spnum);
00194             }
00195             else if (pos==1)
00196             {
00197                 pos++;
00198                 lcd.locate(11,0);
00199                 lcd.printf("%d", kpnum);
00200             }
00201             else if(pos==2)
00202             {
00203                 pos++;
00204                 lcd.locate(3,1);
00205                 lcd.printf("%d", kinum);
00206             }
00207             else if(pos==3)
00208             {
00209                 pos++;
00210                 lcd.locate(11,1);
00211                 lcd.printf("%d", kdnum);
00212             }
00213             wait(0.25);
00214 
00215         }
00216    
00217     }
00218   }