-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
190 lines (155 loc) · 6.68 KB
/
Copy pathmain.py
File metadata and controls
190 lines (155 loc) · 6.68 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QDialog, QHBoxLayout
from PyQt5.QtCore import pyqtProperty, QCoreApplication, QObject, QUrl
from PyQt5.QtQuick import QQuickView
from PyQt5.QtQml import qmlRegisterType, QQmlComponent, QQmlEngine
from PyQt5.QtQuickWidgets import QQuickWidget
import sys
from AlbertaLoop_UI import Ui_MainWindow
# import telemetry_module
from TelemetryModel import TelemetryModel
from HealthCheckModel import HealthCheckModel
from Actions.Command import Launch
from Actions.Command import PrepareLaunch
from Actions.Command import EStop
from Actions.Command import Crawl
from Actions.HealthCheck import HealthCheck
import time
import threading
from datetime import datetime
from argparse import ArgumentParser
from NetworkComms.udp_module import UDPModule
from NetworkComms.telemetry_receiver import TelemetryReceiver
from NetworkComms.cmd_transmitter import CmdTransmitter
import signal # Make Ctrl+C work with PyQt5 Applications
signal.signal(signal.SIGINT, signal.SIG_DFL)
class MWindowWrapper(Ui_MainWindow):
def __init__(self, window, ip, port, telemetry_model, udp_module):
self.setupUi(window)
self.ip = ip
self.port = port
self.command = None
self.udp_module = udp_module
self.current_state = "fault"
self.command_requested = ["none"]
self.healthchk_requested = ["none"]
self.estop_requested = ["none"]
self.cmd_lock = threading.Lock()
# -----------------------------------------------------------------
# Add functionality below!
# User Added QML Widget for Speed Gauge
self.spedometerWidget = QQuickWidget()
self.spedometerWidget.setClearColor(QtCore.Qt.transparent)
self.spedometerWidget.setResizeMode(QQuickWidget.SizeRootObjectToView)
self.spedometerWidget.setSource(QUrl("assets/Guage.qml"))
self.speedGaugeLayout.addWidget(self.spedometerWidget)
# Connect clicked functions
self.launchBtn.clicked.connect(self.launchBtn_clicked)
self.healthChkBtn.clicked.connect(self.healthChkBtn_clicked)
self.crawlBtn.clicked.connect(self.crawlBtn_clicked)
self.prepLaunchBtn.clicked.connect(self.prepLaunchBtn_clicked)
self.eStopBtn.clicked.connect(self.eStopBtn_clicked)
# logo added
pixmap = QtGui.QPixmap("img/Albertaloop_logo.png")
self.albertaloopLogo.setPixmap(pixmap)
self.telemetryTable1.setModel(telemetry_model)
# Clicked function definitions
def launchBtn_clicked(self):
print("Launch button pressed")
if self.command_requested == ["none"]:
if self.current_state == ["ready_to_launch"] :
self.command_requested = ["launch"]
self.executeCommand(Launch(self.udp_module), self.command_requested)
else :
print("Not ready to launch")
else:
print("Waiting for another command: ")
print(self.command_requested)
print("\n")
def healthChkBtn_clicked(self):
print("Health check button pressed")
if self.healthchk_requested == ["none"]:
self.healthchk_requested = ["yes"]
self.executeCommand(HealthCheck(self.udp_module), self.healthchk_requested)
print("Health check requested")
else :
print("Health check already requested")
def crawlBtn_clicked(self):
print("Crawl button pressed")
if self.command_requested == ["none"]:
if self.current_state == ["idle"]:
self.command_requested = ["crawl"]
self.executeCommand(Crawl(self.udp_module), self.command_requested)
print("Crawl requested")
else :
print("Not ready to crawl, pod must be idle")
else:
print("Waiting for another command: ")
print(self.command_requested)
print("\n")
def prepLaunchBtn_clicked(self):
print("Prepare Launch button pressed")
if self.command_requested == ["none"]:
if self.current_state == ["idle"] :
self.command_requested = ["prep_launch"]
self.executeCommand(PrepareLaunch(self.udp_module), self.command_requested)
print("Prepare to launch requested")
else :
print("Not ready for prepare to launch, pod must be idle")
else:
print("Waiting for another command: ")
print(self.command_requested)
print("\n")
def eStopBtn_clicked(self):
print("Estop button pressed")
self.cmd_lock.acquire()
if self.estop_requested == ["none"]:
self.estop_requested = ["yes"]
self.cmd_lock.release()
self.executeCommand(EStop(self.udp_module), self.estop_requested)
print("Emegency stop requested")
else :
print(self.estop_requested)
self.cmd_lock.release()
print("EStop already sending")
# Command Pattern definitions
def executeCommand(self, command, cmd_state):
self.command = command
self.cmd_thread = threading.Thread(target=self.command.execute, args=[cmd_state, self.cmd_lock])
self.cmd_thread.run()
def command_input(self):
text = self.command_line.text()
# exits program TODO (remove later plz)
if text.lower() == "exit":
sys.exit()
print("command >> ", text)
if __name__ == "__main__":
parser = ArgumentParser(description="Albertaloop GUI launch")
parser.add_argument(
"--server_ip", default="127.0.0.1", help="The ip to send the packets to"
)
parser.add_argument(
"--server_port", type=int, default=4000, help="The UDP port to get updates from the pod"
)
parser.add_argument(
"--client_port", type=int, default=3000, help="The UDP port to send packets to"
)
args = parser.parse_args()
app = QApplication(sys.argv)
# Model Classes
TelemetryModel = TelemetryModel()
HealthCheckModel = HealthCheckModel()
TelemetryReceiver = TelemetryReceiver()
CmdTransmitter = CmdTransmitter()
# Controller Classes
TelemetryReceiver.setDataModel(TelemetryModel)
# HealthCheckReq = HealthCheckReq(HealthCheckModel)
UDPModule = UDPModule(args.server_ip, args.server_port, args.client_port,
TelemetryReceiver, CmdTransmitter)
# View Classes
MainWindow = QMainWindow()
mWindowWrapper = MWindowWrapper(MainWindow, args.server_ip, args.server_port,TelemetryModel,
UDPModule)
TelemetryReceiver.start()
MainWindow.show()
sys.exit(app.exec_())