This repository provides a tutorial-oriented simulation of a digital PI control loop applied to a first-order system identified via non-parametric methods.
The objective is to reproduce in simulation the same discrete behavior expected when the controller is later implemented on a microcontroller (Arduino, Teensy, ESP32, etc.), including actuator saturation.
β οΈ Note: This tutorial is for educational purposes only. It focuses on simulation and understanding discrete-time digital control.
- Model a first-order process identified experimentally (non-parametric fit)
- Discretize the plant using Zero-Order Hold (ZOH)
- Implement a discrete PI controller using incremental form
- Add saturation limits to emulate real actuator constraints
- Compare reference tracking and control signal behavior
A first-order model without delay was identified experimentally from step-response data:
In the included example:
This model is discretized with sampling period
using Zero-Order Hold.
Ts = 0.1;
gp = tf(20,[50 1]); % First-order plant
gpd = c2d(gp,Ts,'zoh'); % Discrete plant
[num,den] = tfdata(gpd,'v');The discrete model implemented is:
y(k) = num(2)*u1 - den(2)*y1;The discrete control law implemented is:
error = Ref(k) - y(k);
u = u1 + K0*error + K1*error1;With tuning parameters derived from:
- Proportional gain:
$$K_p$$ - Integral time:
$$T_i$$ - Sampling time:
$$T_s$$
Where
To emulate microcontroller behavior, the controller output is limited to a predefined range:
- Prevents unrealistic actuator commands
- Reflects PWM or DAC limits on embedded hardware
- Avoids integrator wind-up if actuator saturates
Example: 0% β€ u(n) β€ 100%
Without saturation, simulation results may falsely assume an ideal actuator with infinite authority, which never matches microcontroller deployments.
To emulate real microcontroller behavior β such as PWM range or fixed DAC limits β
a hard saturation is enforced:
if u > 100
u=100;
end
if u< 0
u=0;
endBelow are example plots generated with the script:
MIT License
