-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprocessing.py
369 lines (288 loc) · 11.1 KB
/
processing.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/python
# Compute the position of a Lighthouse given
# sensor readings in a known configuration.
#from math import *
import serial
from math import *
import numpy as np
from reception import *
#########################################################
# PROCESSING LightHouse
#########################################################
# Apply changement of coordinate M = [[0, 0, -1], [1, 0, 0], [0, 1, 0]]
# RB_R = Mt . [Bonsai given rotation matrix] . M
# t stand for transposed
# INITIALSATION
# Positionning LH
p1 = [0.2055409, 2.522384, -2.553286]
p2 = [2.326427, 2.428492, 1.591011]
# Rotation Matrices LH1 and LH2 on world Basis
m1 = [[0.8633447, 0.02179115, -0.5041437],
[-0.07533064, -0.9823064, -0.1714628],
[-0.49896, 0.186009, -0.8464276]]
m2 = [[-0.8772146, 0.03632253, 0.4787225],
[0.05409878, -0.9833049, 0.1737381],
[0.4770408, 0.1783039, 0.8606042]]
# Homogeneous coordinate matrices given by Vive sdk
h1 = [[0.8633447, 0.02179115, -0.5041437, 0],
[-0.07533064, -0.9823064, -0.1714628, 0],
[-0.49896, 0.186009, -0.8464276, 0],
[0.2055409, 2.522384, -2.553286, 1]]
h2 = [[-0.8772146, 0.03632253, 0.4787225, 0],
[0.05409878, -0.9833049, 0.1737381, 0],
[0.4770408, 0.1783039, 0.8606042, 0],
[2.326427, 2.428492, 1.591011, 1]]
# Changement base matrix from OpenGL to Blender
R_ob = [[ 1, 0, 0],
[ 0, 0,-1],
[ 0, 1, 0]]
# Translation vectors after base Changement
p1b = np.dot(R_ob,p1)
#print(p1b)
p2b = np.dot(R_ob,p2)
#print(p2b)
# Rotation matrix in the base changement
R1 = np.matmul(np.linalg.inv(R_ob), np.matmul(m1, R_ob))
R2 = np.matmul(np.linalg.inv(R_ob), np.matmul(m2, R_ob))
def vect_uv(angle_scan):
h1 = [[0.8633447, 0.02179115, -0.5041437, 0],
[-0.07533064, -0.9823064, -0.1714628, 0],
[-0.49896, 0.186009, -0.8464276, 0],
[0.2055409, 2.522384, -2.553286, 1]]
h2 = [[-0.8772146, 0.03632253, 0.4787225, 0],
[0.05409878, -0.9833049, 0.1737381, 0],
[0.4770408, 0.1783039, 0.8606042, 0],
[2.326427, 2.428492, 1.591011, 1]]
vecH1_loc = np.array([-cos(angle_scan[0]), 0, sin(angle_scan[0])])
vecV1_loc = np.array([0, -cos(angle_scan[1]), sin(angle_scan[1])])
vecH2_loc = np.array([-cos(angle_scan[2]), 0, sin(angle_scan[2])])
vecV2_loc = np.array([0, -cos(angle_scan[3]), sin(angle_scan[3])])
u = vecH1_loc + vecV1_loc
v = vecH2_loc + vecV2_loc
norm_u = np.linalg.norm(u)
norm_v = np.linalg.norm(v)
# u & v in homogeneous coordinates normalized
u_loc = np.array([u[0]/norm_u, u[1]/norm_u, - u[2]/norm_u, 1])
v_loc = np.array([v[0]/norm_v, v[1]/norm_v, - v[2]/norm_v, 1])
# Transform line from relative coordinates to global lighthouse coordinate system (defined by matrix) (multiply vector by matrix)
# For LH1
# we need to transpose to convert from column-major to row-major
h1 = np.array(h1).T # raw base transform
p1 = np.array([0,0,0,1]) # (0,0,0) in homogeneous coordinates
p1 = np.matmul(h1,p1) # p1 is position of base A
u = np.matmul(h1,u_loc) # u vector after scanning of base A
# now we fix all this to Blender space (swap Z with Y)
swizzle = [0,2,1,3]
p1 = p1[swizzle]
u = u[swizzle]
# For LH2
h2 = np.array(h2).T # raw base transform
p2 = np.array([0,0,0,1]) # (0,0,0) in homogeneous coordinates
p2 = np.matmul(h2,p2) # p2 is position of base A
v = np.matmul(h2,v_loc) # v vector after scanning of base A
p2 = p2[swizzle]
v = v[swizzle]
return u[0:3], v[0:3]
def diode_pos(angle_scan):
h1 = [[0.8633447, 0.02179115, -0.5041437, 0],
[-0.07533064, -0.9823064, -0.1714628, 0],
[-0.49896, 0.186009, -0.8464276, 0],
[0.2055409, 2.522384, -2.553286, 1]]
h2 = [[-0.8772146, 0.03632253, 0.4787225, 0],
[0.05409878, -0.9833049, 0.1737381, 0],
[0.4770408, 0.1783039, 0.8606042, 0],
[2.326427, 2.428492, 1.591011, 1]]
vecH1_loc = np.array([-cos(angle_scan[0]), 0, sin(angle_scan[0])])
vecV1_loc = np.array([0, -cos(angle_scan[1]), sin(angle_scan[1])])
vecH2_loc = np.array([-cos(angle_scan[2]), 0, sin(angle_scan[2])])
vecV2_loc = np.array([0, -cos(angle_scan[3]), sin(angle_scan[3])])
u = vecH1_loc + vecV1_loc
v = vecH2_loc + vecV2_loc
norm_u = np.linalg.norm(u)
norm_v = np.linalg.norm(v)
# u & v in homogeneous coordinates normalized
u_loc = np.array([u[0]/norm_u, u[1]/norm_u, - u[2]/norm_u, 1])
v_loc = np.array([v[0]/norm_v, v[1]/norm_v, - v[2]/norm_v, 1])
# Transform line from relative coordinates to global lighthouse coordinate system (defined by matrix) (multiply vector by matrix)
# For LH1
# we need to transpose to convert from column-major to row-major
h1 = np.array(h1).T # raw base transform
p1 = np.array([0,0,0,1]) # (0,0,0) in homogeneous coordinates
p1 = np.matmul(h1,p1) # p1 is position of base A
u = np.matmul(h1,u_loc) # u vector after scanning of base A
# now we fix all this to Blender space (swap Z with Y)
swizzle = [0,2,1,3]
p1 = p1[swizzle]
u = u[swizzle]
# For LH2
h2 = np.array(h2).T # raw base transform
p2 = np.array([0,0,0,1]) # (0,0,0) in homogeneous coordinates
p2 = np.matmul(h2,p2) # p2 is position of base A
v = np.matmul(h2,v_loc) # v vector after scanning of base A
p2 = p2[swizzle]
v = v[swizzle]
# reshape vectors
u = u[0:3]
v = v[0:3]
p1 = p1[0:3]
p2 = p2[0:3]
p0 = p1
q0 = p2
# STEP: resolve the system of imperfect intersection
w0 = np.array([p1[0] - p2[0], p1[1] - p2[1], p1[2] - p2[2]])
#w0 = p0 - q0
a = np.dot(u, u) #scalar product of u and w0
b = np.dot(u, v)
c = np.dot(v, v)
d = np.dot(u, w0)
e = np.dot(v, w0)
# Resolution of the linear system
#k = np.array([[uu, -uv], [uv, -vv]])
#l = np.array([ABu, ABv])
#lambda_mu = np.linalg.solve(k, l)
denom = a*c - b*b
pS = np.zeros(3)
qT = np.zeros(3)
I = np.zeros(3)
if denom >= 1e-6:
s = (e * b - c * d) / denom
t = (a * e - b * d) / denom
for i in range(3):
pS[i] = p0[i] + s*u[i]
qT[i] = q0[i] + t*v[i]
I[i] = -(pS[i] + qT[i]) / 2
return I
##########################################################
# PROCESSING IMU
##########################################################
# Period of measurement of the IMU
T = 1/120.
# Standard deviation of IMU (m/s^2) considered the same on 3 axis
S_ACC = 0.0423639918
def IMU_pos(prevPos, prevVel, accel):
# Initilisation of arrays. Two vectors the old "0" and the new "1" values
velocity = np.zeros((2,3))
position = np.zeros((2,3))
for i in range(3):
velocity[0][i] = prevVel[i]
position[0][i] = prevPos[i]
# Here begin the function
# First integration
for i in range(3):
velocity[1][i] = velocity[0][i] + accel[i] * T
# Second integration
for i in range(3):
position[1][i] = position[0][i] + velocity[0][i] * T
return [position[1], velocity[1], accel]
#########################################################
# MAIN
#########################################################
def init_position():
# Initialize serial port and prepare data buffer
return Reception()
# Initialize Angle calculated from timings of LH
scanAngle = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
# Initailize positions of diodes 0, 1, 2 and 3
I_diode = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
raw_diode = np.zeros((4,4,3))
# Circular buffer index
cbi = 0
time = 4
I_Accelero = [0, 0, 0]
# Previous velocity
velocity = [0,0,0]
# Standard deviation of IMU
s_I_Accelero = [0, 0, 0]
s_velocity = [0, 0, 0]
s_accelerations = [S_ACC, S_ACC, S_ACC]
wasIMUInit = False
def get_vect_uv(rx):
global I_Accelero, velocity, s_I_Accelero, s_velocity, s_accelerations
global FILTER, S_ACC, raw_diode, time, wasIMUInit, cbi, factor
if not wasIMUInit :
velocity = [0, 0, 0]
wasIMUInit = True
# Refresh data
# base = 0 or 1 (B or C)
# axis = 0 or 1 (horizontal or vertical)
# centroids = array of 4 floats in microseconds
# accelerations = array of 3 floats in G (AKA m/s^2)
base, axis, centroids, accelerations = rx.parse_data()
# Periode of one scan in micro seconds
T_scan = 8333
# Convert time of scanning into angle in radians
for i in range(4):
scanAngle[i][base*2 + axis] = centroids[i] * pi / T_scan
# For Lighthouses
#for i in range(4):
# Scan for diode number 0
return vect_uv(scanAngle[0])
def get_position(rx):
global I_Accelero, velocity, s_I_Accelero, s_velocity, s_accelerations
global FILTER, S_ACC, raw_diode, time, wasIMUInit, cbi, factor
if not wasIMUInit :
velocity = [0, 0, 0]
wasIMUInit = True
# Refresh data
# base = 0 or 1 (B or C)
# axis = 0 or 1 (horizontal or vertical)
# centroids = array of 4 floats in microseconds
# accelerations = array of 3 floats in G (AKA m/s^2)
base, axis, centroids, accelerations = rx.parse_data()
# Periode of one scan in micro seconds
T_scan = 8333
time += 1
# Convert time of scanning into angle in radians
for i in range(4):
scanAngle[i][base*2 + axis] = centroids[i] * pi / T_scan
# For Lighthouses
for i in range(4):
I_diode[i] = diode_pos(scanAngle[i])
FILTER = 1
# Factor of smoothness for low pass filter
factor = 0.1
# Low pass filter
if FILTER == 1 :
for d in range(4):
for xyz in range(3):
I_diode[d][xyz] = (1 - factor) * raw_diode[0][d][xyz] + factor * I_diode[d][xyz]
raw_diode[0][d][xyz] = I_diode[d][xyz]
# Low pass filter using 4 last optical data
elif FILTER == 2 :
for d in range(4):
for xyz in range(3):
raw_diode[cbi][d][xyz] = I_diode[d][xyz]
average = 0
for t in range(4):
average += raw_diode[t][d][xyz]
I_diode[d][xyz] = average / 4
# Circular buffer index
cbi = (cbi+1) % 4
I_LH = [(I_diode[0][0] + I_diode[3][0]) / 2, (I_diode[0][1] + I_diode[3][1]) / 2, (I_diode[0][2] + I_diode[3][2]) / 2]
# Position where the IMU will be at calibration
averagePos = I_LH
# For IMU
# Reset position of IMU at (1/120 * 4)s
# We consider variance on measurement, the same on 3 axis
if time >= 4 :
# off_set allows to calibrate position of the IMU
off_set = averagePos
I_Accelero = [0, 0, 0]
time = 0
for i in range(3):
I_Accelero[i] = off_set[i]
velocity[i] = 0
s_I_Accelero[i] = 0
s_velocity[i] = 0
s_accelerations[i] = S_ACC
# Update data of the accelerometer
I_Accelero, velocity, accel = IMU_pos(I_Accelero, velocity, accelerations)
# Update standard deviation of accelerometer
s_I_Accelero, s_velocity, s_accelerations = IMU_pos(s_I_Accelero, s_velocity, s_accelerations)
return I_diode, [I_Accelero, velocity, accel], [s_I_Accelero, s_velocity, s_accelerations]