Use IR Sensors to Roam

Now that your IR sensors are working properly, let’s try using the values of objectL and objectR to make decisions on where to go.  For example, if both objectL and objectR are zero, it’s okay to go forward because there’s nothing in front of the Propeller Boe-Bot.  If  only objectL is 1, then there must be an object in front of the left sensor, so turn right.  With code that makes these kinds of decisions, your Propeller Boe-Bot can roam autonomously and avoid objects. 

Roaming Code

Here is an example program that checks the values of objectL and objectR as it roams.  If it doesn’t see any objects, it keeps going forward.  If it sees an object in either the left or right sensor, it turns away.  If it sees objects in both sensors, it backs up.

  • Load 3 Roaming with PropBOE-Bot.spin into your board.
  • Set the PWR switch to position-2.
  • Test it avoiding various objects. 
'' 3 Roaming PropBOE-Bot.spin
'' Use IR LED and IR receiver to detect object presence/distance.

OBJ

  system : "Propeller Board of Education"             ' System configuration
  freq   : "PropBOE Square Wave"                      ' Square wave signal generator
  ir     : "PropBOE IR Detect"                        ' IR object detection
  drive  : "PropBOE-Bot Servo Drive"                  ' Propeller Boe-Bot servo control
  time   : "Timing"                                   ' Delay and wait convenience methods
 
VAR

  byte objectL, objectR                               ' Variables to store results

PUB Go                                                ' Startup method

  system.Clock(80_000_000)                            ' System clock -> 80 MHz
  freq.Out(4, 1000, 3000)                             ' P4 sends 1 s, 3 kHz tone to speaker
 
  repeat                                              ' Main loop repeats indefinitely
    objectL := ir.Detect(13, 12)                      ' Check for left object
    objectR := ir.Detect(0, 1)                        ' Check for right object

    if objectL == 0 and objectR == 0                  ' If no objects detected
      drive.Wheels(100, 100)                          ' ...go forward
    elseif objectL == 1 and objectR == 1              ' If both sensors detect objects
      drive.Wheels(-100, -100)                        ' ...back up
    elseif objectR == 1                               ' If only right detects
      drive.Wheels(-100, 100)                         ' ...turn left
    elseif objectL == 1                               ' If only left detects
      drive.Wheels(100, -100)                         ' ...turn right

    time.Pause(20)                                    ' Wait 20 ms & before repeating loop      

 

How It Works

After using the ir.Detect method to load 1/0 values into objectL and objectR, the program uses an if...elseif...elsif...elseif statement to decide which way to go.  When objectL and objectR are both 0, it goes full speed forward with drive.Wheels(100, 100).  If both values are 1 instead, it goes full speed backward with drive.Wheels(-100, -100).  If just objectR is 1, and objectL is 0, then it turns left, away from an obstacle that’s in front of the right sensor.  If just objectL is 1, it turns right, away from the object in front of the left sensor.

Did You Know?

  • You can replace the 1 k-ohm resistors with 2 k-ohm resistors (red-black-red) to make the Propeller Boe-Bot more nearsighted.  Reason being, 2 k-ohm resistors make the IR headlights half as bright.
  • You can also leave the resistors as they are, and instead use distance detection to decide what distance to turn away from the object.  This is the recommended approach, and we’ll try it soon.

Try this

Use black vinyl electrical tape to cover an obstacle.  The electrical tape side will absorb infrared, so it won’t be very visible.  

  • Find an obstacle that’s roughly the same size as the Propeller Boe-Bot, like a small box.
  • Test to see how close it gets before turning away.
  • Cover one side of the box with electrical tape.
  • Repeat the test.  How close does it get now?  (It might even run into the box.)
  • You can also use the LED indicator activity from the previous activity’s Try This to measure how much closer it has to get before it can see the side covered with electrical tape.

Your Turn

You can change the drive.Wheels calls so that the Propeller Boe-Bot will follow your hand when you keep it in front of the robot.  The trick is to:

  • Make it drive forward when both sensors detect your hand.
  • Make it drive forward when neither sensor detects your hand.
  • Make it turn toward your hand instead of away from it when only one sensor detects.
  • Save a copy of  "3 Roaming PropBOE-Bot.spin" under a new name.
  • Modify the program so that it drives forward when neither sensor detects, and drives backward when both sensors detect.  (Keep in mind that you’ll have to keep your hand ahead of it; otherwise, it’ll run into your hand and just keep pushing it.)