-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgyrodata.py
105 lines (92 loc) · 3.71 KB
/
gyrodata.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
import numpy
import pickle
import os
class GyroscopeDataFile(object):
# the file path to read
# a dictionary of angular velocities
# This dictionary will store mappings between the timestamp
# and the angular velocity at that instant.
def __init__(self, filepath):
self.filepath = filepath
self.omega = {}
self.gyrofilename = "./.gyroscopedata.data"
def _getfile_object(self):
return open(self.filepath)
# read the file and populate the Omega dictionary.
def parse(self):
with self._getfile_object() as fp:
# We validate that the first line of the csv file matches our
# expectation. If not, the csv file was probably not compatible
# and will error out over the next few lines.
firstline = fp.readline().strip()
if not firstline == "gyro":
raise Exception("The first line isn't valid")
# The strip function removed any additional whitespace
# (tabs, spaces, newline characters, among others)
# that might be stored in the file.
for line in fp.readlines():
line = line.strip()
parts = line.split(",")
# convert strings into numetric type
timestamp = int(parts[3])
ox = float(parts[0])
oy = float(parts[1])
oz = float(parts[2])
'''
if timestamp < 100000000:
timestamp = timestamp * 10
if timestamp < 100000000:
timestamp = timestamp * 10
'''
print("%s: %s, %s, %s" % (timestamp, ox, oy, oz))
self.omega[timestamp] = (ox, oy, oz)
return
# return a sorted list of timestamps from small to large num
def get_timestamps(self):
return sorted(self.omega.keys())
# extract a specific component of the signal
# get_signal(0) returns the X component of angular velocity
def get_signal(self, index):
return [self.omega[k][index] for k in self.get_timestamps()]
def get_signal_x(self):
return self.get_signal(0)
def get_signal_y(self):
return self.get_signal(1)
def get_signal_z(self):
return self.get_signal(2)
# simple linear interpolation to estimate the angular velocity
def fetch_approximate_omega(self, timestamp):
if timestamp in self.omega:
return self.omega[timestamp]
# walking over the timestamps and finding the timestamp
# that is closest to the one requested.
i = 0
sorted_timestamps = self.get_timestamps()
for ts in sorted_timestamps:
if ts > timestamp:
break
i += 1
t_previous = sorted_timestamps[i - 1] # this is a error in the book
t_current = sorted_timestamps[i]
dt = float(t_current - t_previous)
slope = (timestamp - t_previous) / dt
est_x = (
self.omega[t_previous][0] * (1 - slope) + self.omega[t_current][0] * slope
)
est_y = (
self.omega[t_previous][1] * (1 - slope) + self.omega[t_current][1] * slope
)
est_z = (
self.omega[t_previous][2] * (1 - slope) + self.omega[t_current][2] * slope
)
return (est_x, est_y, est_z)
def read_data(self):
if not os.path.exists(self.gyrofilename):
print("start creating "+self.gyrofilename)
self.parse()
with open(self.gyrofilename, "wb") as fp:
pickle.dump(self, fp)
return self
else:
with open(self.gyrofilename, "rb") as fp:
return pickle.load(fp)