Programming for Robotics with Arduino C++ 5, Challenge 3 solution (find the dead zone) Idea: a hidden dead duty is set in the simulator's RobotParams and the program never reads it. For each duty from 0 up it holds that duty for 1.5 seconds (the first 0.5 s lets the 0.05 s motor lag settle), counts the left wheel's ticks over the last second, and calls the duty 'moving' at 3 ticks or more; the dead duty is the highest duty that did not move. The speed handed to the simulator is duty / 255 x 0.5, so its internal speed_to_pwm gives back exactly that duty. The found value is then used in speed_to_pwm_dz and the steady speeds are compared with the plain mapping. CODE: // x3.cpp -- Challenge 3: find the dead zone of an unknown motor by driving it (host build; the simulator holds a hidden dead duty, the program never reads it). // Method: for each duty from 0 up, hold it for 1.5 s, count the encoder ticks in the last second, and call the duty "moving" if there were at least 3. // The dead duty is the highest duty that did NOT move. Then command 0.02, 0.05 and 0.1 m/s through speed_to_pwm (plain) and through speed_to_pwm_dz with the dead duty found. #include #include #include #include "sim.h" #include "motion.h" static int ticks_at(int duty, int dead_hidden) { RobotParams rp; rp.dead_duty = dead_hidden; rp.tau = 0.05; SensorParams sp; Arena arena; SimHW w(rp, sp, arena, 1); float v = (float)(duty / 255.0 * 0.5); // the speed whose 8-bit duty is exactly `duty` w.drive(v, v); for (int i = 0; i < 500; ++i) w.step(0.001); int32_t l0, r0; w.read_ticks(l0, r0); for (int i = 0; i < 1000; ++i) w.step(0.001); int32_t l1, r1; w.read_ticks(l1, r1); return (int)(l1 - l0); } static double steady(float cmd_speed, bool compensated, uint8_t dead_found, int dead_hidden) { RobotParams rp; rp.dead_duty = dead_hidden; rp.tau = 0.05; SensorParams sp; Arena arena; SimHW w(rp, sp, arena, 1); uint8_t d = compensated ? speed_to_pwm_dz(cmd_speed, 0.5f, dead_found) : speed_to_pwm(cmd_speed, 0.5f); float v = (float)(d / 255.0 * 0.5); w.drive(v, v); for (int i = 0; i < 2000; ++i) w.step(0.001); return w.wheel_l(); } int main() { for (int hidden : {57, 23}) { int dead_found = -1; for (int duty = 0; duty <= 255; ++duty) if (ticks_at(duty, hidden) < 3) dead_found = duty; else break; std::printf("hidden dead zone %d: the sweep found %d (ticks in 1 s at duty %d: %d, at duty %d: %d)\n", hidden, dead_found, dead_found, ticks_at(dead_found, hidden), dead_found + 1, ticks_at(dead_found + 1, hidden)); std::printf(" commanded m/s plain: actual with dead zone: actual\n"); for (float v : {0.02f, 0.05f, 0.1f}) std::printf(" %13.2f %13.3f %22.3f\n", (double)v, steady(v, false, 0, hidden), steady(v, true, (uint8_t)dead_found, hidden)); } return 0; } END CODE OUTPUT: hidden dead zone 57: the sweep found 57 (ticks in 1 s at duty 57: 0, at duty 58: 5) commanded m/s plain: actual with dead zone: actual 0.02 0.000 0.023 0.05 0.000 0.053 0.10 0.000 0.101 hidden dead zone 23: the sweep found 23 (ticks in 1 s at duty 23: 0, at duty 24: 4) commanded m/s plain: actual with dead zone: actual 0.02 0.000 0.022 0.05 0.006 0.052 0.10 0.060 0.101 END OUTPUT Reading the output. The sweep finds 57 for a hidden dead zone of 57 (0 ticks at 57, 5 ticks at 58) and 23 for 23 (0 at 23, 4 at 24), to the exact duty, because the simulator's wheel turns at the smallest step above the threshold at a rate of 4 to 5 ticks a second, over the 3-tick limit. Commanding 0.02, 0.05 and 0.1 m/s through the plain mapping gives 0.000, 0.000 and 0.000 m/s with the dead zone at 57, and 0.000, 0.006 and 0.060 with the dead zone at 23; with the compensation they give 0.023, 0.053 and 0.101 (57) and 0.022, 0.052 and 0.101 (23): within 15 % at 0.02 and 6 % at 0.05, as quantisation of one duty step (about 2.3 mm/s) allows, and within about 1 % at 0.1 m/s. Check on method: this is the easy case. The simulator's dead zone is the same for both directions, constant in time and with no friction beyond the threshold; a real motor has a stiction threshold that is higher than the running threshold (so it must be found while starting, and the sweep can be done up and then down to see the difference), it changes with the battery voltage and with load, and the two motors differ. The 3-tick limit is a choice, and a noisy encoder could count 3 ticks of vibration on a motor that is not turning. The sweep should be run on the real robot, with the wheels off the floor first and then on it, and the result stored with the date and the battery voltage.