Vision · Robot club · about 20 min
Spin until it is in view; find and approach as functions; list comprehensions.
[1 mark]What does while not apriltags(): mean?
not apriltags() is true while the list is empty.[1 mark]What does this program print?
tags = [[2, 60, 120, 35.0], [5, 210, 118, 60.0], [8, 300, 125, 90.0]] mine = [t for t in tags if t[0] == 5] print(mine) print(len(mine))
[[5, 210, 118, 60.0]] 1
The list comprehension keeps only the tags whose id, t[0], is 5. The result is still a list, with one tag in it.
[1 mark]The spin loop ends the moment a tag appears. Why do stop() and wait(0.3) come next?
apriltags() only works when the robot is still[1 mark]What is a heuristic search?
[1 mark]The camera sees 120 degrees. How many degrees of the full circle around the robot are out of view?
[1 mark]In approach, what happens if the tag goes out of view on the way in?
turn_right(30) until the tag is backapproach turns slowly and looks again instead of giving up.[1 mark]Put the hide and seek program in order.
Number the lines 1 to 4 to put them in the right order.
`print("found 5")``set_cv("apriltag")``find(5)``approach(5, 14)``set_cv("apriltag")`
`find(5)`
`print("found 5")`
`approach(5, 14)`Switch on the detector first, then search, report, and servo in. Every camera program is search and approach in a row.
Find marker 5, print found 5, then drive up and stop about 14 cm in front of it.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# camera: the tag detector
set_cv("apriltag")
print(apriltags())The hint students can ask for: Marker 5 is somewhere around you but not in view. Spin until the camera sees it, print found 5, then drive up and stop about 14 cm in front of it.
from bugbot import *
connect()
def bearing_of(cx):
return (cx - 160) * 120 / 320 # pixels to degrees, 120 degree view across 320 pixels
def find(tag_id):
# spin until the tag is in view, then square up to it
while True:
tags = [t for t in apriltags() if t[0] == tag_id]
if tags:
break
turn_right(40)
wait(0.1)
stop()
wait(0.3)
def approach(tag_id, stop_at):
while True:
tags = [t for t in apriltags() if t[0] == tag_id]
if not tags:
turn_right(30)
wait(0.1)
continue
tag = tags[0]
cx, dist = tag[1], tag[3]
if dist <= stop_at:
break
rot = bearing_of(cx) * 3
speed = max(20, min(70, (dist - stop_at) * 3))
drive(speed, 0, rot)
wait(0.1)
stop()
set_cv('apriltag')
find(5)
print('found 5')
approach(5, 14)
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.