The worksheetDownload the PDF
Answers

2.4 Getting round things

Sensing · Robot club · about 20 min

BugBotLab

What this lesson is about

Detect, sidestep, continue: the first obstacle avoidance.

Questions 6 marks in all

  1. [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.

    1. Stop
    2. Slide right until distance() is over 60 cm
    3. Drive forward 35 cm to get past
    4. Slide right 6 cm more
    5. Drive forward until distance() is under 20 cm
    Answer:
    Drive 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.

  2. [1 mark]Why does the program slide another 6 cm after the sensor says the way is clear?

    1. AThe body is wider than the sensor's beam
    2. BThe sensor is always 6 cm wrong
    3. CTo make up for coasting forward
    4. DTo turn the robot back to heading 0
    Answer: A. The sensor looks out from the middle, so it can see past the box before the edge of the robot is clear of it.
  3. [1 mark]Why does sliding sideways, rather than turning, keep this program simple?

    1. AThe robot never turns, so "ahead" still means the same direction
    2. BSliding is faster than turning
    3. CThe sensor does not work after a turn
    4. DTurning would reset position()
    Answer: A. After strafing, distance() still looks the way the robot is going, so the same loops work before and after.
  4. [1 mark]What does this loop do?

    while distance() < 60:
        right(50)
        wait(0.1)
    stop()
    1. ASlides right for as long as something is closer than 60 cm ahead
    2. BSlides right exactly 60 cm
    3. CSlides right until something is closer than 60 cm
    4. DDrives forward for 60 cm
    Answer: A. while distance() < 60 keeps going while the way ahead is blocked. It stops once the sensor sees further than that.
  5. [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")
    Answer:
    stepping right

    25 is equal to 25, and >= is true for equal numbers, so a tie goes right.

  6. [1 mark]What could go wrong if the robot always steps right?

    1. AThere might be no room on the right
    2. BIt would turn round
    3. CThe distance sensor would stop working
    4. DNothing, stepping right always works
    Answer: A. The depth grid's edge columns tell you which side has more room, so the robot can pick the better way.

The task: dodge the box

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.

A solution

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.