-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathangleGui.py
288 lines (241 loc) · 9 KB
/
angleGui.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from __future__ import division
import Tkinter as tk
import numpy as np
import time,sys,re,fileinput
import struct
# PyQT4 imports
from PyQt4 import QtGui, QtCore, QtOpenGL
from PyQt4.QtOpenGL import QGLWidget
# PyOpenGL imports
import OpenGL.GL as gl
import OpenGL.GLUT as glut
import OpenGL.arrays.vbo as glvbo
class GLPlotWidget():
# default window size
def __init__(self,stream,*args,**kwargs):
self.init_time = time.time()
self.width,self.height = kwargs.get('dimensions',(800,600))
self.stream = stream
self.zoom = [1.0,1.0]
self.pan = [0.0,0.0]
self.angle = 45.
self.PIDoutput = 0.
self.lastSampleTime = time.time()
self.samplePeriod = 1
self.isDirty = True
inputFuncs = {
(('KB',ord('q')),):self.close,
}
self.inputHandler = InputHandler(inputFuncs)
self.configParams(**kwargs)
def close(self):
self.stream.close()
sys.exit(0)
def configParams(self,*args,**kwargs):
self.panspeed = kwargs.get('panspeed',1.0e-1)
self.zoomspeed = kwargs.get('zoomspeed',5.0e-1)
self.c_palette = [
(0.41221438595965454, 0.0, 0.0),
(0.79312361597374381, 0.0, 0.0),
(1.0, 0.16372618247436524, 0.0),
(1.0, 0.544607916482724, 0.0),
(1.0, 0.91519554957193794, 0.0),
(1.0, 1.0, 0.44411709117591475)
]
self.c_bg = np.array([ 0.93, 0.92, 0.93, 1.0])
self.c_legend = np.array([0.08,0.08,0.18,0.14])
def getColor(self,i):
return self.c_palette[i%len(self.c_palette)]
def initialize(self):
glut.glutInit()
glut.glutInitDisplayMode(glut.GLUT_RGBA | glut.GLUT_DOUBLE | glut.GLUT_DEPTH)
glut.glutInitWindowSize(self.width,self.height)
glut.glutInitWindowPosition(300,300)
glut.glutCreateWindow("super black hole sunshine party")
glut.glutDisplayFunc(self.paintGL)
glut.glutIdleFunc(self.paintGL)
glut.glutReshapeFunc(self.resizeGL)
self.initGL()
self.inputHandler.attachGLUT()
glut.glutMainLoop()
def initGL(self):
"""Initialize OpenGL, VBOs, upload data on the GPU, etc.
"""
# background color
gl.glEnable( gl.GL_POINT_SMOOTH );
gl.glEnable( gl.GL_LINE_SMOOTH );
gl.glEnable(gl.GL_BLEND);
gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA);
gl.glClearColor(*self.c_bg)
def updateAngle(self):
# Read 4 bytes
nextAngle = self.stream.read(8)
if len(nextAngle)!=8:
return
serialData = struct.unpack("ff",nextAngle)
nextAngle = serialData[0]
pidOutput = serialData[1]
self.angle = np.float(nextAngle*180/np.pi)
self.PIDoutput = np.float(pidOutput)
self.samplePeriod = time.time()-self.lastSampleTime
self.lastSampleTime = time.time()
def paintGL(self):
self.updateAngle()
"""
Paint the scene.
"""
# clear the buffer
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
self.inputHandler.runInputFuncs()
self.isDirty = False
gl.glPushMatrix()
gl.glScalef(1.0,1.0,1.0)
""" Draw shape """
gl.glRotatef(90-self.angle,0,0,1)
self.drawBox([0,0],[0.5,0.01])
gl.glPopMatrix()
self.drawHUD()
glut.glutSwapBuffers()
def resizeGL(self, width, height):
"""
Called upon window resizing: reinitialize the viewport.
"""
# update the window size
self.width, self.height = width, height
# paint within the whole window
gl.glViewport(0, 0, self.width,self.height)
# set orthographic projection (2D only)
gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()
# the window corner OpenGL coordinates are (-+1, -+1)
gl.glOrtho(-1, 1, -1, 1, -1, 1)
def drawHUD(self,):
self.drawText(-1.0,.95,"Angle: {:<.8f}".format(self.angle))
self.drawText(-1.0,.90,"PID Output: {:<.8f}".format(self.PIDoutput))
self.drawText(-1.0,.85,"Sample Period: {:<.8f}".format(self.samplePeriod))
self.drawText(-1.0,.8,"Input: {}".format(hex(self.inputHandler.inputEvents)))
def getMousePos(self,):
viewdim = np.array([self.width,self.height])
cam_mouse = self.inputHandler.mouse[:,self.inputHandler.ind]
mousepos = np.array([cam_mouse[0]-viewdim[0]/2,viewdim[1]/2-cam_mouse[1]])*2/viewdim
mousepos = mousepos / self.camerazoom
mousepos[0] = max(min(mousepos[0],1.0),-1.0)
mousepos[1] = max(min(mousepos[1],1.0),-1.0)
return mousepos
def drawBox(self,p1,p2,c=[0,0,0,0.5]):
gl.glPushMatrix()
gl.glBegin(gl.GL_QUAD_STRIP)
gl.glColor4f(c[0],c[1],c[2],c[3])
gl.glVertex2d(p1[0],p1[1])
gl.glVertex2d(p2[0],p1[1])
gl.glVertex2d(p1[0],p2[1])
gl.glVertex2d(p2[0],p2[1])
gl.glEnd()
gl.glFlush()
gl.glPopMatrix()
def drawText(self,x,y,text,scale=1.0,color=(0,0,0,1)):
default_scale = 0.15
gl.glPushMatrix()
gl.glTranslate(x,y,0)
gl.glScalef(default_scale/self.width*scale,default_scale/self.height*scale,1.0)
gl.glLineWidth(1.0)
gl.glColor4f(*color)
glut.glutStrokeString(glut.GLUT_STROKE_MONO_ROMAN,text)
gl.glPopMatrix()
class InputHandler(object):
INPUT_SHIFTS = {
'MOUSE': 0,
'WHEEL':3,
'SPECIAL':4,
'KB':9,
}
IMAP = {
'MOUSE': {
'LEFTCLICK': 0,
'MIDDLECLICK': 1,
'RIGHTCLICK': 2,
},
'WHEEL': {
'DOWN': 0,
'UP': 1,
},
'SPECIAL': {
'SHIFT': 12,
'CTRL': 14,
'ALT':16,
},
'KB':{
}
}
@staticmethod
def convert_input(eventtype,eventnum):
if eventtype=='KB':
inmap = eventnum
else:
inmap = InputHandler.IMAP[eventtype][eventnum]
return 1<<(inmap+InputHandler.INPUT_SHIFTS[eventtype])
@staticmethod
def convert_inputmap(inputmap):
converted = {}
for inputkey in inputmap:
converted_key = 0x000000
for k in inputkey:
converted_key |= InputHandler.convert_input(*k)
converted[converted_key] = inputmap[inputkey]
return converted
def __init__(self,inputfunc_map):
self.mouse = np.zeros((2,60))
self.ind = 0
self.inputEvents = 0x000000000L
self.inputFuncs = InputHandler.convert_inputmap(inputfunc_map)
def attachGLUT(self):
glut.glutMouseFunc(self.mouseInputHandler)
glut.glutMouseWheelFunc(self.mouseWheelHandler)
glut.glutMotionFunc(self.mouseMoveHandler)
glut.glutPassiveMotionFunc(self.mouseMoveHandler)
glut.glutKeyboardFunc(self.keyboardHandler)
glut.glutKeyboardUpFunc(lambda *x:self.keyboardHandler(*x,isEnd=True))
glut.glutSpecialFunc(self.specialKeyHandler)
glut.glutSpecialUpFunc(lambda *x:self.specialKeyHandler(*x,isEnd=True))
def runInputFuncs(self):
if self.inputEvents in self.inputFuncs:
self.inputFuncs[self.inputEvents]()
def recordInput(self,eventType,eventNum,isEnd=None):
event = 1<<(eventNum+InputHandler.INPUT_SHIFTS[eventType])
if isEnd==None:
# isEnd = (self.inputEvents&event)==event
self.inputEvents |= event
self.runInputFuncs()
# self.inputEvents &= event
self.inputEvents ^= event
elif isEnd:
self.inputEvents &= event
self.inputEvents ^= event
else:
self.inputEvents |= event
def mouseInputHandler(self,*event):
eventNum,isEnd,mouseX,mouseY = event
self.recordInput('MOUSE',eventNum,isEnd)
def mouseMoveHandler(self,*event):
mousex,mousey = event
self.ind = (self.ind+1)%len(self.mouse)
self.mouse[0,self.ind] = mousex
self.mouse[1,self.ind] = mousey
def mouseWheelHandler(self,*event):
mouseWheel,direction,mouseX,mouseY = event
direction = 0 if direction==-1 else 1
self.recordInput('WHEEL',direction,)
def keyboardHandler(self,*event,**kwargs):
key,mousex,mousey = event
self.recordInput('KB',ord(key),isEnd=kwargs.get('isEnd',False))
def specialKeyHandler(self,*event,**kwargs):
keyid,mousex,mousey = event
keyid-=100
if keyid not in InputHandler.IMAP['SPECIAL'].values():
self.inputEvents = 0x0L
return
self.recordInput('SPECIAL',keyid,isEnd=kwargs.get('isEnd',False))
def initializeGui(serial):
x = GLPlotWidget(serial)
print "Loading..."
x.initialize()