Lab 03: DC Gearmotor Position Control with an Encoder
Overview
A brushed DC gearmotor turns an arm, a quadrature encoder on the motor shaft is read at 1 kHz, and a PWM stage applies at most 12 V. The lab goes from the physics to the dominant-pole model $K/(s(\tau s+1))$, identifies $K$ and $\tau$ from a logged voltage step, and designs the position loop on the root locus with P, PD, lead and lag/PI control. Every design is then tested in a sampled loop with saturation, delay and encoder quantization.

The gearmotor, arm and encoder of lab 03.
The system
| quantity | Motor A (demos) | Motor B (homework) | basis |
|---|---|---|---|
| armature resistance $R$, inductance $L$ | 2.4 Ω, 2.0 mH | 1.9 Ω, 1.1 mH | representative values, 12 V 37 mm-class motor |
| torque constant $k_t$ = back-emf constant $k_e$ | 0.019 N m/A | 0.0235 N m/A | representative |
| rotor inertia $J_m$, load inertia at the output $J_L$ | 3.3e-6, 2.0e-3 kg m² | 4.5e-6, 6.0e-3 kg m² | rotor; small arm at the output |
| gear ratio $N$, viscous friction $b$, Coulomb friction $T_c$ | 30, 2e-6 N m s/rad, 2e-3 N m | 50, 3e-6 N m s/rad, not listed | gearbox; friction at the motor |
| encoder | 64 counts/motor rev (1920 per output rev) | 48 counts/motor rev (2400 per output rev) | quadrature |
| PWM limit, sampling | 12 V, 1 ms with a one-sample update delay | same |
The armature obeys $L\,di/dt = V - R i - k_e\omega_m$, and the rotor with the reflected load obeys $J\,d\omega_m/dt = k_t i - b\omega_m - T_c\,\mathrm{sgn}(\omega_m)$ with $J = J_m + J_L/N^2$. The output angle is $\theta = \theta_m/N$, so with $k_e = k_t$
$$ \begin{aligned}G(s) &= \frac{\Theta(s)}{V(s)} = \frac{k_t/N}{s\,[(Ls+R)(Js+b)+k_t^2]}\\&\approx \frac{K}{s(\tau s+1)},\\K &= \frac{k_t}{N(Rb+k_t^2)},\quad \tau = \frac{RJ}{Rb+k_t^2}.\end{aligned} $$For Motor A the poles are $-1172$, $-28.3$ and $0$ 1/s, so the electrical pole is 41 times faster than the mechanical one. The logs were simulated from the nonlinear model with Coulomb friction, PWM ripple and the update delay, and the encoder output is an integer count. They are not measurements from a real motor.
| step | course tool | lecture |
|---|---|---|
| electrical and mechanical model, $\Theta(s)/V(s)$ (demo 1) | modeling, transfer functions | L3, L4 |
| neglect $L$: $K/(s(\tau s+1))$ (demo 1) | dominant poles | L8 |
| $K$, $\tau$ from a voltage step on speed (demo 1) | step response and its parameters | L6, L7 |
| P control, why the settling time cannot be shortened by gain (demo 2) | root locus | L13 |
| overshoot and settling specs as an s-plane region (demo 2) | pole locations | L16 |
| PD by the angle condition (demo 2) | transient design with the root locus | L17 |
| the closed-loop zero raises the overshoot (demo 2) | effect of zeros | L9 |
| lead as the realizable PD, quantization noise on the derivative (demo 3) | lead compensation | L18 |
| ramp error and constant load torque: lag or PI (demo 3) | steady-state error, lag | L12, L18 |
| saturation, delay and quantization in a sampled loop (demo 3) | implementation test | L19 |
Demo 1: from the physics to K/(s(τs+1)) and back from a logged step
The physical model of Motor A has three poles. The electrical pole at $-1172$ 1/s settles within a few milliseconds, while the mechanical pole at $-28.3$ 1/s sets the speed response, so setting $L \to 0$ gives the design model $K/(s(\tau s+1))$ with $K = 1.7314$ rad/s/V and $\tau = 0.0362$ s. On a unit voltage step the two speed responses are almost indistinguishable; in the first milliseconds the third-order response trails the reduced one by about the electrical time constant $L/R = 0.8$ ms.
The log holds only the voltage command and the encoder count. Speed is a difference of counts, and one count is $2\pi/1920$ rad, so a one-sample difference has a speed resolution of 3.3 rad/s, which is useless, while a centered 10-sample difference resolves 0.33 rad/s without lag and is good enough offline. Fitting a first-order step response to the speed increment of the 3 V to 9 V step gives $K = 1.7299$ rad/s/V (0.08 % below the true value) and $\tau = 0.0352$ s, within 0.5 % of $1/|p_{\mathrm{mech}}| = 0.0354$ s, plus an apparent dead time of 1.9 ms from the PWM update delay and the neglected electrical lag. Before the step the motor turns at 4.76 rad/s at 3 V instead of $3K = 5.19$ rad/s, because Coulomb friction takes about 0.25 V; this is why the test steps between two nonzero voltages and fits the increment. The data and the fit are fixed, so this demo is shown as computed by the lab script.

