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

No comments:

Post a Comment