Tuesday, May 10, 2011

Final Competition

Write up

For our final lab, we were required to find, detect and acknowledge golf balls within the playing field followed by searching for a black circular disk in the middle of the arena. The main goal was to collect as many golf balls as you could and drop them off within the black circle in the middle. The playing field was finite and bounded and the golf balls were scattered; six balls on our side and six balls on our opponents side to start. To accomplish these tasks we included the obstacle avoidance and wander functions written in our previous labs.

Our design was simple and easy to implement. It required two reflective sensors pointed directly at the ground under our robot for searching for the black circles to drop off our load of golf balls. These two sensors simply looked for any change in reflected light from the surface. This change in light signified the presence of the drop off zone. The remaining sensors were kept the same from previous labs, as their utility had not changed (i.e., two bump sensors for obstacle avoidance).

In regards to the software aspect of our robot, we had the functions for obstacle avoidance and wander activated until either one team captured and dropped off a majority of the golf balls or the current game timer ran out. The functions for registering the reflected light did just that. They looped until some significant change in light level came about and then sent a signal to cause the robot to center and drop the golf balls off, which in turn activated our gate mechanism. The entire program consisted of a total of three main processes which were constantly active for each of the main tasks; obstacle avoidance, wander and drop golf balls off.

The main issues that presented themselves during this project were mainly in the design of our robot as the code was pretty much taken from previous labs with minor modifications. We had to construct and deconstruct our robot in order to make it vulnerable enough to withstand some of the torment the other robots brought about. Although we were not allowed to equip our robot with offensive weaponry, we were allowed to use the legos within our lego box to make it sturdy. Another issue we had was our battery life. Our battery seemed to go dead rather quickly, which presented significant problems while testing and during the actual competition.

We feel that our robot competed at a premium level for the competition. I believe that the outcome of the tournament were as expected. It really came down to who was able to get the balls to the center first which the winner seemed to do flawlessly.






Code


// Robo Mini Golf - Laster, Szelagowski, Niemann
// Final Competition

////////////// Global Variables
int MAX_LIGHT = 115;
int MIN_LIGHT = 80;
float _timer;
int RIGHT_EYE = 4;
int LEFT_EYE = 3;
int RIGHT_BUMP = 10;
int LEFT_BUMP = 9;
int rVal = 0;
int lVal = 0;
int bumpCtr = 0;
int aFlag = 1;
int lock = 0;
int onLine = 0;

int LEFT_MOTOR = 0;
int RIGHT_MOTOR = 2;

int GATE_MOTOR = 3;
int OPEN_GATE = 100;
int CLOSE_GATE = -100;

float centerTime = 2.5;

int HOLE_SENSOR = 4;
// Main Function
void main()
{
int time;
// Loop For Start Waiting
while( !start_button());
start_process(open());
reset_timer();
motor(LEFT_MOTOR, 75);
motor(RIGHT_MOTOR, 75);
start_process(avoid());
start_process(sense());

}


void sense()
{
while(1)
{
if(normalize(analog(LEFT_EYE)))
{
close();
aFlag = 0;

if(!normalize(analog(RIGHT_EYE)))
{
motor(LEFT_MOTOR, -50);
motor(RIGHT_MOTOR, 50);
sleep(.75);
}

off(RIGHT_MOTOR);
off(LEFT_MOTOR);
sleep(.3);
open();
motor(LEFT_MOTOR, -50);
motor(RIGHT_MOTOR, -50);
sleep(2.0);
motor(LEFT_MOTOR, 75);
motor(RIGHT_MOTOR, -75);
sleep(1.0);
motor(LEFT_MOTOR, 75);
motor(RIGHT_MOTOR, 75);

}
else if( normalize(analog(RIGHT_EYE)) )
{
close();
aFlag = 0;

if(!normalize(analog(LEFT_EYE)))
{
motor(LEFT_MOTOR, 50);
motor(RIGHT_MOTOR, -50);
sleep(.75);
}

off(RIGHT_MOTOR);
off(LEFT_MOTOR);
sleep( .3);
open();
motor(LEFT_MOTOR, -50);
motor(RIGHT_MOTOR, -50);
sleep(2.0);
motor(LEFT_MOTOR, 75);
motor(RIGHT_MOTOR, -75);
sleep(1.0);
motor(LEFT_MOTOR, 75);
motor(RIGHT_MOTOR, 75);
}
aFlag = 1;


}


}

