-
Notifications
You must be signed in to change notification settings - Fork 0
/
plant.py
executable file
·357 lines (304 loc) · 12.5 KB
/
plant.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# Copyright (c) 2016 Konstantinos G. Papdopoulos. All rights reserved.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License v1.0 which accompanies this distribution,
# and is available at http://www.eclipse.org/legal/epl-v10.html
from collections import defaultdict
from scipy.interpolate import pade
import control
import logging
import math
import matplotlib
import matplotlib.pyplot as plt
import random
class Plant:
"""
Controlled process
"""
"""
Creates a Test run object
"""
def __init__(self, args):
"""
Initializes the appropriate test component objects according to the
test_type and the test configuration json object, in order to prepare
the test for running
:param args:
:param json_conf:
:param test_type:
:type args:
:type json_conf:
:type test_type: str
"""
tzi, tpj, plantGp = self.create_plant(args)
def create_plant(self, args):
"""Creates the controlled process determined by the user input
Plant creation supports poles (max:5), zeros (max:5), time delay
:param poles
:param zeros
:param time_delay
:param user_defined_plant
:
:type
:type
:type
:type
"""
kp = random.random();
zerosOrder = args.zeros
polesOrder = args.poles
time_delay = args.time_delay
zeros = self.zerosRandomGeneration(zerosOrder)
poles = self.polesRandomGeneration(polesOrder)
tzi = self.tziDictCreate(zeros, zerosOrder)
tpj = self.tpjDictCreate(poles, polesOrder)
# time delay generation
# --------------------------------------------------------------------------
timeDelay = 100.0*random.random()
# Sort poles and zeros to identify dominant pole and dominant zeros
# ----------------------------------------------------------------------
numGp = self.createZerosCoefficients(zerosOrder, tzi)
denGp = self.createPolesCoefficients(polesOrder, tpj);
kpnumGp = [i * kp for i in numGp]
plantGp = control.tf(kpnumGp,denGp);
Gd = self.createTimeDelayPlant(timeDelay)
Gp = control.series(plantGp, Gd)
print(plantGp)
print(Gd)
print(Gp)
(T , yout) = control.step_response(plantGp)
self.plotStepResponse(T, yout, kp)
return tzi, tpj, plantGp
def tziDictCreate(self, zeros, zerosOrder):
"""
"""
zerosSorted = [];
zerosSorted = sorted(zeros, reverse=True)
tzi = {}; tzi_names = [];
if int(zerosOrder) > 0:
tzi_names = ['tz'+str(i+1) for i in range(int(zerosOrder))]
for i in range(len(tzi_names)):
tzi[tzi_names[i]] = zerosSorted[i]
return tzi
def tpjDictCreate(self, poles, polesOrder):
"""
"""
polesSorted = [];
polesSorted = sorted(poles, reverse=True)
tpj = {}; tpj_names = [];
if int(polesOrder) > 0:
tpj_names = ['tp'+str(j+1) for j in range(int(polesOrder))]
for j in range(len(tpj_names)):
tpj[tpj_names[j]] = polesSorted[j]
return tpj
def polesRandomGeneration(self, polesOrder):
"""
"""
poles = [];
# poles generation
# --------------------------------------------------------------------------
for i in range(int(polesOrder)):
if polesOrder == 0:
poles = [0]
else:
poles.append(random.random())
return poles
def zerosRandomGeneration(self, zerosOrder):
"""
"""
# zeros generation
# --------------------------------------------------------------------------
zeros = [];
for j in range(int(zerosOrder)):
if zerosOrder == 0:
zeros = [0]
else:
zeros.append(random.random())
return zeros
def createTimeDelayPlant(self, timeDelay):
"""
"""
n1 = 0
n2 = 0
n3 = pow(timeDelay,3) / 6
if timeDelay != 0:
n1 = timeDelay
n2 = pow(timeDelay,2) / 2.0
n3 = pow(timeDelay,3) / 6.0
e_exp = [1.0, 1.0/math.factorial(1.0),
(1.0/math.factorial(2)) * pow(timeDelay,2),
(1.0/math.factorial(3)) * pow(timeDelay,3),
(1.0/math.factorial(4)) * pow(timeDelay,4),
(1.0/math.factorial(5)) * pow(timeDelay,5)]
padeApproximationOrder = 20
numGd, denGd = control.pade(timeDelay, padeApproximationOrder)
Gd = control.tf(numGd, denGd)
return Gd
def createZerosCoefficients(self, zerosOrder, tzi):
"""
"""
q0 = 1 ;
print('tzi-dictionary:',tzi)
print('---------------------------------------------------------------')
q0 = 1
print('---------------------------------------------------------------')
print('---------------------------------------------------------------')
q1 = 0
q1 = sum(tzi.values());
print('---------------------------------------------------------------')
q2 = 0
for i in range(int(zerosOrder)):
for j in range(int(zerosOrder)):
if (i != j) and (i < j):
print('i:',i+1,'j:',j+1);
q2 = tzi['tz'+str(i+1)]*tzi['tz'+str(j+1)] + q2
print('q2->',q2)
print('---------------------------------------------------------------')
q3 = 0
for i in range(int(zerosOrder)):
for j in range(int(zerosOrder)):
for k in range(int(zerosOrder)):
if (i != j != k != i) and (i < j < k):
print('i:',i+1,'j:',j+1,'k:',k+1);
q3 = tzi['tz'+str(i+1)] * tzi['tz'+str(j+1)] * \
tzi['tz'+str(k+1)] + q3
print('q3->',q3)
print('---------------------------------------------------------------')
q4 = 0
for i in range(int(zerosOrder)):
for j in range(int(zerosOrder)):
for k in range(int(zerosOrder)):
for l in range(int(zerosOrder)):
if (i != j != k != l != i) and (i < j < k < l):
print('i:',i+1,'j:',j+1,'k:',k+1,'l:',l+1);
q4 = tzi['tz'+str(i+1)] * tzi['tz'+str(j+1)] * \
tzi['tz'+str(k+1)] * tzi['tz'+str(l+1)] + q4
print('q4->',q4)
print('tzi-dictionary:', tzi)
print('---------------------------------------------------------------')
numGp = [q4, q3, q2, q1, q0];
return numGp
def createPolesCoefficients(self, polesOrder, tpj):
"""
"""
pGp0 = 1
print('---------------------------------------------------------------')
pGp1 = 0
pGp1 = sum(tpj.values());
print('---------------------------------------------------------------')
pGp2 = 0
for i in range(int(polesOrder)):
for j in range(int(polesOrder)):
if (i != j) and (i < j):
print('i:',i+1,'j:',j+1);
pGp2 = tpj['tp'+str(i+1)]*tpj['tp'+str(j+1)] + pGp2
print('pGp2->',pGp2)
print('---------------------------------------------------------------')
pGp3 = 0
for i in range(int(polesOrder)):
for j in range(int(polesOrder)):
for k in range(int(polesOrder)):
if (i != j != k != i) and (i < j < k):
print('i:',i+1,'j:',j+1,'k:',k+1);
pGp3 = tpj['tp'+str(i+1)]*tpj['tp'+str(j+1)] * \
tpj['tp'+str(k+1)] + pGp3
print('pGp3->',pGp3)
print('---------------------------------------------------------------')
pGp4 = 0
for i in range(int(polesOrder)):
for j in range(int(polesOrder)):
for k in range(int(polesOrder)):
for l in range(int(polesOrder)):
if (i != j != k != l != i) and (i < j < k < l):
print('i:',i+1,'j:',j+1,'k:',k+1,'l:',l+1);
pGp4 = tpj['tp'+str(i+1)]*tpj['tp'+str(j+1)] * \
tpj['tp'+str(k+1)]*tpj['tp'+str(l+1)] + pGp4
print('pGp4->',pGp4)
print('---------------------------------------------------------------')
pGp5 = 0
for i in range(int(polesOrder)):
for j in range(int(polesOrder)):
for k in range(int(polesOrder)):
for l in range(int(polesOrder)):
for m in range(int(polesOrder)):
if (i != j != k != l != m != i) and (i < j < k < l < m):
print('i:',i+1,'j:',j+1,'k:',k+1,'l:',l+1,'m:',m+1);
pGp5 = tpj['tp'+str(i+1)]*tpj['tp'+str(j+1)] * \
tpj['tp'+str(k+1)]*tpj['tp'+str(l+1)] * \
tpj['tp'+str(m+1)] + pGp5
print('pGp5->',pGp5)
print('---------------------------------------------------------------')
print('pGp0->',pGp0)
print('pGp1->',pGp1)
print('pGp2->',pGp2)
print('pGp3->',pGp3)
print('pGp4->',pGp4)
print('pGp5->',pGp5)
denGp = [pGp5, pGp4, pGp3, pGp2, pGp1, pGp0];
return denGp
def plantRandom(self, args):
"""
"""
alpha = 0.1
pass
def plantDetermined(self, args):
"""
"""
pass
def plantAlpha(self, args):
pass
def plotStepResponse(self, T, yout, kp):
""" The method takes the vector of a step response in T, yout form plus the
dc gain kp at steady state and plot on Figure with the step response plus a
straight horizontal line based in kp
"""
kpLine = [1] * len(T)
fig, ax = plt.subplots()
ax.plot(T, yout)
kpLine = [i * kp for i in kpLine]
ax.plot(T, kpLine)
ax.set(xlabel='time (s)', ylabel='$y_{out}$',
title='Plant $G_p$ Open Loop step response')
ax.legend([ 'step response','dc-gain: kp:' + str(format(kp, '.2f'))])
plt.grid(color='k', linestyle='-.', linewidth=0.1)
plt.show()
def plantEstimation(self, poles,zeros,time_delay,user_defined_plant):
"""The method takes the real plant as an input and returns the settling
time of the plant's step response, the estimated DC gain (plant's steady
state gain, an estimation of the plant's unmodelled dynamics and the
plant transfer function [first order mode] based on the step response
measurements). All above measurements will be used as an input for
initializing the automatic tuning algorith,
Plant creation supports poles (max:5), zeros (max:5), time delay
:param poles
:param zeros
:param time_delay
:param user_defined_plant
:
:type
:type
:type
:type
"""
pass
def plantNormalize(self):
"""The method takes the real plant as an input and returns the settling
time of the plant's step response, the estimated DC gain (plant's steady
state gain, an estimation of the plant's unmodelled dynamics and the
plant transfer function [first order mode] based on the step response
measurements). All above measurements will be used as an input for
initializing the automatic tuning algorithm,
Plant creation supports poles (max:5), zeros (max:5), time delay
Plant creation supports poles (max:5), zeros (max:5), time delay
:param poles
:param zeros
:param time_delay
:param user_defined_plant
:
:type
:type
:type
:type
"""
pass