See as numbers
What a model gets: the sensor views as lists of numbers.
Do this lesson in the simulatorEvery lesson so far told the robot what to do. This module teaches it instead: show it examples, and let it work out the rule for itself. Before any of that, one plain fact that the whole module rests on. A model never sees a wall, a ball or a gap. It sees numbers, and nothing else. This lesson is about which numbers.
The depth grid is 64 numbers
The robot is 15 cm from a wall, facing it. You met the depth grid in lesson 2.3: eight rows of eight, each a distance in centimetres. Printed as rows it is easier to read than one long list.
# 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()
# do this 8 times (row counts from 0)
for row in range(8):
print(" ".join("%3d" % d for d in grid[row * 8: row * 8 + 8]))
This close, the wall fills the view: rows 0 to 4 read 16 or 17 in every column. Rows 5 to 7 look downwards and hit the mat before they reach the wall, so they read less. Rows 2 and 3 are the ones that look straight ahead. (A reading of 400 means nothing in range; you will see it in open space.)
The level rows
Sixteen numbers, rows 2 and 3, are what you will hand to the models in this module. Small enough to look at, big enough to tell the situations apart.
# 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()
level = grid[16:32]
print(level)
print("nearest:", min(level), "farthest:", max(level))
grid[16:32] is a slice: items 16 up to but not including 32, the third and fourth rows of eight.
Same wall, different numbers
Turn a little and read again.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
before = tof_grid()[16:32]
# spin anticlockwise on the spot at 60
turn_left(60)
# pause 0.5 s (the robot keeps doing what it was told)
wait(0.5)
# all motors off
stop()
# pause 0.3 s (the robot keeps doing what it was told)
wait(0.3)
after = tof_grid()[16:32]
print("facing the wall:", before[:8])
print("turned", round(360 - heading()), "degrees left:", after[:8])
The wall has not moved, but every number changed: the columns on one side now look along the wall and read farther. A model that has only ever seen the first list has no idea what the second one means. That is the first lesson about data: the numbers depend on exactly where the robot is and which way it faces, so you have to collect them from everywhere you expect the robot to be.
A picture is numbers too
The camera image is 320 by 240 pixels, and each pixel is three numbers, red, green and blue. That is 230,400 numbers per frame. The blob and tag detectors in Module 4 boil that down to a handful: where the thing is and how big. Choosing a small set of numbers that still tells the situations apart is most of the work in machine learning. Those numbers are called features. In this module the features are the 16 level readings.
Task: see as numbers
Print the level rows' nearest and farthest readings as depth: min <cm>, max <cm>. Do not drive.
# 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()
print(grid)
Challenges
- Print the level rows as two lines of eight.
- Print their mean, rounded to a whole number.
- Print which column (0 to 7) has the farthest reading. That is where the most room is.