-
Notifications
You must be signed in to change notification settings - Fork 1
/
Thirdorder_RTT.py
116 lines (95 loc) · 3.06 KB
/
Thirdorder_RTT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 31 21:02:02 2013
@author: Nokukhanya Khwela
Modified by Rowly Mudzhiba
"""
import numpy as np
from scipy import linalg
from scipy import signal
from plotgraphs import *
import MODminifunc as func
from Tuners import*
# from DDEfunction import DDE
from EulerODE import Euler, closedloop_sim
from scipy import*
# Process Transfer function
Gp_n = [0.125]
Gp_d = [1, 3,3, 1]
# Simulation Settings
SP = 1 # Set Point
tfinal = 100 # simulation period
dt = .1
DT = 5 # Dead time (s)
t = np.arange(0, tfinal, dt)
entries = len(t)
num = 100 # number of tuning constant sets
y = np.zeros((entries, num))
por = np.zeros(num) # What are these two variables?
tr = np.zeros(num)
# Controller choice
Contr_type = 'PI' # Choose controller by typing 'P' , 'PI' or 'PID'
if Contr_type == 'P':
Controller = 1
elif Contr_type == 'PI':
Controller = 2
elif Contr_type == 'PID':
Controller = 3
[k_c, t_i, t_d] = func.RPG(num, Controller) # Random Parameter Generator
# Options: 1= P, 2 = PI, 3 = PID
# Different Ysp inputs
SP_input = 'step' # Choose Set point input by typing 'step' or 'ramp'
step_time = 0
ramp_time = 5.
# u = func.Ramp(t,ramp_time,SP)
u = func.Step(t, step_time, SP)
SP_info = [SP_input, step_time, ramp_time, SP]
kczn, tizn, tdzn = ZN(Gp_n, Gp_d, t, u, Contr_type, DT)
# Ziegler-Nichols settings via function ZN
kcch, tich, tdch = Cohen_Coon(Gp_n, Gp_d, t, u, Contr_type, DT)
## System responce
for k in range(0, num):
if k == num - 1:
kc = kczn # Inserting Ziegler-Nicholas parameters
ti = tizn
td = tdzn
elif k == num - 2: # Inserting Cohen-Coon parameters
kc = kcch
ti = tich
td = tdch
else:
kc = k_c[k]
ti = t_i[k]
td = t_d[k]
if ti == 0:
Gc_d = 1
Gc_n = [kc * ti * td, (kc * ti), kc]
else:
Gc_n = [kc * ti * td, (kc * ti), kc]
Gc_d = [ti, 0]
# The process and controller transfer functions are multiplied to make the
# open loop TF
OL_TF_n = np.polymul(Gp_n, Gc_n)
OL_TF_d = np.polymul(Gc_d, Gp_d)
CL_TF_n = OL_TF_n
CL_TF_d = np.polyadd(OL_TF_d, OL_TF_n)
(A, B, C, D) = signal.tf2ss(OL_TF_n, OL_TF_d)
# Open Loop Transfer Function is converted to State Space
(A_CL, B_CL, C_CL, D_CL) = signal.tf2ss(CL_TF_n, CL_TF_d)
# Closed Loop Transfer Function is converted to State Space
rootsA = np.array(linalg.eigvals(A_CL))
# step_response = signal.lsim((A,B,C,D),u,t,X0=None,interp=1)[1]
# step_responseDDE = DDE(A,B,C,D,t,SP_info,DT)
step_responseEuler = Euler(A, B, C, D, t, u, DT)
if (rootsA.real < 0).all():
for i in range(0, entries): # Stabilty of the Closed Loop is checked
Y = step_responseEuler
y[i, k] = Y[i]
else:
y[:, k] = np.NaN
k_c[k] = kc
t_i[k] = ti
kc = k_c
ti = t_i
y = y.T
fig = plotgraphs(kc, ti, y, num, entries, t, tfinal, dt, SP)