Book
Module 8 - Section 4 - Events and decisions
Module 8 - Section 4 - Events and decisions
Completion requirements
View
After the completion of this module the students will be able to:
- review Edpy code for various events handling
- create code to detect obstacles using Edison robot sensors
- extend the code to avoid obstacles
8.4.1 - Obstacle avoidance
Write the following code to Edpy and program edison to avoid obstacles
#-------------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)
Ed.Drive(Ed.FORWARD, Ed.SPEED_5, Ed.DISTANCE_UNLIMITED)
while Ed.ReadObstacleDetection() != Ed.OBSTACLE_AHEAD:
pass
Ed.Drive(Ed.SPIN_RIGHT,Ed.SPEED_5,180)
Ed.Drive(Ed.FORWARD, Ed.SPEED_5, 10)
You can download the code from here
How could you improve this program?
Can you modify the code so that the robot behaves differently when it detects an obstacle ahead, left or right?