Avoiding Obstacles With Infrared

In order for a robot to negotiate its environment independently, it must be able to both see and avoid obstacles in its path. If you tried out the S3 demo modes, you may remember that in Demo 3 your robot could detect objects with its infrared (IR) sensors and in Demo 4 your robot could detect (with its IR sensors) and avoid obstacles as it roamed around. Robots can be built or programmed to detect objects in a number of different ways, and with a variety of different sensors.


Did You Know?

Infrared Emitters and Receivers as Obstacle Sensors - A common solution for obstacle detection in robotics is the use of Infrared (IR) Emitters and Receivers, which are mounted in the front of your robot. These special LEDs emit non-visble infrared light, similar to that of a television remote control. These emitters, paired with a single infrared receiver, make up the obstacle sensors on your S3.


The above image shows the front of your S3. You may notice that your left and right sides are opposite from the labels on the robot. This is because the robot is facing the opposite direction from you – if you turn 180 degrees around, away from the computer screen, you should notice that now your right and left match the robot's right and left sides. Direction is always determined from the robot's perspective, not yours. If you keep in mind that the IR emitters and receiver are always facing forward on your robot, you shouldn't have any problems creating navigation programs with forward, backward, right, and left direction commands.

The emitters send alternating pulses of infrared light which are reflected back to the receiver only after hitting an obstacle. Since only one LED flashes at a time, your S3 can figure out which LED was emitting light when the receiver detected a reflection. It will use this information to decide on which side the obstacle lies. If the object is directly in front, then the light from both LEDs will reflect to the receiver when they flash.

Every robot sensor has advantages and disadvantages, with each having certain physics limitations. How it is positioned on your robot can also have an effect on its performance. Infrared does not effectively see black objects because they absorb the infrared light instead of reflecting it back to the receiver. The infrared object sensing system on the S3 works best when objects are not too dark in color, and are located between 1” and 12” from the front of the robot.

 

Using the Obstacle Sensors with Feedback

We can create a program in BlocklyProp that demonstrates behavior similar to the Demo 3 program (obstacle detection without roaming).

  • Create a program like the one shown below, with the detect obstacle block. The blocks used here can be found in the CONTROLSENSOR, and ACTIONS categories.

The loop command makes the program run continuously. If an obstacle is not detected, all LEDs will appear green. If an obstacle is directly in front of the S3, all the LEDs will appear red. Obstacles to the left are indicated by a single red LED on the left side, and an obstacle on the right is indicated by a single red LED on the right side.

  • Once you have written the program, compile and then load it on your S3 to test its functionality. 

 

Try This

  • Rewrite the program above so that it provides audio feedback for the three blocked conditions. Give it a try by adding blocks we learned about in previous tutorials.
    • Provide this audio feedback if the path directly in front of the S3 is blocked:
    • Provide this audio feedback if the path to the left is blocked:
    • Provide this feedback if the path to the right is blocked:

It should be fairly easy now to modify either of the programs above into an obstacle avoidance program. Can you see how?

  • Add movement blocks, like the ones below, in the places where they belong. It may require some experimentation (look back at the motion tutorials if you need help).

  • Give your completed obstacle avoidance program a try! Did it work the way you expected?

 

Your Turn

  • Make your own version of the program with the following behaviors:
    • When the path directly in front of the S3 is blocked, stop and rotate 180 degrees in a counterclockwise direction.
    • If an object is detected to the left, make the robot stop and rotate 90 degrees in a clockwise direction.
    • If an object is detected to the right, make the robot stop and rotate 90 degrees in a counterclockwise direction.
  • Run your new program and compare its functionality to the previous example.
  • Make your own version of the program with your own sounds and different avoidance behavior. How would you improve this program?

Special thanks to Parallax friend Whit Stodghill for his assistance in writing, editing, and testing material for these S3 tutorials.