Take-Back-Half
Mediumdifficulty
This builds on Bang-Bang Control, which explains why a heavy flywheel suits an on/off controller and why plain bang-bang keeps rippling around the target.
Settling without losing the fast recovery
Take-back-half keeps a running output, drive, and adds a little to it every loop in proportion to the
error, like an integral term. When the flywheel is slow, drive climbs. When it overshoots, drive
starts falling.
The trick happens each time the speed crosses the target. Crossing means you've gone too far, so set
drive to the average of its current value and its value at the previous crossing.
Try it with numbers. Say the power that would hold the target speed exactly is 0.60, but you don't know
that. The flywheel spins up with drive climbing, and at the first crossing drive has reached 0.80.
There's no previous crossing yet, so remember 0.80. The flywheel overshoots, drive drifts down, and at
the next crossing it's at 0.50. Average that with 0.80 to get 0.65, and remember 0.65. Next crossing,
drive has drifted to 0.57, and averaging with 0.65 gives 0.61. The values close in on 0.60 from both
sides.
is the one gain, and is drive at the previous crossing.
In the demo, the green trace is TBH. It recovers almost as fast as bang-bang on the way up, then stops rippling and holds a flat speed, while bang-bang (red) keeps switching.
When the flywheel is below the target, the power was too low, and when it's above, the power was too
high, so the right power is somewhere between the values at two crossings. Averaging them splits the
difference, a bit like guessing a number by halving the range each time. It isn't a guarantee, though.
If is too large, drive overshoots so far between crossings that it can keep bouncing instead of
settling, so tune on the real mechanism.
Take-back-half on the Control Hub
private double drive; // the current output
private double driveAtZero; // the output at the last error sign change
private Double lastError; // null until the first update
double update(double measured, double target, double dt) {
double error = target - measured;
drive = clamp(drive + GAIN * error * Math.max(0, dt), 0, 1);
if (error != 0) {
if (lastError != null && Math.signum(error) != Math.signum(lastError)) {
drive = (drive + driveAtZero) / 2;
driveAtZero = drive;
}
lastError = error;
}
return drive;
}
An error of exactly zero is skipped so it can't look like an extra crossing.
Sketch a flywheel overshooting, crossing, undershooting, and crossing again. Mark At one crossing drive at each
crossing. Why does averaging bring it closer to the holding power?Show solution
drive is too high, and at the next it's too low, so the holding power is between them.
Averaging lands in the middle of that range, and each new crossing narrows it. The flywheel's own
inertia also smooths the speed between crossings, which helps. With a reasonable gain the output
settles on the power that holds the target speed.
A flywheel is heavy, and the motor can push it faster but can only let it coast slower. What would a PID
controller need to handle that, and how many things would you tune? Why is A PID would need The technique came from VEX flywheel control. Comparing its step response with a tuned PI controller on
the same mechanism is a good experiment.drive clamped to
instead of ?Show solution
kP, kI, kD, an I-zone, and anti-windup to cope, several of which affect each
other. TBH needs GAIN. The clamp is because negative power would drive the flywheel backwards,
which no shooter wants. The only way to slow down is to stop pushing and let friction do it. Allowing
negative values would let one overshoot brake the wheel hard and cost much more recovery time than
coasting.
Starting driveAtZero from a feedforward estimate helps a lot:
TBH then settles in about one crossing instead of several. Watch the units. is volts per unit of
speed, but drive is a power from 0 to 1, so the starting value is .
For a flywheel with at 300 rad/s on a 12 V battery that's . Forget to divide by
the battery and you get 6.0, six times full power, and TBH starts pinned at the maximum.
- Purdue SIGBots (BLRS) wiki - Take-Back-Half (TBH) controller - description source for this lesson; credits Pearman, J. (2015), "Flywheel velocity control," VexForum, for the VEX flywheel application, and Harper, C. S. (2011), Application of the Take-Back-Half algorithm to voltage source converter current control, master's thesis, Mississippi State University, for an academic treatment of the algorithm outside robotics.
- Woodward, S. (2005), "Take-back-half technique tempers temperature-control-loop overshoot," EDN ("Design Ideas") - the original technique, developed for temperature control and later adapted by the FRC and VEX communities for flywheel speed control.
This lesson is an independent reimplementation.