Robotics
Challenges: who's the fastest?
Get the robot into the finish 🏁 as fast as you can. The simulator measures the time – the same on every device. “Share time” gives you a link: whoever opens it sees your code and the time re-checked. Perfect for a class competition – no sign-up needed.
Never programmed before? Start with the robotics lessons.
Sprint
Drive into the finish as fast as you can. Tip: settings() sets speed and acceleration – but careful, accelerating too hard won't get you there faster.
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)
drive_base.settings(straight_speed=200)
drive_base.straight(1100)Slalom
Through zones 1 and 2 into the finish – without getting stuck on the walls. Curves with arc() are often faster than stopping and turning.
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)
drive_base.straight(650)
drive_base.turn(90)
drive_base.straight(580)
drive_base.turn(90)
drive_base.straight(650)
drive_base.turn(-90)
drive_base.straight(450)
drive_base.turn(-90)
drive_base.straight(650)
drive_base.turn(90)
drive_base.straight(210)Lap
A whole lap on the line: via zones 1 and 2 back to the finish. The line follower from the book makes it – but can you go faster without losing the line?
from pybricks.hubs import PrimeHub
from pybricks.pupdevices import Motor, ColorSensor
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)
color_sensor = ColorSensor(Port.C)
drive_base = DriveBase(left_motor, right_motor,
wheel_diameter=56, axle_track=114)
TARGET_BRIGHTNESS = 50 # sensor value exactly at the line's edge (0-100)
KP = 1.2 # the larger, the stronger the correction
BASE_SPEED = 120 # mm/s - better to start slowly!
while True:
brightness = color_sensor.reflection()
error_value = brightness - TARGET_BRIGHTNESS # deviation from the line's edge
steering = KP * error_value # only the P component
drive_base.drive(BASE_SPEED, steering)
wait(10)Maze
Wind your way through the three walls: via zones 1 and 2 into the finish. Every turn costs time – can you find a faster line?
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)
drive_base.turn(90)
drive_base.straight(700)
drive_base.turn(-90)
drive_base.straight(450)
drive_base.turn(-90)
drive_base.straight(700)
drive_base.turn(90)
drive_base.straight(400)
drive_base.turn(90)
drive_base.straight(700)
drive_base.turn(-90)
drive_base.straight(400)Parking
Reverse into the blue parking space without hitting the cars next to it. Fast, but precise!
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)
drive_base.straight(960)
drive_base.turn(-90)
drive_base.straight(-220)Competition: Warehouse
Five missions, 60 seconds of game time, 100 points. You choose the order – you just have to pick up the parcel before you bring it to the shelf. The starter code already picks up the parcel. Can you do it all?
| Mission | Points |
|---|---|
| 1. Pick up the parcel | 20 |
| 2. Bring the parcel to the shelf (pick it up first!) | 25 |
| 3. Visit the charging station | 15 |
| 4. Checkpoint | 15 |
| P. Park in the garage at the end (after the pick-up) | 25 |
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)
# Mission 1: pick up the parcel (straight up)
drive_base.straight(600)Build your own course
Drag out obstacles and finish zones with the mouse (or your finger), set the start and test the course straight away. With “Share” your friends get exactly this course as a link.
Several finish zones are visited in order – the last one is the finish. Missions 🎯 give points, in any order, until the game time is up – that's how you build your own competition.