Gyro Driving & Turns
Encoders answer "how far", the gyro answers "which way". Combine both and the robot can navigate precisely across a 2-meter mat.
Why straight isn't straight
Two identical-looking motors always differ slightly; add wheel slip, mat bumps and a shaky launch, and "straight" drifts further every centimeter. Drift 2° over 1 m and you're 3.5 cm off at the end - enough to miss a model.
Fix: use the gyro's yaw to correct continuously - drift is pulled back the moment it appears.
Gyro straight (P control)
Same P control as line following, with reflected light swapped for yaw:
define gyroStraight (cm)
reset yaw angle [0]
set [Kp] to [2]
motor [A] reset degrees counted [0] ← use the encoder for distance
repeat until <(motor [A] degrees counted) > ((cm) * [degPerCm])>
start moving [steering ((0 - (yaw angle)) * (Kp))]
stop moving- Target yaw is 0, error = 0 − yaw. Robot drifts right (yaw goes positive) → negative steering → corrects left.
degPerCm= 360 / wheel circumference in cm; precompute into a variable.- Tune Kp from 2: snaking → lower it; slow to re-center → raise it.
This block drives FORWARD only
Pass a negative distance and the loop condition "encoder > negative target" is true immediately - the block exits without moving, and nothing reverses the direction. To drive home backwards use a plain move [backward] [x] cm (the return trip rarely needs gyro precision); if you truly need gyro-corrected reversing, build a sign-flipped variant.
Drive toward any heading
Drop the "reset yaw" and add a targetAngle parameter, error = target − yaw. After a turn you drive on the new heading without resetting, so heading errors don't stack up between segments (gyro drift and wheel slip still apply - see "Managing drift" below).
Gyro turns
The hub sensors page showed the basic version; here's what matters:
define turn (angle)
reset yaw angle [0]
if <(angle) > [0]> then
start moving [steering 100]
wait until <yaw angle > ((angle) - [lead])>
else
start moving [steering -100]
wait until <yaw angle < ((angle) + [lead])>
stop movingThree precision tricks
- Lead value: momentum adds 1-3° after the stop. Measure: command 90°, measure actual, subtract the difference. Faster turns need bigger leads.
- Slow turns: set speed to ~25% before turning - less momentum, better repeatability. Restore after.
- Two-stage turns (for high precision): full speed until 15° short of the target, then creep at 10% to the final degree. The slow stage has almost no momentum; the lead shrinks to 0-1°.
define preciseRightTurn (angle)
reset yaw angle [0]
start moving [steering 100]
wait until <yaw angle > ((angle) - [15])>
set movement speed [10] %
start moving [steering 100]
wait until <yaw angle > ((angle) - [1])>
stop moving
set movement speed [50] %Spin vs pivot
| Turn | steering | Character |
|---|---|---|
| Spin | ±100 | Rotates about the midpoint between wheels; smallest footprint; the default |
| Pivot | ±50 | Rotates around one wheel; radius = track width; good for swinging around a model |
Both become angle-parameterized My Blocks.
Managing drift
The gyro drifts (readings creep at standstill), and long programs accumulate error:
- Square the robot in base, then start the program (the heading at start becomes the 0 reference).
- Mid-program, exploit wall contact (see FLL Techniques) to reset yaw and wipe accumulated error.
- Start programs with the hub stationary; launching while moving corrupts the initial reference.
Exercises
- Measure your robot's turn lead: command 90°, repeat 5 times, average the actual angle.
- Build a two-parameter
gyroStraight (cm) (targetAngle)My Block, run "straight 50 → right 90 → straight 30" and compare end-position repeatability against the non-gyro version.
FAQ
Why is every gyro turn off by a few degrees?
Momentum carries the robot 1-3 degrees past the stop command. Measure your lead value and subtract it from the target, or use a two-stage turn: fast until 15 degrees short, then creep at 10% speed.
The robot is standing still - why does yaw keep changing?
Gyros drift; readings creep even at rest. Reset the yaw reference before every launch, and use wall contact mid-run to wipe accumulated error.
Why does my Python gyro turn never finish?
SPIKE App 3's raw yaw is left-turn-positive, the opposite of Word Blocks. A wait condition with the wrong sign waits forever - normalize with a negating yaw() helper (see the Python track).
Next lesson: FLL Techniques - assembling everything into scoring runs.