-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunModes.py
248 lines (217 loc) · 7.85 KB
/
RunModes.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
from DataManager import MemoryManager
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.Qt import Qt
from PyQt5.QtCore import QThread, QRunnable, QThreadPool, pyqtSlot, pyqtSignal
import sys
import time
from MainWindow import MainWindow
from Communication import SerialCommunicator, DeviceSearcher
from Widgets import PortSetWindow
class LiveFlight():
def __init__(self):
pass
def run(self):
self.app = QApplication(sys.argv)
self.mm = MemoryManager()
self.mm.dm.data_processor.set_structure(self.mm.conf['new_structure'])
# Logic threads
self.threadpool = QThreadPool()
self.log = LiveFlightLogic(self)
self.gui = LiveFlightGui(self)
self.af = AdditionalThread(self)
self.af.port_dialog_signal.connect(self.open_port_dialog)
self.af.start()
# main window
self.main_window = MainWindow()
self.main_window.show()
# running it
self.threadpool.start(self.log)
self.threadpool.start(self.gui)
self.app.exec_()
def open_port_dialog(self, args):
def dialog_callback(self, args):
ans = self.dialog.ans
self.dialog.close()
self.mm.dyn['port'] = ans
args['callback']()
self.dialog = PortSetWindow(self.main_window)
self.dialog.set_port(args['ports'])
ans = None
self.dialog.value_changed_signal.connect(
lambda: dialog_callback(self, args))
self.dialog.exec_()
class AdditionalThread(QThread):
port_dialog_signal = pyqtSignal(dict)
def __init__(self, parent):
super().__init__()
self.parent = parent
self.queue = []
self.condition = True
def run(self):
while self.condition:
if len(self.queue)>0:
f = self.queue.pop(0)
f(self, True)
def inner_runner(function):
def wrapper(self, inst = False, **kwargs):
if inst:
return function(**kwargs)
self.queue.append(function)
return wrapper
@inner_runner
def restart_serial(self, *args):
try:
self.parent.log.ser.port = DeviceSearcher().find_device_port()
self.parent.log.ser.reset_serial()
except Exception as e:
print(e)
@inner_runner
def get_possible_readings(self, *args):
return self.parent.mm.conf['new_structure'].values()
@inner_runner
def set_port_menu(self, *args):
ports = DeviceSearcher().list_ports()
self.port_dialog_signal.emit({'ports':ports, 'callback': self.set_port})
@inner_runner
def set_port(self, *args, **kwargs):
try:
self.parent.log.ser.port = self.parent.mm.dyn['port']
except Exception as e:
pass
self.parent.log.ser.reset_serial()
@inner_runner
def send_command_line(self, *args):
text = str(self.parent.main_window.command_box.text())
self.parent.log.ser.writeline(text)
class LiveFlightGui(QRunnable):
def __init__(self, parent):
super().__init__()
self.parent = parent
@pyqtSlot()
def run(self):
self.da = self.parent.mm.dm.new_data_arranger('pressure', 'temperature')
self.parent.main_window.plot1.start_data_ploter(self.da)
self.parent.main_window.find_arduino_menu.triggered.connect(
self.parent.af.restart_serial)
self.parent.main_window.set_port_menu.triggered.connect(
self.parent.af.set_port_menu)
self.parent.main_window.send_command_button.clicked.connect(
self.parent.af.send_command_line)
while True:
self.parent.main_window.plot1.update()
self.update_serial_status()
time.sleep(0.2)
def update_serial_status(self):
color = ''
if not hasattr(self.parent.log, 'ser'):
text = 'Loading...'
color = 'DarkOrange'
else:
status = self.parent.log.ser.connection_status()
if status == 0:
text = 'Waiting for connection...'
color = 'DarkOrange'
elif status == 1:
text = 'Device ready at port '
text += str(self.parent.log.ser.port)
color = 'DarkGreen'
elif status == -2:
text = 'Disconnected!'
color = 'Red'
else:
text = 'Failed to connect'
color = 'Red'
self.parent.main_window.port_status_label.setText(text)
color = 'color : ' + color
self.parent.main_window.port_status_label.setStyleSheet(color)
class LiveFlightLogic(QRunnable):
def __init__(self, parent):
super().__init__()
self.parent = parent
self.angle = 0
self.last_angle = 0
self.left_motor = 50
self.right_motor = 50
self.left_key = False
self.right_key = False
self.left_key = False
self.up_key = False
self.down_key = False
self.power = 0
self.servo = 0
self.engines = 0
def keyPressEvent(self, event):
if event.key() == Qt.Key_N:
self.left_key = True
elif event.key() == Qt.Key_M:
self.right_key = True
elif event.key() == Qt.Key_R:
if self.engines == 0:
self.engines = 1
else:
self.engines = 0
self.ser.add_to_outbuffer("m{}M".format(str(self.engines)))
elif event.key() == Qt.Key_Up:
self.up_key = True
elif event.key() == Qt.Key_Down:
self.down_key = True
elif event.key() == Qt.Key_D:
if self.servo == 0:
self.servo = 1
else:
self.servo = 0
self.ser.add_to_outbuffer("s{}S".format(str(self.servo)))
def keyReleaseEvent(self, event):
if event.key() == Qt.Key_N:
self.left_key = False
elif event.key() == Qt.Key_M:
self.right_key = False
elif event.key() == Qt.Key_Up:
self.up_key = False
elif event.key() == Qt.Key_Down:
self.down_key = False
@pyqtSlot()
def run(self):
self.ser = SerialCommunicator()
self.ser.setOnReadCallback(self.newDataCallback)
while not hasattr(self.ser, 'serial'):
pass
self.parent.threadpool.start(self.ser)
self.parent.main_window.keyPressEvent = self.keyPressEvent
self.parent.main_window.keyReleaseEvent = self.keyReleaseEvent
step = 1
while True:
if self.left_key:
self.angle -= step
if self.right_key:
self.angle += step
if self.up_key and self.power<=100-step:
self.power +=step
if self.down_key and self.power>=step:
self.power -= step
self.angle = self.angle%360
if self.angle != self.last_angle:
self.ser.add_to_outbuffer("a{}A".format(str(self.angle)))
self.last_angle = self.angle
temp_text = 'l:{} r:{} p:{} a:{}'.format(
self.left_motor, self.right_motor, self.power, self.angle)
self.parent.main_window.temp2.setText(temp_text)
l_out = int(self.left_motor*self.power/100)*2
r_out = int(self.right_motor*self.power/100)*2
#motbytes = bytes([0xFF, l_out, r_out, self.servo])
try:
self.ser.writeline(motbytes)
except Exception as e:
pass
time.sleep(0.1)
def newDataCallback(self, data):
new_data = self.parent.mm.dm.append(data)
try:
self.parent.main_window.rocket_map.map_view.addPointToPath(
new_data['x'], new_data['y'], '#123456'
)
except Exception as e:
pass
print(data)
lf = LiveFlight()
lf.run()