Unit voltage step on speed: third-order model and the reduced model with L neglected.

The logged voltage step, speed from encoder count differences, and the fit with K = 1.730 rad/s/V and τ = 35.2 ms.
Demo 2: P control on the root locus and a PD design
The arm must move 0.5 rad with at most 5 % overshoot and a 2 % settling time of at most 0.15 s, without asking for more than 12 V. The overshoot limit gives $\zeta \ge 0.69$ and the settling limit $\sigma = \zeta\omega_n \ge 4/0.15 = 26.7$ 1/s, which together define a region of the s-plane. Under P control the characteristic equation is $\tau s^2 + s + KK_p = 0$; the two branches meet at $-1/(2\tau)$ and go straight up, so $\sigma = 1/(2\tau) = 14.2$ 1/s for every $K_p$ beyond the meeting point and less before it. No gain reaches the region, and the estimate $T_s \approx 4/\sigma = 8\tau = 0.28$ s cannot be improved by gain. The estimate is not a bound: the exact 2 % settling time changes with $K_p$ and is shortest at $K_p = 6.75$ V/rad, where it is 0.198 s, still above the 0.15 s limit. The largest $K_p$ that keeps $\zeta \ge 0.69$ is 8.61 V/rad, and its step settles in 0.292 s.
A PD controller $C(s) = K_d(s+z)$ adds a zero that bends the locus into the region. The demo chooses a target pole $s_d = -\sigma + j\sigma\tan(\cos^{-1}\zeta)$ with $\sigma = 27$ 1/s, places the zero so that $\angle C(s_d)G(s_d) = 180^\circ$, and takes $K_d$ from $|C(s_d)G(s_d)| = 1$. With this plant the closed loop is
$$ \begin{aligned}T(s) &= \frac{KK_d(s+z)}{\tau s^2 + (1+KK_d)s + KK_d z},\\\sigma &= \frac{1+KK_d}{2\tau},\qquad \omega_n^2 = \frac{KK_d z}{\tau},\end{aligned} $$so $K_d$ alone fixes the decay rate and $z$ then sets $\omega_n$ and hence $\zeta$. Both demo designs have $K_d = 0.5216$ V s/rad. For $\zeta = 0.70$ the zero is at $-58.1$, and although the poles alone would give 4.6 % overshoot, the closed-loop zero at $-z$ raises it to 6.7 % and the proportional part $K_p r = 15.1$ V exceeds the PWM limit. Moving the target to $\zeta = 0.80$ puts the zero at $-44.5$ and gives 2.9 % overshoot, a settling time of 0.143 s and $K_p r = 11.6$ V, which meets the overshoot and settling specs and keeps the proportional part of the voltage below 12 V. The voltage check covers only that part. On a reference step the derivative of the error is an impulse, so the ideal PD would also apply an impulse of area $K_d r = 0.26$ V s, which no PWM stage can deliver; demo 3 avoids it with a lead or by differentiating the filtered measurement.
The interactive version takes the zero and the gain directly. It draws the P and PD root loci with the closed-loop poles for the current $K_d$ and compares the step with the zero to a second-order system with the same poles and no zero.
Interactive: PD position control on the root locus
The button loads Python (Pyodide with numpy, scipy, matplotlib and python-control, about 40 MB on the first visit, cached afterwards) into this page; after that, changing a parameter re-runs the computation and redraws the figures. Until then the figures and numbers below are the output of the original demo script with the default parameters.
design model G(s) = 1.730 / (s (0.0352 s + 1)), open-loop poles 0 and -28.4 zeta >= 0.690, sigma >= 26.7 1/s P control: sigma <= 1/(2 tau) = 14.2 1/s for every Kp -> estimate Ts ~ 4/sigma = 8 tau = 0.282 s (spec 0.15 s): not reachable largest Kp with zeta >= 0.69: Kp = 8.61 V/rad exact 2 % settling for Kp up to 8.61 V/rad: shortest 0.198 s at Kp = 6.75 V/rad; 0.292 s at Kp = 8.61 V/rad zeta=0.70: sd = -27.0+27.5j, zero at -58.1, Kd = 0.5216 V s/rad, Kp = Kd z = 30.30 V/rad with the zero: OS = 6.7 %, Ts = 0.138 s; poles only: OS = 4.6 %, Ts = 0.155 s; proportional part Kp r = 15.1 V derivative part on the reference step: impulse of area Kd r = 0.261 V s zeta=0.80: sd = -27.0+20.2j, zero at -44.5, Kd = 0.5216 V s/rad, Kp = Kd z = 23.20 V/rad with the zero: OS = 2.9 %, Ts = 0.143 s; poles only: OS = 1.5 %, Ts = 0.111 s; proportional part Kp r = 11.6 V derivative part on the reference step: impulse of area Kd r = 0.261 V s

