Today’s mission 5 min
Move a motor to exactly the angle you asked for, and make it stay there when something pushes it.
Fix a long beam to the medium motor on port C as a pointer. Every WRO attachment — a gripper, a lift, a pusher — is a motor going to an angle and holding it. Today you learn the four ways Pybricks moves a motor, and the three ways it stops.
The idea — speed, angle, then how to stop 10 min
Every Pybricks motor command has the same shape: how fast, how far, then what to do at the end.
run_angle(speed, angle)— turn this many degrees from where you are now.run_target(speed, target)— go to this angle on the motor’s own scale, wherever you start from.run_time(speed, time)— run for this many milliseconds.run(speed)— start and keep going until you saystop(),brake()orhold().
Speed is in degrees per second. 360 deg/s is one full turn every second. The SPIKE medium motor manages about 1000 deg/s with nothing on it.
The end of a move is chosen with then=:
Stop.HOLD— the default. The motor keeps pushing back to stay exactly on the angle.Stop.BRAKE— resists a little, then gives way.Stop.COAST— lets go. The motor spins freely.
angle() tells you where the motor is now, and reset_angle(0) calls the current position zero.
Blocks vs Python 15 min
SPIKE word blocks
when program starts :: events hat (C v) set speed to (50) % :: motors (C v) run [↻ v] for (90) [degrees v] :: motors wait (0.5) seconds (C v) run [↺ v] for (90) [degrees v] :: motors (C v) go [shortest path v] to position (0) :: motors
Pybricks Python
from pybricks.pupdevices import Motor from pybricks.parameters import Port from pybricks.tools import wait arm = Motor(Port.C) arm.run_angle(500, 90) wait(500) arm.run_angle(500, -90) arm.run_target(300, 0) print("arm is at", arm.angle())
| Block | Python | Watch out |
|---|---|---|
| (port in every block) | arm = Motor(Port.C) | Name the motor once. Then use the name, never the port. |
| set speed to 50 % | 500 in each command | Percent vs degrees per second. There is no percent in Pybricks. |
| run ↻ for 90 degrees | arm.run_angle(500, 90) | Speed first, then angle. Swap them and it still runs — wrongly. |
| run counterclockwise | a negative angle: -90 | Direction is the sign of the number. |
| go shortest path to position 0 | arm.run_target(300, 0) | Pybricks counts past 360: 720 means two turns, not zero. |
| for 1 rotations | run_angle(500, 360) | Pybricks has no rotations. Multiply by 360. |
SPIKE word blocks
when program starts :: events hat (C v) start motor [↻ v] :: motors wait (2) seconds (C v) stop motor :: motors
Pybricks Python
from pybricks.pupdevices import Motor from pybricks.parameters import Port, Stop from pybricks.tools import wait arm = Motor(Port.C) arm.run(400) wait(2000) arm.hold() arm.run_time(400, 2000, then=Stop.COAST)
| Block | Python | Watch out |
|---|---|---|
| start motor | arm.run(400) | Keeps going until told to stop. |
| stop motor | arm.stop() / brake() / hold() | Pybricks makes you choose. stop() lets go; hold() stays put. |
| (no single block) | arm.run_time(400, 2000) | Time is milliseconds: 2000 = 2 seconds. |
Write it from scratch 20 min
New file: pointer. Before you start, turn the pointer by hand so it points straight up.
- Name the motor and print its angle. Two lines after the imports:
arm = Motor(Port.C)andprint(arm.angle()). Turn the pointer by hand and run again. The number follows your hand. - Make “up” zero. Add
arm.reset_angle(0)with the pointer up. Now 0 means “up” for the rest of the program. - Three named positions. Go to 0, then 90, then -90, then 0, with
run_target. Print the angle after each move. - Store the positions in variables. A WRO program never says
run_target(500, 90). It says what 90 means.
Positions with names
UP = 0 RIGHT = 90 LEFT = -90 arm.reset_angle(0) for target in [RIGHT, LEFT, UP]: arm.run_target(500, target) print("asked", target, "got", arm.angle()) wait(300)
Names in capitals, like UP, are a Python habit for numbers that never change while the program runs.
Test and measure 15 min
- The push test. Run
arm.run_target(500, 90), thenwait(5000). Push the pointer while it waits. Repeat withthen=Stop.BRAKEandthen=Stop.COAST. Write one word for how each felt. - The accuracy test. Run
arm.run_angle(800, 360)ten times in a loop and printarm.angle()each time. How far from 360, 720, 1080… does it end up? - The speed test. Try speeds 100, 500, 1000 and 2000. Time each turn by eye. Above what speed does it stop getting faster?
- Direction. Plug a large motor into port A. Run
Motor(Port.A).run_angle(300, 360), thenMotor(Port.A, Direction.COUNTERCLOCKWISE).run_angle(300, 360). ImportDirectionfirst. Next lesson, the left wheel needs exactly this.
Break it, fix it 10 min
One mistake in each. The third one gives no error — the robot just does the wrong thing. Those are the bugs you will meet most at a competition.
Bug 1
from pybricks.pupdevices import Motor from pybricks.parameters import Port arm = Motor(Port.c) arm.run_angle(500, 90)
Bug 2
from pybricks.pupdevices import Motor arm = Motor(Port.C) arm.run_angle(500, 90)
Bug 3 — no error, wrong move
from pybricks.pupdevices import Motor from pybricks.parameters import Port arm = Motor(Port.C) # lift the arm a quarter turn, quickly arm.run_angle(90, 500)
When there is no error, compare what you asked with what happened. Print the numbers. A quarter turn is 90 degrees; if the motor turned more than once, a number is in the wrong place.
Solo challenge — no coach 10 min
No coach, no help. At WRO your coach may not touch the robot or the code. Close this page. You may use your own earlier programs and the Pybricks documentation, nothing else.
The signal arm. Put a pointer on port D too. Write signals, which:
- sets both pointers to 0 with them pointing up;
- waves the C pointer between 45 and -45 three times, fast, then returns it to 0;
- moves D to 90 slowly and holds it there for 3 seconds;
- lets D go limp at the end, so it can be turned by hand;
- prints both angles after every move.
Done means: both pointers finish within 5 degrees of where they were asked to be, and the printed numbers prove it.
Engineering journal 5 min
- the difference between
run_angleandrun_target, in one sentence each - your push-test words for HOLD, BRAKE and COAST
- your accuracy-test numbers, and the worst error
- the fastest useful speed of the medium motor
- which stop mode you will use for attachments, and why