Programming for Robotics with Arduino C++ 11, Challenge 2 solution (two tables of different sizes) Idea: ik_lut() of the chapter with the size of the acos table and of the atan table set independently (8 to 128 entries each), 25 pairs, the worst tip error over the reachable 1 mm grid with 40 mm <= r <= 170 mm (elbow up). The flash is 2 bytes per entry, n + 1 entries per table. CODE: // x2.cpp -- challenge 2: the two lookup tables of ik_lut() need not be the same size. (host build; arm l1 = 100, l2 = 80 mm, assumed) // The acos table (the elbow angle) and the atan table (the direction angles) are swept independently over 8, 16, 32, 64, 128 entries; the cell is the worst tip error // over the reachable 1 mm grid with 40 <= r <= 170 mm (elbow up), in mm, followed by the flash the two tables take in bytes. #include #include #include #include #include "arm.h" static void build_acos(int n, std::vector& t) { t.resize(n + 1); for (int k = 0; k <= n; ++k) t[k] = (uint16_t)std::lround(std::acos(-1.0 + 2.0 * k / n) * 1e4); } static void build_atan(int n, std::vector& t) { t.resize(n + 1); for (int k = 0; k <= n; ++k) t[k] = (uint16_t)std::lround(std::atan((double)k / n) * 1e4); } // ik_lut() of arm.h with a table size of its own for each table static bool ik2(const Arm& a, float x, float y, float& t1, float& t2, const uint16_t* ac, uint8_t nac, const uint16_t* at, uint8_t nat) { float c2 = (x * x + y * y - a.l1 * a.l1 - a.l2 * a.l2) / (2.0f * a.l1 * a.l2); if (c2 > 1.0f || c2 < -1.0f) return false; float s2 = -sqrtf(1.0f - c2 * c2); t2 = -acos_lut(c2, ac, nac); t1 = atan2_lut(y, x, at, nat) - atan2_lut(a.l2 * s2, a.l1 + a.l2 * c2, at, nat); return true; } int main() { const Arm arm = {100.0f, 80.0f}; const int sizes[5] = {8, 16, 32, 64, 128}; std::printf("worst tip error (mm), 40 <= r <= 170: rows = entries in the acos table, columns = entries in the atan table\n"); std::printf(" acos \\ atan"); for (int s : sizes) std::printf(" %10d", s); std::printf("\n"); std::vector ac, at; for (int na : sizes) { build_acos(na, ac); std::printf(" %11d", na); for (int nt : sizes) { build_atan(nt, at); double worst = 0; for (int yi = -170; yi <= 170; ++yi) for (int xi = -170; xi <= 170; ++xi) { double r = std::hypot((double)xi, (double)yi); if (r < 40.0 || r > 170.0) continue; float t1 = 0, t2 = 0; if (!ik2(arm, (float)xi, (float)yi, t1, t2, ac.data(), (uint8_t)na, at.data(), (uint8_t)nt)) continue; float x, y; fk(arm, t1, t2, x, y); double e = std::hypot(x - xi, y - yi); if (e > worst) worst = e; } std::printf(" %10.3f", worst); } std::printf("\n"); } std::printf("flash in bytes for the two tables: 2 (acos entries + 1) + 2 (atan entries + 1), e.g. 64 + 8: %d, 8 + 64: %d, 32 + 32: %d\n", 2 * 65 + 2 * 9, 2 * 9 + 2 * 65, 2 * 33 + 2 * 33); return 0; } END CODE OUTPUT: worst tip error (mm), 40 <= r <= 170: rows = entries in the acos table, columns = entries in the atan table acos \ atan 8 16 32 64 128 8 13.832 13.824 13.820 13.820 13.820 16 6.929 6.924 6.921 6.920 6.920 32 0.524 0.495 0.491 0.491 0.491 64 0.399 0.166 0.158 0.158 0.158 128 0.401 0.106 0.039 0.033 0.033 flash in bytes for the two tables: 2 (acos entries + 1) + 2 (atan entries + 1), e.g. 64 + 8: 148, 8 + 64: 148, 32 + 32: 132 END OUTPUT Reading the output. The acos table decides the accuracy. Down a column (acos table 8, 16, 32, 64, 128 with any atan table of 32 or more) the worst error is 13.8, 6.9, 0.49, 0.158 and 0.033 mm; along a row it hardly changes once the atan table has 32 entries (acos 64: 0.399, 0.166, 0.158, 0.158, 0.158). The reason is the function: acos is steep near plus and minus 1, so its error is large and spreads to the tip, while atan over 0 to 1 is gentle and 16 entries already follow it to well under a tenth of a millimetre. The cheapest pair that stays under 0.5 mm in the table is an acos table of 32 entries with an atan table of 16: 0.495 mm, 2 x 33 + 2 x 17 = 100 bytes of flash. That is a saving of 160 bytes against two tables of 64 (260 bytes, 0.158 mm), 62 % of the table space, and the error is three times as big. It is a thin margin: 0.495 against a limit of 0.5, with the pair one step coarser (atan 8) failing at 0.524. With a margin, an acos table of 64 and an atan table of 16 gives 0.166 mm for 164 bytes. What the saving is worth: 160 bytes is about 3.5 % of the 4552 bytes the sketch with ik_lut() takes, and the chapter's table showed that the code around the tables costs more than the tables do; so the choice matters when the flash is nearly full, not otherwise. Check on method: the error is for points between 40 and 170 mm from the shoulder; closer to the folded or the straight arm (the ends of the ring) every pair is much worse (the chapter's rings for 64 entries show 5 mm), so a pair chosen here is only as good as the promise to keep waypoints in that range. A coarser grid than 1 mm could miss the worst point.