Behaviours · Robot club · about 25 min
Search, approach, return: camera, servoing and states in one machine.
[1 mark]Put the rescue states in the order the robot goes through them.
Number the lines 1 to 4 to put them in the right order.
homedonesearchapproach[1 mark]Which condition moves the machine from search to approach?
[1 mark]Why does the code say if tags and tags[0][3] < 12 rather than just if tags[0][3] < 12?
tags[0] would be an error, and and stops before reading itand makes the test run faster[1 mark]What does this program print?
def step(state, tag_dist, at_home):
if state == "search" and tag_dist is not None:
return "approach"
if state == "approach" and tag_dist is not None and tag_dist < 12:
return "home"
if state == "home" and at_home:
return "done"
return state
state = "search"
for tag_dist, at_home in [(None, False), (60, False), (30, False), (11, False), (None, False), (None, True)]:
state = step(state, tag_dist, at_home)
print(state)[1 mark]How does the home state know where home is?
[1 mark]The task starter stops at the marker with break. What turns it into the full rescue?
state = "home" instead of breaking, and add an elif state == "home": branchset_cv("apriltag") again at the marker[1 mark]The rescue ends with print("home:", position(), "after", tick / 10, "seconds"). If the loop ended on tick 437, how many seconds does it print? Give the exact number.
Find marker 9, print found 9, drive to it, and return home, without touching the box.
# 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=70):
# 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=6):
# where am I?
px, py = position()
return math.hypot(x - px, y - py) < cm
def bearing_of(cx):
return (cx - 160) * 120 / 320
# camera: the tag detector
set_cv('apriltag')
state = 'search'
# do this 590 times (tick counts from 0)
for tick in range(590):
tags = [t for t in apriltags() if t[0] == 9]
if state == 'search':
if tags:
print('found 9')
state = 'approach'
else:
# spin clockwise on the spot at 40
turn_right(40)
elif state == 'approach':
if tags and tags[0][3] < 12:
# leave the loop
break
elif tags:
# forward, sideways, rotation: -100 to 100 each, until the next command
drive(60, 0, bearing_of(tags[0][1]) * 3)
else:
# spin clockwise on the spot at 30
turn_right(30)
# pause 0.1 s (the robot keeps doing what it was told)
wait(0.1)
# all motors off
stop()Plan your program here, then type it in and press Run.