SystemEngine.py 6.95 KB
Newer Older
Alisson Claudino's avatar
Alisson Claudino committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#      Classe SystemEngine                                                                                  #
#      Autor: Alisson Claudino (alisson.claudino@ufrgs.br) -> https://lief.if.ufrgs.br/~itsalissom          #
#      Licença: GNU GPLv2                                                                                   #
#      Propósito: Gerenciar o funcionamento geral do programa, realizando as conexões entre todas as        #
#      classes do sistema.                                                                                  #
#                                                                                                           #                                                                                                          #
# Observações:                                                                                              #
#                                                                                                           #
#                                                                                                           #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #



# importação de classes
16
17
import time
import sys
18
19
from PyQt5 import QtCore,QtGui,QtWidgets
from pyqtgraph import GraphicsLayout
20
from SerialManager import SerialManager
21
from CalibracaoP import Ui_MainWindow
22
from Graph import Graph
23
24
import numpy as np
from math import sin,log
25

26
27
class SystemEngine(object):

Alisson Claudino's avatar
Alisson Claudino committed
28
29
    def __init__(self): #Construtor da classe

30
        self.timer=QtCore.QTimer()
31
32
33
        self.app = QtWidgets.QApplication(sys.argv)
        self.dialog = QtWidgets.QMainWindow()
        self.ser1 = SerialManager()
Alisson Claudino's avatar
Alisson Claudino committed
34
        self.ser1.startPort(str(self.ser1.portList[1].device), 115200)
35
36
        self.uiCalibra1 = Ui_MainWindow()
        self.uiCalibra1.setupUi(self.dialog)
37
38
39
40
41
42
43
44
45
46
47
48
49
50
        self.ser1.serialListPanel(self.uiCalibra1)



        self.x_scale=10
        self.nCurves = 2
        self.y_min=[0,0]
        self.y_max=[32700,10000]


        self.color = ['r','g']
        self.name = ["Força","Tensão no Calibrante"]
        self.unit = ["Tonf","mV"]

51
52
        self.layout=GraphicsLayout()

53
        self.updateTimer()
54
55
56
57
58
        self.graph = Graph(self.time[0]/1000,self.x_scale,self.nCurves, self.y_min, self.y_max, 800, 500, self.color,self.name,self.unit)

        self.layout.addItem(self.graph.axis[0],row=1,col=1,rowspan=1,colspan=1)
        for n in range(1,self.nCurves):
            self.layout.addItem(self.graph.axis[n],row=1,col=n+2,rowspan=1,colspan=1)
59

60
        self.layout.addItem(self.graph,row=1,col=2,rowspan=1,colspan=1)
61
        self.layout.addItem(self.graph.axisTime,row=2,col=2,rowspan=1,colspan=1)
62
63
64
65
66
67
        self.layout.setMinimumSize(500, 600)
        self.layout.setMaximumSize(500,600)




68

69
        self.scene = QtGui.QGraphicsScene()
70

71
        self.scene.addItem(self.layout)
72
73
74
75



        #self.layout.setMaximumSize(770,550)
76
        self.scene.focusItem()
77

78
79
80
81
82
83
84
85
86
87
88
89
90
        self.sceneSelector(self.scene)

        self.uiCalibra1.menuPlay_Pause = QtWidgets.QAction(self.dialog)
        self.uiCalibra1.menuPlay_Pause.setText("Play")
        self.uiCalibra1.menuPlay_Pause.setObjectName("menuPlay_Pause")
        self.uiCalibra1.menuBar.addAction(self.uiCalibra1.menuPlay_Pause)
        self.uiCalibra1.menuPlay_Pause.setCheckable(True)

        self.uiCalibra1.menuPlay_Pause.toggled.connect(self.playPauseButtonAnimation)

        self.uiCalibra1.linkActions()


91
        self.dialog.showMaximized()
92

93

94
95
96
97
98
        self.uiCalibra1.t_max.returnPressed.connect(self.updateScale)
        self.uiCalibra1.f_max.returnPressed.connect(self.updateScale)
        self.uiCalibra1.f_min.returnPressed.connect(self.updateScale)
        self.uiCalibra1.p_max.returnPressed.connect(self.updateScale)
        self.uiCalibra1.p_min.returnPressed.connect(self.updateScale)
