Programming for Robotics with Arduino C++ 11, Challenge 3 solution (a straight line against a curve) Idea: three legs of the chapter's pick-and-place cycle, each done two ways: the joint-space move of the chapter (both joints with a synchronised trapezoid, tip followed every millisecond) and a Cartesian move (a trapezoid along the straight line, inverse kinematics at every 20 ms step, with the acceleration eight times the speed). The Cartesian tip speed is raised in steps of 5 mm/s until a joint would exceed 120 degrees per second or 400 degrees per second squared (steps of 20 ms). Reported: the largest distance of the tip from the straight line, and the time. CODE: // x3.cpp -- challenge 3: a straight line through the air, or a curve in joint space? (host build; arm l1 = 100, l2 = 80 mm, assumed; limits 120 deg/s and 400 deg/s^2 per joint) // For three legs of the pick-and-place cycle the tip path of (a) the joint-space move of the chapter (both joints start and stop together, trapezoid) and (b) a Cartesian move (a trapezoid // along the straight line, inverse kinematics at every 20 ms step) is compared: the largest distance of the tip from the straight line (sampled every millisecond for (a); for (b) // from the exact angles), and the time. For (b) the tip speed is raised, with the acceleration eight times the speed, to the largest value at which no joint exceeds its speed or its acceleration (steps of 20 ms). #include #include #include #include "arm.h" static const float D2R = 3.14159265f / 180.0f, R2D = 180.0f / 3.14159265f; struct Pt { float x, y; }; static double dist_to_line(Pt a, Pt b, double x, double y) { double dx = b.x - a.x, dy = b.y - a.y, len = std::hypot(dx, dy); return std::fabs(dx * (y - a.y) - dy * (x - a.x)) / len; } // joint-space move: returns the time; fills the largest deviation from the line static double joint_move(const Arm& arm, Pt a, Pt b, double& dev) { float a1 = 0, a2 = 0, b1 = 0, b2 = 0; ik(arm, a.x, a.y, true, a1, a2); ik(arm, b.x, b.y, true, b1, b2); Trap j0, j1; plan_sync(j0, j1, (b1 - a1) * R2D, (b2 - a2) * R2D, 120.0f, 400.0f); double T = j0.T > j1.T ? j0.T : j1.T; dev = 0; for (int i = 0; i <= (int)(T * 1000.0); ++i) { float t = i * 0.001f, x, y; fk(arm, a1 + j0.pos(t) * D2R, a2 + j1.pos(t) * D2R, x, y); double d = dist_to_line(a, b, x, y); if (d > dev) dev = d; } return T; } // Cartesian move at tip speed vmax (acceleration 8 vmax): does any joint exceed its limits? returns the time; fills the peak joint speed and acceleration static double cart_move(const Arm& arm, Pt a, Pt b, float vmax, double& peak_v, double& peak_a, double& dev) { double L = std::hypot(b.x - a.x, b.y - a.y); Trap tp; tp.plan((float)L, vmax, 8.0f * vmax); int n = (int)std::ceil(tp.T / 0.02 - 1e-9); double prev1 = 0, prev2 = 0, pv1 = 0, pv2 = 0; peak_v = peak_a = dev = 0; for (int i = 0; i <= n; ++i) { double t = i * 0.02; if (t > tp.T) t = tp.T; double s = tp.pos((float)t) / L; float x = a.x + (float)s * (b.x - a.x), y = a.y + (float)s * (b.y - a.y), t1 = 0, t2 = 0; if (!ik(arm, x, y, true, t1, t2)) return -1.0; dev = std::fmax(dev, dist_to_line(a, b, x, y)); if (i > 0) { double v1 = (t1 - prev1) * R2D / 0.02, v2 = (t2 - prev2) * R2D / 0.02; peak_v = std::fmax(peak_v, std::fmax(std::fabs(v1), std::fabs(v2))); if (i > 1) peak_a = std::fmax(peak_a, std::fmax(std::fabs(v1 - pv1), std::fabs(v2 - pv2)) / 0.02); pv1 = v1; pv2 = v2; } prev1 = t1; prev2 = t2; } return tp.T; } int main() { const Arm arm = {100.0f, 80.0f}; struct { const char* name; Pt a, b; } legs[3] = { {"lower at the pick (120,20)->(120,-38)", {120.0f, 20.0f}, {120.0f, -38.0f}}, {"across above the table (120,20)->(60,20)", {120.0f, 20.0f}, {60.0f, 20.0f}}, {"home to above the pick (110,40)->(120,20)", {110.0f, 40.0f}, {120.0f, 20.0f}}}; std::printf("%-44s %-34s %-52s\n", "leg", "joint-space move", "Cartesian (straight) move at the largest tip speed within the joint limits"); std::printf("%-44s %8s %12s %12s %10s %12s %14s %12s\n", "", "time (s)", "off line (mm)", "tip speed", "time (s)", "off line (mm)", "peak joint v", "peak joint a"); for (auto& l : legs) { double dev_j; double tj = joint_move(arm, l.a, l.b, dev_j); float best = 0; double bt = 0, bv = 0, ba = 0, bd = 0; for (float v = 5.0f; v <= 800.0f; v += 5.0f) { double pv, pa, dv; double t = cart_move(arm, l.a, l.b, v, pv, pa, dv); if (t < 0 || pv > 120.0 || pa > 400.0) break; best = v; bt = t; bv = pv; ba = pa; bd = dv; } std::printf("%-44s %8.3f %12.2f %9.0f mm/s %10.3f %12.3f %11.1f deg/s %9.0f deg/s^2\n", l.name, tj, dev_j, best, bt, bd, bv, ba); } return 0; } END CODE OUTPUT: leg joint-space move Cartesian (straight) move at the largest tip speed within the joint limits time (s) off line (mm) tip speed time (s) off line (mm) peak joint v peak joint a lower at the pick (120,20)->(120,-38) 0.534 3.47 90 mm/s 0.769 0.000 48.0 deg/s 388 deg/s^2 across above the table (120,20)->(60,20) 0.676 23.97 55 mm/s 1.216 0.000 46.1 deg/s 379 deg/s^2 home to above the pick (110,40)->(120,20) 0.347 0.55 90 mm/s 0.373 0.000 48.8 deg/s 394 deg/s^2 END OUTPUT Reading the output. The joint-space move strays from the line by 3.47 mm when lowering at the pick (0.534 s), by 23.97 mm when moving across above the table (0.676 s), and by 0.55 mm for the short move from home to above the pick (0.347 s). The Cartesian move is on the line (0.000: computed from the exact angles) but slower: 0.769 s, 1.216 s and 0.373 s, that is 44 %, 80 % and 7 % more, at tip speeds of 90, 55 and 90 mm/s. So the straight line is worth its time when the path matters and not otherwise. The long move across the table is the one where the curve is large (24 mm on a move of 60 mm) and the line costs most (80 %); if the arm carries a full cup or must pass between two objects it is worth it, and if there is nothing within a few centimetres it is not. Lowering into a gap is the case where a few millimetres matter and the price is 0.24 s. The short move needs no line at all: 0.55 mm and 7 %. The Cartesian times are an upper bound for this scheme, not an optimum. The limit that stopped the speed was the joint acceleration (the peaks are 388, 379 and 394 degrees per second squared, against a limit of 400) while the joint speeds were far below their limit (48, 46 and 49 degrees per second against 120), because I tied the tip acceleration to 8 times the tip speed. A profile with a gentler acceleration and a longer cruise would use the joint speed headroom and take less time; I did not search for it. Check on method: the Cartesian path is exact only because the angles are not rounded; with a servo that takes whole degrees the tip can be 2 mm off the commanded line (challenge 1), so a straight line is only as straight as the servo step allows. What it costs on the Uno is not measured: a Cartesian move calls the inverse kinematics at every 20 ms step, where a joint-space move calls it once per leg; in the chapter's cost table ik_lut() is 188 instructions plus two helpers per call, which is a count and not a time.