Engee 文档
Notebook

连接模型

在本示例中,您将了解如何在 Engee 中建立 LTI 系统的连接模型。从简单的串行和并行连接到复杂的框图。

概述

Engee 提供了许多功能来帮助您创建 LTI 模型关联。这些功能包括

  • 串行和并行连接 (series,parallel)
  • 反馈连接 (feedback,feedback2dof)
  • 输入和输出连接 ([ , ],[ ; ],append,array2mimo)
  • 一般流程图 (connect)

为了说明这些示例,让我们连接必要的库,创建两个具有以下传递函数的系统:

In [ ]:
Pkg.add(["RobustAndOptimalControl", "ControlSystems"])
In [ ]:
using ControlSystems
In [ ]:
H1 = tf(2,[1, 3, 0])
Out[0]:
TransferFunction{Continuous, ControlSystemsBase.SisoRational{Int64}}
   2
--------
s^2 + 3s

Continuous-time transfer function model
In [ ]:
H2 = zpk([],[-5],5)
Out[0]:
TransferFunction{Continuous, ControlSystemsBase.SisoZpk{Int64, Int64}}
   1
5-----
 s + 5

Continuous-time transfer function model

串行连接

例如,使用* 语句或series 函数串联 LTI 模型:

gsconnectingmodels_01.png

In [ ]:
H = H2 * H1
Out[0]:
TransferFunction{Continuous, ControlSystemsBase.SisoZpk{Int64, Complex{Int64}}}
          1
10-----------------
  (s + 5)(s + 3)(s)

Continuous-time transfer function model
In [ ]:
H = series(H1,H2)
Out[0]:
TransferFunction{Continuous, ControlSystemsBase.SisoZpk{Int64, Complex{Int64}}}
          1
10-----------------
  (s + 5)(s + 3)(s)

Continuous-time transfer function model

并行连接

例如,使用+ 运算符或parallel 函数并行连接 LTI 模型:

gsconnectingmodels_02.png

In [ ]:
H = H1 + H2
Out[0]:
TransferFunction{Continuous, ControlSystemsBase.SisoZpk{Float64, ComplexF64}}
   (1.0s + 2.6433981132056608)(1.0s + 0.7566018867943395)
5.0------------------------------------------------------
               (1.0s + 3.0)(1.0s)(1.0s + 5.0)

Continuous-time transfer function model
In [ ]:
H = parallel(H1,H2)
Out[0]:
TransferFunction{Continuous, ControlSystemsBase.SisoZpk{Float64, ComplexF64}}
   (1.0s + 2.6433981132056608)(1.0s + 0.7566018867943395)
5.0------------------------------------------------------
               (1.0s + 3.0)(1.0s)(1.0s + 5.0)

Continuous-time transfer function model

连接反馈

带有负耦合的系统结构图的标准视图:

gsconnectingmodels_03.png

要定义封闭系统,请使用函数feedback

In [ ]:
H = feedback(H1,H2)
Out[0]:
TransferFunction{Continuous, ControlSystemsBase.SisoZpk{Float64, ComplexF64}}
                                    1.0s + 5.0
2.0-----------------------------------------------------------------------------
   (1.0s + 5.663076827457541)(1.0s^2 + 2.3369231725424586s + 1.7658245340262375)

Continuous-time transfer function model

请注意,默认反馈为负。要使反馈为正,请使用以下语法:

In [ ]:
H = feedback(H1, H2, pos_feedback = true)
Out[0]:
TransferFunction{Continuous, ControlSystemsBase.SisoZpk{Float64, ComplexF64}}
                                   1.0s + 5.0
2.0---------------------------------------------------------------------------
   (1.0s^2 + 8.515690830166166s + 19.3914636736468)(1.0s - 0.5156908301661673)

Continuous-time transfer function model

组合输入和输出

您可以通过输入来合并两个型号 H1 和 H2 的输入:

In [ ]:
H = [H1 H2]
Out[0]:
TransferFunction{Continuous, ControlSystemsBase.SisoZpk{Int64, Complex{Int64}}}
Input 1 to output 1
     1
2----------
 (s + 3)(s)

Input 2 to output 1
   1
5-----
 s + 5

