Skip to main content

Lesson 2-5 · Proportional Control

Module 2 · Line Tracking

Lesson 2-5 · Proportional Control

60+ minControl loopsFirst real line followingSlides (PowerPoint)
👩‍🏫

Teacher mode is on. Toggle it off (bottom-right) to preview the student view.

Bouncing kept the robot inside the circle, but it never actually followed the line. This lesson is the real thing: the robot smoothly tracks the edge of the line by making tiny, constant steering adjustments — the same way you'd steer a car around a gentle curve. It's your first control loop, and one of the most important ideas in the whole course.

Learning Objectives

By the end of this lesson you will be able to:

  • Explain a setpoint and why the line's edge (~0.5) is ours
  • Compute error as setpoint − sensor_reading, and read its sign
  • Turn error into a steering correction with a gain Kp
  • Write get_error() and the control loop from the two formulas and arcade(), in your robot.py
  • Tune Kp by watching the robot and adjusting

Steer while you drive: arcade()​

Bounce driving reacted after hitting the line. Following means constantly adjusting so you never leave it — like nudging the wheel around a curve instead of stopping to turn.

For that you need to steer without stopping. arcade(speed, turn) does exactly this — it sets a forward speed and a steering amount in one non-blocking call:

drivetrain.arcade(speed, turn)
# works like:
drivetrain.set_effort(speed - turn, speed + turn)
# then XRPLib scales both so the faster wheel gets the larger of speed and turn

So arcade(0.3, 0.15) slows the left wheel and speeds up the right, and the robot curves left, counterclockwise. A positive turn steers left, a negative one steers right, and zero drives straight. (It's the same rule as Lesson 1-6's Arcade block, where a positive Turn curved left.)

Knowledge Check

What does `arcade(0.3, -0.15)` do?

Setpoint and error​

The setpoint is the reading we want the sensor to hold. Following the edge of the line, that's about 0.5 — halfway between white and black. Error is how far off we are:

error = setpoint − sensor_reading

The sign tells us which way we've drifted; the size tells us how far:

Where the robot isreadingerror = 0.5 − readingMeaning
On the edge (perfect)0.50.0no correction
Drifted onto white0.2+0.3steer toward the line
Drifted onto black0.8−0.3steer back off the line
Far onto white0.1+0.4steer hard toward the line
Three close-ups of the XRP's front following the left edge of a black line with its left sensor. Drifted onto white: the left sensor reads 0.2, error = 0.5 − 0.2 = +0.3, steer right, toward the line. On the edge: it reads 0.5, error 0.0, drive straight. Drifted onto black: it reads 0.8, error = 0.5 − 0.8 = −0.3, steer left, back off the line.
Positive error steers toward the line, negative steers back off it. The sign picks the direction, so the loop needs no if/else.
Knowledge Check

Setpoint is 0.5 and the sensor reads 0.2. What is the error?

From error to steering: the gain Kp​

We don't feed raw error to the motors — we scale it by a proportional gain, Kp, to set how strongly the robot reacts:

correction = error × Kp

With Kp = 0.5, an error of +0.3 gives a correction of +0.15. Then we steer with that correction, with a minus sign in front of it:

drivetrain.arcade(BASE_EFFORT, -correction)

Why the minus? A positive error means the robot drifted onto white, so it has to steer right, back toward the line. In arcade() a positive turn steers left. Negating the correction lines the math up with the robot.

Bigger error → bigger correction → sharper steer. When the robot is right on the edge (error 0), the correction is 0 and it drives straight. Notice there's no if/else — the math handles left vs. right on its own, through the sign.

Knowledge Check

If Kp = 0.6 and error = 0.3, what is the correction?

This lesson's additions to robot.py​

Every pass of the control loop: read the sensor, compute error, compute correction, steer. Hundreds of times a second. Two of those steps become part of your file.

In the LINE SENSOR section, the error becomes a function. It's a sensor question — how far off the edge am I? — so it lives with the sensor functions, and the setpoint is its setting. SETPOINT goes under THRESHOLD; get_error() goes under get_right():

SETPOINT = 0.5 # the reading at the edge of the line

def get_error():
# return SETPOINT minus the left reading
pass

A new section starts here, for everything that drives on the line. Paste it above RUN. The test is the exercise: the loop's outline is inside it.

# ===== LINE TRACKING =====
# Drives the robot along the line using the LINE SENSOR
# functions above. Starts with the proportional follower: read
# the error, scale it by KP, steer with arcade(). Grows over
# the module into following to a cross, parking on it, and
# turning to the next line.
BASE_EFFORT = 0.3 # forward speed while following
KP = 0.5 # proportional gain -- tune this!

def test_follow():
# loop forever:
# error = get_error()
# correction = error times KP
# steer: arcade() with BASE_EFFORT and minus the correction
# time.sleep(0.01), so the loop doesn't flood the
# processor
pass

