Implement discrete controllers
Use difference equations and test delay, quantisation, and timing. This module has 3 lessons and takes about 2h 25m. Each lesson has a practice exercise that runs real Python in your browser and a short quiz.
Start module 12 in the Learning LabThe z domain and difference equations
Translate a discrete recursion into a transfer function.
For y[k+1]=a*y[k]+b*u[k] with zero initial state, z*Y=a*Y+b*U, so G(z)=b/(z-a). Dividing by z gives b*z^-1/(1-a*z^-1), which exposes the one-step input-to-output delay.
The discrete pole is z=a. Stability requires abs(a)<1. Positive a gives same-sign decay; negative a gives alternating decay. A continuous pole s maps under exact sampling to z=exp(s*Ts).
Difference equation coefficients depend on the chosen powers. scipy.signal.lfilter uses coefficients in z^-1, while transfer-function objects normally use descending powers of z. State the convention before using a library function.
See it in action
from scipy import signal u = [1.]*6 # y[k] = 0.8*y[k-1] + 0.2*u[k-1] y = signal.lfilter([0.,0.2],[1.,-0.8],u) print(y)
Indexing and delay are part of the control model.
Incremental PI and quantisation
Implement a controller recursion and measure sensor resolution.
For forward-Euler integration I[k]=I[k-1]+Ki*Ts*e[k-1] and u[k]=Kp*e[k]+I[k], subtraction gives u[k]=u[k-1]+Kp*(e[k]-e[k-1])+Ki*Ts*e[k-1].
A backward-Euler integral instead uses the current error in the last term. Both are valid design choices, but mixing their coefficients changes the controller. Document which recursion you use.
An n-bit converter with 2^n levels over span S has a nominal bin width S/2^n. Quantisation limits the smallest visible error and can cause small limit cycles. Scaling and clipping must happen before values reach the controller.
See it in action
kp, ki, ts = 2., 0.5, 0.1
e, old_e, old_u = 0.8, 1., 0.4
u = old_u+kp*(e-old_e)+ki*ts*old_e
print('Next PI command:',u)
print('12-bit resolution over 10 V:',10/2**12,'V')
Write the discrete equation before translating it into code.
Practical: test a one-sample delay
Show how computation delay changes closed-loop poles.
For y[k+1]=a*y[k]+b*u[k] and proportional u[k]=K*(r-y[k]), the no-delay pole is a-b*K. If the actuator receives the previous command, augment the state with v[k]=u[k-1].
The delayed homogeneous dynamics are [y_next,v_next]=[[a,b],[-K,0]]*[y,v]. This extra state changes the characteristic polynomial to z^2-a*z+b*K. A gain that works without delay may fail with it.
Use a=0.8, b=0.2. For K=5 the no-delay pole is -0.2, while the delayed pair has magnitude one. For K=6 the delayed loop is unstable. A missed deadline is a control event, not just a software performance issue.
Your experiment plan
Use the browser editor or the downloadable notebook. All plant data are simulated. Keep a notebook with parameters, predictions, results, and units.
- Compare delayed and no-delay pole plots for the listed gains.
- Simulate both implementations with the same reference.
- Add sensor quantisation and log its effect near the target.
- Write a timing requirement and a missed-deadline response for this simulated system.
What to hand in
- A delay comparison plot.
- A table of pole magnitudes.
- A clear statement of update order and timing assumptions.
Reflect on your results
Which assumption matters most? Describe one result that surprised you and one experiment that would test your explanation.
The automatic exercise checks the core code behaviour. Review observations and written deliverables yourself or with a mentor.
See it in action
import numpy as np
for gain in [2.,5.,6.]:
no_delay = 0.8-0.2*gain
delayed = np.linalg.eigvals([[0.8,0.2],[-gain,0.]])
print('K:',gain,'No delay:',no_delay,'With delay:',delayed)
Budget computation and communication delay as part of controller design.
Free, open and run by a nonprofit.
This page is part of Control Engineering, a free course of 20 modules and 60 lessons in the engineering.fit( ) Learning Lab. The Learning Lab is built and run by engineering.fit( ), a nonprofit founded by Abdullateef Opeyemi Bakare that helps African engineering students gain research experience. You do not need an account or pay a fee to learn.