Sumo Mao
12212448_10153776432406214_1190982159_n.jpg?oh=95d8bcc45c10c2ee4d7f7dc6f2d9fe9f&oe=5643E5C4

Este proyecto consiste en la construcción y programación de un robot sumo para competir en una lucha oficial de sumo.

MATERIALES :

  • motores DC
  • sensor SRF02
  • atmega16A
  • sensores CNY70
  • puente H
descripción sensor SRF02
S320122big.JPG
El SRF02 es un sensor de distancias por ultrasonidos de bajo costo, capaz de detectar objetos y medir distancia en un rango de 15cm a 6m; se puede conectar directamente a un microcontrolador por el bus I2C o el puerto serial.

las especificaciones tecnicas se pueden ver en :http://www.robot-electronics.co.uk/htm/srf02tech.htm

puente h

l293d.png?w=604&h=545
EL puente H lo utilizamos para separar la parte de potencia a el circuito del micro. ademas se utilizo para el cambio de giro de los motores DC.
por medio de PWM podemos variar la velocidad de los motores.

CODIGO:

La programación del sensor es realmente sencilla ya que este no necesisa calibracion para la distancia

{
    unsigned int range;                            // Holds range data from SRF08
    setup();                                    // General chip setup
    startTimer(0xFFFF);                            // Time to delay for LCD to power up
    waitForTimer();
    sei();
    DDRB= (1<<PORTB3);
    TCCR0 |= (1<<WGM00)|(1<<WGM01)|(1<<COM01)|(1<<CS01);
    TIMSK |= (1<<TOIE0);
    OCR0 =((ciclo1*255)/100);

    DDRD  |= (1<<PORTD7);
    TCCR2 |=(1<<WGM21)|(1<<WGM20)|(1<<COM21)|(1<<CS21);
    TIMSK |= (1<<TOIE2);
    TIFR  |= (1<<TOV2);
    OCR2 =((ciclo2*255)/100);

     DDRA = 0b11111111;
 while(1)
    {
        ciclo1 = 0;
        ciclo2 =0;

        startRanging();                            // Start SRF08 taling a ranging
        startTimer(0x500);                        // 0x2260 gives you a period of 70ms for the timer to finish
        waitForTimer();
        range = getRange();

        /*
         if (PINA==0b00000100)
         {
         if (range<=35 )
         {
         startRanging();                            // Start SRF08 taling a ranging
         startTimer(0x500);                        // Set timer running 1:64 prescaler, 0x2260 gives you a  period of 70ms for the timer to finish
         waitForTimer();
         range = getRange();
         DDRB = 0b11101101;// delante
         PORTB=(1<<PORTB0)|(0<<PORTB1)|(1<<PORTB2)|(0<<PORTB4);
         ciclo1 = 100;
         ciclo2 =0;
         _delay_ms(500);//revisar
         ciclo1 = 0;
         ciclo2 =100;
         _delay_ms(500);//revisar
         ciclo1 = 0;
         ciclo2 = 0;
         }
         }
         */// ejemplo

        while (PINA==0b00000000)
        {

            startRanging();
            startTimer(0x500);
            waitForTimer();
            range = getRange();

          // if ((range>55) && (range<65))// GENERAL
            {

//                startRanging();
//                startTimer(0x500);
//                waitForTimer();
//                range = getRange();
//                _delay_ms(500);
                if ((range>35) && (range<50) )// SE ACERCO
                { // hacer una s

                    DDRB = 0b11111001;//revesra rara
                    PORTB=(1<<PORTB0)|(0<<PORTB1)|(0<<PORTB2)|(1<<PORTB4);
                    ciclo1 = 100;
                    ciclo2 = 100;
                    _delay_ms(900);
                    ciclo1 = 0;
                    ciclo2 = 0;
                    _delay_ms(900);
//                   
//                    DDRB = 0b11111010;
//                    PORTB=(0<<PORTB0)|(1<<PORTB1)|(0<<PORTB2)|(1<<PORTB4);
//                    
//                    ciclo1 = 100;
//                    ciclo2 = 100;
//                    _delay_ms(900);
//                    ciclo1 = 20;
//                    ciclo2 = 80;
//                    _delay_ms(900);
//                    ciclo1 = 0;
//                    ciclo2 = 0;
//                    _delay_ms(700);

                  /*
                    ciclo1 = 50;
                    ciclo2 =50;
                    _delay_ms(900);//revisar//100

                    DDRB = 0b11111001;//revesra rara
                    PORTB=(1<<PORTB0)|(0<<PORTB1)|(0<<PORTB2)|(1<<PORTB4);
                    ciclo1 = 20;
                    ciclo2 = 20;
                    _delay_ms(700);
                    DDRB = 0b11101101;// delante
                    PORTB=(1<<PORTB0)|(0<<PORTB1)|(1<<PORTB2)|(0<<PORTB4);
                    ciclo1 = 50;
                    ciclo2 = 50;
                    _delay_ms(700);
                    ciclo1 = 100;
                    ciclo2 =0;
                    _delay_ms(600);

                */
                }
               if ((range>50) && (range<100))// ESTA IGUAL
                {

                    DDRB = 0b11111010;
                    PORTB=(0<<PORTB0)|(1<<PORTB1)|(0<<PORTB2)|(1<<PORTB4);
                    ciclo1 = 100;
                    ciclo2 = 100;
                    _delay_ms(900);
                    ciclo1 = 0;
                    ciclo2 = 0;
                    _delay_ms(900);

                                      // atacarlo sin asco
//                    DDRB = 0b11101101;// delante
//                    PORTB=(1<<PORTB0)|(0<<PORTB1)|(1<<PORTB2)|(0<<PORTB4);
//                    ciclo1 =100;// 100
//                    ciclo2 =100;
//                    _delay_ms(600);//revisar 100

                }
//                if ((range>65) && (range<80))// SE ALEJO
//                {
//                    //
//                    DDRB = 0b11100101;// delante
//                    PORTB=(1<<PORTB0)|(0<<PORTB1)|(1<<PORTB2)|(0<<PORTB4);
//                    ciclo1 =80;
//                    ciclo2 =100;
//                    _delay_ms(2000);//revisar300
//                    ciclo1 =50;
//                    ciclo2 =100;
//                    _delay_ms(2000);//300
//                    ciclo1 =0;
//                    ciclo2 =0;
//                }
//                if (range<=30)
//                {
//               
//                    DDRB = 0b11111010;
//                    PORTB=(0<<PORTB0)|(1<<PORTB1)|(0<<PORTB2)|(1<<PORTB4);
//                    ciclo1 = 100;
//                    ciclo2 = 0;
//                    _delay_ms(700);
//                    ciclo1 = 0;
//                    ciclo2 = 100;
//                    _delay_ms(700);
//                    ciclo1 = 40;
//                    ciclo2 = 0;
//                    _delay_ms(700);
//                    ciclo1 = 0;
//                    ciclo2 = 0;
//                    _delay_ms(700);
//                    

                }
              //  else
//                {// busca al otro
//                
//                    DDRB = 0b11100101;// delante
//                    PORTB=(1<<PORTB0)|(0<<PORTB1)|(1<<PORTB2)|(0<<PORTB4);
//                    ciclo1 =20;
//                    ciclo2 =40;
//                    _delay_ms(500);//revisar
//                    ciclo1 = 10;
//                    ciclo2 =100;
//                    _delay_ms(200);
//                    ciclo1 =0;
//                    ciclo2 =0;
//                }
            }

//           else
//            {
//                DDRB = 0b11100101;// delante
//                PORTB=(1<<PORTB0)|(0<<PORTB1)|(1<<PORTB2)|(0<<PORTB4);
//                ciclo1 =20;
//                ciclo2 =100;
//                _delay_ms(300);//revisar
//                ciclo1 = 0;
//                ciclo2 =100;
//                _delay_ms(100);
//                ciclo1 =0;
//                ciclo2 =0;
//            }
        }
       // SEGUIDOR();
    }