Continuous-time transfer function model

得到的模型有两个输入和一个输出,如下图所示:

gsconnectingmodels_05.png

同样,您也可以通过输入合并 H1 和 H2 的输出:

In [ ]:
H = [ H1 ; H2 ]
Out[0]:
TransferFunction{Continuous, ControlSystemsBase.SisoZpk{Int64, Complex{Int64}}}
Input 1 to output 1
     1
2----------
 (s + 3)(s)

Input 1 to output 2
   1
5-----
 s + 5

Continuous-time transfer function model

合并后的模型 H 有两个输出和一个输入,对应于下面的流程图:

gsconnectingmodels_06.png

最后,您可以通过以下方法将两个模型的输入和输出结合起来:

In [ ]:
H = append(H1,H2)
Out[0]:
TransferFunction{Continuous, ControlSystemsBase.SisoZpk{Int64, Complex{Int64}}}
Input 1 to output 1
     1
2----------
 (s + 3)(s)

Input 1 to output 2
 1
0-
 1

Input 2 to output 1
 1
0-
 1

Input 2 to output 2
   1
5-----
 s + 5

Continuous-time transfer function model

得出的模型 H 有两个输入和两个输出,并与结构图相对应: gsconnectingmodels_07.png

您可以使用连接功能从基本的 SISO 模型建立 MIMO 模型,例如

In [ ]:
[H1 -tf(10,[1, 10]); 0 H2]
Out[0]:
TransferFunction{Continuous, ControlSystemsBase.SisoZpk{Int64, Complex{Int64}}}
Input 1 to output 1
     1
2----------
 (s + 3)(s)

Input 1 to output 2
 1
0-
 1

Input 2 to output 1
     1
-10------
   s + 10

Input 2 to output 2
   1
5-----
 s + 5

Continuous-time transfer function model

您还可以使用函数array2mimo 。例如

In [ ]:
P = ss(-1,1,1,0);
sys_array = fill(P, 2, 2) # Создает массив систем
mimo_sys = array2mimo(sys_array)
Out[0]:
StateSpace{Continuous, Int64}
A = 
 -1   0   0   0
  0  -1   0   0
  0   0  -1   0
  0   0   0  -1
B = 
 1  0
 0  1
 1  0
 0  1
C = 
 1  1  0  0
 0  0  1  1
D = 
 0  0
 0  0

Continuous-time state-space model

基于流程图建立模型

您可以使用函数和操作的组合来构建简单流程图的模型。例如,请看下面的流程图:

gsconnectingmodels_09.png 图块 F、C、G、S 的数据如下:

In [ ]:
s = tf('s');
F = 1/(s + 1);
G = 100/(s^2 + 5*s + 100);
C = 20*(s^2 + s + 60)/s/(s^2 + 40*s + 400);
S = 10/(s + 10);

闭环系统从 r 到 y 的传递函数如下:

In [ ]:
T = F * feedback(G*C,S)
plot(step(T,10))
Out[0]:

但如果要绘制更复杂的方框图,请使用RobustAndOptimalControl.jl 库中的函数connect 。要使用connect ,请按以下步骤操作:

  • 识别图中的所有块,包括求和块;
  • 命名模块的所有输入和输出通道;
  • 将系统模块的输出(输出)与模块的相应输入(输入)匹配起来。

让我们以下面的结构图为例,看看如何使用该函数。 gsconnectingmodels_11.png

导入并连接库RobustAndOptimalControl.jl

In [ ]:
import Pkg
Pkg.add("RobustAndOptimalControl")
   Resolving package versions...
  No Changes to `~/.project/Project.toml`
  No Changes to `~/.project/Manifest.toml`
In [ ]:
using RobustAndOptimalControl

使用功能named_ss ,为每个单元设置输入和输出信号的名称。

In [ ]:
F1 = named_ss(F, x=:xF, u=:r, y=:u_f)
Out[0]:
NamedStateSpace{Continuous, Float64}
A = 
 -1.0
B = 
 1.0
C = 
 1.0
D = 
 0.0

Continuous-time state-space model
With state  names: xF
     input  names: r
     output names: u_f
