SystemEngine.py 5.68 KB
Newer Older
1
  # importação de classes
2 3
import time
import sys
4 5
from PyQt5 import QtCore,QtGui,QtWidgets
from pyqtgraph import GraphicsLayout
6
from SerialManager import SerialManager
7
from CalibracaoP import Ui_MainWindow
8
from Graph import Graph
9 10
import numpy as np
from math import sin,log
11

12 13 14
class SystemEngine(object):

    def __init__(self):
15
        self.timer=QtCore.QTimer()
16 17 18
        self.app = QtWidgets.QApplication(sys.argv)
        self.dialog = QtWidgets.QMainWindow()
        self.ser1 = SerialManager()
19
        self.ser1.startPort(str(self.ser1.portList[0].device), 115200)
20 21
        self.uiCalibra1 = Ui_MainWindow()
        self.uiCalibra1.setupUi(self.dialog)
22 23 24 25 26 27 28 29 30 31 32 33 34 35
        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"]

36 37
        self.layout=GraphicsLayout()

38
        self.updateTimer()
39 40 41 42 43
        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)
44

45
        self.layout.addItem(self.graph,row=1,col=2,rowspan=1,colspan=1)
46
        self.layout.addItem(self.graph.axisTime,row=2,col=2,rowspan=1,colspan=1)
47 48 49 50 51 52
        self.layout.setMinimumSize(500, 600)
        self.layout.setMaximumSize(500,600)




53

54
        self.scene = QtGui.QGraphicsScene()
55

56
        self.scene.addItem(self.layout)
57 58 59 60



        #self.layout.setMaximumSize(770,550)
61
        self.scene.focusItem()
62

63 64 65 66 67 68 69 70 71 72 73 74 75
        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()


76
        self.dialog.showMaximized()
77

78

79 80 81 82 83
        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)
84

85
        self.uiCalibra1.samplingCBox.currentIndexChanged.connect(self.updateTimer)
86 87 88
        self.timeant=time.time()
        self.timer.timeout.connect(self.updateData)
        self.timer.start(100)
89

90

91 92 93 94 95 96 97 98 99 100 101 102
    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)

103 104 105
    def updateData(self):
        #print(time.time()-self.timeant)
        if(self.uiCalibra1.menuPlay_Pause.isChecked()==True):
106 107
            readData = self.ser1.read()  # Lê o dado da serial
            readData = readData.decode('utf8')
108 109
            dado = readData.split(' ', self.nCurves+1)
            dado[len(dado)-1]=dado[len(dado)-1].split('\n',2)[0]
110
            try:
111 112 113
                #self.uiCalibra1.forceLabel.setText(str(dado[0])+" Tonf")
                #self.uiCalibra1.calibratorLabel.setText(str(dado[1].split()[0])+" mV")
                pass
114 115
            except IndexError:
                print("Erro no Indice do Array Enviado pela Serial")
116
            dado.pop(0)
117
            try:
118
                #print(dado)
119 120
                self.graph.updateGraph(dado)

121 122 123
            except IndexError:
                print("Erro: Array inválido")
                print(dado)
124

125 126 127
        else:
            pass
        self.timeant=time.time()
128

129 130 131
    def updateScale(self):
        try:
            self.x_scale = int(self.uiCalibra1.t_max.text())
132 133 134 135
            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())
136 137
        except ValueError:
            self.x_scale = 100
138 139 140 141
            self.y_min[0] = 0
            self.y_max[0] = 32700
            self.y_min[0] = 0
            self.y_max[0] = 10000
142
            print("Erro!: Campos de Escala Vazios")
143

144 145 146 147
        if(self.x_scale=="0"):
            self.x_scale="1"

        self.layout.clear()
148 149 150
        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)
151 152
        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)
153 154 155
        self.scene.focusItem()

    def updateTimer(self):
156
        self.time=self.uiCalibra1.samplingCBox.currentText().split(" ")
157 158 159 160 161 162 163
        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])