Three lines in the loop and a pause — and no if/else anywhere. The sign of the error does the deciding. You have every piece: get_error() is one subtraction, and arcade() was the first thing in this lesson.

Activity · Follow the line, then tune it​

Step 1 · Write the loop and run it​

Write get_error() and turn the comments in test_follow() into code; set the run line to test_follow(). Put the robot so the left sensor sits on the edge of the line, press the button, and watch. It should hold the edge, nudging left and right as the line curves. If it drives straight off, check the sign of your error calculation against the table above, and the minus sign in front of the correction; if it spins in place, check the order of the two arguments to arcade().

Step 2 · Tune Kp​

Kp is yours to tune by watching the robot. Run the test three times — with KP at 0.2, at 0.5 and at 1.0 — and write down, for each, what the robot does on a curve. Then decide: which of the three is too high, which too low, and what does each one look like?

Knowledge Check

Your robot follows the line but weaves back and forth rapidly, never settling. What should you do to Kp?

Watch the numbers while you tune​

Tuning by eye tells you that something is wrong. Printing tells you what. The problem is that your control loop runs hundreds of times a second, so printing every pass produces a waterfall nobody can read. Print every 50th pass instead.

"Every 50th" needs one arithmetic operator you haven't used yet. % is the remainder operator: a % b is what's left over after dividing a by b as whole numbers. (It has nothing to do with percent — the symbol is the same, the meaning isn't.)

7 % 3 # 1 (3 goes into 7 twice, remainder 1)
10 % 5 # 0 (5 goes into 10 exactly)
50 % 50 # 0
51 % 50 # 1
100 % 50 # 0
149 % 50 # 49
150 % 50 # 0

Look at the last five. The remainder is 0 exactly when the number is a multiple of 50 — so i % 50 == 0 is a test that is true on pass 0, pass 50, pass 100, and false on every pass in between. Put that in an if around the print, and you get one line out of every fifty.

Knowledge Check

What is 120 % 50, and would a print inside `if i % 50 == 0:` happen on pass 120?

Here's the technique with the real sensor and no motors — one more test to paste above RUN. It reads the error 500 times over about ten seconds and prints one line in fifty:

def test_sweep():
# no motors: sweep the robot across the line BY HAND while
# this runs, and watch error and correction change sign at
# the edge
for i in range(500):
error = get_error()
correction = error * KP
if i % 50 == 0:
print(f"i={i} error={error:.2f} correction={correction:.2f}")
time.sleep(0.02)

Set the run line to test_sweep(), press the button, and slide the robot slowly sideways so the left sensor starts on white, crosses the edge, and ends on black. Five hundred passes, ten lines of output, something like:

i=0 error=0.30 correction=0.15
i=50 error=0.28 correction=0.14
i=100 error=0.21 correction=0.11
i=150 error=0.09 correction=0.04
i=200 error=0.01 correction=0.00
i=250 error=-0.12 correction=-0.06
i=300 error=-0.24 correction=-0.12
i=350 error=-0.31 correction=-0.16
i=400 error=-0.33 correction=-0.17
i=450 error=-0.33 correction=-0.17

Your numbers will differ; the shape won't. The output is worth reading as more than a formatting demo. As the sensor reaches the edge the error falls to zero and then goes negative, and the correction follows it. That sign flip is the steering changing direction, with no if/else anywhere — exactly the claim made earlier in the lesson, now in numbers, from your sensor. (If the error never changes sign, the sensor didn't cross the edge — sweep farther — or SETPOINT isn't your edge value from Lesson 2-1.)

Step 3 · Put it in your own loop​

test_sweep() had a for loop, so i was there to count with. Your tracking loop is a while True:, which has no i, so make your own counter — the same count = count + 1 pattern from Lesson 1-9. Three additions to test_follow(): a counter set to zero before the loop; a line at the end of the loop body that adds one to it; and an if that prints the error and correction only when the counter is a multiple of 50. Run it and tune KP again, this time reading the numbers: a too-high gain shows up as a correction that flips sign on nearly every printed line.

Real-world connections​

Proportional control (and its bigger sibling, PID) runs an enormous amount of the physical world:

Cars

Cruise control

The further you are below the set speed, the harder it presses the throttle — error × gain.

Flight

Drone stabilization

Drones hold level by constantly correcting in proportion to how far they've tilted.

Driving

Lane-keeping

Self-driving lane assist steers in proportion to how far the car has drifted from center.

Wrap-up​

  • What's the setpoint for edge-following, and why? (~0.5 — the boundary between white and black.)
  • What do the sign and size of the error each tell you? (Sign = which way to steer; size = how hard.)
  • The robot oscillates — too high or too low Kp? (Too high; decrease it.)

Resources​