Lesson 17: Lawn mower
Cut the grass
- Robot lawn mowers cut the grass in stripes: up the lawn, move over a little, back down, and again.
- Here the pen is the blade: every line the robot draws is grass it has cut.
- The lawns have hedges round them and different sizes, so the robot must use its distance sensor to know where to turn and when to stop.
Words for this lesson
| Word | Meaning |
|---|---|
| lawn | an area of short grass |
| hedge | a line of bushes round a garden; here a wall |
| stripe | one long line of cut grass |
| break | leave a loop at once |
| while True | a loop that runs until a break |
The pattern
| Stripe | Drive | At the hedge, turn | Then |
|---|---|---|---|
| 1 | up | right | 200 mm, right again |
| 2 | down | left | 200 mm, left again |
| 3 | up | right | 200 mm, right again |
| … | … | … | until there is no room: stop |
- The turns go right, left, right, left. One variable,
side, keeps which way to turn next. - 200 mm between stripes: a lawn 800 mm wide gets 4 stripes, a lawn 1400 mm wide gets 7.
Swap two values
from vexcode_vr import *
def main():
side = RIGHT
for stripe in range(4):
if side == RIGHT:
brain.print("right")
side = LEFT
else:
brain.print("left")
side = RIGHT
brain.new_line()
vr_thread(main)
- The brain prints right, left, right, left: each time round,
sidechanges to the other value.
Сложность 3/5 · 200 монет + небольшой бонус за первое завершение сегодня
The robot is a lawn mower, and the pen is its blade. The starter mows one stripe up to the hedge. Then move over: turn right, drive 200 mm, turn right again, and mow back down to the other hedge. The lawns have different lengths, so use the distance sensor both ways.
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 5/5 · 400 монет + небольшой бонус за первое завершение сегодня
Now mow the whole lawn, stripe after stripe. The turns change side each time: right at the top, left at the bottom. Keep the side in the variable side. After you turn, if the side hedge is less than 200 mm away, the lawn is finished: break out of the loop and print Done!.
Нажмите Запустить, чтобы увидеть результат здесь.