Robot Control Basics
This document explains the fundamentals of controlling a robot using the
robot Python library. It covers setup, timing, motor control,
and common movement patterns.
Initialisation
Import the required libraries and create a Robot instance.
The time library is used for delays and timing.
import robot
import time
R = robot.Robot()
Time
Many challenges require you to wait for a specific duration while something is happening, such as turning or driving forwards.
time.sleep(10)
The value passed to sleep is the number of seconds to wait.
Starting and stopping a timer
To measure elapsed time, use time.time() before and after
an action.
import time
time_1 = time.time()
# insert action here
time_2 = time.time()
time_passed = time_2 - time_1
print(time_passed)
Motors
Motors are controlled by setting a power percentage. Positive values move forwards; negative values move backwards.
R.motors[0] = 60
Use R.motors[1] for the second motor. To stop the robot, set
both motors to zero.
R.motors[0] = 0
R.motors[1] = 0
Turning is achieved by running one motor forwards and the other backwards.
import robot
R = robot.Robot()
R.motors[0] = 60
R.motors[1] = 68
Complete Example
The following program moves the robot forwards, backwards, left, and right for 10 seconds each.
import robot
import time
R = robot.Robot()
# Going forwards
R.motors[0] = 100
R.motors[1] = 100
time.sleep(10)
# Going backwards
R.motors[0] = -100
R.motors[1] = -100
time.sleep(10)
# Going left
R.motors[0] = -100
R.motors[1] = 100
time.sleep(10)
# Going right
R.motors[0] = 100
R.motors[1] = -100
time.sleep(10)