Programming for Robotics with Arduino C++ 6, Challenge 2 solution (filtering the derivative) Idea: the same PID as the chapter's section 3 (Kp 1.5, Ki 6, Kd 0.05 s, derivative on the measurement, integral clamp), with the derivative's low-pass time constant swept over 0, 0.01, 0.02, 0.05, 0.1, 0.2 and 0.5 s. The setpoint steps from 0.1 to 0.3 m/s at 1 s. Jitter is the rms of the command's step-to-step change over the last 2 s; overshoot and the 90 % rise time are taken from the TRUE speed. CODE: // x2.cpp -- Challenge 2: how much filtering does the derivative need? (host build; wheel speed PID, Kp 1.5, Ki 6, Kd 0.05 s, derivative on the measurement, integral clamp) // Sweep the time constant of the derivative's low-pass filter. Setpoint 0.1 -> 0.3 m/s at 1 s. For each: the jitter of the command once settled (rms step-to-step change over the last // 2 s, mm/s), the overshoot of the true speed, and the time from the step until the true speed reaches 0.27 (90 % of the way). #include #include #include #include "rig.h" #include "pid.h" int main() { std::printf("derivative filter sweep\n"); std::printf(" filter tau (s) jitter (mm/s) overshoot (%%) 90 %% rise (s)\n"); for (float tau : {0.0f, 0.01f, 0.02f, 0.05f, 0.1f, 0.2f, 0.5f}) { SpeedRig rig(default_robot()); PidConfig c = {1.5f, 6.0f, 0.05f, 0.0f, 0.5f, tau, true, AW_CLAMP, 8.0f}; Pid pid(c); double sq = 0, peak = 0, rise = -1; int n = 0; float prev = 0; for (int i = 0; i < 300; ++i) { float u = pid.update(i < 50 ? 0.1f : 0.3f, rig.speed(), 0.02f); rig.step(u, u); if (i >= 200) { double du = u - prev; sq += du * du; ++n; } prev = u; if (i >= 50) { if (rig.true_speed() > peak) peak = rig.true_speed(); if (rise < 0 && rig.true_speed() >= 0.27) rise = (i - 50 + 1) * 0.02; } } std::printf(" %14.2f %13.2f %13.1f %13.2f\n", tau, std::sqrt(sq / n) * 1000.0, (peak - 0.3) / 0.3 * 100.0, rise); } return 0; } END CODE OUTPUT: derivative filter sweep filter tau (s) jitter (mm/s) overshoot (%) 90 % rise (s) 0.00 17.01 1.4 0.22 0.01 11.94 1.2 0.26 0.02 10.78 1.1 0.12 0.05 6.37 0.8 0.10 0.10 5.97 0.6 0.10 0.20 6.26 0.5 0.10 0.50 4.85 0.6 0.10 END OUTPUT Reading the output. The jitter falls from 17.01 mm/s unfiltered to 6.37 at 0.05 s, 5.97 at 0.1 s, and 4.85 at 0.5 s; the PI without any derivative has about 3.9. Most of the gain comes by 0.05 s, and past 0.1 s there is little to win, because what remains is the noise that comes through the proportional and integral terms. Overshoot does not change much (1.4 % to 0.5 %). So a value of 0.05 to 0.1 s is the sensible choice here: about the same as the 0.05 s sample delay of the speed estimate, and most of the benefit. What is given up for a longer filter is that the derivative responds later, so it damps less when damping is needed; this plant does not need it, so the cost does not show. The rise-time column is not a clean trend (0.22, 0.26, 0.12, then 0.10): a 90 % rise time measured on a speed that moves in 5.76 mm/s steps, with a 20 ms control period, is quantised to 0.02 s, and the first two entries are slower because the unfiltered noise makes the command jump back and forth around the point where it should be rising. It is one run with one seed; it says that heavy noise makes the response ragged, not that 0.01 s is worse than 0. Check on method: the noise is the quantisation of the speed estimate and the simulator's own, small. A real wheel has uneven slots, motor ripple and vibration that put more noise in, which moves the best filter value up. The derivative on this plant is of little use at all (section 3); the sweep is to learn the cost of filtering it, not a reason to use it.