Wiki tecnica, wiki usuario

Dependencies:   mbed

main.cpp

Committer:
Diani
Date:
2019-05-30
Revision:
0:bf193a68e357

File content as of revision 0:bf193a68e357:

#include "mbed.h"
#include "scolor_TCS3200.h"

Serial command(USBTX, USBRX);

PwmOut mybuzzer(D9);
DigitalOut stepper_step(PB_8);
DigitalOut steppeer_dir(PB_9);
DigitalOut stepper_step_2(PA_7);
DigitalOut steppeer_dir_2(PA_6);

scolor_TCS3200 scolor(PA_8, PB_10, PB_4, PB_5, PB_3);

#define INITCMD 0xFF
#define VELOCITY 5000
#define SENSOR 0x00
#define TONO_1 0x01
#define TONO_2 0x02
#define TONO_3 0x03
#define TONO_4 0x04
#define UP 0x05
#define DOWN 0x06
#define LEFT 0x07
#define RIGTH 0x08
#define VEL 0x09
#define DO  100
#define RE  150 
#define MI  200
#define FA  250

// definición de las variables globales 

uint8_t tip_mov;
uint8_t n_vueltas;
uint8_t STEEP;
float velocidad=500;
uint8_t tiempo;

// definición de las funciones

void setup_uart();
void mover_steper_nema();
void leer_datos();
void funcion_pasos();
void funcion_pasos_2();
void buzzer_on();
void leer_color();

int main() {

    setup_uart();

    while(1){    

        leer_datos();
     mover_steper_nema();       

    }    

}

void setup_uart(){

    command.baud(115200);

}

void leer_datos(){

    while(command.getc()!= INITCMD);

         tip_mov=command.getc();
      n_vueltas=command.getc();   

      

}

void mover_steper_nema(){

   switch (tip_mov){

       
        case SENSOR:     

                    if (n_vueltas == 0) 
            {leer_color();}           
                    printf("LEER COLOR\n");

               break;

        case TONO_1:  

                    mybuzzer.write(0);   
                    mybuzzer.period_ms(DO);
                    buzzer_on();       
                    printf("TONO 1\n");

               break;  

        case TONO_2: 

                    mybuzzer.write(0);    
                    mybuzzer.period_ms(RE);
                    buzzer_on();       
                    printf("TONO 2\n");

               break;  

        case TONO_3:

                    mybuzzer.write(0);     
                    mybuzzer.period_ms(MI);
                    buzzer_on();       
                    printf("TONO 3\n");

               break;  

        case TONO_4:

                    mybuzzer.write(0);     
                    mybuzzer.period_ms(FA);
                    buzzer_on();       
                    printf("TONO 4\n");

               break;  

               

        case UP:     

                    steppeer_dir=1;
                    steppeer_dir_2=0;
                    STEEP=200;
                    funcion_pasos();           
                    printf("MOVER ADELANTE\n");

               break;   

        case DOWN:

                steppeer_dir=0;
                steppeer_dir_2=1; 
                STEEP=200; 
                funcion_pasos();
                printf("MOVER ATRAS\n");

                   break;

        case LEFT: 

                steppeer_dir=1;
                steppeer_dir_2=1;
                STEEP=50;  
                funcion_pasos_2();
                printf("MOVER IZQUIERDA\n"); 

                   break;

        case RIGTH: 

                steppeer_dir=0;
                steppeer_dir_2=0;
                STEEP=50;  
                funcion_pasos_2();
                printf("MOVER DERECHA\n");

                    break;

        case VEL: 

                if (n_vueltas==0)
                {velocidad = 250;}
                else if (n_vueltas==1)
                {velocidad = 500;}
                else if (n_vueltas==2)
                {velocidad = 750;}
                printf("%f \n", velocidad);

                    break;

             }

}

void funcion_pasos()
{   wait_us(velocidad);
    for (int a=0; a<n_vueltas; a++){

    for(int i=0; i<STEEP; i++)

     {

        stepper_step=1;
        stepper_step_2=1;
        wait_us(velocidad);
        stepper_step=0;
        stepper_step_2=0;
        wait_us(velocidad);

        
//    printf("i");

    }

    }

    }

void funcion_pasos_2()

{   wait_us(1);
    for(int i=0; i<STEEP; i++)

     {

        stepper_step=1;
        stepper_step_2=1;
        wait_ms(velocidad);
        stepper_step=0;
        stepper_step_2=0;
        wait_ms(velocidad);

    printf("i");

    }

    }

void leer_color(){

    long     red = scolor.ReadRed();
    long     green = scolor.ReadGreen();
    long     blue = scolor.ReadBlue();
    long     clear = scolor.ReadClear();

    if (red <=800) // lee el rojo------------

        {

            if (green>=1200 and green <=1500)

            {

                if (blue>=1200 and blue <=1500)

                {

            

            printf("|FE 01|\n");

                                         

                            }

            }

        }

        

         if (red <=2700)  // lee el verde --------------------------------------

        {

            if (green>=900 and green <=1500)

            {

                if (blue>=1400 and blue <=2000)

                {

            

            printf("|FE 03|\n"); 

                                      

                }

            }

        }

        

         if (red<=2400) // lee el azul------------------------------------

        {

            if (green>=800 and green <=1000)

            {

                if (blue>=600 and blue <=800)

                {

                        

            printf("|FE 02|\n");

                                   

                }

            }

        }

       if (red <=700) // lee el amarillo------------------------------------

        {

            if (green>=500 and green <=700)

            {

                if (blue>=500 and blue <=1500)

                {

                        

            printf("|FE 04|\n");

            

                            }

            }

        }

        

        if (red <=200) // lectura errada------------------------------------

        {

            if (green <=300)

            {

                if (blue <=300)

                {

                        

            printf("|FE 00|\n");

                                 

                }

                

            }

        }

     

  //   printf("RED: %5d     GREEN: %5d     BLUE: %5d     CLEAR: %5d     \n ", red, green, blue, clear);

     }



void buzzer_on()

          

{           tiempo = n_vueltas;

            mybuzzer.write(0.5);

            wait(tiempo);

            mybuzzer.write(0);

            

            }