AppWindow.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. from PyQt5.QtWidgets import *
  4. from PyQt5.QtCore import (QSettings, QThread, pyqtSignal, pyqtSlot)
  5. from vispy import app, visuals, scene
  6. from main_ui import Ui_MainWindow
  7. from settings_ui import Ui_Dialog as Ui_Settings
  8. import numpy as np
  9. import random
  10. from colorsys import hsv_to_rgb
  11. import pprint
  12. import datetime
  13. from functions import *
  14. from listenUDP import listenUDP
  15. from pingThread import pingThread
  16. import vispyHelper
  17. import csv
  18. import socket
  19. random.seed()
  20. class AppWindow(QMainWindow):
  21. csv = None
  22. def __init__(self):
  23. super().__init__()
  24. self.ui = Ui_MainWindow()
  25. self.ui.setupUi(self)
  26. self.settings = QSettings("bexus", "imufusion")
  27. self.ip = getSettings(self.settings, 'ip', "172.16.18.171")
  28. self.port = getSettings(self.settings, 'port', 1234)
  29. logFile = getSettings(self.settings, 'logFile', "default")
  30. self.logFile = open("logs/{}_{}.txt".format(logFile, datetime.datetime.now().strftime('%m%d-%H%M%S')), "w")
  31. self.ui.labelAddress.setText("{}:{}".format(self.ip, self.port))
  32. self.ui.pushButton_reset.clicked.connect(self.reset_pressed)
  33. #self.ui.tableWidget.setColumnCount(4)
  34. #self.ui.tableWidget.setRowCount(4)
  35. #self.ui.tableWidget.setItem(0,1, QTableWidgetItem("TEXT"))
  36. self.udpServer = listenUDP(self.port)
  37. self.udpServer.newMsg.connect(self.onNewMsg)
  38. self.pingThread = pingThread(self.ip)
  39. self.pingThread.pong.connect(self.onPong)
  40. self.data3d = np.array([[0,0,0]], np.float32)
  41. self.view3D = vispyHelper.view3D(self.ui.openGLWidget)
  42. self.viewQuad = vispyHelper.viewQuad(self.ui.openGLWidgetQuad)
  43. self.udpServer.start()
  44. self.pingThread.start()
  45. self.timer = app.Timer()
  46. self.timer.connect(self.update)
  47. self.timer.start(0.1)
  48. self.show()
  49. def onNewMsg(self, data):
  50. self.ui.labelTestData.setText(pprint.pformat(data))
  51. if self.csv == None:
  52. self.csv = csv.DictWriter(self.logFile, data.keys())
  53. self.csv.writeheader()
  54. self.csv.writerow(data)
  55. rx, ry, rz =data["rot"]
  56. heading = data["mag_head"]
  57. if rz != 0:
  58. rx = np.arctan(ry / rz)
  59. ry = np.arctan(rx / rz)
  60. else:
  61. rx = np.arctan(0)
  62. ry = np.arctan(0)
  63. rz = 180 + heading
  64. rx *= 45
  65. ry *= -45
  66. rx += 90
  67. rz -= 90
  68. np.append(self.data3d, data["pos"])
  69. if self.ui.tabWidget.currentIndex() == 0:
  70. self.view3D.update(self.data3d, (rx, ry, rz))
  71. elif self.ui.tabWidget.currentIndex() == 1:
  72. self.viewQuad.update(self.data3d, (rx, ry, rz))
  73. self.ui.label_temp_adc_1.setText("{:.2g} °C".format(data["adc_temperature"]))
  74. self.ui.label_temp_cis_1.setText("{:.2g} °C".format(data["temperature_cis"]))
  75. self.ui.label_temp_mag_1.setText("{:.2g} °C".format(data["temperature_mag"]))
  76. self.ui.label_temp_mpu_1.setText("{:.2g} °C".format(data["temperature_mpu"]))
  77. self.ui.label_AcX.setText("{:g}".format(data["accel_raw"][0]))
  78. self.ui.label_AcY.setText("{:g}".format(data["accel_raw"][1]))
  79. self.ui.label_AcZ.setText("{:g}".format(data["accel_raw"][2]))
  80. self.ui.label_GyX.setText("{:g}".format(data["gyro_raw"][0]))
  81. self.ui.label_GyY.setText("{:g}".format(data["gyro_raw"][1]))
  82. self.ui.label_GyZ.setText("{:g}".format(data["gyro_raw"][2]))
  83. self.ui.label_MaX.setText("{:g}".format(data["mag_raw"][0]))
  84. self.ui.label_MaY.setText("{:g}".format(data["mag_raw"][1]))
  85. self.ui.label_MaZ.setText("{:g}".format(data["mag_raw"][2]))
  86. self.ui.label_pres_cis_1.setText("{:.1g} Pa".format(data["pressure_cis"]))
  87. #self.ui.label_pres_i2c_1.setText("{:.1g} Pa".format(data["pressure_spi"]))
  88. self.ui.label_lat_1.setText("{:.5g}".format(data["latitude"]))
  89. self.ui.label_lon_1.setText("{:.5g}".format(data["longitude"]))
  90. self.ui.label_alt_1.setText("{:.2g} m".format(data["altitude"]))
  91. try:
  92. dt = datetime.datetime.fromtimestamp(data["time"] / 1000)
  93. self.ui.label_date_1.setText(dt.strftime('%d.%m.%Y'))
  94. self.ui.label_time_1.setText(dt.strftime('%H:%M:%S'))
  95. except(OSError):
  96. pass
  97. self.ui.label_hdop_1.setText("{:g}".format(data["hdop"]))
  98. def update(self, ev):
  99. #reset
  100. value = self.ui.progressBar_reset.value()
  101. if value > 0:
  102. value -= 1
  103. self.ui.progressBar_reset.setValue(value)
  104. return
  105. def reset_pressed(self):
  106. value = self.ui.progressBar_reset.value()
  107. value += 10
  108. if value > 100:
  109. value = 0
  110. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  111. sock.sendto(bytes("RESET!!", "utf-8"), (self.ip, 65100))
  112. self.ui.progressBar_reset.setValue(value)
  113. def onPong(self, t):
  114. if t != -1:
  115. self.ui.labelLatency.setText("{:.1f}ms".format(t*1000))
  116. self.ui.labelLink.setText("Online")
  117. self.ui.labelLink.setStyleSheet("color:green")
  118. else:
  119. self.ui.labelLatency.setText("")
  120. self.ui.labelLink.setText("Offline")
  121. self.ui.labelLink.setStyleSheet("color:red")
  122. @pyqtSlot(bool)
  123. def on_actionSettings_triggered(self, triggered):
  124. self.settings
  125. d = QDialog()
  126. d.ui = Ui_Settings()
  127. d.ui.setupUi(d)
  128. d.ui.lineEditIP.setText(self.ip)
  129. d.ui.lineEditPort.setText(str(self.port))
  130. if d.exec_():
  131. self.ip = d.ui.lineEditIP.text()
  132. self.port = safe_cast(d.ui.lineEditPort.text(), int, 1234)
  133. logFile = d.ui.lineEditLogFile.text()
  134. self.settings.setValue('ip', self.ip)
  135. self.settings.setValue('port', self.port)
  136. self.settings.setValue('logFile', logFile)
  137. self.logFile = open("logs/{}_{}.txt".format(logFile, datetime.datetime.now().strftime('%m%d-%H%M%S')), "w")
  138. self.csv = None
  139. self.pingThread.changeIP(self.ip)
  140. self.udpServer.stop()
  141. self.udpServer = listenUDP(self.port)
  142. self.udpServer.newMsg.connect(self.onNewMsg)
  143. self.udpServer.start()
  144. self.ui.labelAddress.setText("{}:{}".format(self.ip, self.port))
  145. # this writes the settings to storage
  146. self.settings.sync()
  147. def on_actionFollow_triggered(self, triggered):
  148. print(triggered)