A comprehensive, first-principles Python library for modeling, analysis, design, simulation, and estimation of linear and nonlinear control systems.
Focus: MIMO Systems, Derivative-Free Optimization, Real-Time MPC, and Hardware-in-the-Loop (HIL)
PyControls is designed to bridge the gap between abstract control theory and reliable numerical implementation. Unlike standard libraries that often wrap compiled binaries (such as LAPACK/BLAS) behind opaque function calls, PyControls implements core algorithms in pure, JIT-compiled Python. This ensures conceptual clarity for students and inspectability for researchers, without sacrificing performance.
Every algorithm exposes its internal mechanics:
- The Extended Kalman Filter (EKF) computes Jacobians dynamically using Complex-Step Differentiation rather than accepting pre-computed matrices.
- The Model Predictive Controller (MPC) explicitly pre-computes condensed quadratic program (QP) matrices, allowing inspection of Hessians and gradients before optimization.
PyControls prioritizes numerically stable methods:
- Complex-Step Differentiation replaces finite differences to eliminate subtractive cancellation errors (
core/ekf.py). - Adaptive Integrators (RK45) are implemented from scratch with access to internal error metrics (
core/solver.py). - Numba JIT Compilation accelerates tight computational loops while preserving Python readability.
All systems are treated as Multiple-Input Multiple-Output (MIMO). Single-input single-output (SISO) systems are handled as degenerate MIMO cases. All state-space representations (A, B, C, D) enforce matrix semantics.
File: core/solver.py
Function: manual_matrix_exp
Discretization of linear systems requires computing the matrix exponential:
exp(A t)Eigen-decomposition methods are numerically unstable for defective matrices. PyControls instead implements Scaling and Squaring:
- Scaling โ The matrix
Ais divided by2^suntil its norm is below 0.5. - Approximation โ A truncated Taylor series or Padรฉ approximant evaluates
exp(A / 2^s). - Squaring โ The result is squared
stimes to recoverexp(A).
This routine is JIT-compiled with Numba, achieving near C-level performance without reliance on heavy external libraries.
File: core/solver.py
Class: NonlinearSolver
PyControls implements an adaptive DormandโPrince (RK45) integrator:
-
Two simultaneous state estimates are computed: 4th-order (
xโ) and 5th-order (xโ). -
The local truncation error is estimated as
||xโ โ xโ||. -
Step size adapts dynamically:
- Error > tolerance โ step rejected and reduced.
- Error โช tolerance โ step size increased.
This maximizes simulation speed while maintaining accuracy, even for stiff systems.
Files: core/ekf.py, core/mpc.py
Jacobian computation avoids finite differences:
f(x + iยทh) โ f(x) + iยทhยทf'(x)Taking the imaginary part yields exact derivatives to machine precision:
f'(x) = Im(f(x + iยทh)) / hPyControls uses h = 1e-20, achieving high accuracy with zero subtractive cancellation error.
File: core/mpc.py
The MPC module features a dual-mode solver that automatically selects the appropriate algorithm.
Trigger: When A and B matrices are provided.
- Uses Alternating Direction Method of Multipliers (ADMM).
- Employs a condensed formulation, optimizing only the input sequence.
- Hessian and inverse are pre-computed during initialization.
- Online execution uses only matrixโvector products and clipping for constraints.
This results in constant-time execution with respect to horizon length during runtime.
Trigger: When a nonlinear model_func is supplied.
- Uses Iterative Linear Quadratic Regulator (iLQR).
- Avoids forming a global Hessian, maintaining O(N) memory and time complexity.
- Includes LevenbergโMarquardt regularization for robustness.
- Forward-pass line search ensures monotonic cost reduction.
File: core/ekf.py
- Supports simultaneous state and parameter estimation.
- Jacobians are computed using Complex-Step Differentiation.
- Automatically detects vectorized user models for efficient Jacobian evaluation.
- Physical parameters can be modeled as slowly varying states, enabling real-time model adaptation.
File: core/ukf.py
- Implements the Van der Merwe scaled sigma-point formulation.
- Ideal for highly nonlinear systems where EKF linearization is insufficient.
The HIL_PWM_Firmware directory contains embedded firmware for real-time hardware interfacing via Arduino/PlatformIO.
Firmware Highlights (src/main.cpp):
- Minimal ASCII serial protocol (
Q:VALUE,A:VALUE). - ADC oversampling with averaging to reduce noise and increase effective resolution.
- Watchdog safety logic zeros actuators on communication loss.
This enables safe and robust real-time control experiments.
โโโ benchmarking/
โ โโโ benchmark_csd.py
โ โโโ benchmark_dare.py
โ โโโ benchmark_ekf_ukf_timing.py
โ โโโ benchmark_expm.py
โ โโโ benchmark_jit.py
โ โโโ benchmark_mpc.py
โ โโโ benchmark_mpc_optimality.py
โ โโโ benchmark_ode.py
โ โโโ benchmark_scalability.py
โ โโโ benchmark_scipy_speed.py
โ โโโ benchmark_stability_margins.py
โ โโโ control_metrics.py
โ โโโ estimation_metrics.py
โโโ core/
โ โโโ __init__.py
โ โโโ analysis.py
โ โโโ control_utils.py
โ โโโ ekf.py
โ โโโ ekf_discrete.py
โ โโโ estimator.py
โ โโโ exceptions.py
โ โโโ math_utils.py
โ โโโ mpc.py
โ โโโ solver.py
โ โโโ state_space.py
โ โโโ transfer_function.py
โ โโโ ukf.py
โโโ docs/
โ โโโ Benchmarks Guide.md
โ โโโ Complexity Analysis.md
โ โโโ Equations and Formulae.md
โ โโโ LICENSE
โ โโโ README.md
โโโ helpers/
โ โโโ config.py
โ โโโ exit.py
โ โโโ plot.py
โ โโโ simulation_runner.py
โ โโโ system_registry.py
โโโ modules/
โ โโโ __init__.py
โ โโโ interactive_lab.py
โ โโโ physics_engine.py
โโโ systems/
โ โโโ __init__.py
โ โโโ battery.py
โ โโโ dc_motor.py
โ โโโ pendulum.py
โ โโโ thermistor.py
โโโ tests/
โ โโโ test_analysis.py
โ โโโ test_control.py
โ โโโ test_jit_vec.py
โ โโโ test_kf_ekf.py
โ โโโ test_math.py
โ โโโ test_models.py
โ โโโ test_modules.py
โ โโโ test_solver.py
โ โโโ test_systems.py
โ โโโ test_ukf_mpc.py
โโโ benchmark_runner.py
โโโ main.py
โโโ pyproject.toml
โโโ requirements.txt
โโโ script.py
โโโ test_runner.pyPyControls includes a comprehensive 13-suite automated benchmarking harness evaluated across 5-run statistical averages:
| Module / Operation | PyControls Metric | Comparison vs SciPy / Industry | Key Engineering Advantage |
|---|---|---|---|
| Matrix Exponential ( |
|
JIT zero-overhead execution for online LPV discretization | |
| Numerical Jacobian (CSD) |
|
Machine-precision derivatives ( |
|
| DARE Solver (SDA, |
|
Quadratic doubling algorithm ( |
|
| ZOH Discretization ( |
|
Exact block matrix exponential with zero wrapper overhead | |
| Numba JIT Acceleration ( |
|
LLVM compilation to native x86 machine instructions | |
| Linear MPC (ADMM, |
Real-time capable up to |
Condensed pre-factored QP matrices with vector clipping | |
| Nonlinear Estimation (UKF) |
|
Superior sigma-point tracking through Coulomb stiction |
For complete derivations, mathematical concepts, and metrics, see docs/Benchmarks Guide.md.
pip install -r requirements.txtNote:
numbais heavily used for solver performance. Ensure it is correctly installed for your architecture.
python main.pypython test_runner.pypython benchmark_runner.pyThis project is licensed under the MIT License. See docs/LICENSE for details.
Version: 5.5.0
Last Updated: 18 August 2026