Lesson 2 of 8Robotics with LEGO SPIKE
Robotics with LEGO SPIKE · Lesson 2 · 30 minutes
Driving with the DriveBase
What you'll learn
- Combine two motors into a DriveBase
- Drive precisely with
straight(),turn()andarc() - Watch movements on the side with wait=
False
The code in this lesson comes from the book “Python for Robots”, chapters 2.4 and 2.5, and runs unchanged in the simulator. German edition on Amazon ↗
Two motors as a team
A driving robot usually has two drive motors, one on the left and one on the right. So you don’t have to control each motor separately, there is the DriveBase: you just tell it “drive 20 cm straight” or “turn 90 degrees”, and it works out how each wheel has to turn.
from pybricks.hubs import PrimeHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Port, Direction
from pybricks.robotics import DriveBase
hub = PrimeHub()
# One motor per wheel, each on its own port
left_motor = Motor(Port.A, positive_direction=Direction.COUNTERCLOCKWISE)
right_motor = Motor(Port.B, positive_direction=Direction.CLOCKWISE)
# DriveBase needs the wheel diameter and the wheel spacing in millimeters
# (measure both on your robot and enter them here!)
drive_base = DriveBase(left_motor, right_motor,
wheel_diameter=56, axle_track=114)
# Drive straight: 200 millimeters (20 cm)
drive_base.straight(200)
# Turn in place: 90 degrees to the right
drive_base.turn(90)
# Drive backward
drive_base.straight(-100)The DriveBase needs two measurements in millimetres: the wheel diameter (wheel_diameter) and the distance between the wheels (axle_track). On the real robot you measure them – in the simulator 56 and 114 fit exactly.
Remember: The two motors are mounted mirrored. That’s why the left motor gets
Direction.COUNTERCLOCKWISEand the right oneDirection.CLOCKWISE.
Loops draw shapes
With a for loop four driving commands become a square. settings() sets the speed and turning rate:
from pybricks.hubs import PrimeHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Port, Direction
from pybricks.robotics import DriveBase
hub = PrimeHub()
left_motor = Motor(Port.A, positive_direction=Direction.COUNTERCLOCKWISE)
right_motor = Motor(Port.B, positive_direction=Direction.CLOCKWISE)
drive_base = DriveBase(left_motor, right_motor,
wheel_diameter=56, axle_track=114)
# Set speed and acceleration individually:
# (speed mm/s, turn rate degrees/s, acceleration, turn acceleration)
drive_base.settings(straight_speed=150, straight_acceleration=200,
turn_rate=100, turn_acceleration=200)
# Drive a small square
for side in range(4):
drive_base.straight(150)
drive_base.turn(90)Try the simulator speed 2× or 4× if the robot is too slow for you.
Driving curves
arc() drives a curve: first the radius in millimetres, then the angle.
from pybricks.hubs import PrimeHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Port, Direction
from pybricks.robotics import DriveBase
hub = PrimeHub()
left_motor = Motor(Port.A, positive_direction=Direction.COUNTERCLOCKWISE)
right_motor = Motor(Port.B, positive_direction=Direction.CLOCKWISE)
drive_base = DriveBase(left_motor, right_motor,
wheel_diameter=56, axle_track=114)
# arc(radius_mm, angle_deg): drives an arc with a 200mm radius, 90 degrees.
# Positive radius = arc to the right, positive angle = forward.
drive_base.arc(200, 90)Doing something on the side: wait=False
Normally your program waits until a movement is finished. With wait=False the movement only starts – and your program carries on straight away. That way you can measure while driving:
from pybricks.hubs import PrimeHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Port, Direction
from pybricks.robotics import DriveBase
from pybricks.tools import wait
hub = PrimeHub()
left_motor = Motor(Port.A, positive_direction=Direction.COUNTERCLOCKWISE)
right_motor = Motor(Port.B, positive_direction=Direction.CLOCKWISE)
drive_base = DriveBase(left_motor, right_motor,
wheel_diameter=56, axle_track=114)
# wait=False: the command starts the movement, but does NOT block.
drive_base.straight(500, wait=False)
# While the robot drives, we can monitor something else at the same time
while not drive_base.done():
distance_so_far = drive_base.distance()
print(f"Driven so far: {distance_so_far} mm")
wait(100)
print("Movement complete!")drive_base.done() is True as soon as the drive is over, and drive_base.distance() returns the distance driven so far.
Your task
Your turn
Exercise 2.4 from the book: use a for loop to drive an equilateral triangle with 200 mm sides. Tip: at every corner the robot has to turn 120 degrees (360 degrees divided by 3 corners). At the end it should be back at its starting point.
from pybricks.hubs import PrimeHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Port, Direction
from pybricks.robotics import DriveBase
hub = PrimeHub()
# One motor per wheel, each on its own port
left_motor = Motor(Port.A, positive_direction=Direction.COUNTERCLOCKWISE)
right_motor = Motor(Port.B, positive_direction=Direction.CLOCKWISE)
# DriveBase needs the wheel diameter and the wheel spacing in millimeters
# (measure both on your robot and enter them here!)
drive_base = DriveBase(left_motor, right_motor,
wheel_diameter=56, axle_track=114)
# Drive straight: 200 millimeters (20 cm)
drive_base.straight(200)
# Turn in place: 90 degrees to the right
drive_base.turn(90)
# Drive backward
drive_base.straight(-100)- Something to think about
Three corners with 120 degrees each add up to 360 degrees.
- Which command helps?
Drive the same distance three times with
drive_base.straight(200)and turn 120 degrees each time.Each side is 200 mm long – so three times
drive_base.straight(200), without reversing.Put
straight()andturn()only once inside aforloop withrange(3). - Gap code – fill in the ___
from pybricks.hubs import PrimeHub from pybricks.pupdevices import Motor from pybricks.parameters import Port, Direction from pybricks.robotics import DriveBase hub = PrimeHub() left_motor = Motor(Port.A, positive_direction=Direction.COUNTERCLOCKWISE) right_motor = Motor(Port.B, positive_direction=Direction.CLOCKWISE) drive_base = ___ for side in ___: drive_base.straight(200) drive_base.turn(___)
Solve it without hints to earn a ⭐ on your progress page.
Show solution🔒 Unlocks after all hints or 2 checks
Try it yourself first – mistakes teach you the most. Here is one possible solution:
from pybricks.hubs import PrimeHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Port, Direction
from pybricks.robotics import DriveBase
hub = PrimeHub()
left_motor = Motor(Port.A, positive_direction=Direction.COUNTERCLOCKWISE)
right_motor = Motor(Port.B, positive_direction=Direction.CLOCKWISE)
drive_base = DriveBase(left_motor, right_motor, wheel_diameter=56, axle_track=114)
for side in range(3):
drive_base.straight(200)
drive_base.turn(120)