Programming for Robotics with Arduino C++ 11, Challenge 1 solution (the servo step for half a millimetre) Idea: an arm of 100 mm and 80 mm links (assumed), every reachable point of the working area (y at least -40 mm, r from 30 to 170 mm) on a 1 mm grid with the elbow up, both joint angles rounded to the step, and the distance between the asked-for tip and the tip the rounded angles give. The error is reported overall and in three rings of r. CODE: // x1.cpp -- challenge 1: which servo step is fine enough for a placement within 0.5 mm? (host build; arm l1 = 100, l2 = 80 mm, assumed) // Over the reachable points of the working area (y >= -40 mm, 30 <= r <= 170 mm, elbow up, 1 mm grid), both joint angles are rounded to the step and the tip error is measured. // Table 1: the error for steps from 2 degrees down to 0.025 degree, overall and in three rings of r. Table 2: the same steps in microseconds of pulse width (the Servo library maps 544..2400 us onto 0..180 degrees: 10.31 us per degree). #include #include #include #include "arm.h" static const float D2R = 3.14159265f / 180.0f, R2D = 180.0f / 3.14159265f; static float quant(float rad, float step_deg) { return std::round(rad * R2D / step_deg) * step_deg * D2R; } int main() { const Arm arm = {100.0f, 80.0f}; std::printf("tip error against the servo's angular step (working area y >= -40, 30 <= r <= 170 mm)\n"); std::printf(" step (deg) in us all: mean / worst (mm) r 30-80: worst r 80-140: worst r 140-170: worst meets 0.5 mm?\n"); for (float step : {2.0f, 1.0f, 0.5f, 0.25f, 0.1f, 0.0970f, 0.05f, 0.0485f, 0.025f}) { double sum = 0, worst = 0, w1 = 0, w2 = 0, w3 = 0; long n = 0; for (int yi = -40; yi <= 170; ++yi) for (int xi = -170; xi <= 170; ++xi) { double r = std::hypot((double)xi, (double)yi); if (r < 30.0 || r > 170.0) continue; float t1 = 0, t2 = 0; if (!ik(arm, (float)xi, (float)yi, true, t1, t2)) continue; float x, y; fk(arm, quant(t1, step), quant(t2, step), x, y); double e = std::hypot(x - xi, y - yi); sum += e; ++n; if (e > worst) worst = e; if (r < 80.0) { if (e > w1) w1 = e; } else if (r < 140.0) { if (e > w2) w2 = e; } else { if (e > w3) w3 = e; } } std::printf(" %10.4f %6.2f %9.3f / %-9.3f %14.3f %15.3f %16.3f %s\n", step, step * 1856.0 / 180.0, sum / n, worst, w1, w2, w3, worst <= 0.5 ? "yes" : "no"); } return 0; } END CODE OUTPUT: tip error against the servo's angular step (working area y >= -40, 30 <= r <= 170 mm) step (deg) in us all: mean / worst (mm) r 30-80: worst r 80-140: worst r 140-170: worst meets 0.5 mm? 2.0000 20.62 1.270 / 4.002 2.052 3.409 4.002 no 1.0000 10.31 0.636 / 2.069 0.986 1.712 2.069 no 0.5000 5.16 0.316 / 1.017 0.519 0.853 1.017 no 0.2500 2.58 0.159 / 0.522 0.259 0.436 0.522 no 0.1000 1.03 0.063 / 0.208 0.098 0.174 0.208 yes 0.0970 1.00 0.062 / 0.199 0.102 0.171 0.199 yes 0.0500 0.52 0.032 / 0.102 0.050 0.084 0.102 yes 0.0485 0.50 0.031 / 0.100 0.051 0.085 0.100 yes 0.0250 0.26 0.016 / 0.051 0.025 0.044 0.051 yes END OUTPUT Reading the output. The error is proportional to the step and is worst in the outer ring: 4.00, 2.07, 1.02, 0.52 and 0.21 mm at the outer ring (r from 140 to 170 mm) for steps of 2, 1, 0.5, 0.25 and 0.1 degree; the inner ring (r from 30 to 80 mm) has about half of that (2.05 mm at 2 degrees). At 1 degree the outer ring's 2.07 mm is about 119 mm times the step in radians, so the rule 'error is about the reach times the step' holds to a factor near 1. The coarsest step that meets 0.5 mm everywhere among those tried is 0.1 degree (1.03 microseconds, worst 0.208 mm); 0.25 degree misses by a little (0.522 mm). The real edge is somewhere between 0.1 and 0.25 degree and I did not try the steps in between. In servo terms: Servo::write(int) gives 1 degree (2.07 mm worst) and cannot meet the target at any radius of the outer ring; Servo::writeMicroseconds(int) gives about 0.097 degree per microsecond (0.199 mm worst) and meets it with a margin of 2.5. The finer half-microsecond tick of the library's timer (0.0485 degree, 0.100 mm) cannot be reached through the public interface, which takes whole microseconds, so the last two lines of the table are what the hardware could do, not what the library offers. Check on method: this is the quantisation of the command only. The chapter's pick-and-place run with 0.8 degree of play (an assumption) has 0.70 mm of tip error even with microsecond steps, so the 0.5 mm target cannot be met on that arm whatever the step: the play is the floor. A real servo also has a dead band of its own and a pulse-to-angle map that is not the library's 544 to 2400 microseconds; none of that is simulated here.