Skip to content

Class 4 · PID with the knobs

P control catches the fall but can't stop rocking. Today you add the other two terms: D, which damps the wobble, and I, which corrects a steady lean. Three knobs set the three gains while the robot runs, so you can feel what each one does instead of re-uploading after every change.

By the end of class

The robot stands untouched for 10 s, then for 60 s.

New parts today

Part What it's for
Three 10 kΩ potentiometers (from your kit) Knobs for Kp, Ki and Kd

Build it

Wire each pot the way you did in the voltage-divider labs: outer legs to 5V and GND, middle leg to an analog pin.

Pot Middle leg → Uno pin Sets
1 A0 Kp
2 A1 Ki
3 A2 Kd

Put the pots on the middle-deck breadboard, or on a small separate breadboard on 50 cm wires taped along the tether, so turning a knob doesn't shove the robot. Label each knob with tape.

The other two terms

Tool: D and I

Back to the cart from Class 3. It overshoots because P only knows how far the cart is from 100, not how fast it's going. By the time it reaches 100 it's moving fast, and nothing tells it to slow down.

D (derivative) pushes against the speed of the error. Change one line:

float push = Kp * error - Kd * vel;   // try Kd = 1.0

Now the cart brakes as it gets close. Too little Kd and it still overshoots; too much and it crawls in slowly.

I (integral) adds up the error over time. A small error that never goes away grows a big integral, which pushes harder and harder until the error is gone. Add a steady force that P and D can't fully cancel, like a slope, and see:

integral += error * dt;
integral = constrain(integral, -50, 50);   // the windup limit
float push = Kp * error + Ki * integral - Kd * vel - 20;   // -20 is the slope

With Ki = 0 (and Kp = 2), the cart settles about 10 short of 100. Set Ki = 1 and it creeps the rest of the way.

The constrain is the windup limit. Without it, if the cart is held back for a while (or a robot lies on the floor), the integral grows huge, and when it's let go it shoots off until that stored-up error unwinds.

For the robot, the three terms look like this:

error    = angle - balancePoint
integral = integral + error * dt        (with a windup limit)
u        = Kp * error + Ki * integral + Kd * gyroRate

The D term uses gyroRate directly. The rate the angle is changing is what the gyro measures, so you don't need to subtract one angle from the last and divide by dt (which would be noisy). Check the sign: D should resist the motion, so when the robot is falling forward, D should add to the forward push just like P does. Limit u to ±255 and send it through setMotor() (dead zone included), as in Class 3. Reset integral to 0 whenever the tilt cutoff trips.

Tool: a knob that gives decimals

map() works with whole numbers only, and it rounds. To turn a pot reading into a decimal range, scale it yourself. Example: a thermostat knob from 15.0 °C to 30.0 °C.

float setTemp = 15.0 + analogRead(A0) / 1023.0 * (30.0 - 15.0);

analogRead(A0) / 1023.0 goes from 0.0 to 1.0. The .0 matters: analogRead(A0) / 1023 is integer division and gives 0 almost every time.

Mission 1 · Knobs on the gains

  1. Read the three pots and turn them into gains. These ranges are starting points, not answers: your motors, wheels and battery height change them. If your best setting ends up at the very end of a knob, widen that range.

    Gain Range Units
    Kp 0 to 60 PWM per degree
    Ki 0 to 100 PWM per degree·second
    Kd 0 to 3 PWM per °/s
  2. Print all three every half second (use millis(), not every loop), in Teleplot or the Serial Monitor.

  3. Add I and D to your controller, with a windup limit on integral (start with ±2, so that even with Ki at the top of its range the I term can't push harder than 200).

Done when

Turning each knob changes its number smoothly across its whole range, and with Ki and Kd at zero the robot behaves like Class 3.

Tuning

Fingers clear

The robot will fall a lot today. Keep your hands around it, not on it, and keep the switch within reach. Tuning wrong can make the wheels buzz or jerk hard; if that happens, switch off before you touch the knobs. If the wheels twitch back and forth right at upright, your dead-zone number may be a little high: try lowering it by 5–10.

Tune in this order, one knob at a time:

  1. P. Ki and Kd at zero. Turn Kp up until the robot catches itself and rocks, like last class. Note the value.
  2. D. Turn Kd up slowly. The rocking calms down. Keep going until it stands with only a small, quick shiver. If it starts buzzing or jittering, you've gone too far: back off a little. (A faint tick as the wheels change direction is normal: gear motors have a little slack, called backlash, between their gear teeth.)
  3. P again. With D in, you can usually raise Kp a bit for a stiffer robot. Raise it until the wobble comes back, then back off.
  4. I. Turn Ki up a little. It should stop a slow lean to one side. Too much and a slow, big rocking comes back.

Every time you find a good setting, write the three numbers in your build log.

Mission 2 · Stand for 10 seconds

Tune until it stands on its own. The tether stays slack and you keep your hands off. If it always rolls off in the same direction, nudge balancePoint by 0.2° at a time and see which way helps.

Done when

The robot stands untouched for 10 s, three times in a row. Film one.

Mission 3 · Stand for 60 seconds

Keep tuning. Plot error and u in Teleplot while it stands: a good tune looks like a small, fast flicker around zero, not a slow wave. It's fine if it wanders a little across the floor; you'll fix that next class. Walk the tether along with it if you need to, without touching the robot.

Done when

It stands untouched for 60 s. Film it, and write the final Kp, Ki and Kd in your build log.

If you finish early

  • Hard-code your best gains as const floats and check that it behaves the same without the knobs. (You'll want a free knob next class.)
  • Try the windup limit at ±0.5 and at ±20. Hold the robot tilted for 5 s with Ki up, then let go. What's the difference?
  • Measure how long your loop really takes (print dt now and then). Is it still about 5 ms with the pots and printing?

Sign-off with Dr. Alvear

  • Three knobs set Kp, Ki and Kd live, and the values are printed
  • PID with a windup limit; integral resets when the robot falls
  • Stands 10 s untouched, then 60 s (video)
  • Final gains written in the build log
  • Build log updated (photo + two sentences)