Lesson 24: Sorting robot
Sort the metal
- Recycling centres use robots to sort things: each kind goes into its own bin.
- The robot reads the colour with the down eye, decides which bin, and carries the disk there with its magnet.
- Sorting is a decision (
if ... else) inside a loop: look, decide, carry, come back, look again.
Words for this lesson
| Word | Meaning |
|---|---|
| sort | put things into groups |
| bin | a box or place where things are put |
| recycling | using old material again |
| abs | the size of a number without its sign: abs(-5) is 5 |
Which way?
| Down eye sees | Turn to heading | Bin |
|---|---|---|
| RED | 270 (left) | red bin |
| BLUE | 90 (right) | blue bin |
turn_to_headingturns to a compass direction, wherever the robot faces now: 0 is up, 90 right, 180 down, 270 left.
Two colours in one condition
from vexcode_vr import *
def main():
colour = "blue"
if colour == "red" or colour == "blue":
brain.print("a disk!")
else:
brain.print("nothing here")
vr_thread(main)
oris true when one side is true, so the brain printsa disk!.
In the exam
Past paper 0478/11/M/J/23 Q9(a): factory robots make furniture. One characteristic of a robot is that it is programmable; state two others. Think of your sorting robot: it has a mechanical structure (wheels, body, magnet), sensors (the colour sensor, the distance sensor), and electrical components (motors, the magnet).
难度 4/5 · 今日首次完成可获 280 金币及少量额外奖励
A recycling robot sorts metal disks. The red bin runs along the left wall, the blue bin along the right wall. Drive up until the down eye sees a red or a blue disk. Then face the right bin (red: heading 270, blue: heading 90), pick the disk up, drive until the wall is 100 mm away and put it down. The starter always goes left.
点击“运行”查看此处输出。
难度 5/5 · 今日首次完成可获 400 金币及少量额外奖励
Now there are 3 or 4 disks on the middle line. Sort them all and count them: after each drop, turn round, drive back until the robot is on the middle line again (location.position(X, MM) is near 0), and face up (heading 0). At the end, print the counts like this: Red: 2, Blue: 1.
点击“运行”查看此处输出。