1、安装
我使用的是pycharm,因此直接搜索control安装即可。注意,最好已经事先安装了scipy,numpy以及matplotlib这几个库以备不时之需。
2、文档地址
https://python-control.readthedocs.io/en/0.8.2/
http://python-control.sourceforge.net/manual/
(第二个我觉得更好一些,第一个版本更新,但是没有代码实例)
3、使用例子
1、创建控制系统:
import control as ctrl
sysTf=ctrl.tf([1],[1,2,1])
sysTf为由传递函数定义的系统。但是按照文档说明,这个库还支持用状态空间定义的系统。不过,作为一个自控的初学者,目前只用得到传递函数。
这个传递函数定义了一个振荡环节,相当于:
G ( s ) = 1 ( s ω n ) 2 + 2 ψ ω n s + 1 G(s)=\frac{1}{(\frac{s}{\omega_n})^2+2\frac{\psi}{\omega_n}s+1}G(s)=(ωns)2+2ωnψs+11
很多课本上都有这个环节的伯德图。若要画出这个图,则需要这么做:
import control as ctrl
import matplotlib.pyplot as plt
import numpy as np
psiList = [0.05,0.2,0.5,0.707,1.0]
sysTfList=[]
for psi in psiList:
sysTfList.append(ctrl.tf([1],[1,2*psi,1]))
mag,phase,omega=ctrl.bode(sysTfList,omega=np.logspace(start=-1,stop=1,num=200),dB=True,deg=False)
plt.show()
当然,图片还是有美中不足的地方:缺少图例。
4、适当修改源码中的一些部分
以下是我修改过的freqplot.py,在pycharm中按下ctrl+B追踪control.bode_plot()这个方法即可找到这个文件。
主要修改是增加了bode()的参数表,比原来的代码增加了一个legend变量,传入一个与sysList等长度的字符串列表作为图例的文字。
原理是新建了一个名为lineList的列表,保存循环中sysList的每一项所绘出的线条对象。
目前仅修改了margins!=True情况时绘出的图形,因此在伯德图中可以显示图例。用法如下:
import control as ctrl
import matplotlib.pyplot as plt
import math
import numpy as np
psiList = [0.05,0.2,0.5,0.707,1.0]
sysTfList=[]
for psi in psiList:
sysTfList.append(ctrl.tf([1],[1,2*psi,1]))
mag,phase,omega=ctrl.bode(sysTfList,omega=np.logspace(start=-1,stop=1,num=200),dB=True,deg=False,
legends=['0.05','0.2','0.5','0.707','1.0'])
plt.show()
——————————————————————————————————————
生成的图片如下:
以下是源代码:
# freqplot.py - frequency domain plots for control systems
#
# Author: Richard M. Murray
# Date: 24 May 09
#
# This file contains some standard control system plots: Bode plots,
# Nyquist plots and pole-zero diagrams. The code for Nichols charts
# is in nichols.py.
#
# Copyright (c) 2010 by California Institute of Technology
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the California Institute of Technology nor
# the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CALTECH
# OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $Id$
import matplotlib
import matplotlib.pyplot as plt
import scipy as sp
import numpy as np
import math
from .ctrlutil import unwrap
from .bdalg import feedback
from .margins import stability_margins
__all__ = ['bode_plot', 'nyquist_plot', 'gangof4_plot',
'bode', 'nyquist', 'gangof4']
#
# Main plotting functions
#
# This section of the code contains the functions for generating
# frequency domain plots
#
# Bode plot
def bode_plot(syslist, omega=None, dB=None, Hz=None, deg=None,
Plot=True, omega_limits=None, omega_num=None, margins=None,legends=[],*args, **kwargs):
"""
Bode plot for a system
Plots a Bode plot for the system over a (optional) frequency range.
Parameters
----------
syslist : linsys
List of linear input/output systems (single system is OK)
omega : list
List of frequencies in rad/sec to be used for frequency response
dB : boolean
If True, plot result in dB
Hz : boolean
If True, plot frequency in Hz (omega must be provided in rad/sec)
deg : boolean
If True, plot phase in degrees (else radians)
Plot : boolean
If True, plot magnitude and phase
omega_limits: tuple, list, ... of two values
Limits of the to generate frequency vector.
If Hz=True the limits are in Hz otherwise in rad/s.
omega_num: int
number of samples
margins : boolean
If True, plot gain and phase margin
\*args, \**kwargs:
Additional options to matplotlib (color, linestyle, etc)
Returns
-