forked from alzmcr/raspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imu.py
36 lines (30 loc) · 1.1 KB
/
imu.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
# Inertial Measurement Unit
from berryimu import Imu as BerryImu
from time import time, sleep
from threading import Thread
class Imu(BerryImu):
def __init__(self):
super(Imu, self).__init__()
self.continue_sampling = True
self.samples = []
self.thread = None
def _sampler(self, seconds=None, interval=0.020, verbose=False):
itime = time(); counter = 0
while self.continue_sampling:
if (time() - itime) > seconds: break
self.samples.append(self.reading())
counter += 1
rtime = (counter * interval) - (time() - itime)
rtime = rtime if rtime > 0 else 0
sleep(rtime)
self.continue_sampling = True
return self.samples
def sampler(self, seconds=None, interval=0.020, verbose=False):
self.thread = Thread(target = self._sampler, args = (seconds, interval, verbose))
self.thread.start()
def stop_sampling(self):
self.continue_sampling = False
self.thread.join()
def get_samples(self):
self.stop_sampling()
return self.samples