Tarea 2-Procesadores 2016-2. Aldair Navarro

Dependencies:   Pulse1 TextLCD mbed

Fork of irda_Sony by Gustavo Ramirez

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <Pulse1.h>
00003 #include "TextLCD.h"
00004 #include "stdio.h" 
00005 #include "math.h" 
00006 
00007 //===============================================================================CONTROL REMOTO SONY
00008 
00009 PulseInOut irda(PTD4);                             //puerto para sensor infrarrojo
00010 Serial pc(USBTX, USBRX);                           //Para Termite
00011 DigitalOut led(LED1); DigitalOut led2(LED2);       // Leds
00012 TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // LCD
00013 AnalogIn  y(PTB3);                                 //entrada analoga
00014 AnalogOut u(PTE30);                                //salida analoga OJO solo se le pueden drenar 1.5mA en circuitos use un Buffer 
00015                                                    //si se ignora se daña la FRDMKL25Z
00016 int header =0;                                   //tiempo de Bit de Inicio (4T)
00017 int head_H = 2900;                         //20% tiempo de Bit de Inicio
00018 int head_L= 1900;
00019 int T_alto=1200;                           //Tiempo maximo de un bit 1
00020 int T_bajo=600;                            //Tiempo maximo de un bit 0
00021 int num_bits = 12;                         //Número de bits
00022 int times[12];                       //almacenador de tiempos de bits
00023 
00024 int dato=0;                                      //Tiempo del Bit actual
00025 int bin[12];                                     //Cadena de Bits                 
00026 //int Serie[20];                                   //Serie 2^n
00027 int numero=0;                                    //Numero Decimal del botón
00028 int boton=0;                                     //boton que se undió
00029 
00030 int  Sp=0, Kp=0, Ki=0, Kd=0, pos=1 , i=0 , j=0 , k=0, valPar=0 ;     //Variables
00031 float pid,ai,ad,ap,med,err,err_v;
00032 //const int base[]={1,10,100};
00033 int  C1=0x0F; int C4=0x0C;                           // cursor y cursor bajo
00034 int binM[20];
00035 
00036 
00037 //=============================================================================== Prototipo de Funciones 
00038 void  PID  (int Sp,  int Kp,  int   Ki, int Kd);
00039 void  LCD1 (int col, int fil, int   val  )     ;
00040 void  LCD2 (int col, int fil, float val  )     ;
00041 
00042 
00043 //================================================================================ Main - Configuración de Parámetros
00044   int main(){
00045      lcd.locate(0,1);  lcd.printf("**PID Control**");
00046      wait(2); lcd.cls(); lcd.writeCommand(C1);
00047     
00048      lcd.locate(0,0);    lcd.printf("Sp=%d",Sp);
00049      lcd.locate(8,0);    lcd.printf("Kp=%d",Kp);
00050      lcd.locate(0,1);    lcd.printf("Ki=%d",Ki);
00051      lcd.locate(8,1);    lcd.printf("Kd=%d",Kd);
00052 
00053     while(1){
00054       ini1: fflush( stdin );
00055          header=0;
00056          led2=0;       led=1;
00057       header = irda.read_low_us();    //funcion para leer un pulso de caida o bajo en header
00058       if (header > head_L && header < head_H) goto seguir;//verificar que este en la tolerancia +-20%
00059       else goto ini1;
00060       
00061 seguir:
00062        //leo los datos de la trama y se meten a un arreglo
00063       wait_us(400);// ES EL TIEMPO DE HEADER QUE NO SE Lee O EL ALTO 
00064       led2=1;
00065       for(i=0;i<(num_bits-1);++i){ // POR OSCILOSCOPIO se determina que llegan (num_bits),datos
00066       dato = irda.read_low_us(); //leer un bit de datos que es pulso arriba en este control
00067       times[i]=dato;
00068       wait_us(400);        }
00069       
00070       wait(0.5); //espero un poquito luego de leer todo el arreglo y ponerlo en pantalla 
00071       
00072       pc.printf("heder= ,%d\n",header);
00073       
00074       for(i=0;i<(num_bits-1);++i){pc.printf("%d,",times[i]); }
00075       
00076       wait(0.1);         pc.printf("\n");
00077   
00078       for(i=0;i<(num_bits-1);++i){  
00079        
00080        if(times[i] > ((T_alto+T_bajo)/2)){ bin[i]=1;      }
00081        else{                             bin[i]=0;      }
00082        
00083             pc.printf("%d ",bin[i]);      }   
00084         
00085          pc.printf("\n");
00086       
00087       
00088      binM[0]=1;
00089      for(i=1;i<(num_bits-1);++i){   binM[i]=binM[i-1]*2; }
00090      
00091      numero=0;   
00092      for(i=0;i<(num_bits-1);++i){     numero=numero+(binM[i]*bin[i]);}
00093       
00094       
00095       pc.printf("numero=%d\n",numero);
00096      
00097      if (numero==128){ boton=1 ;} else if (numero==129){ boton=2 ;}                    //boton 1 a 0 normales
00098 else if (numero==130){ boton=3 ;} else if (numero==131){ boton=4 ;}                    //boton 11 subir,    chan+
00099 else if (numero==132){ boton=5 ;} else if (numero==133){ boton=6 ;}                    //boton 12 bajar,    chan-
00100 else if (numero==134){ boton=7 ;} else if (numero==135){ boton=8 ;}                    //boton 13 derecha,  vol+
00101 else if (numero==136){ boton=9 ;} else if (numero==137){ boton=0 ;}                    //boton 14 derecha,  vol+
00102 else if (numero==144){ boton=11;} else if (numero==145){ boton=12;}                    //boton 15 ir a PID, MUTE
00103 else if (numero==146){ boton=13;} else if (numero==147){ boton=14;}
00104 else if (numero==148){ boton=15;}    else               { boton=16;}
00105     
00106      //pc.printf("boton= %d\n",boton);
00107     wait(0.2);
00108  //=================================================================USAR FLECHAS    
00109  
00110  switch  (boton)  {                       //Toca bajar al Parámetro //Llegó chan-
00111    case 11:                           
00112           if (pos==1) {                  
00113               if (Sp!=999){ Sp++;  LCD1(3,0,Sp) ; pc.printf("Sp=%d",Sp);}}
00114           if (pos==2) {
00115               if (Kp!=999){ Kp++;  LCD1(11,0,Kp);pc.printf("Kp=%d",Kp);}}  
00116           if (pos==3) {
00117               if (Ki!=999){ Ki++;  LCD1(3,1,Ki)  ;pc.printf("Ki=%d",Ki);}}
00118           if (pos==4 ){
00119               if (Kd!=999){ Kd++;  LCD1(3,11,Kd);pc.printf("Kd=%d",Kd);}}
00120                                                         break;
00121    
00122    case 12:                              //Toca bajar al Parámetro //Llegó chan-
00123           
00124           if( pos==1 ){               
00125               if (Sp!=0) { Sp--;  LCD1(3,0,Sp) ; pc.printf("Sp=%d",Sp);}}
00126           if (pos==2) {
00127               if (Kp!=0) { Kp--;  LCD1(11,0,Kp); pc.printf("Kp=%d",Kp);}}   
00128           if (pos==3) {
00129               if (Ki!=0) { Ki--;  LCD1(3,1,Ki) ; pc.printf("Ki=%d",Ki);}}
00130           if (pos==4) {
00131               if (Kd!=0) { Kd--;  LCD1(3,11,Kd); pc.printf("Kd=%d",Kd);}}
00132                                                         break;
00133 
00134    case 13:                                   //Mover hacia derecha, llegó vol+
00135       j=0; valPar=0;     
00136                if(pos==4){  pos=1; LCD1(3,0,Sp)  ; pc.printf("Sp=%d",Sp);}
00137           else if(pos==1){  pos++; LCD1(11,0,Kp) ;pc.printf("Kp=%d",Kp); }     
00138           else if(pos==2){  pos++; LCD1(3,1,Ki)  ;pc.printf("Ki=%d",Ki); }  
00139           else if(pos==3){  pos++; LCD1(11,1,Kd) ; pc.printf("Kd=%d",Kd); }
00140                                                        break;
00141    
00142    case 14:                                   //Mover hacia derecha, llegó vol-
00143       j=0; valPar=0; 
00144                if(pos==1){  pos=4; LCD1(11,1,Kd) ; pc.printf("Sp=%d",Sp);}
00145           else if(pos==4){  pos--; LCD1(3,1, Ki) ; pc.printf("Kp=%d",Kp);}
00146           else if(pos==3){  pos--; LCD1(11,0,Kp) ; pc.printf("Ki=%d",Ki);}
00147           else if(pos==2){  pos--; LCD1(3,0, Sp) ; pc.printf("Kd=%d",Kd);}
00148                                                       break;
00149  
00150    case 15:                                  //ir al PID, llegó Mute          
00151           PID(Sp,Kp,Ki,Kd) ;                  break;
00152    default:  {};     }                        // cierra Switch
00153          
00154          
00155 //=======================================================================Botones
00156  if ((boton==0)||(boton==1)||(boton==2)||(boton==3)||(boton==4)||(boton==5)||(boton==6)||(boton==7)||(boton==8)||(boton==9)){
00157     if(j<3){  
00158        valPar = 10*valPar + boton; 
00159        j++;
00160     
00161       if(pos==1){ Sp=valPar; LCD1(3,0, Sp) ; }
00162  else if(pos==2){ Kp=valPar; LCD1(11,0,Kp) ; }
00163  else if(pos==3){ Ki=valPar; LCD1(3,1, Ki) ; }
00164  else if(pos==4){ Kd=valPar; LCD1(11,1, Kd) ; }   
00165       }}
00166     
00167     } // cierra while 
00168 }     // cierran Main               
00169 
00170   
00171 //=================================================================================CICLO PID INFINITO
00172 void  PID (int Sp, int Kp, int Ki, int Kd) {
00173                                             //======================================= Parches del control  
00174     lcd.writeCommand(C4);                                     //quita cursor bajo
00175     lcd.cls();   lcd.printf("   GUARDADOS!") ; wait(1);       //borra la pantalla
00176     lcd.cls();   lcd.printf(" INICIA EL PID"); wait(1);
00177     lcd.cls();
00178 
00179     lcd.printf("Er=%3.0f",err);    
00180     lcd.locate(8,0);   lcd.printf("Me=%3.0f",med); 
00181     lcd.locate(0,1);   lcd.printf("Sp=%3.0f",Sp);     
00182     lcd.locate(8,1);   lcd.printf("Co=%3.0f",pid);
00183     wait(1);
00184 
00185 while(1){      //=============================================================== CICLO PRINCIPAL CONTROLADOR PID
00186         med = y.read()*999  ;
00187         err = Sp-med        ;                         //se calcula el error
00188         ap  = Kp*err*0.01f  ;                        //se calcula la accion proporcinal
00189         ai  = (Ki*err*0.01f)+ai    ;                //calculo de la integral del error
00190         ad  = Kd*(err-err_v)*0.01f ;                //calculo de la accion derivativa
00191         pid = ap+ai+ad             ;                             
00192         
00193         if(pid<=0)   {pid=0  ;}                      // se verifica que pid sea positivo
00194         if(pid > 999){pid=999;}                      // se verifica que pid sea menor a 1000
00195      
00196         LCD2(3,0,err) ;           // pc.putc(sprintf (mm,"ER%3.0f",err));  wait_ms(30);   
00197         LCD2(11,0,med);           // pc.putc(sprintf(mm,"ME%3.0f",med)); wait_ms(30); 
00198         LCD2(3,1,Sp)  ; 
00199         LCD2(11,1,med);           // pc.putc(sprintf(mm,"ID%3.0f",pid));  wait_ms(30); 
00200             
00201         err_v = err;                          //  guarda error
00202         u.write(pid/999);                     //  se envia el valor pid (normalizado) a puerto analogico de salida (D/A)
00203      
00204   /*    pc.printf("%3.0f\t",ER);  pc.printf("%3.0f\t",ME);
00205         pc.printf("%3.0f\t",SP);  pc.printf("%3.0f\n",ID);*/
00206         wait_ms(300);                
00207     
00208     } //  cierra while
00209  }    // funtion
00210   
00211 //==============================================================================Funciones Mostrar en LCD
00212 void LCD1 (int col, int fil, int val ){  
00213      lcd.locate(col,fil);  lcd.printf(" ");                 
00214      lcd.locate(col,fil);  lcd.printf("%d", val);  }       
00215      
00216 void LCD2 (int col, int fil, float val ){
00217      lcd.locate(col,fil);  lcd.printf("    ");
00218      lcd.locate(col,fil);  lcd.printf("%3.0f", val); }