Skip to content

About

Python control systems study of a PI-controlled plant, covering step response, pole-zero analysis, Bode plots, stability margins, and gain sensitivity.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

PI Control

Step response and stability of a PI-controlled plant

Python PI control 24 figures

SciPy Stability Gain Lab CSV + JSON


Live Links

What Link
Repository https://github.com/rohaan2802/PI_Control
GitHub Pages https://rohaan2802.github.io/PI_Control/
SciPy LTI reference https://docs.scipy.org/doc/scipy/reference/signal.html#continuous-time-linear-systems

Table of Contents

  1. Live Links
  2. Overview
  3. What's Inside
  4. Model and Stability
  5. Baseline Results
  6. How the Metrics Work
  7. Gain Experiments
  8. Limitations
  9. Folder Structure
  10. How to Run
  11. Interactive Gain Lab
  12. Reproduce and Export
  13. Screenshots
  14. About the Author

Overview

A Python study of a proportional–integral controller for the damped plant G(s) = 1/[s(s + 2)]. It examines reference tracking, closed-loop stability, and open-loop frequency margins.

What's Inside

Resource File
Original assignment script original_pi_controller.py (reference copy)
Analysis and CLI main.py
Plot exporters plots.py
Verification notes verification.md
Gain comparison CLI compare.py
Python project file pyproject.toml
Browser simulator gain_lab.js
Dependencies requirements.txt
Website preview index.html

Model and Stability

Under negative unity feedback, L(s) = (Kp s + Ki)/[s²(s + 2)] and T(s) = (Kp s + Ki)/(s³ + 2s² + Kp s + Ki). With positive gains, the Routh array gives Ki < 2 Kp. The controller zero is −Ki/Kp.

From the plant to the characteristic polynomial

The plant has one integrator and one pole at −2. The controller adds another open-loop integrator and a zero at −Ki/Kp. Substituting the loop into the unity feedback equation gives the characteristic polynomial [1, 2, Kp, Ki]. The first column of its Routh array is 1, 2, (2 Kp − Ki)/2, Ki. Positive gains on the boundary Ki = 2 Kp give imaginary-axis poles, so the CLI classifies that case as not asymptotically stable.

Baseline Results

The original gains are Kp = 2.02488613 and Ki = 0.06465480. The 160 s simulation gives 1.441 s rise time, 7.738% overshoot, 16.29 s settling time, and 63.303° phase margin. The stable closed-loop DC gain is one. Gain margin is infinite for the baseline loop.

How the Metrics Work

Rise time uses the first 10% and 90% crossings of the unit reference, with interpolation between samples. Overshoot is measured relative to the unit target. Settling uses the last sample outside the ±2% band. Steady-state error comes from DC gain. Stability margins come from the open loop, while closed-loop magnitude and phase describe reference tracking.

Reading a report

Field How to interpret it
sampled_final The simulated output at the final time, which can differ from the analytical limit
peak_time_s The sampled time of the largest response in the simulation window
settling_time_s The first sample after the last excursion outside the 2% band. A null value means no settling time was established.
gain_crossover_rad_s Frequency where the open-loop magnitude is one
phase_margin_deg Angular distance to −180° at unity gain

Gain Experiments

The gallery holds one gain fixed while sweeping the other. It also shows the poles and unit-step responses around the Routh boundary. These sweeps illustrate sensitivity to the gains. They do not automate controller tuning.

Limitations

This model assumes a linear continuous-time plant, ideal feedback, a unit step, and zero initial conditions. There is no hardware validation. Finite sampling limits peak and settling-time precision, especially near the stability boundary.

Folder Structure

PI_Control/
├── pi_control/      Analysis and export code
├── docs/            Verification notes
├── assets/          24 figures, previews and site assets
├── pyproject.toml    Installable project configuration
├── requirements.txt
├── index.html
└── README.md

How to Run

python -m venv .venv
.venv/Scripts/python -m pip install -r requirements.txt
.venv/Scripts/python -m pi_control --output outputs

Omit the output flag to print the report only. Use --kp, --ki, or --duration to compare another configuration. Output files are generated on demand.

Interactive Gain Lab

Open the Gain Lab and vary Kp or the ratio Ki / (2 Kp). The chart compares the selected gains with the original controller and the unit reference. Set the ratio to 1 with Set boundary, or restore the original gains with Reset to baseline.

For the unit step, the browser solves y‴ + 2y″ + Kp y′ + Ki y = Ki. The initial output and first derivative are zero, and the second derivative just after the step is Kp. The browser uses fourth-order Runge–Kutta with a 0.01 s step over 30 s. The Python command-line study uses SciPy and defaults to a 160 s window.

