-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocessing.py
More file actions
182 lines (142 loc) · 5.43 KB
/
processing.py
File metadata and controls
182 lines (142 loc) · 5.43 KB
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
from data import SensorData, Vec3
from enum import Enum
from receiver import Receiver
import time
class Signal3:
x: [float]
y: [float]
z: [float]
t: [float]
def __init__(self):
self.x = []
self.y = []
self.z = []
self.t = []
def append(self, data: Vec3, t: float):
self.x.append(data.x)
self.y.append(data.y)
self.z.append(data.z)
self.t.append(t)
class History3(Signal3):
def __init__(self, history_length=20):
super().__init__()
self.history_length = history_length
def append(self, data: Vec3, t: float):
super().append(data, t)
# limit data to not overload the RAM
self.x = self.x[-self.history_length:]
self.y = self.y[-self.history_length:]
self.z = self.z[-self.history_length:]
self.t = self.t[-self.history_length:]
class Derivation3(History3):
last_data: Vec3
def __init__(self, history_length=20, absolute=False):
super().__init__(history_length)
self.absolute = absolute
self.last_update = None
def abs(self, data: float):
if self.absolute:
return abs(data)
else:
return data
def append(self, data: Vec3, t: float):
derive = False
if self.last_update is not None:
super().append(Vec3((self.abs(data.x) - self.abs(self.last_data.x)) / (t - self.last_update),
(self.abs(data.y) - self.abs(self.last_data.y)) / (t - self.last_update),
(self.abs(data.z) - self.abs(self.last_data.z)) / (t - self.last_update)), t)
self.last_data = data
self.last_update = t
class SensorHistory:
rot: History3
acc: History3
gyro: History3
gyro_derivation: Derivation3
def __init__(self, history_length=20):
self.rot = History3(history_length)
self.acc = History3(history_length)
self.gyro = History3(history_length)
self.gyro_derivation = Derivation3(history_length)
def append(self, data: SensorData):
self.rot.append(data.rot, data.time)
self.acc.append(data.acc, data.time)
self.gyro.append(data.gyro, data.time)
self.gyro_derivation.append(data.gyro, data.time)
class EventType(Enum):
THRESHOLD_POS = 1,
THRESHOLD_NEG = 2,
EDGE_RISE = 3,
EDGE_FALL = 4
class Dimension(Enum):
X = 1,
Y = 2,
Z = 3
class EventType(Enum):
THRESHOLD_POS = 1,
THRESHOLD_NEG = 2,
EDGE_RISE = 3,
EDGE_FALL = 4
class Event:
type: EventType
class Event3:
type: EventType
dimension: Dimension
def __init__(self, position, window_index, value, type: EventType, dimension: Dimension):
self.position = position
self.window_index = window_index
self.value = value
self.type = type
self.dimension = dimension
class EventProcessor3:
def __init__(self, signal: Signal3, window_length=5, cooldown=10,
threshold_pos=50, threshold_neg=-50):
self.signal = signal
self.window_length = window_length
self.cooldown = cooldown
self.threshold_pos = threshold_pos
self.threshold_neg = threshold_neg
def analyze(self):
events = []
max_threshold_pos = 0
min_threshold_neg = 0
for (signal, dimension) in [(self.signal.x, Dimension.X),
(self.signal.y, Dimension.Y),
(self.signal.z, Dimension.Z)]:
for index, val in enumerate(signal[-self.cooldown:]):
if val > self.threshold_pos:
events.append(Event3(self.signal.t[index-self.cooldown+len(signal)], index,
val, EventType.THRESHOLD_POS, dimension))
if val > max_threshold_pos:
max_threshold_pos = val
elif val < self.threshold_neg:
events.append(Event3(self.signal.t[index-self.cooldown+len(signal)], index,
val, EventType.THRESHOLD_NEG, dimension))
if val < min_threshold_neg:
min_threshold_neg = val
for e in events:
if e.window_index < self.cooldown - self.window_length:
events.remove(e)
elif e.value < max_threshold_pos and e.type == EventType.THRESHOLD_POS:
events.remove(e)
elif e.value > min_threshold_neg and e.type == EventType.THRESHOLD_NEG:
events.remove(e)
return events
class Processor:
def __init__(self):
self.history = SensorHistory()
self.gyro_dev_processor = EventProcessor3(self.history.gyro_derivation)
self.callbacks = []
self.old_nevents = 0
async def add_receiver(self, receiver: Receiver):
await receiver.add_callback(self.new_data)
async def new_data(self, data: SensorData):
self.history.append(data)
gyro_deriv_events = self.gyro_dev_processor.analyze()
if len(gyro_deriv_events) > 0 or self.old_nevents > len(gyro_deriv_events):
for cb in self.callbacks:
await cb(gyro_deriv_events)
self.old_nevents = len(gyro_deriv_events)
async def add_callback(self, callback):
self.callbacks.append(callback)
async def remove_callback(self, callback):
self.callbacks.remove(callback)