/
Hud.py
253 lines (218 loc) · 10.7 KB
/
Hud.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
249
250
251
252
253
from pyquaternion import Quaternion
import DisplayHelper
from direct.gui.OnscreenText import OnscreenText
from Utils import getAllSerialPorts
from panda3d.core import (CompassEffect, Filename, LightNode, LineSegs,
LQuaternionf, LVecBase4, LVector4f, NodePath,
PandaNode, Shader, TextNode, WindowProperties)
from Enumerations import State
from tkinter import Tk
from tkinter.filedialog import askopenfilename
class HeadsUpDisplay():
def __init__(self, app):
self.time = "0"
self.fps = "0"
self.fileInput = ""
self.selectedComPort = ""
self.selectedEntryId = 0
self.entryDict = {}
self.timeDelta = 0
self.instVel = 0.0
self.velocityLogs = []
self.currentLogQuat = ["!"] #TODO remove this
self.lastLogQuat = []
self.velLines = LineSegs("lines")
self.velLines.setColor(0, 1, 1, 0)
self.db = app.db
segsnode = self.velLines.create(False)
self.velocityLinesNode = app.render.attachNewNode(segsnode)
self.simulatorTextDisplays(app)
self.simulatorInteractives(app)
def simulatorTextDisplays(self, app):
# SIMULATOR TEXT
self.timeText = OnscreenText(text = 'Time:' + self.time, pos = (-1.25, 0.90), scale = 0.07, align=TextNode.ALeft)
self.fpsText = OnscreenText(text = 'FPS:' + self.fps, pos = (-1.25, 0.80), scale = 0.07, align=TextNode.ALeft)
self.velocityText = OnscreenText(text = 'Velocity: ' + str(self.instVel), pos = (.75, 0.80), scale = 0.1, align=TextNode.ALeft)
# START SCREEN TEXT
self.selectedFile = OnscreenText(text = 'Selected File: ' + self.fileInput, pos = (-1.3, 0.2), scale = 0.07, align=TextNode.ALeft, wordwrap=40)
self.selectedComPortText = OnscreenText(text = 'Select a serial port:', pos = (-1.0, 0.3), scale = 0.12, align=TextNode.ALeft)
self.titleText = OnscreenText(text = 'Tic Player', pos = (-.625, 0.3), scale = 0.25, align=TextNode.ALeft)
self.loadingText = OnscreenText(text = 'Waiting for data...', pos = (-.125, -0.1), scale = 0.1, align=TextNode.ALeft)
self.logNameText = OnscreenText(text = "Log name: ", pos = (-1.0, 0.1), scale = 0.12, align=TextNode.ALeft)
def simulatorInteractives(self, app):
# SIMULATOR INTERACTIVES
self.playButton = DisplayHelper.buildButton((1.2, 0, -.9), 0.05, "Pause", app.pauseSim)
self.restartButton = DisplayHelper.buildButton((1.00, 0, -.9), 0.05, "Restart", app.restartSim)
self.resetButton = DisplayHelper.buildButton((.80, 0, -.9), 0.05, "Reset", app.resetCamera)
self.slider = DisplayHelper.buildSlider((0,1), 0, (-.60, 0, -.9), 0.7, 3, app.updateValue)
# START SCREEN INTERACTIVES
self.startSerialButton = DisplayHelper.buildButton((-.1, 0, -.1), 0.1, "Read from Serial", self.displaySerialOptions)
self.startLogsButton = DisplayHelper.buildButton((-.1, 0, .1), 0.1, "Read from Logs", self.displayFileOptions)
self.startFromDbButton = DisplayHelper.buildButton((-.1, 0, -.3), 0.1, "Replay Saved", self.displayDbOptions)
self.quitButton = DisplayHelper.buildButton((-.05, 0, -.5), .1, "Quit", app.quit)
self.launchFromFileButton = DisplayHelper.buildButton((.2, 0, 0), 0.1, "Start", app.startLogs)
self.launchFromSerialButton = DisplayHelper.buildButton((-0.1, 0, -.1), 0.1, "Start", app.startSerial)
self.launchFromDbButton = DisplayHelper.buildButton((-0.1, 0, 0), 0.1, "Start", app.startDb)
self.openFilesButton = DisplayHelper.buildButton((-.35, 0, 0), 0.1, "Select File", self.openFiles)
# BUILD COMS MENU AND DB ENTRIES MENU
comsOptions = getAllSerialPorts()
allEntries = self.db.getAllEntries()
entryOptions = [self.createEntryString(x) for x in allEntries]
if len(entryOptions) == 0:
entryOptions = [ "No data found"]
else:
self.selectedEntry = entryOptions[0]
self.selectedEntryId = allEntries[0][0]
self.menu = DisplayHelper.buildOptionMenu("Select a coms port", 0.15, self.handlePortSelect, comsOptions, 0, (1,1,1,1), (.25, 0, .3))
self.dbMenu = DisplayHelper.buildOptionMenu("", 0.15, self.handleEntrySelect, entryOptions, 0, (1,1,1,1), (-1, 0, .3))
self.backButton = DisplayHelper.buildButton((-.8, 0, .8), 0.1, "Back To Menu", self.drawStartScreen)
lastEntryId = self.db.getLastEntry()
if lastEntryId is None:
lastEntryId = 0
self.logNameInput = DisplayHelper.buildTextInput("Entry_" + str(lastEntryId + 1), 0.12, self.handleTextInput, "", 1, None, None, pos = (-.25, 0, 0.1))
def refreshEntryOptions(self):
self.menu.options = getAllSerialPorts()
allEntries = self.db.getAllEntries()
self.dbMenu.options = [self.createEntryString(x) for x in allEntries]
def createEntryString(self, entry):
entryString = entry[2] + "-" + entry[1]
self.entryDict[entryString] = entry[0]
return entryString
def getVelocity(self, currentLogQuat, lastLogQuat, timeDelta):
currentQuat = Quaternion(currentLogQuat[0], currentLogQuat[1],currentLogQuat[2], currentLogQuat[3])
currentAxis = currentQuat.axis
currentRadians = currentQuat.radians
lastQuat = Quaternion(lastLogQuat[0], lastLogQuat[1],lastLogQuat[2], lastLogQuat[3])
lastAxis = lastQuat.axis
lastRadians = lastQuat.radians
angularDisplacement = currentAxis * currentRadians
lastAngularDisplacement = lastAxis * lastRadians
return (lastAngularDisplacement - angularDisplacement) / timeDelta
def getMagnitude(self, vec):
return ( vec[0] ** 2 + vec[1] ** 2 + vec[2] ** 2 ) ** 0.5
def updateHud(self, currentLogQuat, lastLogQuat, timeDelta, runTime):
framesPerSecond = str(round(1/timeDelta))
instVel = self.getVelocity(currentLogQuat, lastLogQuat, timeDelta)
self.timeText.setText("Time: " + runTime)
self.fpsText.setText("FPS: " + framesPerSecond)
self.velocityText.setText("Velocity: " + str(self.getMagnitude(instVel)))
#self.quatText.setText('Q:'.join(self.currentLogQuat)) #TODO remove this
quatstring = "Q: "
for i in range( 0 ,4):
quatstring= quatstring + "{:.1f}".format(currentLogQuat[i]) + ", "
self.quatText.setText(quatstring) #TODO remove this
self.velocityLogs.append(self.instVel)
def getStartIndex(self, logs):
if len(logs) > 10:
return len(logs) - 11
return 0
def handleTextInput(self, value):
self.logNameText = value
def showSimulationUi(self):
self.timeText.show()
self.fpsText.show()
self.velocityText.show()
self.playButton.show()
self.restartButton.show()
self.resetButton.show()
self.slider.show()
def hideSimulationUi(self):
self.timeText.hide()
self.fpsText.hide()
self.velocityText.hide()
self.playButton.hide()
self.restartButton.hide()
self.resetButton.hide()
self.slider.hide()
def hideLaunchFromSerialUi(self):
self.menu.hide()
self.backButton.hide()
self.launchFromSerialButton.hide()
self.selectedComPortText.hide()
self.logNameText.hide()
self.logNameInput.hide()
self.loadingText.hide()
def hideLaunchFromLogsUi(self):
self.menu.hide()
self.backButton.hide()
self.launchFromFileButton.hide()
self.selectedFile.hide()
self.openFilesButton.hide()
self.loadingText.hide()
def drawStartScreen(self):
self.hideSimulationUi()
self.hideLaunchFromSerialUi()
self.hideLaunchFromLogsUi()
self.hideDbOptions()
self.startSerialButton.show()
self.startLogsButton.show()
self.startFromDbButton.show()
self.quitButton.show()
self.titleText.show()
def displayFileOptions(self):
self.hideStartScreen()
self.launchFromFileButton.show()
self.selectedFile.show()
self.backButton.show()
self.openFilesButton.show()
def hideStartScreen(self):
self.startSerialButton.hide()
self.startLogsButton.hide()
self.startFromDbButton.hide()
self.titleText.hide()
self.quitButton.hide()
def displaySerialOptions(self):
self.hideStartScreen()
self.backButton.show()
self.launchFromSerialButton.show()
self.logNameInput.show()
self.selectedComPortText.show()
self.logNameText.show()
self.menu.show()
def displayDbOptions(self):
self.refreshEntryOptions()
self.hideStartScreen()
self.launchFromDbButton.show()
self.dbMenu.show()
self.launchFromDbButton.show()
self.backButton.show()
def hideDbOptions(self):
self.launchFromDbButton.hide()
self.logNameInput.hide()
self.dbMenu.hide()
self.launchFromDbButton.hide()
self.backButton.hide()
def startSimulation(self):
self.hideLaunchFromLogsUi()
self.hideLaunchFromSerialUi()
self.hideDbOptions()
self.hideStartScreen()
self.showSimulationUi()
def openFiles(self):
self.fileInput = askopenfilename()
self.selectedFile.setText('Selected File: ' + self.fileInput)
# def destroyStartSceen(self):
# self.entry = None
# self.menu = None
def handlePortSelect(self, value):
self.selectedComPort = value
def handleEntrySelect(self, value):
self.selectedEntry = value
self.selectedEntryId = self.entryDict[value]
# def drawGraph(self, task):
# lastX = -.9
# startY = 1
# startIndex = self.getStartIndex(self.velocityLogs)
# print(startIndex)
# for index, vel in enumerate(self.velocityLogs[startIndex:]):
# mag = self.getMagnitude(vel)
# print(mag)
# print(index)
# if index == startIndex:
# self.hud.velLines.moveTo(lastX, startY + mag * index / 2, 0 )
# else:
# self.hud.velLines.drawTo(lastX + (index), 0, startY + mag)
# self.lineNode = self.hud.velLines.create(True)
# self.lineNodePath = NodePath(self.lineNode)
# self.lineNodePath.reparentTo(self.render2d)
# return Task.cont