void SEGUIDOR(void)
{
    DDRA = 0b11111111;

        if (PINA==0b00000001)// reversa0b

        {
            DDRB = 0b11111010;
            PORTB=(0<<PORTB0)|(1<<PORTB1)|(0<<PORTB2)|(1<<PORTB4);
            ciclo1 = 00;
            ciclo2 =100;//80
            _delay_ms(600);
            ciclo1 = 0;
            ciclo2 =0;
            _delay_ms(600);

        }
        if (PINA==0b00000010)
        {
            DDRB = 0b11111010;
            PORTB=(0<<PORTB0)|(1<<PORTB1)|(0<<PORTB2)|(1<<PORTB4);
            ciclo1 = 0;//80
            ciclo2 =100;
            _delay_ms(600);
            ciclo1 = 0;
            ciclo2 =0;
            _delay_ms(600);

        }
        if (PINA==0b00000011)//uno reversa y el otro no
        {
            DDRB = 0b11111001;//revesra rara
            PORTB=(1<<PORTB0)|(0<<PORTB1)|(0<<PORTB2)|(1<<PORTB4);
            ciclo1 = 100;
            ciclo2 = 100;
            _delay_ms(400);
            DDRB = 0b11101101;// delante
            PORTB=(1<<PORTB0)|(0<<PORTB1)|(1<<PORTB2)|(0<<PORTB4);
            ciclo1 = 70;
            ciclo2 = 100;
            _delay_ms(400);
            ciclo1 = 0;
            ciclo2 =0;
            _delay_ms(600);

        }
        if (PINA==0b00001100)
        {
            DDRB = 0b11111001;// nota probar en eque direccion gira
            PORTB=(1<<PORTB0)|(0<<PORTB1)|(0<<PORTB2)|(1<<PORTB4);
            ciclo1 = 100;
            ciclo2 = 100;
            _delay_ms(400);
            DDRB = 0b11101101;// delante
            PORTB=(1<<PORTB0)|(0<<PORTB1)|(1<<PORTB2)|(0<<PORTB4);
            ciclo1 = 70;// revisar
            ciclo2 = 100;
            _delay_ms(400);
            ciclo1 = 0;
            ciclo2 =0;
            _delay_ms(600);

        }
        if (PINA==0b00000100)
        {
            DDRB = 0b11101101;// delante
            PORTB=(1<<PORTB0)|(0<<PORTB1)|(1<<PORTB2)|(0<<PORTB4);
            ciclo1 = 100;
            ciclo2 =80;
            _delay_ms(400);
            ciclo1 = 0;
            ciclo2 =0;
            _delay_ms(600);

        }
        if (PINA==0b00001000)
        {
            DDRB = 0b11101101;// delante
            PORTB=(1<<PORTB0)|(0<<PORTB1)|(1<<PORTB2)|(0<<PORTB4);
            ciclo1 = 80;
            ciclo2 =100;
            _delay_ms(400);
            ciclo1 = 0;
            ciclo2 = 0;
        }
}

