Sensing · Robot club · about 20 min
64 readings, 8 columns: seeing left and right, not just ahead.
[1 mark]The grid is 8 rows of 8, and a reading is at grid[row * 8 + col]. At what position in the list is row 2, column 5?
[1 mark]Which rows of the grid look level, straight ahead?
[1 mark]Why are the bottom rows of the grid always close readings?
[1 mark]Which direction does column 0 look?
[1 mark]What does this program print?
row = [30, 90, 60, 20]
line = ""
for d in row:
line += "#" if d < 40 else ("+" if d < 80 else ".")
print(line)#.+#
Under 40 is #, under 80 is +, and anything further is .. 30 and 20 are close, 60 is medium and 90 is far.
[1 mark]What does this program print?
left_side = 45
right_side = 80
if left_side < right_side:
print("more room on the right")
else:
print("more room on the left")more room on the right
The left reading is smaller, so things are closer on the left, and there is more room on the right.
[1 mark]The level-row readings for columns 0 to 7 are 35, 38, 90, 120, 110, 40, 33, 30. Which way is the way through?
There are two blocks ahead, one left and one right, with a gap between. Without moving, print clear: column <n> for the column with the largest reading in the level rows: that is the way through.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# 64 distances, 8 rows of 8
grid = tof_grid()
best_col = 0
best = 0
# find the column whose reading in the level rows is the largest
print("clear: column", best_col)The hint students can ask for: Without moving, look at tof_grid() and print the column (0 to 7) with the largest reading: that is the way through.
from bugbot import *
connect()
grid = tof_grid()
best_col = 0
best = 0
for col in range(8):
d = min(grid[row * 8 + col] for row in (2, 3)) # the level rows
if d > best:
best = d
best_col = col
print('clear: column', best_col)
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.