Roambot

From Randomdata wiki
Jump to navigation Jump to search

Roambot

Is the bot that won the HiTB2011AMS hackerspaces challenge. http://www.randomdata.nl/blogs/node/42


Here's the code!


#define DROP_DISTANCE 25

task main()
{
    SetSensorLight(IN_1);
    SetSensorLight(IN_2);
    SetSensorType(IN_1,IN_TYPE_LIGHT_INACTIVE);
    SetSensorType(IN_2,IN_TYPE_LIGHT_INACTIVE);
    SetSensorMode(IN_1,SENSOR_MODE_PERCENT);
    SetSensorMode(IN_2,SENSOR_MODE_PERCENT);
    
    SetSensorLowspeed(IN_3);
    SetSensorTouch(IN_4);
    
    int lightambient = 100;
    int lighttreshold = 75;
    for (int i = 0; i < 3; i++)
    {
        PlayTone(440, 100);
        Wait(1000);
    }
    
    int bouncecounter = 0;
    int lightsource = 0;
    
    OnFwd(OUT_A, 40);
    OnRev(OUT_B, 40);
    for (int i = 0; i < 20; i++)
    {
        if (Sensor(IN_1) < lightambient) lightambient = Sensor(IN_1);
        if (Sensor(IN_2) < lightambient) lightambient = Sensor(IN_2);
        if (Sensor(IN_1) > lightsource) lightsource = Sensor(IN_1);
        if (Sensor(IN_2) > lightsource) lightsource = Sensor(IN_2);
        PlayTone(440, 20);
        Wait(100);
    }
    Off(OUT_AB);
    lighttreshold = 5 + (lightambient + lightsource) / 2;
    
    PlayTone(784, 125);
    Wait(150);
    PlayTone(660, 125);
    Wait(150);
    PlayTone(660, 275);
    Wait(300);
    PlayTone(588, 125);
    Wait(150);
    PlayTone(660, 125);
    Wait(150);
    PlayTone(784, 125);
   
    OnFwd(OUT_C, 50);
   
    while(1) 
    {
        ClearScreen();
        NumOut(1, 20, SensorUS(IN_3));
        //NumOut(1, 30, Sensor(IN_4));        
        NumOut(1, 40, lighttreshold);
        NumOut(1, 50, lightambient);

        if (Sensor(IN_4) || SensorUS(IN_3) > DROP_DISTANCE)
        {
            OnFwd(OUT_AB, 30);
            Wait(1000);
            bouncecounter++;
            if (bouncecounter == 10)
            {
                bouncecounter = 0;
                lightambient = 100;
                lightsource = 0;
                OnFwd(OUT_A, 40);
                OnRev(OUT_B, 40);
                for (int i = 0; i < 20; i++)
                {
                    if (Sensor(IN_1) < lightambient) lightambient = Sensor(IN_1);
                    if (Sensor(IN_2) < lightambient) lightambient = Sensor(IN_2);
                    if (Sensor(IN_1) > lightsource) lightsource = Sensor(IN_1);
                    if (Sensor(IN_2) > lightsource) lightsource = Sensor(IN_2);
                    PlayTone(440, 20);
                    Wait(100);
                }
                Off(OUT_AB);
                lighttreshold = 5 + (lightambient + lightsource) / 2;
            }
        }
        else
        {
        
            int x1 = SENSOR_1;
            int x2 = SENSOR_2;
            NumOut(1, 1, x1);
            NumOut(1, 10, x2);
            if ((x1 < lighttreshold) && (x2 < lighttreshold))
            {
                TextOut(1,20,"A");
                OnRev(OUT_A, 75);
                OnFwd(OUT_B, 75);
            }
            else if (x1 < lighttreshold)
            {
                TextOut(1,20,"D");
                int d1 = x2 - x1;
                OnRev(OUT_A, 50 - d1);
                OnRev(OUT_B, 60);
            }
            else if (x2 < lighttreshold)
            {
                TextOut(1,20,"C");
                int d1 = x1 - x2;
                OnRev(OUT_B, 50 - d1);
                OnRev(OUT_A, 60);
            }
            else
            {
                TextOut(1,20,"B");
                OnRev(OUT_AB, 75);
            }
        }
    }
}