2

I am trying to create a basic obstacle avoidance behavior, but I want it to only function if the light sensors are over a certain value.

while (!myFinch.isBeakDown())
{
    myFinch.getLeftLightSensor();
    light = myFinch.getLeftLightSensor();   

    if (light >= 20)
    {
        if (myFinch.isObstacleLeftSide())
        {
            myFinch.setWheelVelocities(-255,-255,750);
            myFinch.setWheelVelocities(100,-255, 500);
        }   
        else if (myFinch.isObstacleRightSide())
        {
            myFinch.setWheelVelocities(-255,-255,750);
            myFinch.setWheelVelocities(-255, 100, 500);
        }  
        else
        {
            myFinch.setWheelVelocities(255,255);
        }    
    }
}

EDIT Working Obstacle Avoidance code without light pretest. Obstacle avoidance works without the light pretest and I also tested the IR sensors in the netbeans sensor test.

 // Run so long as the Finch is not pointed beak down
  while(!myFinch.isBeakDown()) 
{


    if(myFinch.isObstacleLeftSide()) 
    {

        myFinch.setWheelVelocities(-255,-255,750);
        myFinch.setWheelVelocities(100,-255, 500);

    }
    // If there's an obstacle on the right, set LED blue, back up for 750 ms
    // and turn for 500 ms
    else if(myFinch.isObstacleRightSide()) 
    {

        myFinch.setWheelVelocities(-255,-255,750);
        myFinch.setWheelVelocities(-255, 100, 500);


    }
    // Else, robot goes straight
    else
    {
        myFinch.setWheelVelocities(255,255);

    }
   }

1 Answers1

0

The value of light is never updated, use

while (!myFinch.isBeakDown())
{
    light = myFinch.getLeftLightSensor();
    myFinch.sleep(1);

    if (light >= 20)
    {
        if (myFinch.isObstacleLeftSide())
        {
            myFinch.setWheelVelocities(-255,-255,750);
            myFinch.setWheelVelocities(100,-255, 500);
        }   
        else if (myFinch.isObstacleRightSide())
        {
            myFinch.setWheelVelocities(-255,-255,750);
            myFinch.setWheelVelocities(-255, 100, 500);
        }  
        else
        {
            myFinch.setWheelVelocities(255,255);
        }    
    }
}
Chris K
  • 1,703
  • 1
  • 14
  • 26
  • Thanks. I corrected it and I can now use light as a pretest condition for general movement, but I still can't get obstacle avoidance to work. It completely ignores the IR sensors for some reason. – residentburge Jul 07 '15 at 00:47
  • Are you able to get `isObstacleLeftSide()` or `isObstacleRightSide()` to return true by putting a hand in front of the sensors? – Chris K Jul 07 '15 at 00:56
  • No, not while using light as the pretest condition. I've used the sensor test in netbeans and they work fine. They also work if the application contains only an obstacle avoidance behavior. As soon as light is introduced, they don't return anything. – residentburge Jul 07 '15 at 01:01
  • Not sure, maybe remove `myFinch.getLeftLightSensor();` and add a `myFinch.sleep(1);` after `light = myFinch.getLeftLightSensor();`? – Chris K Jul 07 '15 at 01:28
  • It didn't work. Thanks for the help anyway. I'll keep working on it. – residentburge Jul 07 '15 at 02:04