8.4.3 - Right and Left obstacle detection

An important part of coding is making decisions. See what Bill Gates has to say about decision making in the video from code.org


Write the following program to have the Edison robot continuously drive avoiding obstacles to the left and right.

#-------------Setup---------------- import Ed Ed.EdisonVersion = Ed.V2 Ed.DistanceUnits = Ed.CM Ed.Tempo = Ed.TEMPO_MEDIUM #--------Your code below----------- #turn on obstacle detection Ed.ObstacleDetectionBeam(Ed.ON) while True: Ed.Drive(Ed.FORWARD, Ed.SPEED_5, Ed.DISTANCE_UNLIMITED) obstacle = Ed.ReadObstacleDetection() if obstacle>Ed.OBSTACLE_NONE: #there is an obstacle Ed.Drive(Ed.BACKWARD, Ed.SPEED_5, 3) if obstacle==Ed.OBSTACLE_LEFT: Ed.Drive(Ed.SPIN_RIGHT, Ed.SPEED_5, 90) elif obstacle==Ed.OBSTACLE_RIGHT: Ed.Drive(Ed.SPIN_LEFT, Ed.SPEED_5, 90) elif obstacle==Ed.OBSTACLE_AHEAD: Ed.Drive(Ed.SPIN_RIGHT, Ed.SPEED_5, 90)


You can download the program from here

In the above program we are using the if control structure to give the robot the ability to make decisions without human guidance. When this occurs in a robot it is now called an autonomous robot, as it has artificial intelligence. 

An ‘if’ statement asks whether a condition is true or false. If the result is true the program executes the block of statements following the if statement. If the result is false and there is an elif then this will be evaluated just like the if.  If there is an else statement the block of statements following the else statement will execute. 

The above program has three different paths that it can take when an obstacle is detected based on where an obstacle is. Explain in your own words what these three paths cause the robot to do.

  • Obstacle detected ahead 
  • Obstacle detected on right
  • Obstacle detected on left

Because the robot can make decisions is it alive!? Why do you think so?