State Observers and Output Feedback
The position servo of Pole Placement needs the angle, the speed and the current. The production version has only an encoder: no current shunt amplifier (it costs money and board space), and a speed obtained by differencing encoder counts, which at jumps in steps of because one count is . Feeding that into makes the motor buzz. The answer is to estimate the missing states with a model running in the controller, corrected continuously by the measurement: a state observer. It works when the plant is observable.
The idea: a model that is corrected by the error
Run a copy of the plant model in software, driven by the same input . Its output should match the measured ; when it does not, nudge the estimate in proportion to the difference:
Subtract this from and the estimation error obeys
independent of and . Choose so that has fast, stable eigenvalues and the error dies away from any starting value. Without the correction () the error would follow itself: for a plant with an integrator (the motor angle) a wrong initial estimate would never decay.
Design by duality. The eigenvalues of equal those of , which is a state-feedback problem for the pair . So L = ct.place(A.T, C.T, observer_poles).T: the pole-placement tools design observers too.
Where to put the observer poles. A common rule is 2 to 5 times faster than the controller poles, so the estimate converges before the controller relies on it. Much faster makes large, and a large passes measurement noise straight into the estimate. The Kalman filter chooses from noise statistics instead of pole positions; ct.lqe computes its steady-state gain.
The separation principle
Feed the estimate back, . In the coordinates the closed loop is
The matrix is block-triangular, so its eigenvalues are those of (the controller) together with those of (the observer). You can design and separately. Better still, the observer error is not excited by : with a correct initial estimate the step response is exactly the state-feedback response. The controller plus observer together form an ordinary dynamic compensator from to , of the same order as the plant.
Worked example: estimating speed and current from the encoder
Given: the motor position model (, ) and the state-feedback gain of the previous lesson (poles , ).
Find: for observer poles at , and ; the step response with the observer in the loop; the decay of an initial estimate error of .
Solve:
- Observability: has rank 3: the encoder alone reveals speed (its derivative) and current (through the torque).
- The slowest observer pole, , is about 6 times faster than the controller's dominant pair, so any estimate error has decayed to after .
- By separation, the closed-loop poles are the three controller poles plus the three observer poles, and the step response equals the state-feedback one ( overshoot).
Check:
import numpy as np
import control as ct
Ra, La, k, J, b = 2.0, 1e-3, 0.02, 2e-6, 1e-6
A = np.array([[0, 1, 0], [0, -b / J, k / J], [0, -k / La, -Ra / La]])
B = np.array([[0], [0], [1 / La]])
C = np.array([[1, 0, 0]])
wd = 150 * np.sqrt(1 - 0.7**2)
K = ct.place(A, B, [complex(-105, wd), complex(-105, -wd), -2000.0])
N = -1.0 / (C @ np.linalg.solve(A - B @ K, B)).item()
L = ct.place(A.T, C.T, [-600.0, -700.0, -2500.0]).T
print("observable:", np.linalg.matrix_rank(ct.obsv(A, C)) == 3)
print("L =", ", ".join(f"{v:.4g}" for v in L.ravel()))
# Plant + observer, state z = [x, xhat]; u = -K xhat + N r
Acl = np.block([[A, -B @ K], [L @ C, A - B @ K - L @ C]])
Bcl = np.vstack([B * N, B * N])
print("closed-loop poles:", np.round(np.sort(np.linalg.eigvals(Acl).real), 1))
t = np.linspace(0, 0.1, 10001)
step = ct.step_info(ct.ss(Acl, Bcl, np.hstack([C, 0 * C]), 0), T=t)
print(f"step with observer: overshoot {step['Overshoot']:.1f} %, settling {1e3 * step['SettlingTime']:.1f} ms")
# Regulation from a wrong initial estimate: theta(0) = 0.1 rad, estimate 0
sys = ct.ss(Acl, Bcl, np.eye(6), 0)
z = ct.forced_response(sys, T=t, U=0 * t, X0=[0.1, 0, 0, 0, 0, 0]).outputs
for ms in (0, 2, 5, 10):
i = ms * 100
print(f"t = {ms:2d} ms: theta = {z[0, i]:7.4f} rad, estimate = {z[3, i]:7.4f} rad, "
f"speed error = {z[1, i] - z[4, i]:8.2f} rad/s")observable: True
L = 1799, -1.309e+05, 9.501e+04
closed-loop poles: [-2500. -2000. -700. -600. -105. -105.]
step with observer: overshoot 4.6 %, settling 40.4 ms
t = 0 ms: theta = 0.1000 rad, estimate = 0.0000 rad, speed error = 0.00 rad/s
t = 2 ms: theta = 0.0966 rad, estimate = 0.1004 rad, speed error = -14.91 rad/s
t = 5 ms: theta = 0.0729 rad, estimate = 0.0778 rad, speed error = -7.01 rad/s
t = 10 ms: theta = 0.0282 rad, estimate = 0.0287 rad, speed error = -0.61 rad/s
The estimate catches the true angle within a few milliseconds, while the controller (slower) is only beginning to correct the position: the observer has done its job before it matters much.
Why not just differentiate the encoder?
At one encoder count () in one sample is , so a differenced speed jumps between levels apart. The observer integrates a model instead, and uses the encoder only through the slowly acting correction. The program runs the motor at (about ), samples a quantised encoder, and compares the two speed estimates. The observer is discretised with a zero-order hold, as a microcontroller would run it (Sampled-Data Loops).
import numpy as np
import control as ct
Ra, La, k, J, b = 2.0, 1e-3, 0.02, 2e-6, 1e-6
A = np.array([[0, 1, 0], [0, -b / J, k / J], [0, -k / La, -Ra / La]])
B = np.array([[0], [0], [1 / La]])
C = np.array([[1, 0, 0]])
Ts, q = 1e-4, 2 * np.pi / 4000 # 10 kHz, one encoder count in rad
L = ct.place(A.T, C.T, [-600.0, -700.0, -2500.0]).T
plant = ct.c2d(ct.ss(A, B, np.eye(3), 0), Ts) # exact plant over one sample
obs = ct.c2d(ct.ss(A - L @ C, np.hstack([B, L]), np.eye(3), 0), Ts)
x = np.zeros(3); xh = np.zeros(3); th_prev = 0.0
w_true, w_diff, w_obs = [], [], []
for n in range(3000): # 0.3 s at 3 V
y = np.floor(x[0] / q) * q # quantised encoder angle
w_true.append(x[1]); w_diff.append((y - th_prev) / Ts); w_obs.append(xh[1])
th_prev = y
x = plant.A @ x + plant.B[:, 0] * 3.0
xh = obs.A @ xh + obs.B @ np.array([3.0, y])
w_true, w_diff, w_obs = (np.array(v[1000:]) for v in (w_true, w_diff, w_obs)) # last 0.2 s
print(f"true speed {w_true.mean():.1f} rad/s")
print(f"differenced encoder: rms error {np.sqrt(np.mean((w_diff - w_true) ** 2)):5.2f} rad/s")
print(f"observer estimate: rms error {np.sqrt(np.mean((w_obs - w_true) ** 2)):5.2f} rad/s")true speed 149.3 rad/s
differenced encoder: rms error 7.85 rad/s
observer estimate: rms error 0.04 rad/s
The observer's speed error is about 200 times smaller here, because the simulated model is exact. Its accuracy rests on the model: a wrong or an unmodelled load torque biases it, which is why practical designs add a disturbance state or keep the observer poles moderately fast.
Guided lab: observer on a motor rig
Hand calculation. Observer poles at , , : estimate errors fall to under 1 % within about ; the speed estimate's noise is set mainly by the slowest observer pole.
Simulation. Move the observer poles to , , in the second program: the rms error rises from to about , still better than differencing but much noisier. Fast observers pass quantisation noise through.
What you would measure. Encoder motor on a current-limited supply with a microcontroller logging, at , the differenced and the observed speed while the motor runs at a constant ; a separate tachometer or a long-window count average gives the reference speed. Expect the differenced speed to show steps of and the observer to stay within a few rad/s, with a bias of a few percent if or is off by 10 %.
Hazards and safe setup. As for any motor rig: clamp it, guard the shaft, set the current limit; log data rather than reach near the shaft while it turns.
Pitfalls
- Observer poles far faster than needed: noisy estimates and large gains.
- An observer with a wrong model: it converges to the wrong answer without complaint; check estimate against a measured state during commissioning.
- Feeding the observer a different from what the actuator really applied (for example before a saturation): the estimate drifts. Use the saturated, applied value.
- Forgetting that separation holds for the model, not for every model error: test robustness by simulating with perturbed and .
Practice
- For and , find for observer poles at and .
- A controller has dominant poles at . Suggest a range for the observer poles.
Answer
- , polynomial ; desired : .
- Two to five times faster in real part: about to , then check the noise in simulation.
Test yourself
The estimation error of a Luenberger observer evolves as
- always
The separation principle says the closed-loop poles of controller plus observer are
- only the controller poles
- the eigenvalues of together with those of
- the product of both
- the plant poles
How can ct.place design an observer gain?
- it cannot
L = ct.place(A.T, C.T, poles).T, by dualityL = ct.place(A, C, poles)L = K.T
Why is an observer's speed estimate smoother than a differenced encoder signal?
- it uses a faster sample rate
- it integrates a model and uses the measurement only through a correction with limited bandwidth
- it ignores the encoder
- it has no poles