-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_processor.py
227 lines (165 loc) · 5.66 KB
/
data_processor.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
#!/usr/bin/env python
# =====================================================================
#
# =====================================================================
import os, sys, time
import math, re, time
import multiprocessing as mp
import py3toolbox as tb
import math_helper as mh
import low_pass_filters as lpf
from scipy.spatial.transform import Rotation
MODULE_NAME = 'DataProcessor'
def get_config():
config = tb.load_json('./config.json')
return config
# =================================================
#
# Data Processor
# - Process signals and apply filters
#
# =================================================
class DataProcessor(mp.Process):
def __init__(self, q_in, q_out, q_mon):
mp.Process.__init__(self)
self.config = get_config()[MODULE_NAME]
self.q_in = q_in
self.q_out = q_out
self.q_mon = q_mon
self.processed_data = {}
def apply_test_filter(self):
ravg_fltr = lpf.RunningAvarageFilter(10)
exp_fltr = lpf.ExponentialFilter(0.2)
"""
mX_min = None
mX_max = None
mY_min = None
mY_max = None
mZ_min = None
mZ_max = None
"""
pX = 0
pY = 0
pZ = 0
t = time.time()
while True:
try :
data = self.q_in.get()
data['Type'] = 'QUATERNION'
data['avg.aX'], data['avg.aY'], data['avg.aZ'] = ravg_fltr.compute([data['aX'], data['aY'], data['aZ']])
data['exp.aX'], data['exp.aY'], data['exp.aZ'] = exp_fltr.compute([data['aX'], data['aY'], data['aZ']])
quat = []
rot_aX , rot_aY, rot_aZ = mh.rotate_vector ( [ data['qX'], data['qY'], data["qZ"], data['qW'] ], [data['exp.aX'], data['exp.aY'], data['exp.aZ']] )
pX += rot_aX * (time.time() - t) * (time.time() - t)
pY += rot_aY * (time.time() - t) * (time.time() - t)
pZ += rot_aZ * (time.time() - t) * (time.time() - t)
t = time.time()
data["pX"] = pX
data["pY"] = pY
data["pZ"] = pZ
"""
if mX_min is None : mX_min = data["mX"]
if mX_max is None : mX_max = data["mX"]
if mY_min is None : mY_min = data["mY"]
if mY_max is None : mY_max = data["mY"]
if mZ_min is None : mZ_min = data["mZ"]
if mZ_max is None : mZ_max = data["mZ"]
if data["mX"] < mX_min : mX_min = data["mX"]
if data["mX"] > mX_max : mX_max = data["mX"]
if data["mY"] < mY_min : mY_min = data["mY"]
if data["mY"] > mY_max : mY_max = data["mY"]
if data["mZ"] < mZ_min : mZ_min = data["mZ"]
if data["mZ"] > mZ_max : mZ_max = data["mZ"]
"""
data['Roll'], data['Pitch'], data['Yaw'] = mh.get_euler( [ data['qX'], data['qY'], data["qZ"], data['qW'] ] , degrees = True )
self.processed_data = data
self.output_data()
except Exception as e:
print (str(e))
pass
def apply_no_filter_for_raw(self):
Yaw = 0
t = time.time()
while True:
try :
data = self.q_in.get()
if bool(data) == True:
data['Roll'] = - math.atan2(-data['aX'], data['aZ']) / math.pi * 180
data['Pitch'] = - math.atan2(data['aY'], data['aZ']) / math.pi * 180
Yaw +=data['gZ'] * (time.time() - t)
data['Yaw'] = Yaw
t = time.time()
data['Type'] = 'EULR'
print (self.q_in.qsize(),self.q_out[0].qsize() )
self.processed_data = data
self.output_data()
except Exception:
pass
def apply_no_filter_for_quat(self):
t = time.time()
pX = 0
pY = 0
pZ = 0
while True:
try :
data = self.q_in.get()
#pX += data["aX"] * (time.time() - t)
#pY += data["aY"] * (time.time() - t)
#pZ += data["aZ"] * (time.time() - t)
#t = time.time()
#data["pX"] = pX
#data["pY"] = pY
#data["pZ"] = pZ
data['Type'] = 'QUATERNION'
#data['Type'] = 'YPR'
self.processed_data = data
self.output_data()
except Exception:
pass
def apply_complementary(self):
Yaw = 0
t = time.time()
while True:
try :
data = self.q_in.get()
if bool(data) == True:
data['Roll'] = - math.atan2(-data['aX'], data['aZ']) / math.pi * 180
data['Pitch'] = - math.atan2(data['aY'], data['aZ']) / math.pi * 180
Yaw +=data['gZ'] * (time.time() - t)
data['Yaw'] = Yaw
t = time.time()
data['Type'] = 'YPR'
print (self.q_in.qsize(),self.q_out[0].qsize() )
self.processed_data = data
self.output_data()
except Exception:
pass
def output_data (self):
if bool(self.processed_data) :
if type(self.q_out) is list:
for q in self.q_out:
q.put(self.processed_data)
else:
self.q_out.put(self.processed_data)
def run(self):
self.q_mon.put(MODULE_NAME)
try :
if self.config['filter'] == 'COMPLEMENTARY':
self.apply_complementary()
elif self.config['filter'] == 'KALMAN':
self.apply_kalman()
elif self.config['filter'] == 'FOURIER':
self.apply_fourier()
elif self.config['filter'] == 'LAPLACE':
self.apply_laplace()
elif self.config['filter'] == 'NOFLTR_FOR_RAW':
self.apply_no_filter_for_raw()
elif self.config['filter'] == 'NOFLTR_FOR_QUAT':
self.apply_no_filter_for_quat()
elif self.config['filter'] == 'TEST':
self.apply_test_filter()
#self.apply_no_filter_for_quat()
except Exception as err:
exit (1)
if __name__ == '__main__':
pass