1y = adc.read()2e = r - y3I = I + Ki*T*e4D = a*D - b*(y - y_old)5dac.write(Kp*e + I + D)6y_old = y; wait_tick()T = 10 msu 79.6 Ny[k] —y 0.000 my 0.273u 79.6
r = 1 m00.511.5200.511.5time (s)mCART POSITION yanalogT = 10 msT = 50 msT = 80 ms00.10.20.30.40.5-3000300time (s)NFORCE u, HELD EACH TICKT = 10 ms
Lit wire with moving dashes: a sample or a force is flowing along itBlocks: timer, ADC, DAC and cart; the controller is the code cardPlots: cart position and the held force, computed from the sampled loopPills: the sample y[k] going in, the force u[k] going out, once per tickA PID controller running on a computer: a timer starts each pass, the ADC reads the cart, six lines of Python compute the force, and the DAC holds it until the next tick. The plots below are simulated from the sampled loop: the cart position for the analog controller and for 10, 50 and 80 ms ticks, and the force as a staircase. The update rules follow Åström and Murray, Feedback Systems, §11.5: a running sum for the integral, a backward difference for the filtered derivative (filter time Td/N, N between 5 and 20; we use 10), and the derivative taken on the measurement.

The loop

  1. In "The PID controller" a cart was pushed by u = Kp·e + Ki·∫e dt − Kd·dy/dt. A computer cannot do calculus all the time. It runs a loop: every T seconds read the sensor, work out u, write it, wait.

PID as sums

Choosing T

What is happening

# step 1/10 · loop
# about: Timer: every T, ADC: read y, PID code, DAC: write, hold u, Cart (plant)

the cart     y'' + 10 y' + 20 y = u
command      r = 1 m (a step)
gains        Kp = 300, Ki = 200, Kd = 20

the loop, once every T seconds:
  read  y      (sensor → ADC → number)
  compute u    (a few lines of Python)
  write u      (number → DAC → force)
  wait         for the next tick

We take T = 10 ms: 100 passes a second.

The cart and the gains are the ones from "The PID controller": 1 kg, 10 N·s/m, 20 N/m, Kp = 300, Ki = 200, Kd = 20. Every curve and number in the figure is simulated from the sampled loop, with the cart moved forward in 10-microsecond steps between ticks. adc.read, dac.write and wait_tick stand for whatever your hardware library provides. The Python on the last steps is plain Python 3 and prints the overshoot the figure shows.