-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMotionNet.py
49 lines (45 loc) · 1.89 KB
/
MotionNet.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
import serial
import Queue
import struct
import collections
import math
# This class handles data collection from MotionNet of Texas A&M University
class MotionNet:
def __init__(self, port, baudrate):
self.port = 'COM' + str(port)
self.baudrate = baudrate
self.serial = serial.Serial(self.port, 115200, timeout=10)
self.data = [0 for x in range(50)]
self.data_package = Queue.Queue(maxsize=50)
self.parsed_data = [0 for x in range(6)]
self.data_queue = collections.deque(maxlen=100)
def read(self):
while True:
while (self.data_package.qsize() < 50):
self.data_package.put(self.serial.read(1))
i = 1
while (i == 1):
self.data[0] = self.data_package.get()
if (ord(self.data[0]) == 16):
self.data[1] = self.data_package.get()
if (ord(self.data[1]) == 1):
j = 1
k = 2
while (j == 1):
self.data[k] = self.data_package.get()
if (ord(self.data[k]) == 16):
self.data[k + 1] = self.data_package.get()
if (ord(self.data[k + 1]) == 4):
j = 0
i = 0
k = k + 1
for l in range(1, 7):
datastr = self.data[l*2] + self.data[l*2 + 1]
self.parsed_data[l-1] = struct.unpack(">h", datastr)[0]
gyro_x = self.parsed_data[3]/32.75
gyro_y = self.parsed_data[4]/32.75
gyro_z = self.parsed_data[5]/32.75
gyro_mag = math.sqrt(gyro_x*gyro_x + gyro_y*gyro_y + gyro_z*gyro_z)
self.data_queue.append(gyro_mag)
def get_data(self):
return self.data_queue