2020年12月3日 星期四

Python 的控制系統套件 : control

現代控制系統書籍大都使用 MATLAB 與 LabVIEW 模擬計算, 例如下面這兩本 :

Modern Control Systems, 13/e (Robert H. Bishop, 2016)

現在的情況是, MATLAB 能做的 Python 也可以辦到, 在控制系統也不例外, Python 的控制系統套件名為 python-control, 目前還很年輕 (最新 v0.8.3 版), 說明文件參考 :


此專案為開放原始碼, 寄存於 GitHub :


這個套件是以 Numpy, SciPy 與 MatplotLib 為基礎建構的, 可用來進行回授控制系統分析與設計, 包含時域與頻域響應, 例如步階響應, 脈衝響應, 繪製波德圖與奈奎斯圖等, 也能分析系統的穩定度與可觀測度等, 還實作了可用來預測的卡爾曼濾波器. 最重要的是, 此套件的 control.matlab 模組提供了與 MATLAB 相同的函數來模擬 MATLAB 的功能, 這對已熟悉 MATLAB 的使用者而言非常方便.  

以前在學校念控制系統時都是純手算, 根軌跡與波德圖也是手繪, 現在依靠軟體或程式就能輕易模擬系統響應, 也能繪製出美美的圖形, 這個時代的學生真幸福. 

安裝 control 套件只要在命令列直接用 pip 安裝即可 : 

pip install control 

C:\Users\User>pip install control   
Collecting control
  Downloading control-0.8.3.tar.gz (249 kB)
Requirement already satisfied: numpy in c:\python37\lib\site-packages (from control) (1.16.0)
Requirement already satisfied: scipy in c:\python37\lib\site-packages (from control) (1.4.1)
Requirement already satisfied: matplotlib in c:\python37\lib\site-packages (from control) (3.2.1)
Requirement already satisfied: python-dateutil>=2.1 in c:\python37\lib\site-packages (from matplotlib->control) (2.7.5)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in c:\python37\lib\site-packages (from matplotlib->control) (2.3.1)
Requirement already satisfied: kiwisolver>=1.0.1 in c:\python37\lib\site-packages (from matplotlib->control) (1.0.1)
Requirement already satisfied: numpy in c:\python37\lib\site-packages (from control) (1.16.0)
Requirement already satisfied: cycler>=0.10 in c:\python37\lib\site-packages (from matplotlib->control) (0.10.0)
Requirement already satisfied: six in c:\python37\lib\site-packages (from cycler>=0.10->matplotlib->control) (1.12.0)
Requirement already satisfied: setuptools in c:\python37\lib\site-packages (from kiwisolver>=1.0.1->matplotlib->control) (40.9.0)
Requirement already satisfied: six in c:\python37\lib\site-packages (from cycler>=0.10->matplotlib->control) (1.12.0)
Requirement already satisfied: numpy in c:\python37\lib\site-packages (from control) (1.16.0)
Using legacy 'setup.py install' for control, since package 'wheel' is not installed.
Installing collected packages: control
    Running setup.py install for control ... done
Successfully installed control-0.8.3

然後隨便找一個範例來測試, 例如模仿 MATLAB 工具箱函數的二階函數時域與頻域響應分析程式, 原始碼參考 :

 
#second-order.py 
import os
import matplotlib.pyplot as plt   # MATLAB plotting functions
from control.matlab import *  # MATLAB-like functions

# Parameters defining the system
m = 250.0           # system mass
k = 40.0            # spring constant
b = 60.0            # damping constant

# System matrices
A = [[0, 1.], [-k/m, -b/m]]
B = [[0], [1/m]]
C = [[1., 0]]
sys = ss(A, B, C, 0)

# Step response for the system
plt.figure(1)
yout, T = step(sys)
plt.plot(T.T, yout.T)
plt.show(block=False)

# Bode plot for the system
plt.figure(2)
mag, phase, om = bode(sys, logspace(-2, 2), Plot=True)
plt.show(block=False)

