-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.py
158 lines (120 loc) · 4.52 KB
/
main.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
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from figure import *
from tkinter import *
from tkinter import ttk
import time
import serial
import threading
ApplicationGL = False
class PortSettings:
Name = "COM1"
Speed = 9600
Timeout = 2
class IMU:
Roll = 0
Pitch = 0
Yaw = 0
myport = PortSettings()
myimu = IMU()
def RunAppliction():
global ApplicationGL
myport.Name = Port_entry.get()
myport.Speed = Baud_entry.get()
ApplicationGL = True
ConfWindw.destroy()
ConfWindw = Tk()
ConfWindw.title("Configure Serial Port")
ConfWindw.configure(bg = "#2E2D40")
ConfWindw.geometry('300x150')
ConfWindw.resizable(width=False, height=False)
positionRight = int(ConfWindw.winfo_screenwidth()/2 - 300/2)
positionDown = int(ConfWindw.winfo_screenheight()/2 - 150/2)
ConfWindw.geometry("+{}+{}".format(positionRight, positionDown))
Port_label = Label(text = "Port:",font =("",12),justify= "right",bg = "#2E2D40",fg = "#FFFFFF")
Port_label.place(x = 50, y =30,anchor = "center")
Port_entry = Entry(width = 20,bg = "#37364D", fg = "#FFFFFF", justify = "center")
Port_entry.insert(INSERT,myport.Name)
Port_entry.place(x = 180, y = 30,anchor = "center")
Baud_label = Label(text = "Speed:",font =("",12),justify= "right",bg = "#2E2D40",fg = "#FFFFFF")
Baud_label.place(x = 50, y =80,anchor = "center")
Baud_entry = Entry(width = 20,bg = "#37364D", fg = "#FFFFFF", justify = "center")
Baud_entry.insert(INSERT,str(myport.Speed))
Baud_entry.place(x = 180, y = 80,anchor = "center")
ok_button = Button(text = "Ok",width = 8,command = RunAppliction,bg="#135EF2",fg ="#FFFFFF")
ok_button.place(x = 150, y = 120,anchor="center")
def InitPygame():
global display
pygame.init()
display = (640,480)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
pygame.display.set_caption('IMU visualizer (Press Esc to exit)')
def InitGL():
glClearColor((1.0/255*46),(1.0/255*45),(1.0/255*64),1)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
gluPerspective(100, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0.0,0.0, -5)
def DrawText(textString):
font = pygame.font.SysFont ("Courier New",25, True)
textSurface = font.render(textString, True, (255,255,0), (46,45,64,255))
textData = pygame.image.tostring(textSurface, "RGBA", True)
glDrawPixels(textSurface.get_width(), textSurface.get_height(), GL_RGBA, GL_UNSIGNED_BYTE, textData)
def DrawBoard():
glBegin(GL_QUADS)
x = 0
for surface in surfaces:
for vertex in surface:
glColor3fv((colors[x]))
glVertex3fv(vertices[vertex])
x += 1
glEnd()
def DrawGL():
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
gluPerspective(90, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0.0,0.0, -5)
glRotatef(round(myimu.Pitch,1), 0, 0, 1)
glRotatef(round(myimu.Roll,1), -1, 0, 0)
DrawText("Roll: {}° Pitch: {}°".format(round(myimu.Roll,1),round(myimu.Pitch,1)))
DrawBoard()
pygame.display.flip()
def SerialConnection ():
global serial_object
serial_object = serial.Serial( myport.Name, baudrate= myport.Speed, timeout = myport.Timeout)
def ReadData():
while True:
serial_input = serial_object.readline()
if(len(serial_input) == 9 and serial_input[0] == 0x24 ):
X = [serial_input[2], serial_input[1]]
Ax = int.from_bytes(X,byteorder = 'big',signed=True)
Y = [serial_input[4], serial_input[3]]
Ay = int.from_bytes(Y,byteorder = 'big',signed=True)
myimu.Roll = Ax/16384.0*90
myimu.Pitch = Ay/16384.0*90
def main():
ConfWindw.mainloop()
if ApplicationGL == True:
InitPygame()
InitGL()
try:
SerialConnection()
myThread1 = threading.Thread(target = ReadData)
myThread1.daemon = True
myThread1.start()
while True:
event = pygame.event.poll()
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
break
DrawGL()
pygame.time.wait(10)
except:
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
DrawText("Sorry, something is wrong :c")
pygame.display.flip()
time.sleep(5)
if __name__ == '__main__': main()