-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectornav.py
234 lines (170 loc) · 6.43 KB
/
vectornav.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
228
229
230
231
232
233
234
import numpy as np
import serial
import struct
import threading
import time
from array import array
from datetime import datetime
class ImuData:
def __init__(self, t=0.0, freq=0, ypr=np.zeros(3), a=np.zeros(3), \
W=np.zeros(3)):
self.t = t
self.freq = freq
self.ypr = ypr
self.a = a
self.W = W
class Vectornav(threading.Thread):
def __init__(self, thread_id, port, baud, t0):
'''Instantiate the IMU thread.
Args:
thread_id: (int) - Thread ID
port: (string) - Port name of the IMU
baud: (int) - Baud rate of the IMU
t0: (datetime object) - Epoch
'''
threading.Thread.__init__(self)
self.thread_id = thread_id
self._lock = threading.Lock()
self._on = True
self._t0 = t0
self._port = port
self._baud = baud
self._t = (datetime.now() - t0).total_seconds()
self._ypr = np.zeros(3)
self._a = np.zeros(3)
self._W = np.zeros(3)
# This is specific message has 41 bytes. You should update this to match
# your configuration.
self._len_payload = 41
print('IMU: initialized')
def run(self):
'''Start the thread.
'''
print('IMU: reading from {} at {}'.format(self._port, self._baud))
# In case the port is not properly closed,
try:
temp = serial.Serial(self._port, self._baud)
temp.close()
except:
print('\033[91m' + 'Unable to open IMU port at ' + self._port
+ ':' + str(self._baud) + '\033[0m')
return
# Open the serial port and start reading.
with serial.Serial(self._port, self._baud, timeout=1) as s:
# Clear the buffer first.
print('IMU: clearing buffer')
num_bytes = s.in_waiting
s.read(num_bytes)
print('IMU: starting main loop')
while self._on:
imu_sync_detected = False
# Check if there are bytes waiting in the buffer.
num_bytes = s.in_waiting
if num_bytes == 0:
# Reduce/delete this sleep time if you are reading data at
# a faster rate.
time.sleep(0.01)
continue
# IMU sends 0xFA (int 250) as the first byte. This marks the
# begining of the message.
imu_sync_detected = self.check_sync_byte(s)
if not imu_sync_detected:
continue
# If the sync byte us detected, read the rest of the message.
success = self.read_imu_data(s)
if not success:
continue
print('IMU: thread closed')
def check_sync_byte(self, s):
'''Check if the sync byte is detected.
IMU sends 0xFA (int 250) as the first byte. This marks the begining of
the message.
Args:
s: (serial object) - Already open serial port of the IMU.
Return:
bool - True if the sync byte is detected in the current buffer.
'''
# Iterate over all the bytes in the current buffer.
for _ in range(s.in_waiting):
byte_in = s.read(1)
# Check if the sync byte 0xFA (int 250) is detected.
int_in = int.from_bytes(byte_in, 'little')
if int_in == 250:
return True
return False
def read_imu_data(self, s):
'''Read and parse the payload of the IMU message.
Args:
s: (serial object) - Already open serial port of the IMU.
Return:
bool - True if the operation is succesfull
'''
# Read data.
N = self._len_payload
data = s.read(N)
# Check if there are unexpected errors in the message.
# Last two bytes of the payload is the checksum bytes.
checksum_array = array('B', [data[N-1], data[N-2]])
checksum = struct.unpack('H', checksum_array)[0]
# Compare the received checksum value against the calculated checksum.
crc = self.calculate_imu_crc(data[:N-2])
if not crc == checksum:
print('IMU CRC error')
return False
# If the checksum is valid, parse the data.
return self.parse_data(data)
def parse_data(self, data):
'''Parse the bytes of the sensor measurements
Args:
data: (byte array) - data read from the serial port
Return:
bool - True if the operation is succesfull
'''
try:
with self._lock:
self._ypr[0] = struct.unpack('f', data[3:7])[0]
self._ypr[1] = struct.unpack('f', data[7:11])[0]
self._ypr[2] = struct.unpack('f', data[11:15])[0]
self._a[0] = struct.unpack('f', data[15:19])[0]
self._a[1] = struct.unpack('f', data[19:23])[0]
self._a[2] = struct.unpack('f', data[23:27])[0]
self._W[0] = struct.unpack('f', data[27:31])[0]
self._W[1] = struct.unpack('f', data[31:35])[0]
self._W[2] = struct.unpack('f', data[35:39])[0]
except:
print('IMU: error parsing data')
return False
return True
def calculate_imu_crc(self, data):
'''Calculate the 16-bit CRC for the given message.
Args:
data: (byte array) - data read from the serial port
Return:
unsigned short - CRC checksum value
'''
data = bytearray(data)
crc = np.array([0], dtype=np.ushort)
for i in range(len(data)):
crc[0] = (crc[0] >> 8) | (crc[0] << 8)
crc[0] ^= data[i]
crc[0] ^= (crc[0] & 0xff) >> 4
crc[0] ^= crc[0] << 12
crc[0] ^= (crc[0] & 0x00ff) << 5
return crc[0]
def output_data(self):
'''Output the current measurements.
Return:
ImuData - current IMU data
'''
with self._lock:
data = ImuData(
self._t,
self._ypr,
self._a,
self._W
)
return data
def end_thread(self):
'''Call to end the IMU thread.'''
self._on = False
print('IMU: thread close signal received')