# Nyquist plot for the system
plt.figure(3)
nyquist(sys, logspace(-2, 2))
plt.show(block=False)

# Root lcous plot for the system
rlocus(sys)

if 'PYCONTROL_TEST_EXAMPLES' not in os.environ:
    plt.show()

將此測試程式存檔後, 在命令列執行 :

D:\test>python second-order.py   

結果會用 matplotlib 繪製四張時域與頻域分析圖形 :






哇, 真的是太棒了, 效果不輸 MATLAB. 

除了安裝 control 套件外, 還需要安裝 slycot 套件, 因為部分模擬 MATLAB 的函數中會用到, 但我直接安裝卻出現與 PEP517 有關的錯誤 : 

C:\Users\User>pip3 install slycot  
Collecting slycot
  Downloading slycot-0.4.0.0.tar.gz (1.5 MB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
Requirement already satisfied: numpy in c:\python37\lib\site-packages (from slycot) (1.16.0)
Building wheels for collected packages: slycot
  Building wheel for slycot (PEP 517) ... error
  ERROR: Command errored out with exit status 1:
   command: 'c:\python37\python.exe' 'c:\python37\lib\site-packages\pip\_vendor\pep517\_in_process.py' build_wheel 'C:\Users\User\AppData\Local\Temp\tmptijbstmo'
       cwd: C:\Users\User\AppData\Local\Temp\pip-install-dq3ymzle\slycot

  -- Trying "NMake Makefiles (Visual Studio 15 2017 Win64 v141)" generator - failure
  --------------------------------------------------------------------------------

  ********************************************************************************
  scikit-build could not get a working generator for your system. Aborting build.

  Building windows wheels for Python 3.7 requires Microsoft Visual Studio 2017.
  Get it with "Visual Studio 2017":

    https://visualstudio.microsoft.com/vs/

  ********************************************************************************
  ----------------------------------------
  ERROR: Failed building wheel for slycot
Failed to build slycot
ERROR: Could not build wheels for slycot which use PEP 517 and cannot be installed directly

我到加州大學爾灣分校流體動力研究所網站找到 slycot 的 wheel 檔就可以順利安裝了 :


D:\test>pip3 install slycot-0.3.5-cp37-cp37m-win_amd64.whl    
Processing d:\test\slycot-0.3.5-cp37-cp37m-win_amd64.whl
Installing collected packages: slycot
Successfully installed slycot-0.3.5

注意, 要下載與自己電腦 Python 版本相同者, 否則無法安裝, 例如我的是 Python 3.7 :

D:\test>python
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

若下載 cp38 的 slycot 就無法安裝 :

D:\test>pip3 install slycot-0.3.5-cp38-cp38-win_amd64.whl  
ERROR: slycot-0.3.5-cp38-cp38-win_amd64.whl is not a supported wheel on this platform.

會用到 slycot 套件的 control.matlab 函數例如 ss2tf(), 此函數可以將系統從狀態空間 (state space) 轉換為轉移函數 (transfer function), 參考 :


用現成範例測試確實可用 : 

>>> from control.matlab import *   
>>> A = [[1., -2], [3, -4]]
>>> B = [[5.], [7]]
>>> C = [[6., 8]]
>>> D = [[9.]]
>>> sys1 = ss2tf(A, B, C, D)
>>> sys1

9 s^2 + 113 s + 118
-------------------
   s^2 + 3 s + 2

第二種作法是先呼叫 ss() 建立一個狀態空間系統, 再呼叫 ss2tf(), 參考 :


>>> sys_ss = ss(A, B, C, D)  
>>> sys_ss
A = [[ 1. -2.]
 [ 3. -4.]]

B = [[5.]
 [7.]]

C = [[6. 8.]]

D = [[9.]]
>>> sys2 = ss2tf(sys_ss)  
>>> sys2

9 s^2 + 113 s + 118
-------------------
   s^2 + 3 s + 2

還不錯, 這樣沒有 MATLAB 還是可以進行控制系統分析. 

沒有留言:

張貼留言