https://github.com/crissccl/digital_controlsim
Tutorial-oriented simulation of a discrete-time PI control loop applied to a first-order system. Includes actuator saturation to emulate real microcontroller behavior. Designed for educational purposes and digital control learning.
https://github.com/crissccl/digital_controlsim
arduino digital-control education esp32 first-order-system matlab pi-controller saturation simulation teensy tutorial
Last synced: 3 months ago
JSON representation
Tutorial-oriented simulation of a discrete-time PI control loop applied to a first-order system. Includes actuator saturation to emulate real microcontroller behavior. Designed for educational purposes and digital control learning.
- Host: GitHub
- URL: https://github.com/crissccl/digital_controlsim
- Owner: CrissCCL
- License: mit
- Created: 2025-10-26T12:43:19.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2025-10-26T17:19:18.000Z (3 months ago)
- Last Synced: 2025-10-26T18:29:07.303Z (3 months ago)
- Topics: arduino, digital-control, education, esp32, first-order-system, matlab, pi-controller, saturation, simulation, teensy, tutorial
- Language: MATLAB
- Homepage:
- Size: 46.9 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ๐งช Digital Control Simulation โ First Order System + Saturation
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.
## ๐ฏ Goals of the Tutorial
- 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
## ๐งฉ System Model
A first-order model without delay was identified experimentally from step-response data:
$$
G_p(s) = \frac{K}{\tau s + 1}
$$
In the included example:
$$
G_p(s)= \frac{20}{50s + 1}
$$
This model is discretized with sampling period
$$T_s = 0.1 s$$
using Zero-Order Hold.
```matlab
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:
```matlab
y(k) = num(2)*u1 - den(2)*y1;
```
## โ๏ธ Digital PI Controller (Incremental Form)
The discrete control law implemented is:
$$
u(k)=u(k-1)+K_0 e(k)+K_1 e(k-1)
$$
```matlab
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
$$
K_0 = K_p + \frac{K_p}{2T_i}T_s
$$
$$
K_1 = -K_p + \frac{K_p}{2T_i}T_s
$$
## ๐ Actuator Saturation
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:
```matlab
if u > 100
u=100;
end
if u< 0
u=0;
end
```
Below are example plots generated with the script:
## ๐ License
MIT License