This repository contains some adaptive control examples using the Python Control Systems Library.
The blog post Adaptive PI Control with Python walks through an example and might be helpful.
The blog post Linear Aircraft Models describes the models contained in the aircraft-dynamics directory.
To get started check out the Python Control Systems Library documentation.
Switch to Python 3.8 before running the code.
pyenv local 3.8.16This is tested with Python Control 0.9.3.post2.
The following installation instructions were written for OSX, but should be easily adapted to other environments.
From the docs:
The package requires numpy and scipy, and the plotting routines require matplotlib. In addition, some routines require the slycot library in order to implement more advanced features
Start by installing the basic dependencies
pip3 install numpy scipy matplotlibTo install the Slycot first install some dependencies.
First a FORTRAN compiler is needed.
The post How to install gfortran on Mac OS X was helpful pointing to Sourceforge to download gfortran for OSX and extract the archive with the tar command:
gunzip gcc-12.1-m1-bin.tar.gz
sudo tar -xvf gcc-12.1-m1-bin.tar -C /Now we can check that it was installed
gfortran --version
# GNU Fortran (GCC) 12.1.0
# Copyright (C) 2022 Free Software Foundation, Inc.
# This is free software; see the source for copying conditions. There is NO
# warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.Next CMake was downloaded from https://cmake.org/download/, installed, and to the shell path .bashrc or .zshrc:
PATH="/Applications/CMake.app/Contents/bin":"$PATH"Install Slycot with the following command.
pip3 install slycotThe following section describes the couple errors I encountered during installation and the steps to solve the errors.
The first error I encountered during installation was.
ERROR: Could not build wheels for scipy which use PEP 517 and cannot be installed directly
First upgrade pip.
/Library/Developer/CommandLineTools/usr/bin/python3 -m pip install --upgrade pipNext installed Ninja with
brew install ninjaNext error
Could NOT find Python (missing: Python_LIBRARIES Interpreter Development
NumPy Development.Module Development.Embed)
Couldn't quite figure this one out after a few minutes of searching, but saw a comment that using Python 3.8 worked. Install Python 3.8 and switch using
pyenv install 3.8
pyenv local 3.8.16Afterwards everything worked as expected.
The Python Control Systems Library was then easily installed:
pip3 install controlThis section contains some errors I had encountered previously when installing dependencies. Attempting to install Slycot resulted in the error:
AttributeError: module 'enum' has no attribute 'IntFlag'?
The following Stack Overflow post Why Python 3.6.1 throws AttributeError: module 'enum' has no attribute 'IntFlag'? provides the following solution:
pip3 uninstall -y enum34A second attempt to install Slycot resulted in another error:
AttributeError: type object 'Callable' has no attribute '_abc_registry'
Another Stack Overflow post AttributeError: type object 'Callable' has no attribute 'abc_registry' gave the following solution:
pip3 uninstall typingFinally Slycot was installed successfully.
Yet another error was encountered when attempting to import Slycot and yet another Stack Overflow post, How to fix ImportError: cannot import name wrapper using slycot offered a solution: brew install gcc49 which unfortunately did not work:
gcc@4.9: This formula either does not compile or function as expected on macOS
versions newer than High Sierra due to an upstream incompatibility.
Discussion in a relatively recent issue Slycot/issues/46 suggested instead of installing gcc, to instead rename the wrapper in site-packages/slycot directory, the location of which was easily found with python3 -v followed by import slycot and on my environment was in:
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/slycot
The next attempt to import Slycot resulted in the error:
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'
Which was finally solved by a quick reinstallation of numpy.
pip3 uninstall numpy
pip3 install numpyFinally all the dependencies were installed and working correctly.
Some examples and commonly used references:
- Cruise control design example (as a nonlinear I/O system)
- control.NonlinearIOSystem
- control.LinearIOSystem
- control.InterconnectedSystem
- LTI system representation
num = np.array([1.])
den = np.array([J, B])
sys = control.TransferFunction(num, den)
sys = StateSpace(A, B, C, D)
s = TransferFunction.s
G = (s + 1)/(s**2 + 2*s + 1)Note: What does the "at" (@) symbol do in Python?
If you see an @ in the middle of a line, that's a different thing, matrix multiplication.