-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathDW1000Time.py
132 lines (105 loc) · 3.54 KB
/
DW1000Time.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
"""
This python module deal with time/timestamp/distance measurement/management of DW1000
"""
import DW1000Constants as C
class DW1000Time():
"""
This class manages timestamp
"""
_timestamp = 0
def __init__(self, data=None, timestamp=None, index=None):
if timestamp is None:
self.timestamp = 0
self.set_timestamp(data, timestamp, index)
def __del__(self):
self._timestamp = 0
def set_timestamp(self, data, timestamp, index):
"""
This function sets the specified timestamp into the data that will be sent.
Args:
data: The data where you will store the timestamp
timeStamp = The timestamp's value
index = The bit from where you will put the timestamp's value
Returns:
The data with the timestamp added to it
"""
for i in range(0, C.LENGTH_TIMESTAMP):
data[i+index] = int((timestamp >> (i * 8)) & C.MASK_LS_BYTE)
def get_timestamp(self, data, index):
"""
This function gets the timestamp's value written inside the specified data and returns it.
Args:
data : the data where you want to extract the timestamp from
index : the index you want to start reading the data from
Returns:
The timestamp's value read from the given data.
"""
timestamp = 0
for i in range(0, C.LENGTH_TIMESTAMP):
timestamp |= data[i+index] << (i*8)
return timestamp
def wrap_timestamp(self, timestamp):
"""
This function converts the negative values of the timestamp
due to the overflow into a correct one.
Args :
timestamp : the timestamp's value you want to correct.
Returns:
The corrected timestamp's value.
"""
if timestamp < 0:
timestamp += C.TIME_OVERFLOW
return timestamp
def get_as_float(self):
"""
This function returns timestamp as microseconds
"""
self.get_as_micro_seconds()
def get_as_micro_seconds(self):
"""
This function returns timestamp as microseconds
"""
return (self.timestamp % C.TIME_OVERFLOW) * C.TIME_RES
def get_as_meters(self):
"""
This function returns travel distance as meters with respect to travel time(timestamp)
"""
return (self.timestamp%C.TIME_OVERFLOW)*C.DISTANCE_OF_RADIO
def is_valid_timestamp(self):
"""
This function check and returns whether timestamp is valid or not
"""
return 0 <= self.timestamp & self.timestamp <= C.TIME_MAX
'''
## Operator overloading
# assign
def __setattr__(self, assign):
if self == assign:
return self
self.timestamp = assign.get_timestamp()
return self
# add
def __iadd__(self, add):
self.timestamp += add.get_timestamp()
return self
def __add__(self, add):
return (self += add)
# subtract
def __isub__(self, sub):
self.timestamp -= sub.get_timestamp()
return self
def __sub__(self, sub):
return (self -= sub)
# multiply
def __imul__(self, factor):
self.timestamp *= factor
return self
def __mul__(self, factor):
return (self *= factor)
# divide
def __idiv__(self, factor):
self.timestamp /= factor
return self
def __div__(self, factor):
return (self /= factor)
'''