ISR(TIMER0_OVF_vect)
{
    OCR0 =((ciclo1*255)/100);
}
ISR(TIMER2_OVF_vect)
{
    OCR2 =((ciclo2*255)/100);

}
void startRanging(void)
{
    i2c_transmit(SRF08, 0, 81);                    // Cause SRF08 to start a ranging returning results in CM
}

unsigned int getRange(void)
{
    unsigned int x;
    x = i2cRead(SRF08,2) << 8;                    // Get high and then low bytes of the range
    x += i2cRead(SRF08,3);

    return(x);
}

unsigned char getLight(void)
{
    char x;
    x = i2cRead(SRF08, 1);                        // Read the light data
    return(x);
}

void setup(void)
{
    TCCR1A = 0x00;            // Set timer up in CTC mode
    TCCR1B = 0x08;
    TWBR = 0x20;            // 100MHz I2C clock frequency
}

void startTimer(unsigned int time)
{
    TCNT1 = 0x00;             // Clear timer to zero
    OCR1A = time;            // Time set to count to
    TIFR = 0x10;            // Clear timer campare match flag
    TCCR1B = START_CLK;        // Start timer running 1:64 prescaler
}
void waitForTimer(void)
{
    while(!(TIFR&0x10));    // wait for timer to set compare match flag
    TCCR1B = STOP_CLK;        // Stop the timer
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License