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")[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")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()Plan your program here, then type it in and press Run.