Programming for Robotics with Arduino C++ 5, Challenge 2 solution (keep the heading honest) Idea: part 1 adds a small float step to a float heading until the sum equals the heading (the heading has frozen) and compares where that happens with dth x 2^24. Part 2 models a wheel pair that gains exactly one tick of difference per update (0.0038397 rad) and keeps the heading three ways: a float that is added to and wrapped into (-pi, pi], a float computed from the integer count, and a uint16 binary angle computed from the count (the method of OdomFixed). Each is compared with the truth, the count times the step in double, modulo one turn. CODE: // x2.cpp -- Challenge 2: where does a float heading stop being right, and what keeps it right? (host build; float is 32-bit, as on the Uno) // Part 1: adding a small step dth to a float th until th + dth == th (the heading freezes); compare the heading at which it freezes with dth x 2^24. // Part 2: the heading of a wheel pair that gains ONE tick of difference per update (0.0038397 rad, 0.19 rad/s at 50 Hz) kept three ways: a float added to and wrapped into (-pi, pi], // a float computed afresh from the integer count, and a uint16 binary angle computed from the integer count (as OdomFixed does). Truth: the count times the step in double. #include #include #include #include int main() { std::printf("part 1: float heading += dth each update; the first update after which the heading no longer changes\n"); std::printf(" dth (rad) updates until frozen heading when frozen (rad) turns dth x 2^24 (rad)\n"); for (float dth : {1.0e-3f, 1.0e-4f, 1.0e-5f}) { float th = 0.0f; long n = 0; while (th + dth != th) { th += dth; ++n; } std::printf(" %9.0e %20ld %25.2f %5.1f %16.2f\n", (double)dth, n, (double)th, th / 6.2831853, (double)dth * 16777216.0); } const double TWO_PI = 6.283185307179586; const double STEP = 0.0038397; // rad per tick of wheel difference (0.576 mm / 150 mm) const float STEPF = 0.0038397f; const float PER_COUNT = 0.0038397f; const uint32_t KA16 = (uint32_t)std::lround(STEP / TWO_PI * 65536.0 * 65536.0); // binary angle units per count, times 65536 std::printf("\npart 2: one count of wheel difference per update, error in degrees against the truth (count x 0.0038397 rad in double, modulo one turn)\n"); std::printf(" updates at 50 Hz float added+wrapped float from the count uint16 from the count\n"); float th = 0.0f; long n = 0; for (long target : {100000L, 1000000L, 10000000L, 100000000L}) { for (; n < target; ++n) { th += STEPF; if (th > 3.14159265f) th -= 6.2831853f; } double truth = std::fmod(STEP * (double)target, TWO_PI); auto err = [&](double est) { return std::remainder(est - truth, TWO_PI) * 180.0 / 3.14159265358979; }; double fresh = std::fmod((double)((float)(target % 1000000L) * PER_COUNT) + (double)(target / 1000000L) * (double)(1000000.0f * PER_COUNT), TWO_PI); // see the note: counts are split so each float is exact uint16_t bam = (uint16_t)(((uint32_t)target * KA16) >> 16); std::printf(" %9ld %6.1f h %19.4f %20.4f %21.4f\n", target, target / 50.0 / 3600.0, err((double)th), err(fresh), err(bam * TWO_PI / 65536.0)); } std::printf("note: 'from the count' converts the exact integer to a float and multiplies once (the count is split at 1,000,000 so the float of it is exact); the error then comes from the\n"); std::printf(" float constant 0.0038397 and the product, and grows only in proportion to the angle, never by accumulating rounding: it is a scale error, as a wheel-radius error is.\n"); return 0; } END CODE OUTPUT: part 1: float heading += dth each update; the first update after which the heading no longer changes dth (rad) updates until frozen heading when frozen (rad) turns dth x 2^24 (rad) 1e-03 25150895 32768.00 5215.2 16777.22 1e-04 17866579 2048.00 325.9 1677.72 1e-05 22888953 256.00 40.7 167.77 part 2: one count of wheel difference per update, error in degrees against the truth (count x 0.0038397 rad in double, modulo one turn) updates at 50 Hz float added+wrapped float from the count uint16 from the count 100000 0.6 h 0.0962 0.0001 0.0034 1000000 5.6 h 0.9641 -0.0028 0.0343 10000000 55.6 h 9.6432 -0.0280 0.3432 100000000 555.6 h 96.4345 -0.2798 3.4429 note: 'from the count' converts the exact integer to a float and multiplies once (the count is split at 1,000,000 so the float of it is exact); the error then comes from the float constant 0.0038397 and the product, and grows only in proportion to the angle, never by accumulating rounding: it is a scale error, as a wheel-radius error is. END OUTPUT Reading the output. A float stops growing when its step is under half the spacing of the floats near it, and the spacing doubles at every power of two, so the freeze is at a power of two near dth x 2^24 (the second column shows 32768 rad for 1e-3, against 16777; 2048 for 1e-4, against 1678; 256 for 1e-5, against 168). That is a long way off, 5200 turns for the first, and the real problem comes much earlier, as the chapter showed: before the freeze each addition is rounded to a multiple of the spacing, and with a constant step the rounding error has the same sign every time. In part 2 the wrapped float is 0.96 degrees off after a million updates, 9.6 after ten million (55 hours at 50 Hz) and 96 after a hundred million, growing in proportion to the number of updates. The two integer-count versions have errors that grow only in proportion to the angle: 0.0028 and 0.034 degrees per million updates for the float and the binary angle. That is a scale error (the constant is not exact), like a wheel-radius error, not an accumulation of rounding, and it is 30 to 340 times smaller than the drift. Which one on the Uno? The binary angle: it is an integer, it wraps by itself, and it needs no float at all; the float-from-count version needs the count split into parts to keep the conversion exact (done here in double on the PC, which is not available on the Uno, where double is 32-bit), so it is shown as a comparison, not as a recipe. In practice a robot spins for seconds, not days, and this error would be hidden under slip and calibration; it matters for robots that run continuously and are never re-referenced. Check on method: the step of one count per update is an assumption chosen to make the rounding visible; real wheel pairs do not gain exactly one count per cycle. The truth is the count times a decimal constant in double, so it contains no error but the float constant and the arithmetic.