Line Following

SPIKE Prime Robotics Camp — Day 5

 

 

 

 

Teach your robot to follow a line

The Camp

  1. First Drive
  2. Maze Runner
  3. Gyro Precision
  4. Obstacle Avoidance
  5. Line Following + Showcase (today!)

The last day — and the big showcase.

Today's Mission

Teach your robot to:

  • Read reflected light
  • Follow the edge of a line
  • Steer by proportional control

Then demo your best run of the week!

Rule of the Week

Bugs are sensor data.

The robot did exactly what the code said.
What did the code actually say?

Debug the Dance

A move sequence has one deliberate bug.

"Step left… clap… step left… spin." Wait — should that be step right?

Find it and fix it — that's reading the steps like the robot does.

Catch-Up: Sensors & Decisions

Yesterday the robot sensed the world and decided what to do.


if sensor.distance() < 200:
    robot.straight(-100)
    robot.turn(90)
					

Today: turn a reading into smooth steering.

The Color Sensor

It can read how much light bounces back.

Reflection


line_sensor = ColorSensor(Port.D)
print(line_sensor.reflection())   # 0 to 100
					

Black tape reflects little.
White floor reflects a lot.

Needs Contrast

Black electrical tape on white poster board or a light floor.

No contrast → the robot can't tell line from floor.

Calibrate the Readings

Measure on your tape and your floor.

BLACK, WHITE, threshold


BLACK = 10    # measured on the tape
WHITE = 80    # measured on the floor
threshold = (BLACK + WHITE) / 2
					

The threshold is the midpoint between them.

Follow the EDGE

The robot rides the edge of the line —
half on black, half on white.

That's why a little wobble is normal.

Proportional Control

Steer in proportion to how far off you are.

A little off → steer a little.
Way off → steer hard.

Deviation


deviation = line_sensor.reflection() - threshold
					

How far the reading is from the edge.
Positive one way, negative the other.

Steer By It


while True:
    deviation = line_sensor.reflection() - threshold
    robot.drive(DRIVE_SPEED, GAIN * deviation)
    wait(10)
					

drive(speed, turn_rate) drives continuously.

Tune the GAIN

GAIN decides how hard it steers per unit of deviation.

Too Low vs. Too High

  • Too low → sloppy, drifts off the line
  • Too high → wobbles back and forth
  • Just right → smooth follow

Change only GAIN and watch. That's the lesson.

Energizer + Snack

Get up, move, refuel.

Keep food away from the kits and laptops!

Build It

Calibrate, follow, then tune the gain.

The Loop That Matters

  1. Measure BLACK and WHITE
  2. Set the threshold
  3. Run and watch
  4. Tune GAIN, repeat

"Smooth-ish" is a win!

Showcase!

This is the camp finale.

Each team demos their best run of the week
and explains one thing they tuned.

What We Built This Week

  1. First Drive — distance & angle
  2. Maze Runner — named moves (functions)
  3. Gyro Precision — an inner compass
  4. Obstacle Avoidance — sense & decide
  5. Line Following — proportional control

What We Learned Today

  • Reflection is light bounced back (0–100)
  • Threshold = midpoint of BLACK and WHITE
  • Deviation = how far off the edge
  • Proportional control: steer by the deviation
  • Tune GAIN: sloppy ↔ wobbly ↔ just right
Questions?

Amazing work this week!