Tuning
Nine PID gains ship as 0. This page gets them to working values. The autotuner handles the first two, and you hand-tune the rest.
On this page
Do the measurements first
Verify
wheel_distance_inanddistance_between_wheelsbefore you tune anything. Bad geometry looks exactly like bad gains — the robot consistently stops short, and the natural reaction is to raisekpuntil it doesn’t. That produces a twitchy controller papering over an arithmetic error, and it falls apart the moment you changemax_outputor drive a different distance.
Test it directly: set a rough distance_kp (0.2 is a reasonable guess), run driveTo(24, 3000), and measure with a tape what the robot traveled. If it’s off by the same percentage at several distances, that’s geometry, so fix wheel_distance_in. If it’s off by a different amount each time, or oscillates, that’s tuning.
See Configuration for the formula.
Tuning order
Work in this order. Each stage depends on the one before it.
distance_*: straight-line driving. The autotuner does this.heading_correction_*: keeping straight moves straight.turn_*: turning in place.- Motion feel: slew rates,
chase_power,dlead.
The autotuner
RW 2.0 ships with a Twiddle optimizer that tunes distance_kp and distance_kd for you. It drives 10 inches forward and back over and over, scores each attempt, and adjusts.
The binding already exists in telop():
controller_1.ButtonY.pressed(runDistanceAutoTune);
You do not need to add it.
Running it
- Set your ports and chassis measurements in
robot-config.cppfirst. - Download the program and start driver control.
- Put the robot on the field or a similar surface with at least 3 feet of clear space in front and behind it. It will drive back and forth on its own, repeatedly, for a few minutes.
- Press Y.
- Watch the controller. It shows
TUNING...with the currentKpandKd; the brain adds the cost score. - Wait for
TUNE DONEorGOOD VALUES!, then read the finalKpandKd.
Then copy the values by hand
The autotuner does not save anything. Its results live in RAM and vanish when the program stops. Write the
KpandKdoff the screen, put them intorobot-config.cpp, and re-download:
double distance_kp = 0.35, distance_ki = 0, distance_kd = 0.12;
It only ever sets ki to zero. That’s intentional, and a fine place to leave it.
The robot stops responding when tuning finishes.
runDistanceAutoTune()ends in a deliberate infinite loop with the drive held. That is normal and means it’s done — restart the program to get driver control back.
Two limitations
- It only tunes distance. There is no turn autotuner. The code contains an
evaluateTurnGainsfunction, but nothing calls it and norunTurnAutoTune()exists to bind. You tune turn gains by hand, below. - It tunes for a 10-inch move. Gains that suit 10 inches make a good starting point for everything else, but check a long drive (
driveTo(48, 3000)) afterward and nudge if it overshoots.
Remove the binding when you’re done
Leave controller_1.ButtonY.pressed(runDistanceAutoTune); in place and a stray Y press during a match sends the robot driving back and forth on its own. Delete the line once your gains are set.
How it scores
From include/autotune.h:
| Constant | Value | Meaning |
|---|---|---|
TUNE_DISTANCE_IN |
10.0 | Inches driven per trial |
TUNE_TIME_LIMIT_MS |
2000 | Timeout per trial |
TWIDDLE_TOLERANCE |
0.01 | Stop when the step sizes shrink below this |
TWIDDLE_MAX_ITERS |
100 | Hard iteration cap |
GOOD_ENOUGH_COST |
0.75 | Stop early at this score — shows GOOD VALUES! |
The cost function is:
cost = 3.0 × |final error| + 2.0 × overshoot + 0.001 × elapsed ms
Final error carries the most weight, overshoot next, and time almost none, so the tuner chases accuracy and breaks ties on speed. It samples overshoot at 100 Hz through the move rather than reading it once at the end.
Twiddle starts at kp = 0.2, kd = 0.0 with step sizes {0.05, 0.02}. It nudges each parameter up; a better score grows the step by 10%, and a worse one sends it the other direction, shrinking the step by 10% when neither helps.
Tuning by hand
For turn_* and heading_correction_*, and for refining what the autotuner produced.
The process
1. Start with P alone. Set ki and kd to zero. Raise kp until the robot reaches the target and oscillates around it: visible overshoot, back and forth once or twice.
2. Back off about 20%. Sit right at the edge of oscillation.
3. Add D to damp. Raise kd until the oscillation disappears and the approach is crisp. A good starting ratio is kd ≈ kp × 3 for turns, less for straight driving. Too much kd makes the robot crawl in or jitter. Back off if you hear the motors chattering.
4. Add I only if you must. If the robot stops short and sits there, a small ki (start around kp / 100) closes the gap. Otherwise leave ki at zero.
Change one gain at a time and run the same test move three times before deciding. Battery voltage moves results around enough that a single run will mislead you — and tune on a fresh-ish battery, since gains tuned at 30% charge will be too aggressive at full.
Turn gains
Test with turnToAngle(90, 1500), then check a small turn (15°) and a large one (180°).
| Symptom | Fix |
|---|---|
| Doesn’t reach the target | Raise turn_kp |
| Overshoots and comes back | Raise turn_kd |
| Oscillates around the target | Lower turn_kp, then raise turn_kd |
| Slow, creeping approach | Lower turn_kd |
| Good on 90° but short on 15° | Small turn_ki, or lower turn_kd |
Heading correction gains
These keep straight moves straight. Tune them after your distance gains, with a long test:
driveTo(48, 4000);
Run it down a taped line. If the robot ends up parallel but offset sideways, you have a mechanical or odometry problem. If it ends up angled, raise heading_correction_kp.
| Symptom | Fix |
|---|---|
| Veers off heading during the move | Raise heading_correction_kp |
| Snakes side to side down the line | Lower heading_correction_kp, raise heading_correction_kd |
| Buzzes or fights itself when stopped | Lower heading_correction_kp, or set heading_correction = false |
These gains also hold heading while the robot is stationary, which is why an over-tuned value shows up as a robot that vibrates when it should be still.
Motion feel
Once the PID gains are solid, these settings shape how the robot moves. All are in src/robot-config.cpp.
Slew rates
double max_slew_accel_fwd = 24;
double max_slew_decel_fwd = 24;
double max_slew_accel_rev = 24;
double max_slew_decel_rev = 24;
Max volt change per 10 ms. At 24 the limiter does nothing, since output can swing the full range in a cycle or two. Lower them if the robot wheelies on launch, tips forward when stopping, or burns rubber instead of accelerating. Try 6 to 10 and work up.
Acceleration and deceleration are separate per direction, so you can allow a fast launch while keeping a gentle stop.
chase_power
double chase_power = 2.5;
Limits how hard boomerang cuts its corner. Lower it if boomerang paths wander or the robot drifts, which means the drive is slipping. Raise it for more speed once the path repeats.
dlead
You pass this per call rather than setting it globally:
boomerang(24, 36, 1, 90, 0.5, 3000);
// ^^^ dlead
Higher = curvier approach. Never above 0.6. Start at 0.4 and adjust: raise it if the robot arrives at the right spot but the wrong heading, lower it if the path swings wider than you want.
dir_change_start / dir_change_end
bool dir_change_start = true;
bool dir_change_end = true;
Tell the slew limiter whether to expect a direction change at each end of a chained move. true is smoother and more accurate, false is faster. Set them to false once your routine is reliable and you’re hunting for tenths.
A tuning session, start to finish
- Measure and set
wheel_distance_inanddistance_between_wheels. - Run the autotuner. Copy
Kp/Kdintodistance_kp/distance_kdand re-download. - Verify with
driveTo(48, 4000). Check both the distance traveled and whether it ended straight. - Tune
heading_correction_*on that same long drive. - Tune
turn_*withturnToAngle(90, 1500), then verify at 15° and 180°. - Test
moveToPoint(24, 24, 1, 3000), which exercises distance, heading, and odometry together. - Test
boomerangand adjustdlead, thenchase_powerif it slips. - Delete the Button Y autotuner binding.
- Re-check on a full battery. Gains tuned low will be aggressive at 100%.