This example is interactive. Click the play button on any cell to execute it, or run all cells in sequence.

PID Controller

Simulation of a PID controller tracking a step-changing setpoint.

You can also find this example as a single file in the GitHub repository.

The control system uses a PID controller block that computes the control signal based on the error between setpoint and output. The plant is modeled as an Integrator with a gain. As a block diagram it looks like this:

block diagram of a pid controlled plant

Import and Setup

First let's import the required classes and blocks:

Python
Loading...

System Parameters

We define the plant gain and PID parameters.

Python
Loading...

System Definition

Now we can construct the system by instantiating the blocks we need and collecting them in a list:

Python
Loading...

The connections form a feedback control loop. The Adder block with signature "+-" computes the error signal by subtracting the plant output from the setpoint.

Python
Loading...

Simulation Setup and Execution

We initialize the simulation with the RKCK54 solver (Runge-Kutta Cash-Karp 5th order with adaptive step size).

Python
Loading...
12:43:59 - INFO - LOGGING (log: True)
12:43:59 - INFO - BLOCKS (total: 6, dynamic: 2, static: 4, eventful: 0)
12:43:59 - INFO - GRAPH (nodes: 6, edges: 8, alg. depth: 4, loop depth: 0, runtime: 0.102ms)
12:43:59 - INFO - STARTING -> TRANSIENT (Duration: 100.00s)
12:43:59 - INFO - --------------------   1% | 0.0s<0.2s | 1131.2 it/s
12:43:59 - INFO - ####----------------  20% | 0.1s<189:23:32 | 1733.3 it/s
12:43:59 - INFO - ########------------  40% | 0.1s<0.1s | 2326.1 it/s
12:43:59 - INFO - ############--------  60% | 0.2s<36:31:17 | 1894.0 it/s
12:43:59 - INFO - ################----  80% | 0.2s<0.0s | 1818.4 it/s
12:43:59 - INFO - #################### 100% | 0.3s<--:-- | 1785.0 it/s
12:43:59 - INFO - FINISHED -> TRANSIENT (total steps: 455, successful: 291, runtime: 284.07 ms)

Results

Let's plot the setpoint, output, and error signals to see how well the PID controller tracks the setpoint:

Python
Loading...
Output