Python output
(appears after the first live run)
Show code
Loading the source...Demo 3: lead as the realizable PD, lag and PI for steady-state error, hardware test
A PD needs the derivative of the angle, but the angle arrives from the encoder in steps of one count, $q = 2\pi/1920 = 3.27\times10^{-3}$ rad. Differentiating this staircase at 1 kHz turns every count into a voltage jump of $K_d q/T_s \approx 1.8$ V, which on a real motor is audible PWM chatter and extra heating. A lead
$$ C(s) = K_c\,\frac{s+z}{s+p} $$
adds phase where it is needed and caps the high-frequency gain at $K_c$, so one count moves the voltage by only $K_c q \approx 0.07$ V. Because the hardware adds about 1 % of overshoot through the PWM delay, the electrical pole and the discretization, the lead targets $\zeta = 0.85$ and $\sigma = 28$ 1/s, a margin over demo 2. Its zero is placed on the mechanical pole, $z = 1/\tau = 28.4$ 1/s; the loop then becomes $K_cK/(\tau s(s+p))$, the angle condition gives $p = 2\sigma = 56$ 1/s, and the magnitude condition gives $K_c = 22.10$ V/rad (DC gain $K_c z/p = 11.20$ V/rad). Each controller runs in a sampled loop on the third-order plant with the 12 V limit, the one-sample PWM delay and the encoder quantization. The PD comparison uses the PID class, which differentiates the measurement rather than the error, so its closed loop has no zero and it overshoots less than the forward-path PD of demo 2.
The interactive version takes the lead zero, pole and gain, the encoder resolution, extra loop delay and the step size. It can add the demo’s lag, sized from the current lead for a 0.01 rad error to a 2 rad/s ramp with its zero at $\sigma/10 = 2.8$ 1/s. Steps larger than about 0.54 rad ($12\,\mathrm{V}/K_c$) saturate the PWM stage at the first sample.
Interactive: lead against PD with the encoder, the 12 V limit and the PWM delay
The button loads Python (Pyodide with numpy, scipy, matplotlib and python-control, about 40 MB on the first visit, cached afterwards) into this page; after that, changing a parameter re-runs the computation and redraws the figures. Until then the figures and numbers below are the output of the original demo script with the default parameters.
encoder resolution q = 3.27e-03 rad; a one-count step differentiated over Ts = 3.27 rad/s PD: Kd = 0.5623 V s/rad, zero 39.3 (Kp = 22.10 V/rad) lead: C(s) = 22.10 (s + 28.4)/(s + 56.0); HF gain 22.1 V/rad, DC gain 11.20 V/rad PD, raw derivative of counts OS = 0.7 %, Ts = 0.123 s, rms sample-to-sample du = 0.84 V, max|u| = 11.0 V PD, derivative filter Tf = 5 ms OS = 0.0 %, Ts = 0.133 s, rms sample-to-sample du = 0.12 V, max|u| = 11.0 V lead (Tustin) OS = 1.4 %, Ts = 0.119 s, rms sample-to-sample du = 0.04 V, max|u| = 10.9 V lead ramp error at 3 s = 0.1031 rad; load error at 3 s = +0.1129 rad; 0.5 rad step: OS = 1.4 %, Ts = 0.12 s lead + lag ramp error at 3 s = 0.0093 rad; load error at 3 s = +0.0114 rad; 0.5 rad step: OS = 13.0 %, Ts = 0.72 s 2 rad move, lead OS = 0.1 %, Ts = 0.190 s, time at the 12 V limit = 67 ms 2 rad move, lead + lag OS = 18.5 %, Ts = 0.931 s, time at the 12 V limit = 85 ms

