The answersDownload the PDF
Worksheet

9.4 Asking

Talking to each other · Robot club · about 20 min

BugBotLab
NameClassDate

What this lesson is about

A question and a reply, with a timeout; going where you are told.

Questions 6 marks in all

  1. [1 mark]What does this program print?

    import math
    sx, sy = 30, 40
    x, y = 0, 0
    print(f"the scout is {math.hypot(sx - x, sy - y):.0f} cm away")
  2. [1 mark]The Scout says at 25,55. You stop 13 cm in front of it, aiming for (sx, sy - 13). What y, in cm, do you drive to?

  3. [1 mark]Before two robots can share positions, what must they agree?

    1. AThe same origin and the same directions for x and y
    2. BThe same speed
    3. CThe same colour LED
    4. DThe same heading
  4. [1 mark]You drove to where the Scout said it was, but the Scout had moved. What should your program have done?

    1. AAsked again, because the answer was only true when it was sent
    2. BDriven faster
    3. CTrusted the first answer
    4. DSent its own position to the Scout
  5. [1 mark]In the Scout task, a student writes others() to find the Scout. What happens?

    1. AIt does not work: others() exists only in games, so in a task you have to ask by radio
    2. BIt returns the Scout's position
    3. CIt returns the Scout's reply text
    4. DIt sends where are you? for you
  6. [1 mark]What does this program print?

    import math
    sx, sy = 30, 40
    print(round(math.hypot(sx - 0, (sy - 13) - 0)))

The task: meet the scout

Ask the Scout where it is and stop in the green square, 13 cm in front of it, without touching it.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# maths: atan2, hypot, sin, cos, radians
import math

def wrapped(h):
    return (h + 180) % 360 - 180

def go_to(x, y, speed=60):
    # where am I?
    px, py = position()
    a = math.radians(wrapped(math.degrees(math.atan2(x - px, y - py)) - heading()))
    # forward, sideways, rotation: -100 to 100 each, until the next command
    drive(speed * math.cos(a), speed * math.sin(a), wrapped(0 - heading()) * 3)

def near(x, y, cm=4):
    # where am I?
    px, py = position()
    return math.hypot(x - px, y - py) < cm
def ask(question, seconds=1.0):
    # send a question and return the first reply that arrives within `seconds`, or None
    send(question)
    for tick in range(int(seconds * 10)):
        # pause 0.1 s (the robot keeps doing what it was told)
        wait(0.1)
        for sender, text in messages():
            return text
    return None

def numbers_in(text):
    # every number in a message, as floats: "at 25,55" -> [25.0, 55.0]
    out = []
    for word in text.replace(",", " ").split():
        try:
            out.append(float(word))
        except ValueError:
            pass
    return out
reply = ask('where are you?')
print('scout says:', reply)

Plan your program here, then type it in and press Run.

QR code
Do it on the robot
www.bugbotlab.com/learn/9-4-asking/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Ask again once you arrive and print how far off the first answer was.
  2. End facing the Scout: turn to the bearing of (sx, sy) from where you stopped.
  3. Print no answer and stay put if the Scout does not reply within a second.