1speed = code * 3000 / 10242error = 1500 - speed3volts = 15 + 0.04 * error4dac.write(volts)ADC—DAC—1.000 VT 10 ms1000 rpmerror +501.0 → 35.04 V1.000 V
goal 150001603204806401000125015001750time (ms)rpmMOTOR SPEEDT = 10 msT = 80 ms0160320480640010203040time (ms)voltsDAC OUTPUT (HELD)T = 10 msT = 80 ms
Lit wire with moving dashes: a signal is flowing along itBlocks: motor, tachometer and clock; chips: ADC and DAC; the computer is the code cardPlots: motor speed (dots = the samples) and the held DAC voltage, computed from the modelThe travelling pill is one value on its way round the loop during a tickA computer controls a motor's speed in a loop that ticks every sample period: the tachometer is sampled by the ADC, the computer runs a short Python rule, and the DAC holds the new voltage until the next tick. The plots below are simulated from the motor model: with 10 ms ticks the speed settles calmly; with 80 ms ticks the same rule overshoots and rings. Real loops also lose a little time computing between the snapshot and the new voltage, and usually put an anti-alias filter (a low-pass filter) before the ADC. Both add delay, which pushes the stability limit to even shorter sample periods. This lesson treats the computing time as zero.

The parts

  1. Our plant is a small DC motor spinning at 1000 rpm, and we want 1500 rpm. Its speed changes smoothly at every instant. A tachometer, a speed sensor, turns it into a voltage: 1 V per 1000 rpm. A computer cannot read that directly.

One tick

The loop runs

The maths at the ticks

Sampling too slowly

What is happening

# step 1/11 · plant
# about: Motor (plant), Tachometer (sensor)

plant    DC motor, speed 1000 rpm now
goal     1500 rpm
sensor   tachometer, 1 V per 1000 rpm
         1000 rpm → 1.000 V

motor model  200 ms time constant,
             100 rpm per volt when settled

The speed is an analog signal: it has a
value at every instant. A computer only
handles numbers, one at a time.

The motor is a first-order model with illustrative numbers: 100 rpm per volt when settled and a 200 ms time constant. Every curve and number in the figure is simulated tick by tick from that model, including the ADC's rounding down and the DAC's rounding. The Python on the steps is plain Python 3 with the math module. Between ticks it moves the motor with the exact solution of its equation, because the voltage is held constant, just as the figure does.