Primer_corte_avance

Dependencies:   mbed

Fork of 01-04EntregaPrimerCorte by ferney alberto beltran molina

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #include "main.h"
00004 
00005 #define MEM_SIZE 5
00006 #define MEM_TYPE char
00007 MEM_TYPE buffer[MEM_SIZE];
00008 
00009 
00010 Serial command(USBTX, USBRX);
00011 DigitalIn button(USER_BUTTON);
00012 
00013 
00014 int main() {
00015     init_servo();
00016     init_serial();
00017     debug_m("inicio \n");
00018     
00019     
00020     uint32_t read_cc;
00021     while(1)
00022     {
00023         read_cc=read_command();
00024         debug_m("Entrando TC...\n");
00025 
00026         switch (read_cc) {
00027 
00028             case  0x01: 
00029             debug_m("Tc 1 Iniciado..\n");
00030             moving();
00031             debug_m("Tc 1 Finalizado\n");
00032             break;
00033             case  0x02: 
00034             debug_m("Tc 2 Iniciado.. \n");
00035             pares();
00036             debug_m("Tc 2 Finalizado\n");
00037             break;
00038             default: debug_m("Error de comando. \nSe espera  entre 0x01 - 0x08 \n");break ;      
00039         }
00040     }
00041 }
00042 
00043 
00044 
00045 uint32_t read_command()
00046 {
00047    // retorna los byte recibidos concatenados en un entero, 
00048    // Formato | FF | TC | NM | GR | FD
00049    // Datos  | Inicio | Tele-comando | Numero Motor | Grados | Cierre
00050     //char intc=command.getc();
00051     buffer[0] = command.getc();
00052 
00053  inicio:   
00054   
00055     while(buffer[0] != 0xFF ){
00056     
00057         buffer[0] = command.getc();
00058         
00059         debug_m("comando inicio invalido \n");
00060         }
00061         
00062         printf("inicio encontrado: %c \n",  buffer[0] ); 
00063         
00064             buffer[1] = command.getc();
00065         if (buffer[1] != 0xFF && buffer[1] != 0xFD){     
00066             buffer[2] = command.getc();
00067             }else{
00068                 buffer[0] = buffer[1];
00069                 goto inicio; 
00070             }
00071         if (buffer[2] != 0xFF && buffer[2] != 0xFD){     
00072             buffer[3] = command.getc();
00073             }else{
00074                 buffer[0] = buffer[2];
00075                 goto inicio; 
00076             }
00077         if (buffer[3] != 0xFF && buffer[3] != 0xFD){     
00078             buffer[4] = command.getc();
00079             }else{
00080                 buffer[0] = buffer[3];
00081                 goto inicio; 
00082             }
00083      //para que evalue el cierre 
00084         if (buffer[4] != 0xFF && buffer[4] == 0xFD){     
00085             debug_m("comando Cierre valido \n");
00086             }else{
00087                 buffer[0] = buffer[4];
00088                 goto inicio; 
00089             }
00090 
00091           
00092           
00093         //intc=command.getc();
00094         int tele = buffer[1];
00095         printf("Tele siguente: %c \n",  tele ); 
00096 
00097 
00098     return tele;
00099  
00100 }
00101 
00102 
00103 void init_serial()
00104 {
00105     command.baud(9600);
00106     debug_m("Serial 9600 \n");    
00107 }
00108 
00109 
00110 void moving(){
00111     debug_m("se inicia el comado mover..\n");    
00112     
00113     char nmotor = buffer[2];
00114     printf("Motor: %c \n",  nmotor ); 
00115     char grados = buffer[3];
00116     printf("Grados: %c \n",  grados ); 
00117     //char endc = buffer[4];
00118     //printf("Cierre: %c \n",  endc ); 
00119     mover_ser(nmotor,grados); 
00120     
00121     debug_m("fin del comado mover..\n");    
00122     
00123 }
00124 void pares(){
00125     debug_m("se inicia el comado pares..\n");    
00126     
00127     char par = buffer[2];
00128     printf("Par: %c \n",  par ); 
00129     char dire = buffer[3];
00130     printf("Dir: %c \n",  dire ); 
00131     char endcc = buffer[4];
00132     printf("Cierre: %c \n",  endcc ); 
00133     while(button == 0) {
00134     mover_par(par,dire); 
00135     }
00136     centro(par);
00137     debug_m("fin del comado pares..\n"); 
00138     }
00139 void debug_m(char *s , ... ){
00140     #if DEBUG
00141     command.printf(s);
00142     #endif  
00143 }