Skip to content
English

Movement

The robot has two wheels, each with its own motor: left wheel motor and right wheel motor on the starter robot. You drive it by setting the speed of each wheel:

Left wheel Right wheel The robot…
Forward Forward Drives forward
Backward Backward Drives backward
Backward Forward Spins to the left
Forward Backward Spins to the right
Fast Slow Curves to the right

The robot driving forward, driving backward, spinning to the left, spinning to the right and curving to the right, with an arrow for the speed of each wheel

Speeds are in radians per second, up to 6.28 (one turn of the wheel per second). Negative speeds turn the wheel backward.

  1. Get each motor by its name.
  2. Set its position to infinity, so that it turns without limit.
  3. Set its velocity.

The motors keep the last speed you set, so the robot keeps moving while your program does other things. This program drives forward, backward, and spins to the left, for one second each:

from controller import Robot
robot = Robot()
timeStep = int(robot.getBasicTimeStep())
maxSpeed = 6.28 # The maximum speed of the wheels, in radians per second
leftWheel = robot.getDevice("left wheel motor")
rightWheel = robot.getDevice("right wheel motor")
leftWheel.setPosition(float("inf"))
rightWheel.setPosition(float("inf"))
def drive(left, right, seconds):
# Set the speed of each wheel, and keep it for some time
leftWheel.setVelocity(left)
rightWheel.setVelocity(right)
start = robot.getTime()
while robot.step(timeStep) != -1:
if robot.getTime() - start >= seconds:
break
drive(maxSpeed, maxSpeed, 1.0) # Forward
drive(-maxSpeed, -maxSpeed, 1.0) # Backward
drive(-2.0, 2.0, 1.0) # Spin to the left
drive(0, 0, 0.1) # Stop
print("Done")

Each motor has a position sensor, also called an encoder, which counts how far the wheel has turned, in radians. It is useful to drive a fixed distance. Turning backward makes the count go down.

from controller import Robot
robot = Robot()
timeStep = int(robot.getBasicTimeStep())
leftWheel = robot.getDevice("left wheel motor")
rightWheel = robot.getDevice("right wheel motor")
leftWheel.setPosition(float("inf"))
rightWheel.setPosition(float("inf"))
# Each wheel has a position sensor, which counts how far it has turned
leftEncoder = leftWheel.getPositionSensor()
rightEncoder = rightWheel.getPositionSensor()
leftEncoder.enable(timeStep)
rightEncoder.enable(timeStep)
robot.step(timeStep) # Step once, so that the sensors have a value
start = leftEncoder.getValue()
leftWheel.setVelocity(5.0)
rightWheel.setVelocity(5.0)
while robot.step(timeStep) != -1:
turned = leftEncoder.getValue() - start
if turned > 10.0: # Stop after 10 radians
break
leftWheel.setVelocity(0)
rightWheel.setVelocity(0)
print("Left wheel:", round(leftEncoder.getValue() - start, 2), "rad")

Timing a move is simple, but the result changes with the speed. Encoders measure the move itself, so they are more reliable. For precise turns, the gyro is better still.

Next, learn to read the sensors.