void avoid()
{
while(1){

while( aFlag )
{
if( digital( LEFT_BUMP )) // Left Motor
{
if( timer() < 5. ) { if( bumpCtr == 2 ) { //left randomTurn(1); bumpCtr=0; } else { goRight(); //goLeft(); bumpCtr++; } } else { goRight(); //goLeft(); bumpCtr = 1; reset_timer(); } open(); } if( digital( RIGHT_BUMP )) // Right Motor { if( timer() < 5. ) { if( bumpCtr==2 ) { randomTurn(2); bumpCtr=0; } else { goLeft(); //goRight(); bumpCtr++; } } else { goLeft(); //goRight(); bumpCtr=1; reset_timer(); } open(); } } } } void goRight() { // Turning right close(); sleep(0.2); goBack(0.4); if(aFlag) { motor(LEFT_MOTOR, 75); motor(RIGHT_MOTOR, -75); sleep(0.5); motor(LEFT_MOTOR, 75); motor(RIGHT_MOTOR, 75); } } void goLeft() { // Turning left close(); sleep(0.2); goBack(0.4); if(aFlag) { motor(RIGHT_MOTOR, 75); motor(LEFT_MOTOR, -75); sleep(0.5); motor(LEFT_MOTOR, 75); motor(RIGHT_MOTOR, 75); } } void goBack(float val) { motor(RIGHT_MOTOR, -40); motor(LEFT_MOTOR, -40); sleep(val); } void randomTurn(int val) { float randNum; int randMotor; int i; close(); randNum = (float)random(3)/10.0 + .2; fd(LEFT_MOTOR); fd(RIGHT_MOTOR); tone(940.0, 0.7); if(aFlag) { if(val == 1) { motor(LEFT_MOTOR, -100); motor(RIGHT_MOTOR, 100); sleep(randNum); } else { motor(LEFT_MOTOR, 100); motor(RIGHT_MOTOR, -100); sleep(randNum); } motor(RIGHT_MOTOR, 75); motor(LEFT_MOTOR, 75); } } void open() { motor(GATE_MOTOR, OPEN_GATE); sleep(.3); motor(GATE_MOTOR, 30); } void close() { motor(GATE_MOTOR, CLOSE_GATE); sleep(.3); motor(GATE_MOTOR, -30); } void reset_timer() { _timer= seconds(); } float timer() { return seconds() - _timer; } int normalize( int light ) { if(light > 60)
{
return 1;
}

else //( light > 50)
{
return 0;
}
}

Tuesday, March 29, 2011

Lab 6 - Harvester

Lab 6 Write-up

For lab six, we were required to find, detect and acknowledge flat, black circular objects within the playing field followed by searching for strong light sources after a sixty second time period. The goal of this was to find all ten black objects before our sixty seconds were up and we reached the light. The playing field was finite and bounded and the black circles were scattered, so we included the obstacle avoidance and wander functions written in our previous labs.
Our design was simple and easy to implement. It required two reflective sensors pointed directly at the ground under our robot for searching for the black circles to harvest. These two sensors simply looked for any change in reflected light from the surface. This change in light signified the presence of a harvestable object. The remaining sensors were kept the same from previous labs, as their utility had not changed.
In regards to the software aspect of our robot, we had the functions for obstacle avoidance and wander activated until the sixty second timer was up at which point they were turned off. The new functions for registering the reflected light did just that. They looped until some significant change in light level came about and then sent a signal to cause a beep, which acknowledged the harvestable object. At the time when the sixty seconds were up we allowed our robot to keep harvesting while searching for the light as it might get lucky and find a piece of food along the way.
This project brought about some issues with creating new processes/threads within a loop, regardless of the time between each process of thread was called. If it hadn’t been for one of the other groups already encountering this problem, it may have taken us quite a bit of time to discover the source of these problems. Once that issue was cleared up, the rest of the processes/threads seemed to communicate well together allowing a smooth running program.
We feel that our robot competed at a premium level for the competition. Since the wander function was random, it really came down to chance and whose robot had a smoother ride. Although we did not take the competition by a landslide, we are still pleased with the rankings and results.




//code
// Line Follower - Laster, Szelagoski, Niemann


//harvester code lab 6
//laster, szelagowski, niemann

// Global Variables
int MAX_LIGHT = 115;
int MIN_LIGHT = 80;

int rVal = 0;
int lVal = 0;
int bumpCtr = 0;
int flag = 1;
int wFlag = 1;
int lock = 0;

int Lmotor = 2;
int Rmotor = 0;
int Lreflect = 5;//5
int Rreflect = 2;
int Leye = 3;
int Reye = 4;
int Lbumper = 9;
int Rbumper = 10;

int line, lineThreshold;

int main()
{

// Loop For Start Waiting
while( !start_button());

printf( "Harvester\n" );

//start thread to turn off robot when off button is pressed
//start_process(stopper());

//start_process(seek());


//start thread to count for 60 seconds
start_process(count());

//start thread to avoid obstacles
start_process(avoid());

//start thread to wander around
start_process(wander());

//start thread to harvest left
start_process(eatLeft());

//start thread to harvest right
start_process(eatRight());
}


void wander()
{
float rand;
int randDir;

while(wFlag)
{
//check if robot is avoiding
if(!lock)
{
//go straight for 3 sec
motor(Lmotor, 70);
motor(Rmotor, 50);
sleep(3.0);

rand = (float)random(10) / 10.0;
randDir = random(2);

//check if robot is avoiding
if(!lock)
{
//turn right
if(randDir)
{
motor(Lmotor, 50);
motor(Rmotor, -50);
}
//turn left
else if(randDir == 0)
{
motor(Lmotor, -50);
motor(Rmotor, 50);
}
//else don't turn


//turn for random 0.5 < time < 1.5 sec sleep(rand + 0.5); } } } } void seek() { //turn till it senses light motor(Lmotor, -50); motor(Rmotor, 50); while(analog(Leye) > 95 || analog(Reye) > 95)
{
//printf("Leye= %d, Reye= %d\n", analog(Leye), analog(Reye) );
}
start_process(beeper());

off(Lmotor);
off(Rmotor);

motor(Lmotor, 100);
motor(Rmotor, 75);

while(!digital(Lbumper) && !digital(Rbumper));

off(Lmotor);
off(Rmotor);

}

void eatLeft()
{
//loop until time > 60 or off button pressed
while( 1 )
{
//get left reflective sensor value
lVal = analog(Lreflect);

//if the value is > 50 then it sensed a black dot
if( lVal > 50 )
{
//start_process(beeper());
beep();
lVal = analog(Lreflect);
//loop until it passes the dot
while( lVal > 50)
lVal = analog(Lreflect);
}

}


}

void eatRight()
{
//loop until time > 60 or off button pressed
while( 1 )
{
//get right reflective sensor value
rVal = analog(Rreflect);

//if the value is > 50 then it sensed a black dot
if( rVal > 50 )
{
//start_process(beeper());
beep();
rVal = analog(Rreflect);
//loop until it passes the dot
while( rVal > 50)
rVal = analog(Rreflect);
}

}


}

void avoid()
{
while( wFlag )
{
if (digital(Lbumper))//left
{
lock = 1;
avoidLeft();
motor(Lmotor, 70);
motor(Rmotor, 50);
}

if (digital(Rbumper))//right
{
lock = 1;
avoidRight();
motor(Lmotor, 70);
motor(Rmotor, 50);
}
lock = 0;

}
}



void avoidLeft()
{
bk(Lmotor);
bk(Rmotor);
sleep(0.25);
motor(Lmotor, 50);
motor(Rmotor, -50);
sleep(0.5);
}

void avoidRight()
{
bk(Lmotor);
bk(Rmotor);
sleep(0.25);
motor(Rmotor, 50);
motor(Lmotor, -50);
sleep(0.5);
}



void count()
{
//count till 60
sleep(60.);
//turn off wandering
wFlag = 0;
flag = 0;
lock = 1;

//turn off the motors
off(Lmotor);
off(Rmotor);

tone(500.0, 1.);
//start thread to start searching for light
start_process(seek());



}

Wednesday, March 9, 2011

Lab 5 - line follow

Nick Niemann, Keith Szelagowski, Kevin Laster


For our Lab 5 , we were required to find and detect a line, then follow the line without jumping off of it. The goal is to complete the loop as quickly as possible without jumping off of the line.  We also had to include wandering with obstacle avoidance just in case our robot would jump off the line, it would have to find the line again and complete the loop. This would then be tested in a competition amongst other CPE470 students at the University of Nevada, Reno.


Our design was simple, we mounted the two optosensors on each side of the robot as far away as possible. This allowed for less adjustment and helped with time. For our software, we would move forward until the robot encountered a reflection off of the black line. If the reflection came from the right sensor, then we would back up a bit and turn right slightly. The opposite would happen for our left sensor. The robot would back up and turn left slightly. We added the back up feature because the robot wouldn't turn sharp enough and just go off of the line. We also had another thread going at the same time to deal with going off the line. We simply just incorperated obsticle avoidance from a previous lab.



At first we had some problems with mounting the optosensors because they wouldnt stay in the right place. We then built something a little sturdier that would allow the sensor to be a bit more stable. Another problem we were facing was that our turning wasn't as sharp as we would like it to be. This was fixed by just adding the back up feature as mentioned before. The cause of the robot not turning sharp enough was because of our front wheels. The front wheels were creating friction against the robot when turning. For the next assignment we will more than likely incorperate the nubs in replace of the front wheels.

As the contest went on we felt really confident that our robot was going to perform substantially. While seeing other robots conquer at such fast speeds, we now realize we could have made great improvements to our robot to compete better. Next lab I feel that we need to modify our robots hardware and not focus so much on the software to make up for the hardware flaws. Although our robot was not the fastest, we felt as if it was really reliable for not jumping off of the line. We felt that this was the ultimate goal as other robots were jumping off of the line.

// Line Follower - Laster, Szelagoski, Niemann

// Global Variables
int MAX_LIGHT = 115;
int MIN_LIGHT = 80;
float _timer;
int RIGHT_EYE = 6;
int LEFT_EYE = 5;
int rVal = 0;
int lVal = 0;
int bumpCtr = 0;
int flag = 1;
int wFlag = 1;
int lock = 0;
int onLine = 0;

int leftMotor = 0;
int rightMotor = 1;

int Lsensor = 4;
int Rsensor = 3;
int line, lineThreshold;

// Main Function
void main()
{
    // Initialize Variables
    int time;
    int lightLeft, lightRight;
  
    // Loop For Start Waiting  
    while( !start_button());
    reset_timer();
    printf( "Line Followerv2\n" );
    start_process(lineDetect());
    //start_process(straight());
    start_process(avoid());
    start_process(stopper());
  
}

/*
void straight()
{
    while(flag)
      {
        //loop till left sensor hits while
        while(!lVal&&!rVal && !lock)
          {
            motor(leftMotor, 20);
            motor(rightMotor, 20);
        }
    }
  
}
*/

void lineDetect()
{
  
    while(flag)
      {
        lVal = analog( Lsensor );
        lVal = normalize(lVal);
      
      
        printf("lVal=%d \n", lVal);
      
        // if Rsensor>Lsesnor
        while(lVal)
          {
            //start lock
            lock = 1;
          
            // Turn left
            if(onLine)
              {
                motor(rightMotor, -25);
                motor(leftMotor, -100);
                sleep(0.1);
            }
          
            motor(rightMotor, 50);
            sleep(0.5);
            lVal = analog( Lsensor );
            lVal = normalize(lVal);
            onLine = 1;
        }
      
        rVal = analog( Rsensor );
        rVal = normalize(rVal);
      
      
        printf("rVal=%d \n", rVal);
      
        // If Rsensor>Lsensor
        while (rVal)
          {
            //start lock
            lock = 1;
          
            // Turn right
            if(onLine)
              {
                motor(leftMotor, -25);
                motor(rightMotor, -100);
                sleep(0.1);
            }
            motor(leftMotor, 50);
            sleep(0.5);
            rVal = analog( Rsensor );
            rVal = normalize(rVal);
            onLine = 1;
          
        }              
        //end lock
        lock = 0;
      
        motor(leftMotor, 100);
        motor(rightMotor, 100);
      
      
      
    }
}








// Function Definitions
void avoid()
{
    while( wFlag )
      {
        if( digital( 11 )) // Left Motor
          {
            if( timer() < 7. )
              {
                if( bumpCtr == 4 )
                  {
                    randomTurn();
                    bumpCtr=0;
                }
                else
                  {
                    avoidLeft();
                    bumpCtr++;
                }
            }
            else
              {
                avoidLeft();
                bumpCtr = 1;
                reset_timer();
            }
        }
      
        if( digital( 9 )) // Right Motor
          {
            if( timer() < 7. )
              {
                if( bumpCtr==4 )
                  {
                    randomTurn();
                    bumpCtr=0;
                }
                else
                  {
                    avoidRight();
                    bumpCtr++;
                }
            }
            else
              {
                avoidRight();
                bumpCtr=1;
                reset_timer();
            }
        }
      
    }
}

void randomTurn()
{
    int i;
    float randNum = (float)random(100)/100. + 1.5;
    int randMotor = (int)random(2);
  
    fd(0);
    fd(1);
    tone(940.0, 0.7);
    motor(randMotor, 100);
    motor((randMotor-1)*-1, -100);
    sleep(randNum);
  
}


void avoidLeft()
{
    bk(0);
    bk(1);
    sleep(0.5);
    motor(0, 100);
    motor(1, -100);
    sleep(0.7);
}

void avoidRight()
{
    bk(0);
    bk(1);
    sleep(0.5);
    motor(1, 100);
    motor(0, -100);
    sleep(0.7);
}

void reset_timer()
{
    _timer= seconds();
}

float timer()
{
    return seconds() - _timer;
}
int normalize( int light )
{
    if(light < 50)
      {
        return 0;
    }
  
    else //( light > 50)
      {
        return 1;
    }
}

void stopper()
{
    while (flag)
      {
        if (stop_button())
          {
            flag = 0;
        }
    }
}

Wednesday, February 23, 2011

Lab 4 light sensing

//**************WANDERING*******************

// Global Variables
int MAX_LIGHT = 115;
int MIN_LIGHT = 80;
float _timer;
int RIGHT_EYE = 6;
int LEFT_EYE = 5;
int RLightVal = 0;
int LLightVal = 0;
int bumpCtr = 0;
int flag = 1;
// Main Function
void main()
{
    // Initialize Variables
    int time;
    int lightLeft, lightRight;
  
    // Loop For Start Waiting  
    while( !start_button());
    reset_timer();
    printf( "Wandering\n" );
    start_process(lightDetect());
    start_process(wander());
    start_process(avoid());
     
}

void lightDetect()
{
    while(flag)
      {
        RLightVal = analog( RIGHT_EYE );
        LLightVal = analog( LEFT_EYE );

      
        if( (RLightVal  < 90 ) || ( LLightVal < 90 ) )  
          {
            flag = 0;
            off(0);
            off(1);
        }
    }
}

// Function Definitions
void wander()
{
    while(1)
      {      
        while(flag)
          {
            // Move Forward For Time
            motor(0, 60);
            motor(1, 60);
            sleep(3.0);
      
            // Turn Random Direction
            if(flag)
              {
                randomTurn();
            }

        }
    off(0);
    off(1);
    }
  
}

void avoid()
{
    while( 1 )
    {
    if( digital( 11 )) // Left Motor
      {
        if( timer() < 7. )
          {
            if( bumpCtr == 4 )
              {
                randomTurn();
                bumpCtr=0;
            }
            else
              {  
                avoidLeft();
                bumpCtr++;
            }
        }
        else
          {
            avoidLeft();
            bumpCtr = 1;
            reset_timer();
        }
    }
  
    if( digital( 9 )) // Right Motor
      {
        if( timer() < 7. )
          {
            if( bumpCtr==4 )
              {
                randomTurn();
                bumpCtr=0;
            }
            else
              {  
                avoidRight();
                bumpCtr++;
            }
        }
        else
          {
            avoidRight();
            bumpCtr=1;
            reset_timer();
        }
    }

    }
}

void randomTurn()
{
    int i;
    float randNum = (float)random(100)/100. + 1.5;
    int randMotor = (int)random(2);
  
    fd(0);
    fd(1);
    tone(940.0, 0.7);
    motor(randMotor, 100);
    motor((randMotor-1)*-1, -100);
    sleep(randNum);
  
}


void avoidLeft()
{
    bk(0);
    bk(1);
    sleep(0.5);
    motor(0, 100);
    motor(1, -100);
    sleep(0.7);
}

void avoidRight()
{
    bk(0);
    bk(1);
    sleep(0.5);
    motor(1, 100);
    motor(0, -100);
    sleep(0.7);
}

void reset_timer()
{
    _timer= seconds();  
}

float timer()
{
    return seconds() - _timer;
}
int normalize( int light )
{
    int output = 0;
  
  
    if(light < MIN_LIGHT)
      {
        return 100;
    }
  
    else if( light > MAX_LIGHT)
        {
          return 0;
      }
    
      output = 100 - (( light - MIN_LIGHT ) * ( 100 / ( MAX_LIGHT - MIN_LIGHT )));
  
    if( output < 0 )
      {
        output = 10;
    }
    if( output > 100 )
      {
        output = 100;
    }
  
    return output;
}





//**************LIGHT SEEK and OBSTACLE AVOID*******************

// Global Variables
int MAX_LIGHT = 115;
int MIN_LIGHT = 80;
float _timer;
int RIGHT_EYE = 6;
int LEFT_EYE = 5;

// Main Function
void main()
{
    // Initialize Variables
    int time;
    int lightLeft, lightRight;
  
    // Loop For Start Waiting  
    while( !start_button());
    reset_timer();
  
    // Loop For Action
    while( !stop_button())
      {
        /*-- Light Seeking --*/
        printf( "Light Seeker\n" );
      
        // Calculate Light
        lightRight = normalize(analog(RIGHT_EYE));
        lightLeft = normalize(analog(LEFT_EYE));
      
        // Adjust Right Motor
        motor( 1, lightLeft );
      
        // Adjust Left Motor
        motor( 0, lightRight );
      
        // Sleep
        sleep(1.);
      
        printf("\n RIGHT=%d  LEFT=%d", lightRight, lightLeft);
      
        avoid();
                      
    }
  
    // Turn Motors Off  
    off(0);
    off(1);
}

// Function Definitions


void avoid()
{
    int bumpCtr = 0;  
    if( digital( 11 )) // Left Motor
      {
        if( timer() < 7. )
          {
            if( bumpCtr == 4 )
              {
                randomTurn();
                bumpCtr=0;
            }
            else
              {  
                avoidLeft();
                bumpCtr++;
            }
        }
        else
          {
            avoidLeft();
            bumpCtr = 1;
            reset_timer();
        }
    }
  
    if( digital( 9 )) // Right Motor
      {
        if( timer() < 7. )
          {
            if( bumpCtr==4 )
              {
                randomTurn();
                bumpCtr=0;
            }
            else
              {  
                avoidRight();
                bumpCtr++;
            }
        }
        else
          {
            avoidRight();
            bumpCtr=1;
            reset_timer();
        }
    }
  
    fd( 1 );
    fd( 0 );
}

void randomTurn()
{
    float randNum = (float)random(100)/100. + 1.5;
    int randMotor = (int)random(2);
  
    bk(0);
    bk(1);
    tone(940.0, 0.7);
    motor(randMotor, 100);
    motor((randMotor-1)*-1, -100);
    sleep(randNum);  
}


void avoidLeft()
{
    bk(0);
    bk(1);
    sleep(0.5);
    motor(0, 100);
    motor(1, -100);
    sleep(0.7);
}

void avoidRight()
{
    bk(0);
    bk(1);
    sleep(0.5);
    motor(1, 100);
    motor(0, -100);
    sleep(0.7);
}

void reset_timer()
{
    _timer= seconds();  
}

float timer()
{
    return seconds() - _timer;
}

int normalize( int light )
{
    int output = 0;
  
  
    if( light < MIN_LIGHT )
      {
        return 100;
    }
  
    else if( light > MAX_LIGHT)
        {
          return 0;
      }
    
      output= 100 - (( light - MIN_LIGHT ) * ( 100 / ( MAX_LIGHT - MIN_LIGHT )));
  
    if( output < 0 )
      {
        output = 10;
    }
    if( output > 100 )
      {
        output = 100;
    }
  
    return output;
}







//**************LIGHT AVOID****************
// Global Variables
int MAX_LIGHT = 115;
int MIN_LIGHT = 80;
int RIGHT_EYE = 6;
int LEFT_EYE = 5;

// Main Function
void main()
{
    // Initialize Variables
    int lightLeft, lightRight;
  
    // Loop For Start Waiting  
    while( !start_button());
  
    // Loop For Action
    while( !stop_button())
      {
        /*-- Light Avoiding --*/
        // Calculate Light
        lightRight = normalize(analog(RIGHT_EYE));
        lightLeft = normalize(analog(LEFT_EYE));
      
        // Adjust Right Motor
        motor( 1, lightRight );
      
        // Adjust Left Motor
        motor( 0, lightLeft );
      
        // Sleep
        sleep(1.);

        printf("\n RIGHT=%d  LEFT=%d", lightRight, lightLeft);
            
                      
    }
  
    // Turn Motors Off  
    off(0);
    off(1);
}

// Function Definitions

int normalize( int light )
{
    int output = 0;
  
  
    if(light < MIN_LIGHT)
      {
        return 100;
    }
  
    else if( light > MAX_LIGHT)
        {
          return 0;
      }
    
      output = 100 - (( light - MIN_LIGHT ) * ( 100 / ( MAX_LIGHT - MIN_LIGHT )));
  
    if( output < 0 )
      {
        output = 10;
    }
    if( output > 100 )
      {
        output = 100;
    }
  
    return output;
}




//****************LIGHT SEEK****************

// Global Variables
int MAX_LIGHT = 115;
int MIN_LIGHT = 80;
int RIGHT_EYE = 6;
int LEFT_EYE = 5;

// Main Function
void main()
{
    // Initialize Variables
    int lightLeft, lightRight;
  
    // Loop For Start Waiting  
    while( !start_button());
  
    // Loop For Action
    while( !stop_button())
      {
        /*-- Light Seeking --*/
        printf( "Light Seeker\n" );
      
        // Calculate Light
        lightRight = normalize(analog(RIGHT_EYE));
        lightLeft = normalize(analog(LEFT_EYE));
      
        // Adjust Right Motor
        motor( 1, lightLeft );
      
        // Adjust Left Motor
        motor( 0, lightRight );
      
        // Sleep
        sleep(1.);
      
        printf("\n RIGHT=%d  LEFT=%d", lightRight, lightLeft);
                      
    }
  
    // Turn Motors Off  
    off(0);
    off(1);
}

// Function Definitions

int normalize( int light )
{
    int output = 0;
  
  
    if( light < MIN_LIGHT )
      {
        return 100;
    }
  
    else if( light > MAX_LIGHT)
        {
          return 0;
      }
    
      output= 100 - (( light - MIN_LIGHT ) * ( 100 / ( MAX_LIGHT - MIN_LIGHT )));
  
    if( output < 0 )
      {
        output = 10;
    }
    if( output > 100 )
      {
        output = 100;
    }
  
    return output;
}

Wednesday, February 9, 2011

Lab 3 - Crazy Corner Conundrum

For our Lab 3, we were required to construct a vehicle capable of maneuvering a maze, avoiding obstacles, and are able to get itself out of corner if stuck. The entire code was assembled within a week’s time and the final project was then tested in a competition amongst other CPE470 students at the University of Nevada, Reno.

            The robot, which was built using Legos, was coded using an interactive-C compiler. The code was developed and tested by Keith Szelagowski, Kevin Laster, and Nick Niemann. A simple algorithm was created for the robot to easily maneuver around obstacles using sensors that can detect if the robot were to bump into an object. The code we developed also took in account if the robot bumped into an object four times within seven seconds, then at which point it would back up, turn a great amount, and then continue on with its normal program until the stop button is pressed.
           
            The hardware on our robot was more conventional.  We did not add anything that enhanced our robot in any way.  The only additions we added to the physical robot were longer bumpers.  This was needed because when the original bumpers were applied they would catch the front wheels of the robot, which caused more friction and slowed the robot down.  Another feature we added was to push the ends of the bumpers in a centimeter.  This allowed the robot to essentially ride a wall.  If the bumpers were on the ends, then it would trigger the sensor and turn when it would not need to.  We tried to enhance our robot to go faster by changing the gear ration to a one to one.  This was a problem for us. It made our robot go faster, but when the bumper was hit at such a fast speed it caused our bumper to fall off.  We went with the safe approach and thought we could win with “slow and steady wins the race.”
           
            There were two major problems that we faced when developing this lab. The first was the issue of the robot not being able to turn in the direction that we wanted. It seemed that it would only turn one direction, but not the other. We quickly found out this was a sensor wiring issue, so a replacement of the sensor quickly fixed the problem. The other problem that we encountered was setting up a reasonable time period for the robot to sense that it was in a corner. Although the typical algorithm called for a time period of four seconds, we debated how this was calculated. It could have always been running continuous four-second intervals, to where if the robot encounters four bumps within that four-second period, it would enact the corner algorithm. The alternative, which we chose, was that if a bump is encountered, it is then compared to how long it was since the last bump. If the time period between four consecutive bumps is less then four seconds, then the corner algorithm is run. Due to the constraints on our motors, we set the time limit to seven seconds, as it yielded the best results.

            Our robot was put to the test in the corner maze and successfully finished the maze.  Although it wasn’t at the fastest time, we did complete the task.  Our robot finished at a total of 46 seconds, which wasn’t as good as some of the others that were in the 20’s.  We felt that if we had spent more time to make our robot faster and more durable we could have finished among the leaders.








//corner program
// Global Variables
 float _timer;
void main()
{
    int bumpCtr = 0, time;
    printf("Corner Contest\n");
   
    while(!start_button());
    reset_timer();
   
    while(!stop_button())
      { 
       
        fd(0);
        fd(1);
       
        if (digital(11))//left
          {
            if (timer()<7.)
              {
                if (bumpCtr==4)
                  {
                    randomTurn();
                    bumpCtr=0;
                }
                else
                  {  
                    avoidLeft();
                    bumpCtr++;
                }
            }
            else
              {
                avoidLeft();
                bumpCtr=1;
                reset_timer();
            }
        }
       
        if (digital(9))//right
          {
            if (timer()<7.)
              {
                if (bumpCtr==4)
                  {
                    randomTurn();
                    bumpCtr=0;
                }
                else
                  {  
                    avoidRight();
                    bumpCtr++;
                }
            }
            else
              {
                avoidRight();
                bumpCtr=1;
                reset_timer();
            }
        }      
       
        off(0);
        off(1);
    }
}
void randomTurn()
{
    float randNum = (float)random(100)/100. + 1.5;
    int randMotor = (int)random(2);
  
   
    bk(0);
    bk(1);
    tone(940.0, 0.7);
    motor(randMotor, 50);
    motor((randMotor-1)*-1, -50);
    sleep(randNum);   
}

void avoidLeft()
{
    bk(0);
    bk(1);
    sleep(0.5);
    motor(0, 50);
    motor(1, -50);
    sleep(0.7);
}
void avoidRight()
{
    bk(0);
    bk(1);
    sleep(0.5);
    motor(1, 50);
    motor(0, -50);
    sleep(0.7);
}
void reset_timer()
{
    _timer= seconds();  
}
float timer()
{
    return seconds() - _timer;
}

Tuesday, February 1, 2011

Lab 2

lab2
nick niemann
keith szelagowski
kevin laster

//beeper
void main()
{
    printf("Beeper Program 14\n");
    tone(500.0, 3);
    sleep(1.0);
    tone(100.0, 3);
    sleep(1.0);
    tone(1000.0, 2);
   
}

//motor
void main() {
    printf("Team 14 \nMotor Program");
   
   
    motor(0,50);
    motor(3,50);
    sleep(3.0);
   
    beep();
    bk(0);
    bk(3);
   
    motor(0,-50);
    motor(3,-50);
    sleep(2.0);
   
    beep();
    off(0);
    sleep(2.0);
   
    fd(0);
    fd(3);
    motor(0,100);
    motor(3,100);
    sleep(3.0);
    beep();
    off(0);
    off(3);
}


//sensor
void main() {
    while(1)
      {
        while (digital(11))//left
          {
            motor(3, 50);
            motor(0, -50);
            if(!digital(11))
              {
                printf("left..n");
                off(0);
                off(3);
                break;
            }
        }
       
        while (digital(9))//right
          {
            motor(0, 50);
            motor(3, -50);
            if(!digital(9))
              {
                printf("right..n");
                off(0);
                off(3);
                break;   
            }
        }
    }
}


//obstacle avoidance
void main() {
    printf("obstacle\n");
    while(!start_button());
    while(!stop_button())
      {
        fd(0);
        fd(3);
        if (digital(11))//left
          {
              bk(0);
              bk(3);
              sleep(0.5);
              motor(0, 50);
              motor(3, -50);
              sleep(0.5);
          }
       
        if (digital(9))//right
          {
              bk(0);
              bk(3);
              sleep(0.5);
              motor(3, 50);
              motor(0, -50);
              sleep(0.5);
        }
    }
    off(0);
    off(3);
}