Module 8 - Section 3 - Follow the torch
Site: | ΕΛ/ΛΑΚ Moodle |
Course: | Python Lab |
Book: | Module 8 - Section 3 - Follow the torch |
Printed by: | Guest user |
Date: | Tuesday, 3 December 2024, 7:28 PM |
Description
After the completion of this module the students will be able to:
- identify when there is a need to use the if statement in Edpy
- use while loops inside the Edpy environment
- complete a code for the Edison robot to follow a light source
8.3.1 - Torch following with EdPy
Write the following program to have the Edison robot follow light from a torch/flashlight.
#-------Setup------
import Ed
Ed.EdisonVersion = Ed.V2
Ed.DistanceUnits = Ed.CM
Ed.Tempo = Ed.TEMPO_MEDIUM
#--------Your code below-----
Ed.Drive(Ed.FORWARD,Ed.SPEED_5,Ed.DISTANCE_UNLIMITED)
while True:
if Ed.ReadRightLightLevel() - Ed.ReadLeftLightLevel() < 0:
Ed.Drive(Ed.FORWARD_LEFT,Ed.SPEED_5,Ed.DISTANCE_UNLIMITED)
else:
Ed.Drive(Ed.FORWARD_RIGHT,Ed.SPEED_5,Ed.DISTANCE_UNLIMITED)
You can find the program in the left pane of Edpy with the name Follow_torch
Shine a torch/flashlight at Edison and the robot will drive towards the light.
The presence of the torch on either the left or right of the robot will cause the robot to read higher light levels on that side of the robot. The logic of this program says that when the right light level minus the left right level is less than zero, the robot drives left, else the robot drives to the right.
It is often useful when there are lots of calculations and different possible values in a program to “trace” the different values that can occur and predict the associated behaviour. Fill out the following tracing table for this program.