Python output
(appears after the first live run)
Show code
Loading the source...The plant contains an integrator, so the error to a step is zero, but a ramp $r = vt$ leaves the error $v/K_v$ with $K_v = K\,C(0)$, and a constant load torque $T_L$ at the output acts like an input voltage $d = -T_L R/(N k_t)$ and leaves $e_{ss} = |d|/C(0)$. With the lead alone $K_v = 19.4$ 1/s, so a 2 rad/s ramp leaves 0.103 rad, and a 0.3 N m load ($-1.26$ V) leaves 0.113 rad. A lag $(s+z_g)/(s+z_g/\beta)$ multiplies $C(0)$ by $\beta$; with $\beta = 11$ and $z_g = 2.8$ 1/s the ramp error falls to 0.0093 rad and the load error to 0.0114 rad. A PI factor $(s+z_g)/s$ makes $C(0)$ infinite and removes the load error down to about one encoder count. The price is a slow closed-loop pole near $-z_g$ that does not quite cancel the lag zero: the 0.5 rad step now overshoots 13.0 % (lag) or 14.3 % (PI) and settles in about 0.7 s. A lower $z_g$ shrinks this overshoot but slows the recovery from the load.

Ramp reference and constant load torque: lead, lead + lag and lead + PI on the hardware-like loop.
For a 2 rad move the lead asks for $K_c r = 44$ V while the PWM stage gives 12 V, so the loop runs open while it is saturated. The lead alone still arrives with 0.1 % overshoot in 0.190 s. With an integrator the controller state keeps integrating the large error during the 87 ms at the limit, and the arm overshoots 20.8 %. The lag pole at $z_g/\beta \approx 0.25$ 1/s acts almost like an integrator on this time scale and winds up nearly as much (18.5 %), so an integrator or a slow lag needs anti-windup, for example the clamping in ctrlprac.sim.PID.

A 2 rad move with the 12 V limit: the lag and the PI wind up while the voltage is saturated.
Homework: position control of Motor B (100 points)
The homework positions a heavier arm with Motor B, which has a 50:1 gearbox and a coarser encoder, starting from its datasheet-class values and one simulated voltage-step log. The specs are a 0.3 rad move with at most 4 % overshoot and 0.15 s settling within 12 V, and a 0.8 N m load torque may move the arm by at most 0.005 rad. The full problem statements are in homework.md, and the starter script hw_starter.py is in the lab folder of the lab materials on GitHub; the data are in its data/ folder. Clone the repository, or download it as a zip file.
- Model and dominant pole (10 points; L3, L8)
- Identification from the log (20 points; L7)
- P control (10 points; L13, L16)
- PD design (20 points; L17, L9)
- Lead and the encoder (15 points; L18)
- Load torque (15 points; L12, L18)
- Engineering judgement (10 points)