Module 8 - Section 4 - Events and decisions

Site: ΕΛ/ΛΑΚ Moodle
Course: Python Lab
Book: Module 8 - Section 4 - Events and decisions
Printed by: Guest user
Date: Thursday, 21 November 2024, 3:20 PM

Description

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?

8.4.2 - Event Handling

Write the following program to have the Edison robot continuously drive and 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.RegisterEventHandler(Ed.EVENT_OBSTACLE_AHEAD, "avoidObstacle") Ed.Drive(Ed.FORWARD, Ed.SPEED_5, Ed.DISTANCE_UNLIMITED) while True: pass def avoidObstacle(): Ed.Drive(Ed.SPIN_RIGHT,Ed.SPEED_5,180) Ed.Drive(Ed.FORWARD, Ed.SPEED_5, 10)


You can download the code from here 

This program uses Python Event Handling. In event handling, certain events, such as an obstacle being detected, are registered with the program along with an associated function and then when these events occur the associated functions are called. The associated functions are called event handlers and they act as an interrupt, which means when the event occurs, the main program is paused while the given function is run.

In our example above, the obstacle-detected-ahead event is registered in line 13 using RegisterEventHandler. The first parameter is the event that is going to occur and the second parameter is the function that will be called when the event occurs. Thus, when an obstacle is detected ahead the avoidObstacle function will be called.

Describe what the robot does.


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?