-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyfomcon.py
397 lines (340 loc) · 15.3 KB
/
pyfomcon.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QMessageBox
import numpy as np
from fotf import *
#gui
from pyGui import fotfviewergui, createnewfotfgui
STATUSBAR_TIME = 5000
#__all__ = ['loadsets','gg1','gg2','gg3']
class FotfViewForm(QMainWindow, fotfviewergui.Ui_MainWindow_fotfviewer):
def __init__(self):
QMainWindow.__init__(self)
fotfviewergui.Ui_MainWindow_fotfviewer.__init__(self)
self.setWindowIcon(QIcon('index.png'))
self.setupUi(self)
# Checks for frequency domain
self.lowerFreq = self.higherFreq = self.freqDataPoints = True
# Checks for time Domain
self._input = self._STOPTIME = self._STARTTIME = self._greaterthan = self._stepok = True
#Personal Edits and Method calls/Subscribed Events
self.foregroundRole()
self.reloadAllFOTransFunc()
self.pushButton_AddFotf.clicked.connect(self.addnewFotf)
self.pushButton_EditFOTF.clicked.connect(self.editFOTF)
self.pushButton_DeleteFOTF.clicked.connect(self.deleteFOTF)
self.pushButtonViewInConsole.clicked.connect(self.ViewInConsole)
self.pushButtonGetOustaloop.clicked.connect(self.OustaloopModel)
self.pushButton_StabilityTest.clicked.connect(self.StabilityTest)
self.pushButton_BodePlot.clicked.connect(self.BodePlot)
self.pushButtonSimulate.clicked.connect(self.Step)
self.comboBoxFOTF.currentIndexChanged.connect(self.ComboBoxFOTFempty)
self.lineEdit_LowerFreq.editingFinished.connect(self._LowerFreq)
self.lineEdit_HigherFreq.editingFinished.connect(self._HigherFreq)
self.lineEdit_FreqDataPoints.editingFinished.connect(self._FreqDataPoints)
self.lineEdit_inputTime.editingFinished.connect(self._isinputok)
self.lineEdit_StartTime.editingFinished.connect(self._isstarttimeok)
self.lineEdit_StepTime.editingFinished.connect(self._issteptimeok)
self.lineEdit_StopTime.editingFinished.connect(self._isstoptimeok)
self.isDialogActive =False
self.show()
def Exit(self):
reply = QMessageBox.question(self, "Exit?", "Would you like to exit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
sys.exit()
def reloadAllFOTransFunc(self):
g1, g2, g3,g4 = loadsets()
xvalues = list(locals().items())
self.comboBoxFOTF.clear()
for i, j in xvalues:
if isinstance(j, FOTransFunc):
self.comboBoxFOTF.addItem(i, j)
#self.comboBoxTimeDomainType.addItems(["Step","Impulse"])
self.comboBoxTimeDomainType.addItems(["Step"])
def addnewFotf(self):
createnew = newfotfgui()
createnew.exec_()
try:
sysname, zero, pole, dt = createnew.lineEditSysName.text(), createnew.lineEdit_ZeroPoly.text(),\
createnew.lineEdit_PolePoly.text(), createnew.lineEdit_DelayText.text()
if (sysname or zero or pole or dt) != "":
self.comboBoxFOTF.addItem(createnew.lineEditSysName.text(),
newfotf(createnew.lineEdit_ZeroPoly.text(),
createnew.lineEdit_PolePoly.text(),
createnew.lineEdit_DelayText.text()))
self.comboBoxFOTF.setCurrentIndex(self.comboBoxFOTF.count()-1)
except:
self.statusbar.showMessage('pyfomcon.addnewFotf: FOTF Addition Failed', STATUSBAR_TIME)
print('\nfofopdtguiclass._addData: FOFOPDT Addition Failed\n')
def editFOTF(self):
createnew = newfotfgui()
createnew.foregroundRole()
createnew.lineEditSysName.setText(self.comboBoxFOTF.currentText())
currentindex = self.comboBoxFOTF.currentIndex()
x = self.comboBoxFOTF.currentData()
num, nnum,den,nden, dt = fotfparam(x)
if num.size > 1 and 1 < nnum.size == num.size:
Zeros= poly2str(num,nnum)
else:
a = num**nnum
Zeros = str(a[0])
if den.size > 1 and 1 < nden.size == den.size:
Poles = poly2str(den,nden)
else:
b = den**nden
Poles = str(b[0])
createnew.lineEdit_ZeroPoly.setText(Zeros),
createnew.lineEdit_PolePoly.setText(Poles),
createnew.lineEdit_DelayText.setText(str(dt))
createnew.exec_()
_sysname = createnew.lineEditSysName.text()
_zero = createnew.lineEdit_ZeroPoly.text()
_pole = createnew.lineEdit_PolePoly.text()
_dt = createnew.lineEdit_DelayText.text()
self.comboBoxFOTF.setCurrentIndex(currentindex)
if _sysname != "":
self.comboBoxFOTF.setCurrentText(_sysname)
if _zero != "" and _pole != "" and _dt != "":
try:
self.comboBoxFOTF.setItemData(currentindex, newfotf(_zero,_pole,float(_dt)))
except:
QMessageBox.question(self, 'Error',
"Input values are not correct.\nEDIT ABORTED!",
QMessageBox.StandardButtons(QMessageBox.Ok))
else:
QMessageBox.question(self, 'Error',
"Input values are not correct.\nEDIT ABORTED!",
QMessageBox.StandardButtons(QMessageBox.Ok))
else:
QMessageBox.question(self, 'Error',
"System Name Empty!.\nEDIT ABORTED!",
QMessageBox.StandardButtons(QMessageBox.Ok))
def deleteFOTF(self):
self.comboBoxFOTF.removeItem(self.comboBoxFOTF.currentIndex())
def ViewInConsole(self):
x = self.comboBoxFOTF.itemData(self.comboBoxFOTF.currentIndex())
sysname = self.comboBoxFOTF.currentText()
if x != None and isinstance(x,FOTransFunc):
self.statusbar.showMessage('View Console for Transfer Function of {}'.format(sysname), STATUSBAR_TIME)
print( sysname + ':')
print(x)
def OustaloopModel(self):
x = self.comboBoxFOTF.itemData(self.comboBoxFOTF.currentIndex())
if x != None and isinstance(x,FOTransFunc):
print(self.comboBoxFOTF.currentText() + '.Oustaloop():')
print(x.oustaloop())
def StabilityTest(self):
x = self.comboBoxFOTF.itemData(self.comboBoxFOTF.currentIndex())
if x != None and isinstance(x, FOTransFunc):
x.isstable(doPlot=True)
def BodePlot(self):
x = self.comboBoxFOTF.itemData(self.comboBoxFOTF.currentIndex())
if isinstance(x,FOTransFunc):
try:
lowExp = int(self.lineEdit_LowerFreq.text())
highExp = int(self.lineEdit_HigherFreq.text())
dataPoints= int(self.lineEdit_FreqDataPoints.text())
x.freqresp(lowExp,highExp,dataPoints)
except:
pass
else:
QMessageBox.question(self, 'Error',"There is no FOTF object in the Combo Box, Use the 'Add' button",
QMessageBox.StandardButtons(QMessageBox.Ok))
def Step(self):
#TODO:check that stop is greater than start
start = float(self.lineEdit_StartTime.text())
stop = float(self.lineEdit_StopTime.text())
step = float(self.lineEdit_StepTime.text())
intnumofsteps = int((stop-start)/step)
t = np.linspace(start,stop,intnumofsteps)
u = np.ones_like(t) * float(self.lineEdit_inputTime.text())
sys = self.comboBoxFOTF.itemData(self.comboBoxFOTF.currentIndex())
if self.comboBoxTimeDomainType.currentText() == "Step":
sys.step(t, u, output= False, plot=True)
elif self.comboBoxTimeDomainType.currentText() == "Impulse":
#TODO: Code for Impulse time domain
pass
def ComboBoxFOTFempty(self):
if self.comboBoxFOTF.count() == 0:
self.pushButtonSimulate.setDisabled(True)
self.pushButtonGetOustaloop.setDisabled(True)
self.pushButton_StabilityTest.setDisabled(True)
self.pushButton_BodePlot.setDisabled(True)
self.pushButtonViewInConsole.setDisabled(True)
self.pushButton_DeleteFOTF.setDisabled(True)
self.pushButton_EditFOTF.setDisabled(True)
else:
self.pushButtonGetOustaloop.setDisabled(False)
self.pushButton_StabilityTest.setDisabled(False)
self.pushButtonViewInConsole.setDisabled(False)
self.pushButton_DeleteFOTF.setDisabled(False)
self.pushButton_EditFOTF.setDisabled(False)
self._FreqCheck()
self._TimeCheck()
def _ShowError(self, message, obj = None, obj2 = None):
try:
if self.isDialogActive == False:
self.isDialogActive = True
if obj != None:
obj.setCursorPosition(0)
obj.setSelection(0, len(obj.text()))
self.statusbar.showMessage('Error: '+message, STATUSBAR_TIME)
raise ValueError(QMessageBox.question(self, 'Error', message, QMessageBox.StandardButtons(QMessageBox.Ok)))
except:
self.isDialogActive = False
# FREQUENCY DOMAIN VARIABLES CHECKS
def _LowerFreq(self):
if int(self.lineEdit_LowerFreq.text()) < 0:
self.lowerFreq =True
else:
self.lowerFreq = False
self._ShowError("'freq exponent (-int)' must be a -ve integer", self.lineEdit_LowerFreq)
self._FreqCheck()
def _HigherFreq(self):
if int(self.lineEdit_HigherFreq.text()) > 0:
self.higherFreq = True
else:
self.higherFreq = False
self._ShowError("'freq exponent (int)' must be a +ve integer",self.lineEdit_HigherFreq)
self._FreqCheck()
def _FreqDataPoints(self):
if int(self.lineEdit_FreqDataPoints.text()) >= 500:
self.freqDataPoints = True
else:
self.freqDataPoints = False
self._ShowError("'Data points (int)' must be a +ve integer >= 500", self.lineEdit_FreqDataPoints)
self._FreqCheck()
def _FreqCheck(self):
if self.lowerFreq and self.higherFreq and self.freqDataPoints:
self.pushButton_BodePlot.setEnabled(True)
else:
self.pushButton_BodePlot.setEnabled(False)
#TIME DOMAIN VARIABLES CHECKS
def _issteptimeok(self):
if 0 < float(self.lineEdit_StepTime.text()) <= 0.087:
self._stepok = True
else:
self._stepok = False
self._ShowError('0 < "Step(s)" < 0.087', self.lineEdit_StepTime)
self._TimeCheck()
def _isinputok(self):
if float(self.lineEdit_inputTime.text()) < 0:
self._input = False
self._ShowError('"input" must be > 0',self.lineEdit_inputTime)
else:
self._input = True
self._TimeCheck()
def _isstarttimeok(self):
if float(self.lineEdit_StartTime.text()) < 0:
self._STARTTIME =False
self._ShowError('"Start(s)" must be > 0',self.lineEdit_StartTime)
else:
self._STARTTIME = True
self._TimeCheck()
def _isstoptimeok(self):
if float(self.lineEdit_StopTime.text()) < 0:
self._STOPTIME = False
self._ShowError('"Stop)" must be > 0',self.lineEdit_StopTime)
else:
self._STOPTIME = True
self._TimeCheck()
def _TimeCheck(self):
if float(self.lineEdit_StartTime.text()) < float(self.lineEdit_StopTime.text()):
self._greaterthan =True
else:
self._greaterthan = False
self._ShowError('"Stop(s)" must be > Start(s)')
if self._input and self._STOPTIME and self._STARTTIME and self._greaterthan and self._stepok:
self.pushButtonSimulate.setEnabled(True)
else:
self.pushButtonSimulate.setEnabled(False)
def closeEvent(self,event):
reply = QMessageBox.question(self, "Exit?", "Are you sure you want to exit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
sys.exit()
else:
event.ignore()
class newfotfgui(QDialog, createnewfotfgui.Ui_dialogCreateNewFOTF):
def __init__(self):
QDialog.__init__(self)
createnewfotfgui.Ui_dialogCreateNewFOTF.__init__(self)
self.setWindowIcon(QIcon('index.png'))
self.setupUi(self)
self.lineEditSysName.textChanged.connect(self._checkSysName)
self.lineEdit_ZeroPoly.textChanged.connect(self._zeroPolyChanged)
self.lineEdit_PolePoly.textChanged.connect(self._polePolyChanged)
self.lineEdit_DelayText.textChanged.connect(self._delayChanged)
self.pushButtonOK.clicked.connect(self.close)
self.pushButtonCancel.clicked.connect(self.close)
self.sysnamecheck = self.zeroCheck = self.poleCheck = False
self.delayCheck = True #in gui intitla delay is always 0
self.show()
#region Button OK Check
def _checkOkButton(self):
if self.sysnamecheck and self.zeroCheck and self.delayCheck and self.poleCheck:
self.pushButtonOK.setEnabled(True)
else:
self.pushButtonOK.setEnabled(False)
#endregion
#region lineEdit Values Check
def _checkSysName(self):
try:
self.sysnamecheck = len(self.lineEditSysName.text().strip(" ")) >= 1
self._checkOkButton()
except:
self.sysnamecheck = False
self._checkOkButton()
def _zeroPolyChanged(self):
try:
self.zeroCheck = str2poly(self.lineEdit_ZeroPoly.text())
self._checkOkButton()
except:
self.zeroCheck = False
self._checkOkButton()
def _delayChanged(self):
try:
self.delayCheck = float(self.lineEdit_DelayText.text()) >= 0
self._checkOkButton()
except:
self.delayCheck = False
self._checkOkButton()
def _polePolyChanged(self):
try:
self.poleCheck = str2poly(self.lineEdit_ZeroPoly.text())
self._checkOkButton()
except:
self.poleCheck = False
self._checkOkButton()
#endregion
def closeEvent(self, event):
sender = self.sender().text()
close = QMessageBox.question(self, "{0}?".format(sender),
"Are you sure you would like to '{0}' this form?".format(sender),
QMessageBox.Yes | QMessageBox.No)
if close == QMessageBox.Yes:
if sender == "OK":
pass
else:
self.lineEditSysName.clear()
self.lineEdit_ZeroPoly.clear()
self.lineEdit_PolePoly.clear()
self.lineEdit_DelayText.clear()
event.accept()
else:
event.ignore()
def loadsets():
return gg1(),gg2(), gg3(),gg4()
def gg1():
return newfotf(1., '14994s^{1.31}+6009.5s^{0.97}+1.69', 0)
def gg2():
return newfotf(1., '0.8s^{2.2}+0.5s^{0.9}+1', 0)
def gg3():
return newfotf('-2s^{0.63}+4', '2s^{3.501}+3.8s^{2.42}+2.6s^{1.798}+2.5s^{1.31}+1.5', 0)
def gg4():
return newfotf('s+1','s^2.5+0.5s^1.5+100',0)
if __name__ == "__main__":
app = QApplication(sys.argv)
fomcon = FotfViewForm()
app.exec_()