99

100
        self.uiCalibra1.samplingCBox.currentIndexChanged.connect(self.updateTimer)
101
102
103
        self.timeant=time.time()
        self.timer.timeout.connect(self.updateData)
        self.timer.start(100)
104

105

106
107
108
109
110
111
112
113
114
115
116
117
    def playPauseButtonAnimation(self):
        if (self.uiCalibra1.menuPlay_Pause.text() == "Play"):
            self.uiCalibra1.menuPlay_Pause.setText("Pause")
            self.uiCalibra1.startTimeLabel.setText(self.uiCalibra1.currentTimeLabel.text())
        else:
            self.uiCalibra1.menuPlay_Pause.setText("Play")

    def sceneSelector(self, scene):
        self.uiCalibra1.CentralGraph.setScene(scene)
        self.uiCalibra1.CentralGraph.setBackgroundBrush(QtCore.Qt.black)
        self.uiCalibra1.CentralGraph.setInteractive(False)

118
119
120
    def updateData(self):
        #print(time.time()-self.timeant)
        if(self.uiCalibra1.menuPlay_Pause.isChecked()==True):
121
122
            readData = self.ser1.read()  # Lê o dado da serial
            readData = readData.decode('utf8')
123
            dado = readData.split(' ', self.nCurves+1)
Alisson Claudino's avatar
Alisson Claudino committed
124

125
            dado[len(dado)-1]=dado[len(dado)-1].split('\n',2)[0]
126
            try:
Alisson Claudino's avatar
Alisson Claudino committed
127
128
                self.uiCalibra1.forceLabel.setText(str(dado[0])+" Tonf")
                self.uiCalibra1.calibratorLabel.setText(str(dado[1].split()[0])+" mV")
129
                pass
130
131
132
            except IndexError:
                print("Erro no Indice do Array Enviado pela Serial")
            try:
133
134
                self.graph.updateGraph(dado)

135
136
137
            except IndexError:
                print("Erro: Array inválido")
                print(dado)
138

Alisson Claudino's avatar
Alisson Claudino committed
139

140
141
142
        else:
            pass
        self.timeant=time.time()
Alisson Claudino's avatar
Alisson Claudino committed
143

144
145
146
    def updateScale(self):
        try:
            self.x_scale = int(self.uiCalibra1.t_max.text())
147
148
149
150
            self.y_min[0] = int(self.uiCalibra1.f_min.text())
            self.y_max[0]   = int(self.uiCalibra1.f_max.text())
            self.y_min[1] = int(self.uiCalibra1.p_min.text())
            self.y_max[1] = int(self.uiCalibra1.p_max.text())
151
152
        except ValueError:
            self.x_scale = 100
153
154
155
156
            self.y_min[0] = 0
            self.y_max[0] = 32700
            self.y_min[0] = 0
            self.y_max[0] = 10000
157
            print("Erro!: Campos de Escala Vazios")
158

159
160
161
162
        if(self.x_scale=="0"):
            self.x_scale="1"

        self.layout.clear()
163
164
165
        self.graph = Graph(self.time[0] / 1000, self.x_scale, self.nCurves, self.y_min, self.y_max, 800, 500,self.color, self.name, self.unit)
        self.layout.addItem(self.graph.axis[1],row=1,col=3,rowspan=1,colspan=1)
        self.layout.addItem(self.graph.axis[0],row=1,col=1,rowspan=1,colspan=1)
166
167
        self.layout.addItem(self.graph.axisTime,row=2,col=2,rowspan=1,colspan=1)
        self.layout.addItem(self.graph,row=1,col=2,rowspan=1,colspan=1)
168
169
170
        self.scene.focusItem()

    def updateTimer(self):
171
        self.time=self.uiCalibra1.samplingCBox.currentText().split(" ")
172
173
174
175
176
177
178
        self.time[0]=int(self.time[0])
        print(self.time[0])
        if(self.time[0]<100):
            self.time[0]=1000*self.time[0]

        self.timer.stop()
        self.timer.start(self.time[0])