/
PythonApplication1.py
178 lines (121 loc) · 3.43 KB
/
PythonApplication1.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
import sys
import time
import ovr
from socket import *
import numpy as np
from math import cos, sin, radians, degrees, atan2, sqrt, acos, pi
from Quaternion import Quat
from collections import namedtuple
import datetime
def convert_to_degrees(radians):
return (radians*180) / pi
def quaternion_to_euler(x,y,z,w):
sqw = w * w
sqx = x * x
sqy = y * y
sqz = z * z
normal = math.sqrt(sqw + sqx + sqy + sqz)
pole_result = (x * z) + (y * w)
if (pole_result > (0.5 * normal)): # singularity at north pole
ry = math.pi/2 #heading/yaw?
rz = 0 #attitude/roll?
rx = 2 * math.atan2(x, w) #bank/pitch?
return Euler(rx, ry, rz)
if (pole_result < (-0.5 * normal)): # singularity at south pole
ry = -math.pi/2
rz = 0
rx = -2 * math.atan2(x, w)
return Euler(rx, ry, rz)
r11 = 2*(x*y + w*z)
r12 = sqw + sqx - sqy - sqz
r21 = -2*(x*z - w*y)
r31 = 2*(y*z + w*x)
r32 = sqw - sqx - sqy + sqz
rx = math.atan2( r31, r32 )
ry = math.asin ( r21 )
rz = math.atan2( r11, r12 )
return Euler(rx, ry, rz)
ovr.initialize(None)
session, luid = ovr.create()
hmdDesc = ovr.getHmdDesc(session)
print hmdDesc.ProductName
aux = 0
aux2 = 9
host = "192.168.1.35"
print host
port = 4446
port2 = 4447
s = socket(AF_INET, SOCK_STREAM)
s2 = socket(AF_INET, SOCK_STREAM)
print "Socket Made"
s.bind((host,port))
s2.bind((host,port2))
print "Socket Bound"
s.listen(5)
s2.listen(5)
print "Listening for connections..."
q,addr = s.accept()
q2,addr2 = s2.accept()
pos=0
posH = 0
time.sleep(5)
ts = ovr.getTrackingState(session, ovr.getTimeInSeconds(), True)
if ts.StatusFlags & (ovr.Status_OrientationTracked | ovr.Status_PositionTracked):
time.sleep(0.200)
pos =ts.HeadPose.ThePose.Orientation.x
posH =ts.HeadPose.ThePose.Orientation.y
degaux = convert_to_degrees(pos)
degauxH = convert_to_degrees(posH)
degaux = degaux+90
degaux = round(degaux)
deg = degaux
degauxH = degauxH+90
degauxH = round(degauxH)
degH = degauxH
data = str(degH)
q2.send(data)
i=0
t=0
while(1):
ts = ovr.getTrackingState(session, ovr.getTimeInSeconds(), True)
if ts.StatusFlags & (ovr.Status_OrientationTracked | ovr.Status_PositionTracked):
pos =ts.HeadPose.ThePose.Orientation.x
deg = convert_to_degrees(pos*2.2)
deg = deg+90
deg = round(deg)
posH =ts.HeadPose.ThePose.Orientation.y
degH = convert_to_degrees(posH*2.2)
degH = degH+90
degH = round(degH)
if(abs(degaux-deg)>4):
if((deg < 0.0) | (deg >170.0)):
continue
if (deg>110.0):
q2.send("A")
deg=105.0
data=str(deg)
q.send(data)
elif (deg<109.0):
q2.send("P")
data=str(deg)
q.send(data)
else:
data = str(deg)
q.send(data)
pos=0
degaux = deg
deg = 0
pos=0
deg = 0
if(abs(degauxH-degH)>8):
data = str(degH)
print data
q2.send(data)
posH=0
degauxH = degH
degH = 0
posH=0
degH = 0
time.sleep(0.200)
ovr.destroy(session)
ovr.shutdown()