In [ ]:
G1 = named_ss(G, x=:xG, u=:u, y=:y_m)
Out[0]:
NamedStateSpace{Continuous, Float64}
A = 
  0.0   16.0
 -6.25  -5.0
B = 
 0.0
 2.0
C = 
 3.125  0.0
D = 
 0.0

Continuous-time state-space model
With state  names: xG1 xG2
     input  names: u
     output names: y_m
In [ ]:
C1 = named_ss(C, x=:xC, u=:e, y=:u_c)
Out[0]:
NamedStateSpace{Continuous, Float64}
A = 
 0.0    4.0    0.0
 0.0    0.0   16.0
 0.0  -25.0  -40.0
B = 
 0.0
 0.0
 8.0
C = 
 2.34375  0.15625  2.5
D = 
 0.0

Continuous-time state-space model
With state  names: xC1 xC2 xC3
     input  names: e
     output names: u_c
In [ ]:
S1 = named_ss(S, x=:xS, u=:y_m, y=:y)
Out[0]:
NamedStateSpace{Continuous, Float64}
A = 
 -10.0
B = 
 4.0
C = 
 2.5
D = 
 0.0

Continuous-time state-space model
With state  names: xS
     input  names: y_m
     output names: y

使用函数sumblock 指定加法器。

In [ ]:
Sum1 = sumblock("e = r - y")
Out[0]:
sumblock: NamedStateSpace{Continuous, Float64}
D = 
 1.0  -1.0

Continuous-time state-space model
With state  names: 
     input  names: r y
     output names: e
In [ ]:
Sum2 = sumblock("u = u_f + u_c")
Out[0]:
sumblock: NamedStateSpace{Continuous, Float64}
D = 
 1.0  1.0

Continuous-time state-space model
With state  names: 
     input  names: u_f u_c
     output names: u

重要的是要仔细标记连接。连接的原则是一个程序块的输出连接到另一个程序块的输入。例如,信号еSum1 的输出,但却是程序块C 的输入。

In [ ]:
connections = [
    :e => :e # output e Sum1 to input C
    :y => :y # output y S to input Sum1
    :u => :u # output u Sum2 to input G
    :u_f => :u_f # output u_f F to input Sum2
    :u_c => :u_c # output u_c C to input Sum2
    :y_m => :y_m # output G to input S
]
Out[0]:
6-element Vector{Pair{Symbol, Symbol}}:
   :e => :e
   :y => :y
   :u => :u
 :u_f => :u_f
 :u_c => :u_c
 :y_m => :y_m

定义输入信号w1 和输出信号z1 也很重要。

In [ ]:
w1 = [:r]
z1 = [:y_m]
Out[0]:
1-element Vector{Symbol}:
 :y_m

将所有计算数据输入connect 。您可以在 Julia System Integration 文档中了解有关所有函数参数的更多信息。

In [ ]:
P = connect([F1, C1, G1, S1, Sum1, Sum2], connections; w1,z1,unique = false)
Out[0]:
NamedStateSpace{Continuous, Float64}
A = 
 -1.0  0.0       0.0       0.0   0.0    0.0    0.0
  0.0  0.0       4.0       0.0   0.0    0.0    0.0
  0.0  0.0       0.0      16.0   0.0    0.0    0.0
  0.0  0.0     -25.0     -40.0   0.0    0.0  -20.0
  0.0  0.0       0.0       0.0   0.0   16.0    0.0
  2.0  4.6875    0.3125    5.0  -6.25  -5.0    0.0
  0.0  0.0       0.0       0.0  12.5    0.0  -10.0
B = 
 1.0
 0.0
 0.0
 8.0
 0.0
 0.0
 0.0
C = 
 0.0  0.0  0.0  0.0  3.125  0.0  0.0
D = 
 0.0

Continuous-time state-space model
With state  names: xF##feedback#587 xC1##feedback#587 xC2##feedback#587 xC3##feedback#587 xG1##feedback#587 xG2##feedback#587 xS##feedback#587
     input  names: r
     output names: y_m

让我们来构建闭环系统的瞬态过程。

In [ ]:
plot(stepinfo(step(P,5)))
Out[0]:

结论

本文介绍了连接模型的方法。您可以在 Building Systems 中了解更多有关转换结构图的函数。