Your first move
Drive, wait, stop, the LED, and the safety rule.
Do this lesson in the simulatorModules F1 to F4 taught you Python with the robot mostly standing still. This module is about the robot itself: how it moves and how you make it move well. Start with the simplest drive there is.
Drive, wait, stop
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# the LED: green
led("green")
# start driving forward at 50 % speed
forward(50)
# keep going for 2 seconds
wait(2)
# motors off
stop()
# the LED: off
led("off")
forward(50) starts the robot moving and returns straight away. The robot keeps going while your program does other things; here it waits two seconds. stop() switches the motors off.
Try forward(20), then forward(90). Same two seconds, different distance.
How BugBot moves
Look at the robot in the view. It has no wheels. Four small motors with off-centre weights vibrate its feet, and the pattern of vibration slides it across the mat. Each of the four feet pushes in a slightly different direction, and mixing them lets the robot go forward, backward, sideways, or spin, all without turning first. Lesson 1.4 is about that.
It also means no two BugBots move exactly the same, and none goes perfectly straight. Watch the trail behind the robot: it curves a little. That is not a bug in your program; it is what a vibration drive does, and later lessons show how the sensors fix it.
The safety rule
Take the last three lines out and run it: the robot is told to go, and nobody ever tells it to stop.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# drive forward at 50 (keeps going until the next command)
forward(50)
# pause 2 s (the robot keeps doing what it was told)
wait(2)
It still stopped, about half a second after the program ended. That is the motion deadman: if the robot hears nothing from your program for half a second, it stops on its own. A program that crashes cannot leave a robot running into a wall.
wait() counts as "hearing from the program", which is why the first version drove for the full two seconds. Later, when you write loops that drive, a wait(0.1) inside the loop keeps the robot informed.
Backwards
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# drive forward at 60 (keeps going until the next command)
forward(60)
# pause 1.5 s (the robot keeps doing what it was told)
wait(1.5)
# drive backward at 60
backward(60)
# pause 1.5 s (the robot keeps doing what it was told)
wait(1.5)
# all motors off
stop()
print("back where I started?", position())
position() says where the robot thinks it is compared with where it started. Not quite (0, 0): driving by time is never exact, and the drive drifts. Lesson 1.2 fixes the first problem and Module 2 the second.
Task: cross the line
Drive from the start into the green zone and stop inside it, using only forward, wait and stop.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# drive forward at 50 (keeps going until the next command)
forward(50)
# pause 2 s (the robot keeps doing what it was told)
wait(2)
# all motors off
stop()
If it stops short, wait longer or go faster. Run it twice: the two runs will not be identical.
Challenges
- Make the LED red while driving and green when stopped.
- Drive forward for 1 s, backward for 1 s, three times over, and print
position()at the end. - What is the slowest speed that still moves the robot? Try 10, 15, 20.