-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpid_GUI.py
More file actions
executable file
·176 lines (142 loc) · 5.87 KB
/
pid_GUI.py
File metadata and controls
executable file
·176 lines (142 loc) · 5.87 KB
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
#! /usr/bin/python
##############################################################################
#
# PID GUI
#
# Description:
# Interfaz para el ajuste interactivo de los parametros del
# algoritmo PID que fue implementado en Arduino
#
# Author:
# Hugo Arboleas <harboleas@citedef.gob.ar>
#
##############################################################################
#
# Copyright 2015 Hugo Arboleas
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#############################################################################
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4.Qwt5 import *
from main_window import *
import sys
import serial
import struct
import time
class Adquisicion(QtCore.QThread):
"Thread para la adquisicion de datos serie"
def __init__(self, port):
super(Adquisicion, self).__init__()
self.port = port
def run(self):
while True:
# Leo la process variable (2 Bytes) enviada por el arduino
data_read = self.port.read(2)
if len(data_read) == 2 :
data = struct.unpack("h", data_read)[0] # convierte 2 bytes a int
self.emit(QtCore.SIGNAL("data_ready"), data)
self.msleep(5)
class App(QtGui.QMainWindow):
def __init__(self):
self.app = QtGui.QApplication(sys.argv)
QtGui.QMainWindow.__init__(self)
self.main_win = Ui_MainWindow()
self.main_win.setupUi(self)
self.main_win.pid_on.lower()
self.main_win.pid_off.lower()
self.main_win.pid_on.setVisible(False)
try:
# Provoco un reset del Arduino para una conexion limpia
arduino = serial.Serial("/dev/ttyUSB0")
arduino.setDTR(False)
time.sleep(1)
arduino.flushInput()
arduino.setDTR(True)
arduino.close()
self.port = serial.Serial("/dev/ttyUSB0", 115200, timeout = 0)
except:
self.port = None
print("No se puede abrir el puerto")
else:
self.adquisicion = Adquisicion(self.port)
QtCore.QObject.connect(self.adquisicion, QtCore.SIGNAL("data_ready"), self.update_data)
self.adquisicion.start()
self.main_win.plot.insertLegend(QwtLegend(), QwtPlot.BottomLegend)
self.main_win.plot_e.insertLegend(QwtLegend(), QwtPlot.BottomLegend)
self.main_win.plot.setCanvasBackground(QtCore.Qt.black)
self.main_win.plot.setAxisScale(0, 0, 10000)
self.pv_graf = QwtPlotCurve("Process Variable")
self.pv_graf.attach(self.main_win.plot)
self.pv_graf.setPen(QtGui.QPen(QtCore.Qt.blue, 2))
self.sp_graf = QwtPlotCurve("Set Point")
self.sp_graf.attach(self.main_win.plot)
self.sp_graf.setPen(QtGui.QPen(QtCore.Qt.green, 2))
self.main_win.plot_e.setCanvasBackground(QtCore.Qt.black)
self.main_win.plot_e.setAxisScale(0, -1000, 1000)
self.e_graf = QwtPlotCurve("error")
self.e_graf.attach(self.main_win.plot_e)
self.e_graf.setPen(QtGui.QPen(QtCore.Qt.red, 2))
self.pv_y = [0 for i in range(300)]
self.sp_y = [0 for i in range(300)]
self.e_y = [0 for i in range(300)]
self.x = range(300)
self.pid_on = 0
self.set_param()
self.show()
self.app.exec_() # Lazo principal
def set_param(self):
self.sp = self.main_win.sp.value()
self.Ts = self.main_win.Ts.value()
self.Kp = self.main_win.Kp.value()
self.Ki = self.main_win.Ki.value()
self.Kd = self.main_win.Kd.value()
if self.port:
self.port.write(struct.pack("B", self.pid_on)) # Envia 1 byte (PID on/off)
self.port.write(struct.pack("B", self.Ts)) # Envia 1 byte (Ts = Tiempo de muestreo)
self.port.write(struct.pack("f", self.Kp)) # Envia 4 bytes (Kp)
self.port.write(struct.pack("f", self.Ki)) # Envia 4 bytes (Ki)
self.port.write(struct.pack("f", self.Kd)) # Envia 4 bytes (Kd)
self.port.write(struct.pack("h", self.sp)) # Envia 2 bytes (Set Point)
def on(self):
self.main_win.boton_pid_on.setDisabled(True)
self.main_win.boton_pid_off.setEnabled(True)
self.main_win.pid_on.setVisible(True)
self.main_win.pid_off.setVisible(False)
self.pid_on = 1
self.set_param()
def off(self):
self.main_win.boton_pid_on.setEnabled(True)
self.main_win.boton_pid_off.setDisabled(True)
self.main_win.pid_off.setVisible(True)
self.main_win.pid_on.setVisible(False)
self.pid_on = 0
self.set_param()
def update_data(self, data):
# Actualiza los datos de los graficos
# cada vez que recibe un dato del arduino (cada Ts ms)
self.pv_y = self.pv_y[1:] + [data]
self.sp_y = self.sp_y[1:] + [self.sp]
self.e_y = self.e_y[1:] + [self.sp - data]
self.main_win.pv.display(data)
self.main_win.e.display(self.sp - data)
self.pv_graf.setData(self.x, self.pv_y)
self.sp_graf.setData(self.x, self.sp_y)
self.e_graf.setData(self.x, self.e_y)
self.main_win.plot.replot()
self.main_win.plot_e.replot()
if __name__ == "__main__":
app = App()
# vim: set ts=8 sw=4 tw=0 et :