top of page
Search

FLL Line Squaring: Align Your Robot on a Black Line (SPIKE Prime Python)

Nov 7
5 min read

FLL line squaring is a simple trick that makes a robot's position predictable: you drive toward a black line, stop each wheel separately when its own color sensor reaches the line, and the robot ends up sitting square to it. Because the mat's lines don't move, squaring on one "resets" the small angle and distance errors that build up during a run. Below is how it works, when to use it, and a complete SPIKE Prime Python program you can adapt.

Why robots drift, and why lines fix it

Even a well-built robot is never perfect. Wheels slip a little, the robot starts a few millimeters off in base, and every turn adds a degree or two of error. Over a long run those small mistakes add up, and the robot reaches a model at a slightly wrong angle.

Gyro steering helps a lot (we covered it in Drive Straight with a Gyro), but a gyro only knows which way the robot is facing relative to where it started. It doesn't know where the robot actually is on the mat. A line on the mat is a real, fixed landmark. Squaring on it corrects both the robot's angle and its distance along the field at the same time.

What you need

  • Two color sensors mounted at the front, facing down, about 1–2 cm above the mat and spread apart (the wider, the more accurate the squaring).

  • A two-wheel drive base where each wheel has its own motor. This program assumes the left motor is on port A, the right on port B, and the sensors on ports C (left) and D (right).

  • Your own reflection readings. Hold each sensor over black and over white and note the numbers. Lighting changes between your home table and a tournament, so re-check on competition day.

How the two-phase method works

  1. Find the line. Both wheels drive forward at a moderate speed. The moment the left sensor sees dark, the left wheel stops; the same for the right. If the robot came in crooked, one wheel stops first and the other keeps going until it catches up.

  2. Settle on the edge. Phase 1 always overshoots a little. So in phase 2 each wheel uses a small proportional controller to move forward or backward until its sensor reads halfway between black and white: the edge of the line. When both sensors stay close to the edge for a short time, the robot is square.

The "edge" target matters. A sensor reading pure black could be anywhere on a thick line, but the halfway value only happens at the boundary, which makes it very repeatable.

The SPIKE Prime Python code

This uses the SPIKE App 3 Python modules (motor, color_sensor and runloop). We tested the logic in a simulator with the hardware functions stubbed out; tune the numbers on your own robot.

from hub import port
import motor, color_sensor, runloop

LEFT_MOTOR, RIGHT_MOTOR = port.A, port.B
LEFT_SENSOR, RIGHT_SENSOR = port.C, port.D
LEFT_DIR, RIGHT_DIR = -1, 1   # flip a sign if a wheel spins backward

BLACK = 15                    # measure on your own mat
WHITE = 95
EDGE = (BLACK + WHITE) // 2   # the edge of the line
KP = 4                        # degrees/sec per point of error
MAX_SPEED = 150

def drive(left_speed, right_speed):
    motor.run(LEFT_MOTOR, int(left_speed) * LEFT_DIR)
    motor.run(RIGHT_MOTOR, int(right_speed) * RIGHT_DIR)

def stop():
    motor.stop(LEFT_MOTOR)
    motor.stop(RIGHT_MOTOR)

def clamp(value, limit):
    return max(-limit, min(limit, value))

async def square_on_line(speed=200, timeout_ms=3000):
    # Phase 1: drive forward; each wheel stops when its sensor finds black
    left_done = right_done = False
    elapsed = 0
    while not (left_done and right_done) and elapsed < timeout_ms:
        if color_sensor.reflection(LEFT_SENSOR) < EDGE:
            left_done = True
        if color_sensor.reflection(RIGHT_SENSOR) < EDGE:
            right_done = True
        drive(0 if left_done else speed, 0 if right_done else speed)
        await runloop.sleep_ms(10)
        elapsed += 10
    stop()

    # Phase 2: nudge each wheel until its sensor sits on the edge
    settled = 0
    elapsed = 0
    while settled < 10 and elapsed < 1500:
        left_error = color_sensor.reflection(LEFT_SENSOR) - EDGE
        right_error = color_sensor.reflection(RIGHT_SENSOR) - EDGE
        drive(clamp(KP * left_error, MAX_SPEED), clamp(KP * right_error, MAX_SPEED))
        if abs(left_error) < 5 and abs(right_error) < 5:
            settled += 1
        else:
            settled = 0
        await runloop.sleep_ms(10)
        elapsed += 10
    stop()

async def main():
    await square_on_line()

runloop.run(main())

Tuning tips

  • Wheel spins the wrong way? Flip the sign of LEFT_DIR or RIGHT_DIR. On most drive bases one motor is mounted mirrored, so one of them is -1.

  • Robot shakes back and forth in phase 2? Lower KP or MAX_SPEED. If it creeps too slowly, raise KP a little.

  • Approach speed. Faster phase-1 speeds overshoot more. Around 150–250 degrees per second is a good starting range.

  • Always keep the timeouts. If the robot misses the line, the timeout stops it instead of letting it drive off the table.

  • Approach at a sensible angle. Squaring fixes small errors, not big ones. Come at the line within roughly 20–30 degrees of square.

Using squaring in a real FLL run

Teams usually square at a few key moments: right after leaving base, before a mission that needs precise alignment, and after a long drive or several turns. A common pattern is: gyro-drive toward a line, square on it, then use encoder or gyro moves for the last short distance. Our guide on gyro turns vs encoder turns pairs well with this.

Squaring also makes runs easier to plan. When you know the robot will always start the next part of the run from the same line, you can break a long route into short, reliable pieces. Our post on FLL robot game strategy covers how to plan runs around landmarks like this.

Keep it honest in your engineering notebook: record the reflection values you measured, the KP you settled on, and how much more consistent your runs became. Judges love seeing a team test, measure and improve.

Common questions

Can I square on a line with only one color sensor?

Not truly. With one sensor you can line-follow or stop at a line, but you can't measure the robot's angle to it. Some teams square against a wall instead, by driving gently backward into it.

Is this still useful now that SPIKE has been retired?

Yes. LEGO Education retired SPIKE in June 2026, but SPIKE robots can still be used in the 2026–27 BIOGLOW season, and FIRST has said its next K–8 programs stay compatible with SPIKE through 2028–29. The idea itself (two sensors, stop each wheel at a landmark) works on any robot.

Why does my robot square well at home but not at the tournament?

Usually lighting. Re-measure black and white on the competition table during practice time and update BLACK and WHITE before your matches.

Learn this with Fusion Robotics Education

Sensors, proportional control and reliable autonomous code are exactly what our courses teach step by step. Start with the LEGO SPIKE Prime course (grades 4–8) or the free FLL Competition-Ready course, and browse the full catalog of online courses.

  • Free self-paced courses to get started.

  • All-Access for $39/month, with a 7-day free trial.

  • Remote 1-on-1 live lessons with a coach for $149/month (text (619) 786-5731).

In San Diego? Our weekend group sessions in Scripps Ranch are 1.5 hours a week for ages 6–18, with at most 10 kids and every child working on their own project at their own pace. Your first session is free: text (619) 786-5731 to book it.

 
 
 

Comments


bottom of page