Talking to each other · Robot club · about 20 min
A question and a reply, with a timeout; going where you are told.
[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")[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?
[1 mark]Before two robots can share positions, what must they agree?
[1 mark]You drove to where the Scout said it was, but the Scout had moved. What should your program have done?
[1 mark]In the Scout task, a student writes others() to find the Scout. What happens?
others() exists only in games, so in a task you have to ask by radiowhere are you? for you[1 mark]What does this program print?
import math sx, sy = 30, 40 print(round(math.hypot(sx - 0, (sy - 13) - 0)))
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.
no answer and stay put if the Scout does not reply within a second.