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.
In the exam
Robot lawn mowers and robot vacuum cleaners appear in IGCSE Computer Science (0478) questions on robotics. A robot has:
- a mechanical structure (wheels, body, the blade),
- sensors to get data about its surroundings (your distance sensor finds the hedge),
- a microprocessor that runs the program and makes decisions (your
ifandwhile), - actuators that move it (the motors in
drive_forandturn_for), - and it is programmable: you can give it a new program for a new lawn.
Past paper 0478/11/M/J/24 Q8(c): a farm robot plants seeds, stops at a fence, then turns and carries on. Explain how it uses sensors and a microprocessor to know it has reached the fence. Answer like your mower: a (proximity/ultrasonic) sensor sends data to the microprocessor; it compares the distance with a stored value; if the distance is at or below it, the fence is reached, so it sends signals to the motors to stop and turn; this repeats.
Kesulitan 3/5 · 200 koin + bonus kecil pada penyelesaian pertama hari ini
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.
Klik Jalankan untuk melihat output di sini.
Kesulitan 5/5 · 400 koin + bonus kecil pada penyelesaian pertama hari ini
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!.
Klik Jalankan untuk melihat output di sini.