Programming for Robotics with Arduino C++ 6, Challenge 3 solution (bits for the integral) Idea: PiQ stores ki_dt = round(Ki x 0.02 x 2^F) as an integer and an accumulator with the same F fractional bits, clamped to 0..500 mm/s. For Ki of 1.0 and 0.3 per second and F of 4, 6, 8, 10, 12 and 16 it prints the integral gain it really implements (the stored count divided back, as a percentage of the wanted Ki) and the steady error of the TRUE speed over the last 2 s of a 30 s run, on the speed loop with Kp 1.5. CODE: // x3.cpp -- Challenge 3: how many fractional bits does the integral gain of an integer PI need? (host build; speed in mm/s, step 0 -> 300, Kp 1.5, output 0..500 mm/s) // PiQ stores the integral gain times the sample time with F fractional bits: ki_dt = round(Ki x 0.02 x 2^F), and keeps its accumulator with the same F bits. // For Ki = 1.0 and 0.3 /s and several F: the integral gain it actually implements (as a % of the wanted one) and the steady error of the TRUE speed over the last 2 s of 30 s. #include #include #include #include #include "rig.h" template class PiQ { public: PiQ(double kp, double ki) : kp_((int32_t)std::lround(kp * 256)), ki_((int32_t)std::lround(ki * 0.02 * (double)(1L << F))), acc_(0) {} int32_t gain() const { return ki_; } int16_t update(int16_t sp, int16_t meas) { int32_t e = (int32_t)sp - meas; acc_ += ki_ * e; int32_t hi = 500L << F; if (acc_ > hi) acc_ = hi; else if (acc_ < 0) acc_ = 0; int32_t u = ((kp_ * e) >> 8) + (acc_ >> F); if (u > 500) u = 500; else if (u < 0) u = 0; return (int16_t)u; } private: int32_t kp_, ki_, acc_; }; template static void row(double ki) { SpeedRig rig(default_robot()); PiQ pi(1.5, ki); double err = 0; int n = 0; for (int i = 0; i < 1500; ++i) { int16_t out = pi.update(300, (int16_t)std::lround(rig.speed() * 1000.0f)); rig.step(out / 1000.0f, out / 1000.0f); if (i >= 1400) { err += (0.3 - rig.true_speed()) * 1000.0; ++n; } } double implemented = (double)pi.gain() / (double)(1L << F) / 0.02; std::printf(" %4.1f %2d %10d %16.1f %% %17.2f\n", ki, F, (int)pi.gain(), 100.0 * implemented / ki, err / n); } int main() { std::printf("integer PI: fractional bits F of the integral gain, the gain actually implemented, and the steady speed error (mm/s)\n"); std::printf(" Ki F ki_dt count gain implemented steady error mm/s\n"); for (double ki : {1.0, 0.3}) { row<4>(ki); row<6>(ki); row<8>(ki); row<10>(ki); row<12>(ki); row<16>(ki); } return 0; } END CODE OUTPUT: integer PI: fractional bits F of the integral gain, the gain actually implemented, and the steady speed error (mm/s) Ki F ki_dt count gain implemented steady error mm/s 1.0 4 0 0.0 % 120.55 1.0 6 1 78.1 % -0.42 1.0 8 5 97.7 % -0.37 1.0 10 20 97.7 % -0.37 1.0 12 82 100.1 % -0.41 1.0 16 1311 100.0 % -0.44 0.3 4 0 0.0 % 120.55 0.3 6 0 0.0 % 120.55 0.3 8 2 130.2 % 1.05 0.3 10 6 97.7 % 3.98 0.3 12 25 101.7 % 3.24 0.3 16 393 99.9 % 3.55 END OUTPUT Reading the output. The gain that is stored is a count, and the count can be too small to hold the gain. Ki = 1: at F = 4 the count is round(0.02 x 16) = 0, so the integral does nothing and the loop is P-only: its steady error is 300 / (1 + 1.5) = 120 mm/s, which is the printed 120.55. At F = 6 the count is 1 (78.1 % of the gain) and the loop already works; F = 8 gives 97.7 %, and F = 12 100.1 %. Ki = 0.3 needs more: F = 4 and 6 give a count of 0 (the same 120.55), F = 8 gives a count of 2 (130 % of the wanted gain: too much, because 0.006 x 256 = 1.5 rounds up), and F = 10 to 16 are within 3 %. So the smallest F is 6 for Ki = 1 and 8 for Ki = 0.3 if a gain error of 30 % is acceptable, and 10 if it is not; in general, F must be big enough that Ki x dt x 2^F is a count of at least about 10 (rounding is at most half a count, so 5 % of the gain). The steady errors at the bottom of the table (about 3.5 mm/s for Ki = 0.3, and about -0.4 for Ki = 1) are not the bits: with Ki = 0.3 the loop is simply slow, with an integral time constant of Kp / Ki = 5 s, and it has not finished converging in 30 s; the float controller of the chapter shows the same slowness. What goes wrong at the bottom is the first two rows of each group: the integral is dead, and nothing in the output says so. Check on method: the accumulator is a 32-bit integer and F = 16 with the clamp at 500 << 16 is 32.8 million, safe; at larger F or a larger output range it could overflow, which has to be checked as a bound rather than found in a test. All of this is on the simulator, with one plant and one setpoint.