Sensing · Robot club · about 20 min
Detect, sidestep, continue: the first obstacle avoidance.
[1 mark]Put the steps for getting round a box in order.
Number the lines 1 to 5 to put them in the right order.
StopSlide right until distance() is over 60 cmDrive forward 35 cm to get pastSlide right 6 cm moreDrive forward until distance() is under 20 cmDrive forward until distance() is under 20 cm Stop Slide right until distance() is over 60 cm Slide right 6 cm more Drive forward 35 cm to get past
Approach, step sideways until the way is clear, a bit extra for the body, then drive past. The sensor decides when each step is done.
[1 mark]Why does the program slide another 6 cm after the sensor says the way is clear?
[1 mark]Why does sliding sideways, rather than turning, keep this program simple?
distance() still looks the way the robot is going, so the same loops work before and after.[1 mark]What does this loop do?
while distance() < 60:
right(50)
wait(0.1)
stop()while distance() < 60 keeps going while the way ahead is blocked. It stops once the sensor sees further than that.[1 mark]What does this program print?
left_side = 25
right_side = 25
if right_side >= left_side:
print("stepping right")
else:
print("stepping left")stepping right
25 is equal to 25, and >= is true for equal numbers, so a tie goes right.
[1 mark]What could go wrong if the robot always steps right?
Drive until the box is close, print box ahead, step sideways until the way is clear, go past, and finish in the green zone without touching the box.
# 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 20 cm
while distance() > 20:
# 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()
print("box ahead")The hint students can ask for: Drive until distance() says the box is close, print box ahead, step sideways until the way is clear, go past, and into the green zone.
from bugbot import *
connect()
while distance() > 20:
forward(50)
wait(0.1)
stop()
print('box ahead')
while distance() < 60:
right(50)
wait(0.1)
stop()
right(50, distance=6) # a little further: the body is wider than the sensor's beam
forward(60, distance=55)
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.