juan carlos cortes parra / Mbed 2 deprecated PWM_myopenlab_dd

Dependencies:   mbed TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "string"
00002 #include "mbed.h"
00003 #include "sstream"
00004 #include <string>
00005 #include "TextLCD.h"
00006 
00007 TextLCD lcd(PTE20,PTE21,PTE22,PTE23,PTE29,PTE30, TextLCD::LCD16x2);// Rs, E, d4, d5, d6, d7, RW=GND
00008 Serial pc(USBTX,USBRX);
00009 DigitalIn pulso(PTC5);
00010 DigitalOut right(PTC7);
00011 DigitalOut left(PTC0);
00012 DigitalIn start(PTC3),finish(PTC7);
00013 Timer Timer1;
00014 int rea,ns;
00015 string cadena;
00016 using namespace std;
00017 void velocidad(int a,float ad);
00018 int dist(char datos[5]);
00019 PwmOut l1(PTB3),l2(PTC2);
00020 int selec;
00021 float cambio;
00022 float rpm=0,auxrpm=0;
00023 int contador=0;
00024 int distancia=0,disa=0;
00025 bool antirebote=false;
00026 int recorrido=0;
00027 char cade;
00028 //using namespace boost;
00029 float h;
00030 void inicio(){
00031     while(1){
00032         if(start==0){
00033             break;
00034         }
00035         velocidad(2,0.5);
00036     }
00037     }
00038 void rampa (int sel){
00039     for(float i=1;i<=10;i++){
00040      velocidad(sel,i*10);
00041      wait(0.01);
00042     }
00043     }
00044 int main(){
00045     velocidad(0,0);
00046     inicio();
00047     velocidad(0,0);
00048     lcd.printf("esta en los extremos");
00049     int red=0;
00050     char buffer[5];
00051     while(1){
00052         if((pulso==0)&&(antirebote==false)){
00053             contador++;
00054             disa++;
00055             antirebote=true;
00056             auxrpm=Timer1.read_ms();
00057             Timer1.stop();
00058             Timer1.reset();
00059             rpm=10000/auxrpm;
00060            
00061             if(recorrido>0){
00062             Timer1.start();}
00063             }
00064         if((pulso==1)&&(antirebote==true)){
00065             antirebote=false;
00066             }
00067          
00068          if(contador==6){
00069             
00070             contador=0;     
00071                 distancia=distancia+1;
00072                
00073             //distancia=distancia+1;
00074             }
00075            
00076             if(red>0){
00077                 if(selec==1){
00078                     velocidad(selec,0.3);
00079                     pc.printf("uuno");
00080                     
00081                     
00082                     if(disa==2){velocidad(selec,0.4);}
00083                     if(disa==3){velocidad(selec,0.6);}
00084                     if(disa==4){velocidad(selec,0.8);}
00085                     if((disa<recorrido)&&(disa>4)){
00086                         
00087                         velocidad(selec,1);   
00088                     }  if( disa >= red ){
00089                         distancia=0;
00090                         disa=0;
00091                         red=0;
00092                         recorrido=0;
00093                         velocidad(0,0);
00094                     }
00095                         
00096                 }if (selec==2){
00097                     velocidad(selec,0.3);
00098                     pc.printf("dos");
00099                     
00100                     if(disa==2){velocidad(selec,0.4);}
00101                     if(disa==3){velocidad(selec,0.6);}
00102                     if(disa==4){velocidad(selec,0.8);}
00103                     if((disa<recorrido)&&(disa>4)){
00104                         
00105                         velocidad(selec,1);
00106                     
00107                     
00108                     }if( disa >= red ){
00109                         distancia=0;
00110                         selec=0;
00111                         disa=0;
00112                         red=0;
00113                         recorrido=0;
00114                         velocidad(0,0);
00115                     }
00116                 } 
00117             }
00118 
00119          if(pc.readable()){// captura string
00120          
00121           pc.gets(buffer,8);
00122           buffer[0]=' ';
00123           cade=buffer[1];
00124           if(red==0){
00125           red=dist(buffer);
00126           }
00127           if(red>0){
00128               recorrido=red*6;
00129               }
00130           //pc.printf("%s",buffer);
00131          // dist(buffer);
00132          //pc.printf("%s",cadena);
00133          pc.printf("%d",recorrido);
00134          }
00135         
00136         if(cade=='d'){
00137             selec=1; 
00138             
00139             } 
00140         if (cade=='i'){ 
00141             selec=2; 
00142             
00143             
00144             } 
00145         if(cade=='n'){selec=0;     
00146             }
00147             
00148             
00149             
00150             
00151              
00152                
00153             
00154           wait(0.01);
00155          
00156            // selec=1;
00157           
00158           
00159           //velocidad(selec,cambio);
00160    
00161            
00162        
00163         }
00164       
00165 }
00166 
00167 void velocidad(int a ,float ad){
00168     switch (a){
00169         case 0:
00170             lcd.cls();
00171             lcd.locate(0,0);     
00172             lcd.printf("%d mm",distancia);
00173             lcd.locate(0,1);
00174             lcd.printf(" V :%.2f rpm",rpm);
00175             l1=0;
00176             l2=0;
00177             
00178             break;
00179         case 1:
00180             
00181             if(rpm<10){
00182                 pc.printf("00%.1f ,%d ",rpm,distancia);
00183                 }
00184             if((rpm>10)&&(rpm<100)){
00185                 pc.printf("0%.1f ,%d ",rpm,distancia);
00186                 } 
00187             if(rpm>100){
00188                 pc.printf("0%.1f ,%d ",rpm,distancia);
00189                 }  
00190            
00191             lcd.cls();
00192             lcd.locate(0,0);     
00193             lcd.printf("%d mm",distancia);
00194             lcd.locate(0,1);
00195             lcd.printf(" V :%.2f rpm",rpm);
00196             l1.write(ad); wait(0.02);
00197             
00198             l2=0;
00199             break;
00200         case 2:
00201               if(rpm<10){
00202                 pc.printf("00%.1f ,%d ",rpm,distancia);
00203                 }
00204             if((rpm>10)&&(rpm<100)){
00205                 pc.printf("0%.1f ,%d ",rpm,distancia);
00206                 } 
00207             if(rpm>100){
00208                 pc.printf("0%.1f ,%d ",rpm,distancia);
00209                 }  
00210             lcd.cls();   
00211             lcd.locate(0,0);     
00212             lcd.printf("%d mm",distancia);
00213             lcd.locate(0,1);
00214             lcd.printf("%.2f rpm",rpm);
00215             wait(0.02);
00216             l2.write(ad);
00217             l1=0;
00218             break;
00219         
00220   }  
00221       
00222 }
00223 
00224 int dist(char datos[8]){
00225    int numero=0,contador=0;
00226    char c=' ';
00227     for(int i = 8;i >= 0;i --){
00228        c=datos[i];
00229        //pc.putc(c);
00230        if(c=='0'){contador=contador+1;  }
00231        if(c=='1'){
00232             contador=contador+1;
00233             if(contador==1){numero=numero+1;}
00234             if(contador==2){numero=numero+10;}
00235             if(contador==3){numero=numero+100;} }
00236         if(c=='2'){
00237             contador=contador+1;
00238             if(contador==1){numero=numero+2;}
00239             if(contador==2){numero=numero+20;}
00240             if(contador==3){numero=numero+200;} }
00241         if(c=='3'){
00242             contador=contador+1;
00243             if(contador==1){numero=numero+3;}
00244             if(contador==2){numero=numero+30;}
00245             if(contador==3){numero=numero+300;} }
00246         if(c=='4'){
00247             contador=contador+1;
00248             if(contador==1){numero=numero+4;}
00249             if(contador==2){numero=numero+40;}
00250             if(contador==3){numero=numero+400;} }
00251         if(c=='5'){
00252             contador=contador+1;
00253             if(contador==1){numero=numero+5;}
00254             if(contador==2){numero=numero+50;}
00255             if(contador==3){numero=numero+500;} }
00256         if(c=='6'){
00257             contador=contador+1;
00258             if(contador==1){numero=numero+6;}
00259             if(contador==2){numero=numero+60;}
00260             if(contador==3){numero=numero+600;} }
00261         if(c=='7'){
00262             contador=contador+1;
00263             if(contador==1){numero=numero+7;}
00264             if(contador==2){numero=numero+70;}
00265             if(contador==3){numero=numero+700;} }
00266         if(c=='8'){
00267             contador=contador+1;
00268             if(contador==1){numero=numero+8;}
00269             if(contador==2){numero=numero+80;}
00270             if(contador==3){numero=numero+800;} }
00271         if(c=='9'){
00272             contador=contador+1;
00273             if(contador==1){numero=numero+9;}
00274             if(contador==2){numero=numero+90;}
00275             if(contador==3){numero=numero+900;} }
00276        
00277         }
00278    
00279     
00280       
00281     
00282      
00283    return numero;
00284                
00285                
00286             
00287     
00288     }