Seeing more · Robot club · about 20 min
bumped(), stalls from velocity, back off and go round.
[1 mark]The robot drives into a 2 cm tall box, but distance() never saw it. Why?
distance() only works when the robot is still[1 mark]Which sensor does bumped() use?
bumped() is true for a moment afterwards.[1 mark]How does the stall check know the robot is stuck?
velocity() with the speed it asked forbumped()distance() is under 5[1 mark]What does this program print?
speeds = [0, 10, 25, 30, 29, 28, 1, 0]
for tick, vy in enumerate(speeds):
if tick > 5 and vy < 2:
print("stalled at tick", tick)
breakstalled at tick 6
Tick 0 is slow too, but tick > 5 ignores the start while the robot gets up to speed. The first real stall is tick 6.
[1 mark]Which of these can a stall check catch that bumped() might miss?
Tick every answer that is true.
[1 mark]Put the states of the bump and back behaviour in the order they run after a bump.
Number the lines 1 to 4 to put them in the right order.
go: drive forward until `bumped()`back: drive backward for a few ticksside: slide right for a few more ticksgo: carry on forwardgo: drive forward until `bumped()` back: drive backward for a few ticks side: slide right for a few more ticks go: carry on forward
A bump starts a small behaviour that runs to completion, like the avoid layer in lesson 5.4.
[1 mark]After a bump, a tag is right in front of you. What is the polite response?
Reach the green zone past a box the depth sensor cannot see, printing bumped when you hit it.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
while position()[1] < 80:
# drive forward at 60 (keeps going until the next command)
forward(60)
# 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: There is a box the depth sensor cannot see (it is too low). Drive for the goal; when bumped() says you hit it, print bumped, back off, go round, and carry on.
from bugbot import *
connect()
state = 'go'
ticks = 0
while position()[1] < 80:
if state == 'go':
forward(60)
if bumped():
print('bumped')
state = 'back'
ticks = 0
elif state == 'back':
backward(50)
ticks += 1
if ticks > 8:
state = 'side'
ticks = 0
elif state == 'side':
right(60)
ticks += 1
if ticks > 18:
state = 'go'
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.