Sensing · Robot club · about 15 min
distance() and loops that react to it.
[1 mark]How does the time-of-flight sensor measure distance?
[1 mark]distance() reads 70 cm. The robot runs forward(60, distance=20) towards the wall. About what does distance() read now? Give a whole number.
[1 mark]This loop stops the robot. Where does it end up?
while distance() > 30:
forward(40)
wait(0.1)
stop()[1 mark]Why is there a wait(0.1) inside the loop? Choose all that apply.
Tick every answer that is true.
[1 mark]In a "drive until the wall is close" loop, how does the program know how far the wall is?
[1 mark]What does this program print?
for d in [80, 45, 30]:
if d > 40:
print(d, "fast")
else:
print(d, "slow")80 fast 45 fast 30 slow
Only 80 and 45 are more than 40. The same test in a driving loop means fast while far, slow when close.
[1 mark]What does this program print?
readings = [60, 52, 44]
for i in range(3):
print(f"step {i}: wall in {readings[i]} cm")step 0: wall in 60 cm step 1: wall in 52 cm step 2: wall in 44 cm
i counts 0, 1, 2, and readings[i] picks the reading at that position, so each step shows its own number.
Drive up to the wall and stop in the green band in front of it, without touching it. Adjust the number and the speed until the robot stops inside the band every time.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# as long as the thing ahead is further than 30 cm
while distance() > 30:
# drive forward at 50 (keeps going until the next command)
forward(50)
# pause 0.1 s (the robot keeps doing what it was told)
wait(0.1)
# all motors off
stop()The hint students can ask for: Drive until distance() says the wall is close, then stop. The green zone is the band just before the wall.
from bugbot import *
connect()
while distance() > 20:
forward(50)
wait(0.1)
stop()
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.