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);
}