Your first program
The two lines at the top, drive, wait, stop, print, and the rule that stops the robot on its own.
Do this lesson in the simulatorWelcome to Robot club. There is no exam here and nothing to revise: you write a few lines of Python, the robot in the page does what you said, and you carry on from there.
This module is the Python you need to start, and nothing more. If you want the whole language properly, the GCSE course next door teaches it from the beginning.
New to the site? How it all works is a ten-minute tour of the lessons, the tasks, the competitions and every button on the screen.
The two lines at the top
Every program here starts the same way.
# the commands the robot understands
from bugbot import *
# the robot itself, ready to take them
connect()
print("hello from the robot")
from bugbot import * brings in the commands. connect() gets you the robot. print() writes a line under the program, which is how the robot talks back to you.
Press Run. The robot does nothing yet, but the line appears.
Make it move
from bugbot import *
connect()
# the light on top, so you can see when it is working
led("green")
# start driving forward at half speed
forward(50)
# let it run for two seconds
wait(2)
# motors off
stop()
led("off")
Three commands do the work here:
forward(50)starts the robot going at 50 % speed and returns straight away. It does not wait.wait(2)lets two seconds pass while the robot carries on doing the last thing it was told.stop()switches the motors off.
Try forward(20), then forward(90). Same two seconds, very different distances.
It stops on its own
Take out the last two lines and run it again: nothing ever tells the robot to stop.
from bugbot import *
connect()
forward(60)
wait(1.5)
It still stopped, about half a second after the program ended. If the robot hears nothing from your program for half a second, it stops by itself, so a program that crashes cannot drive the robot into a wall. wait() counts as hearing from the program.
The other directions
from bugbot import *
connect()
# forwards
forward(60)
wait(1)
# backwards
backward(60)
wait(1)
# sideways, without turning to face that way
right(60)
wait(1)
left(60)
wait(1)
stop()
print("ended up at", position())
BugBot has no wheels. Four little motors shake its feet, and mixing them slides it in any direction, which is why it can go sideways without turning first.
position() tells you where the robot thinks it is, counting from where it started. It will not be exactly (0, 0). Driving by time is never exact, and the robot leans a little as it goes. Module 2 is about fixing that with the sensors.
Task: cross the line
Drive from the start into the green zone and stop inside it, using forward, wait and stop.
from bugbot import *
connect()
forward(50)
wait(1)
stop()
If it stops short, wait longer or drive faster.
Challenges
- Make the light red while the robot drives and green when it stops.
- Drive there sideways instead, with
right()orleft(). - Print
position()before and after the drive, and work out how far 1 second at speed 50 takes you.