Gyro Driving & Turns
Encoders answer "how far", the gyro answers "which way". Combine both and the robot holds position 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.
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 errors never accumulate.
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.
Next lesson: FLL Techniques - assembling everything into scoring runs.