Skip to main content

Take-Back-Half

Mediumdifficulty

Read first

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.

drive+=g e Δton a sign change of e:drive←12(drive+drivelast-cross)\begin{gathered} \text{drive} \mathrel{+}= g\,e\,\Delta t \\[3pt] \text{on a sign change of } e:\quad \text{drive} \leftarrow \tfrac12\big(\text{drive} + \text{drive}_\text{last-cross}\big) \end{gathered}

gg is the one gain, and drivelast-cross\text{drive}_\text{last-cross} 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.

Live demoBang-bang vs. takeback-half — fire a shot, watch the recovery
bang-bang — chatters around targettakeback-half — settles smoothlytarget
Bang-bang ripple: —TBH holding drive: —
Idea
Why halfway?

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 gg is too large, drive overshoots so far between crossings that it can keep bouncing instead of settling, so tune gg on the real mechanism.

Take-back-half on the Control Hub​

FTCTakeback-half velocity controller
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.

ExerciseTracing the crossings

Sketch a flywheel overshooting, crossing, undershooting, and crossing again. Mark drive at each crossing. Why does averaging bring it closer to the holding power?

Show solution

At one crossing 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.

ExerciseWhy a flywheel, and why 0 to 1

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 drive clamped to [0,1][0, 1] instead of [−1,1][-1, 1]?

Show solution

A PID would need kP, kI, kD, an I-zone, and anti-windup to cope, several of which affect each other. TBH needs GAIN. The clamp is [0,1][0, 1] 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.

The technique came from VEX flywheel control. Comparing its step response with a tuned PI controller on the same mechanism is a good experiment.

Starting driveAtZero from a feedforward estimate helps a lot: TBH then settles in about one crossing instead of several. Watch the units. kVk_V is volts per unit of speed, but drive is a power from 0 to 1, so the starting value is kV⋅vtarget/Vbatk_V \cdot v_\text{target} / V_\text{bat}. For a flywheel with kV=0.02k_V = 0.02 at 300 rad/s on a 12 V battery that's 6/12=0.56 / 12 = 0.5. Forget to divide by the battery and you get 6.0, six times full power, and TBH starts pinned at the maximum.

Sources & references
  • 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.