AppWindow.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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, time
  13. from collections import deque
  14. from functions import *
  15. from listenUDP import listenUDP
  16. from pingThread import pingThread
  17. import vispyHelper
  18. import csv
  19. import socket
  20. random.seed()
  21. class AppWindow(QMainWindow):
  22. csv = None
  23. rate = 10
  24. vel = [0] * 3
  25. vel2 = [0] * 3
  26. pos = [0] * 3
  27. pos2 = [0] * 3
  28. oldSlaveCount = 0
  29. def __init__(self):
  30. super().__init__()
  31. self.ui = Ui_MainWindow()
  32. self.ui.setupUi(self)
  33. self.settings = QSettings("bexus", "imufusion")
  34. self.master_rate = [0] * self.rate
  35. self.slave_rate = [0] * self.rate
  36. self.ip = getSettings(self.settings, 'ip', "172.16.18.171")
  37. self.port = getSettings(self.settings, 'port', 1234)
  38. logFile = getSettings(self.settings, 'logFile', "default")
  39. self.logFile = open("logs/{}_{}.txt".format(logFile, datetime.datetime.now().strftime('%m%d-%H%M%S')), "w", newline='')
  40. self.ui.labelAddress.setText("{}:{}".format(self.ip, self.port))
  41. self.ui.pushButton_reset.clicked.connect(self.reset_pressed)
  42. self.udpServer = listenUDP(self.port)
  43. self.udpServer.newMsg.connect(self.onNewMsg)
  44. self.ui.tableWidget.setColumnCount(3)
  45. self.ui.tableWidget.setHorizontalHeaderLabels(("Master", "Slave", "Fusion"))
  46. self.fields = self.udpServer.master.keys()
  47. self.ui.tableWidget.setRowCount(len(self.fields))
  48. self.ui.tableWidget.setVerticalHeaderLabels(self.fields)
  49. self.pingThread = pingThread(self.ip)
  50. self.pingThread.pong.connect(self.onPong)
  51. self.data3d = np.array([[0,0,0]], np.float32)
  52. self.data3d2 = np.array([[0,0,0]], np.float32)
  53. self.view3D = vispyHelper.view3D(self.ui.openGLWidget)
  54. self.viewQuad = vispyHelper.viewQuad(self.ui.openGLWidgetQuad)
  55. self.udpServer.start()
  56. self.pingThread.start()
  57. self.timer = app.Timer()
  58. self.timer.connect(self.update)
  59. self.timer.start(0.1)
  60. self.show()
  61. def onNewMsg(self, data):
  62. ### MAIN STUFF ###
  63. #self.ui.labelTestData.setText(pprint.pformat(data))
  64. if self.csv == None:
  65. self.csv = csv.DictWriter(self.logFile, data.keys())
  66. self.csv.writeheader()
  67. self.csv.writerow(data)
  68. self.master_rate.append(time.time() * 1000)
  69. self.master_rate.pop(0)
  70. if data["slave_count"] != self.oldSlaveCount:
  71. self.slave_rate.append(time.time() * 1000)
  72. self.slave_rate.pop(0)
  73. self.oldSlaveCount = data["slave_count"];
  74. delayMaster = (max(self.master_rate) - min(self.master_rate)) / self.rate
  75. delaySlave = (max(self.slave_rate) - min(self.slave_rate)) / self.rate
  76. self.ui.labelLink_Master.setText("{:.1f}/s".format(1000 / delayMaster if delayMaster!=0 else 0))
  77. self.ui.labelLink_Slave.setText("{:.1f}/s".format(1000 / delaySlave if delaySlave!=0 else 0))
  78. rx, ry, rz =data["rot"]
  79. heading = data["mag_head"]
  80. if rz != 0:
  81. rx = np.arctan(ry / rz)
  82. ry = np.arctan(rx / rz)
  83. else:
  84. rx = np.arctan(0)
  85. ry = np.arctan(0)
  86. rz = 180 + heading
  87. rx *= 45
  88. ry *= -45
  89. rx += 90
  90. rz -= 90
  91. #self.data3d = np.append(self.data3d, [data["pos"]], axis=0)
  92. #self.data3d = np.append(self.data3d, [[data["latitude"], data["longitude"], data["altitude"]]], axis=0)
  93. self.vel[0] += data["global_accel"][0]
  94. self.vel[1] += data["global_accel"][1]
  95. self.vel[2] += data["global_accel"][2]
  96. self.pos[0] += self.vel[0]
  97. self.pos[1] += self.vel[1]
  98. self.pos[2] += self.vel[2] - 2
  99. self.vel2[0] += data["slave_global_accel"][0]
  100. self.vel2[1] += data["slave_global_accel"][1]
  101. self.vel2[2] += data["slave_global_accel"][2]
  102. self.pos2[0] += self.vel2[0]
  103. self.pos2[1] += self.vel2[1]
  104. self.pos2[2] += self.vel2[2] - 2
  105. self.data3d = np.append(self.data3d, [self.pos], axis=0)
  106. self.data3d2 = np.append(self.data3d2, [self.pos], axis=0)
  107. if self.ui.tabWidget.currentIndex() == 0:
  108. self.view3D.update((self.data3d, self.data3d), (rx, ry, rz))
  109. elif self.ui.tabWidget.currentIndex() == 1:
  110. self.viewQuad.update((self.data3d, self.data3d2), (rx, ry, rz))
  111. else:
  112. i = 0
  113. for k in self.fields:
  114. self.ui.tableWidget.setItem(i,0, QTableWidgetItem(str(data[k])))
  115. self.ui.tableWidget.setItem(i,1, QTableWidgetItem(str(data["slave_"+k])))
  116. self.ui.tableWidget.setItem(i,2, QTableWidgetItem(str(data["fusion_"+k])))
  117. i+=1
  118. self.ui.label_temp_adc_1.setText("{:,.2g} °C".format(data["adc_temperature"]))
  119. self.ui.label_temp_cis_1.setText("{:.2g} °C".format(data["temperature_cis"]))
  120. self.ui.label_temp_mag_1.setText("{:.2g} °C".format(data["temperature_mag"]))
  121. self.ui.label_temp_mpu_1.setText("{:.2g} °C".format(data["temperature_mpu"]))
  122. self.updateVector("label_Ac", data["accel"])
  123. self.updateVector("label_Gy", data["gyro"])
  124. self.updateVector("label_Ma", data["mag"])
  125. self.updateVector("label_Ga", data["global_accel"])
  126. self.updateVector("label_Rot", data["rot"])
  127. self.updateVector("label_Pos", self.pos)
  128. self.ui.label_pres_cis_1.setText("{:f} Pa".format(data["pressure_cis"]))
  129. self.ui.label_pres_i2c_1.setText("{:f} Pa".format(data["pressure_spi"]))
  130. self.ui.label_lat_1.setText("{:.5g}".format(data["latitude"]))
  131. self.ui.label_lon_1.setText("{:.5g}".format(data["longitude"]))
  132. self.ui.label_alt_1.setText("{:.2f} m".format(data["altitude"]))
  133. ##--------------------------------------------------------------------------------##
  134. self.ui.label_temp_mp.setText("{:g} °C".format(data["adc_temperature_internal"]))
  135. self.ui.label_temp_i2c.setText("{:g} °C".format(data["temperature_i2c"]))
  136. self.ui.label_voltage.setText("{:.3f} V".format(data["adc_voltage"] * 1.0603))
  137. self.ui.label_current.setText("{:.2f} mA".format(data["adc_current"] * 1000))
  138. ##--------------------------------------------------------------------------------##
  139. try:
  140. dt = datetime.datetime.fromtimestamp(data["time"] / 1000)
  141. #self.ui.label_date_1.setText(dt.strftime('%d.%m.%Y'))
  142. self.ui.label_time_1.setText(dt.strftime('%H:%M:%S'))
  143. except(OSError):
  144. pass
  145. self.ui.label_hdop_1.setText("{:g}".format(data["hdop"]))
  146. def updateVector(self, elem, data, f = "{:g}"):
  147. getattr(self.ui, elem+"X").setText(f.format(data[0]))
  148. getattr(self.ui, elem+"Y").setText(f.format(data[1]))
  149. getattr(self.ui, elem+"Z").setText(f.format(data[2]))
  150. def update(self, ev):
  151. #reset
  152. value = self.ui.progressBar_reset.value()
  153. if value > 0:
  154. value -= 1
  155. self.ui.progressBar_reset.setValue(value)
  156. return
  157. def reset_pressed(self):
  158. value = self.ui.progressBar_reset.value()
  159. value += 10
  160. if value > 100:
  161. value = 0
  162. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  163. sock.sendto(bytes("RESET!!", "utf-8"), (self.ip, 65100))
  164. self.ui.progressBar_reset.setValue(value)
  165. def onPong(self, t):
  166. if t != -1:
  167. self.ui.labelLatency.setText("{:.1f}ms".format(t*1000))
  168. self.ui.labelLink.setText("Online")
  169. self.ui.labelLink.setStyleSheet("color:white;background-color:green")
  170. else:
  171. self.ui.labelLatency.setText("")
  172. self.ui.labelLink.setText("Offline")
  173. self.ui.labelLink.setStyleSheet("color:white;background-color:red;")
  174. @pyqtSlot(bool)
  175. def on_actionSettings_triggered(self, triggered):
  176. self.settings
  177. d = QDialog()
  178. d.ui = Ui_Settings()
  179. d.ui.setupUi(d)
  180. d.ui.lineEditIP.setText(self.ip)
  181. d.ui.lineEditPort.setText(str(self.port))
  182. if d.exec_():
  183. self.ip = d.ui.lineEditIP.text()
  184. self.port = safe_cast(d.ui.lineEditPort.text(), int, 1234)
  185. logFile = d.ui.lineEditLogFile.text()
  186. self.settings.setValue('ip', self.ip)
  187. self.settings.setValue('port', self.port)
  188. self.settings.setValue('logFile', logFile)
  189. self.logFile = open("logs/{}_{}.txt".format(logFile, datetime.datetime.now().strftime('%m%d-%H%M%S')), "w")
  190. self.csv = None
  191. self.pingThread.changeIP(self.ip)
  192. self.udpServer.stop()
  193. self.udpServer = listenUDP(self.port)
  194. self.udpServer.newMsg.connect(self.onNewMsg)
  195. self.udpServer.start()
  196. self.ui.labelAddress.setText("{}:{}".format(self.ip, self.port))
  197. # this writes the settings to storage
  198. self.settings.sync()
  199. def on_actionFollow_triggered(self):
  200. pass