Reproduce and Export

python -m pi_control --output outputs/baseline
python -m pi_control.compare --csv outputs/gain_comparison.csv

The first command exports metrics.json, step_response.csv, step_response.png, pole_zero.png, open_loop_bode.png and closed_loop_bode.png. The CSV columns are time in seconds, output, and signed tracking error. The comparison command exports seven configurations with their actual gains and measured results.

Gain choice Stability Overshoot 2% settling
Original PI gains Stable 7.74% 16.29 s
Half Kp Stable 9.92% 30.68 s
Double Kp Stable 17.62% 3.72 s
Double Ki Stable 10.87% 19.30 s
Ki = 1.8 Kp Stable 94.64% 110.01 s
Ki = 2 Kp Marginal — —
Ki = 2.2 Kp Unstable — —

This comparison uses the same 160 s horizon. Doubling Kp settles sooner in this model at the cost of more overshoot. Moving Ki near the boundary leaves the stable response weakly damped.

To install the project after activating a Python 3.14 virtual environment, run python -m pip install -e . in the project root. The configuration in pyproject.toml adds the pi-control and pi-compare console commands. The Python module commands work directly from the repository with the dependencies installed.

Screenshots

24 captures on consistent 1440 × 900 frames. Select any preview to open the full image.

Model

Plant and PI controller · Open-loop transfer function · Closed-loop transfer function · Routh stability condition · Baseline poles and zero · Slow pole and zero close-up · Model assumptions · Command-line workflow

Step

Full unit-step response · First peak close-up · Rise-time thresholds · Settling inside the 2% band · Baseline step metrics · Tracking error over time · Why the horizon matters

Frequency

Open-loop magnitude · Open-loop phase · Closed-loop magnitude · Closed-loop phase · Baseline stability margins

Tradeoffs

Proportional gain sweep · Integral gain sweep · Pole movement as Ki rises · Across the stability boundary

Plant and PI controller

Plant and PI controller

Continuous-time transfer functions from the original assignment.

Open-loop transfer function

Open-loop transfer function

The loop combines the controller and plant. Its response determines the stability margins.

Closed-loop transfer function

Closed-loop transfer function

Unity feedback gives a third-order characteristic polynomial.

Routh stability condition

Routh stability condition

For positive gains, strict stability requires Ki < 2 Kp.

Full unit-step response

Full unit-step response

The long horizon shows the slow approach to the analytical final value.

First peak close-up

First peak close-up

The baseline peak occurs at about 3.14 s.

Rise-time thresholds

Rise-time thresholds

The first crossings of 0.1 and 0.9 define the 10–90% rise time.

Settling inside the 2% band

Settling inside the 2% band

The estimated settling time is 16.29 s on the baseline grid.

Baseline step metrics

Baseline step metrics

Metrics use the unit target as the steady-state value.

Tracking error over time

Tracking error over time

The signed difference between the unit target and the output.

Baseline poles and zero

Baseline poles and zero

The three poles lie in the left half-plane.

Slow pole and zero close-up

Slow pole and zero close-up

The real pole and controller zero are close together but distinct.

Open-loop magnitude

Open-loop magnitude

The gain crossover is where the open-loop magnitude reaches 0 dB.

Open-loop phase

Open-loop phase

The open-loop phase is evaluated at gain crossover to obtain phase margin.

Closed-loop magnitude

Closed-loop magnitude

This response describes reference tracking across frequencies.

Closed-loop phase

Closed-loop phase

Closed-loop phase is separate from the open-loop stability margins.

Baseline stability margins

Baseline stability margins

An infinite gain margin indicates no finite −180° phase crossover in the baseline loop.

Proportional gain sweep

Proportional gain sweep

Simulated responses at half, baseline, and double proportional gain with Ki held fixed.

Integral gain sweep

Integral gain sweep

Simulated responses at half, baseline, and double integral gain with Kp held fixed.

Pole movement as Ki rises

Pole movement as Ki rises

Blue points are stable and orange points lie beyond the Routh boundary.

Across the stability boundary

Across the stability boundary

Responses on either side of Ki = 2 Kp illustrate why the strict inequality matters.

Model assumptions

Model assumptions

The study uses an ideal continuous-time plant and a linear controller.

Why the horizon matters

Why the horizon matters

A finite sample and the true steady-state limit answer different questions.

Command-line workflow

Command-line workflow

Reproduce the study and export plots or the machine-readable report.

About the Author

Mohammad Rohaan · Control systems coursework.

GitHub profile

About

Python control systems study of a PI-controlled plant, covering step response, pole-zero analysis, Bode plots, stability margins, and gain sensitivity.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages