Programming for Robotics with Arduino C++ 6, Challenge 1 solution (the price of windup) Idea: one function runs the wheel-speed PI (Kp 1.0, Ki 6.0, output 0..0.5 m/s) with a chosen anti-windup mode, asks for 0.8 m/s (out of reach) for a time T and then 0.2 m/s, and returns the time until the TRUE speed is within 10 % of 0.2 (capped at 3 s). main() sweeps T over 0.25, 0.5, 1 and 2 s for no protection, clamping, and back-calculation with kb of 1, 4, 8 and 20 per second. CODE: // x1.cpp -- Challenge 1: how much does windup cost, and how hard should back-calculation pull? (host build; wheel speed PI, Kp 1.0, Ki 6.0, output 0..0.5 m/s) // The setpoint is 0.8 m/s (out of reach) for a time T, then 0.2 m/s. For T from 0.25 to 2 s: the time until the true speed is back within 10 % of 0.2 (up to 3 s), // for no anti-windup, clamping the integral, and back-calculation with gain kb of 1, 4, 8 and 20 per second. #include #include #include #include "rig.h" #include "pid.h" static double recover(AntiWindup aw, float kb, double hold_s) { SpeedRig rig(default_robot()); PidConfig c = {1.0f, 6.0f, 0.0f, 0.0f, 0.5f, 0.0f, true, aw, kb}; Pid pid(c); int hold = (int)std::lround(hold_s / 0.02); double t_rec = 3.0; for (int i = 0; i < hold + 150; ++i) { float u = pid.update(i < hold ? 0.8f : 0.2f, rig.speed(), 0.02f); rig.step(u, u); if (i >= hold && std::fabs(rig.true_speed() - 0.2) <= 0.02) { t_rec = (i - hold + 1) * 0.02; break; } } return t_rec; } int main() { std::printf("time (s) after the setpoint drops from 0.8 to 0.2 m/s until the true speed is within 10 %% of 0.2 (3.00 = not within 3 s), by how long the setpoint was out of reach\n"); std::printf(" out of reach for no anti-windup clamp back-calc kb=1 kb=4 kb=8 kb=20\n"); for (double T : {0.25, 0.5, 1.0, 2.0}) std::printf(" %14.2f s %14.2f %5.2f %14.2f %4.2f %4.2f %5.2f\n", T, recover(AW_NONE, 0, T), recover(AW_CLAMP, 0, T), recover(AW_BACKCALC, 1, T), recover(AW_BACKCALC, 4, T), recover(AW_BACKCALC, 8, T), recover(AW_BACKCALC, 20, T)); return 0; } END CODE OUTPUT: time (s) after the setpoint drops from 0.8 to 0.2 m/s until the true speed is within 10 % of 0.2 (3.00 = not within 3 s), by how long the setpoint was out of reach out of reach for no anti-windup clamp back-calc kb=1 kb=4 kb=8 kb=20 0.25 s 0.74 0.08 0.68 0.44 0.10 0.08 0.50 s 0.98 0.10 0.82 0.54 0.12 0.10 1.00 s 1.48 0.10 0.96 0.56 0.12 0.10 2.00 s 2.50 0.10 1.10 0.58 0.12 0.10 END OUTPUT Reading the output. Without protection the integral grows for as long as the setpoint is out of reach (Ki x error x time, here about 6 x 0.3 per second), and it has to be unwound by an equal amount of negative error afterwards, so the recovery time grows with T: 0.74, 0.98, 1.48 and 2.50 s. Clamping does not depend on T (0.08 to 0.10 s): the integral stops growing the moment the output saturates, so there is nothing to unwind. Back-calculation lands between, depending on kb: its pull-back is proportional to how far the output is cut, so with kb = 1 (a time constant of 1 s) the integral is still draining after 0.68 to 1.10 s and the recovery still grows with T (but much more slowly than with no protection); with kb = 8 and 20 it matches clamping (0.10 to 0.12 s). The rule of thumb that kb should be about the same as Ki or a few times larger is consistent with this: the pull-back has to be faster than the integral's own growth. Check on method: one plant, one seed, one controller; the speed estimate is quantised and 50 ms old, which makes the 10 % band (0.18 to 0.22) about three quanta of 5.76 mm/s wide, so differences of 0.02 s in the clamp column are one control step, not a trend. Clamping looks best here because the output limit and the plant's limit are the same; when they differ (a software limit under the motor's real one) the integral can stop in the wrong place, as the undershoot of the clamp in the chapter's table shows.