Step 5
Sensors, rates & delay
Run the same controller at 500, 50 and 20 Hz with a late, coarse sensor, and find gains that survive the slow one.
The control loop
A real controller is a loop: read the sensors, compute a torque, command the motor, wait, repeat. It runs at a fixed rate (updates per second, Hz), so it acts every seconds. In between it is blind, and the motor keeps the last torque.
Here the controller is step 4's PD law on the base, torque : is the base angle, its angular velocity, and and the gains.
The simulator takes steps of ms, so one update every seconds means physics steps per update: 1 at 500 Hz, 25 at 20 Hz.
A late, coarse sensor
This step's sensor is imperfect, like a real one:
- Latency: every reading of
arm.q,arm.qdandarm.state()is 20 ms old. - Quantisation: the angle comes from a 4096-tick encoder, so it moves in steps of rad. The velocity is the change over the last 10 ms (step 1's finite difference), so it jitters.
Why that hurts
Feedback corrects what it saw. At 20 Hz with 20 ms latency, the controller acts on where the arm was 20 ms ago and keeps that torque for 50 ms more: by the next update, its picture is 70 ms old. A high gain pushes hard on that stale error, overshoots, pushes hard back... The loop can oscillate, or even grow until the motor saturates.
| Step 4's gains (, ) | Settles to ±1° |
|---|---|
| 500 Hz | in 0.4 s |
| 50 Hz | in 0.8 s, 3 % overshoot |
| 20 Hz | never: it keeps swinging |
The cure is gentler gains: a slower controller only needs its information to be fresh on its own, slower timescale. (Or run faster and buy a better sensor.) Keep near critical damping, with kg·m².
Latency comes back in Noisy Sensors & Kalman Filters, step 3, where predicting ahead makes up for it.
Your task
- Write
run_at_rate(kp, kd, rate, duration, label):round(duration * rate)times, readarm.state(), sendpd_torqueto the base, callshow(...), then step the simulation times. - The program runs step 4's gains at 500, 50 and 20 Hz. Compare them in Plots.
- Choose
KPandKDfor 20 Hz: the step must settle within ±1° in 1.5 s with under 5 % overshoot.