-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstream_data.py
More file actions
179 lines (145 loc) · 5.67 KB
/
Copy pathstream_data.py
File metadata and controls
179 lines (145 loc) · 5.67 KB
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
# A script to stream continuous data readings form the YOST
# 3-Space LX Evaluation Kit using Python 3.
# Utilize the Python API for the sensor, the threespace_api.
from threespace_api import *
import threespace_api as ts_api
import csv
import time
def get_port():
found = False
com_port = ""
# Get all LX devices
devices = ts_api.getComPorts(filter=ts_api.TSS_FIND_LX)
if len(devices) > 0:
# Take first (and presumably only) device.
com_port = devices[0]
found = True
if found:
print(f"Device found: {com_port}")
return com_port
else:
print("No device was found.")
return None
def get_csv_data(data, opened, file_name):
ugly_time = time.time()
time_stamp = time.ctime(ugly_time)
row = {
'Time':time_stamp,'AccelCorrectedX' : data[0], 'AccelCorrectedY' : data[1], 'AccelCorrectedZ' : data[2],
'GyroCorrectedX' : data[3], 'GyroCorrectedY' : data[4], 'GyroCorrectedZ' : data[5],
'Pitch' : data[6], 'Yaw' : data[7], 'Roll' : data[8]}
if not opened:
with open(file_name, 'w') as stream_data:
field_names = ['Time','AccelCorrectedX','AccelCorrectedY','AccelCorrectedZ','GyroCorrectedX','GyroCorrectedY','GyroCorrectedZ', 'Pitch', 'Yaw', 'Roll']
# Set field names in the CSV file
writer = csv.DictWriter(stream_data, fieldnames = field_names)
# Create header
writer.writeheader()
# Write data to file
writer.writerow(row)
else:
with open(file_name, 'a') as stream_data:
time_tuple = (time_stamp,)
data_with_time = time_tuple + data
writer = csv.writer(stream_data)
writer.writerow(data_with_time)
def stream():
streaming = True
opened = False
# These variables determine the filename scheme
# Counter variable keeps track of number of csv files stored per session
file_name = "imu_data.csv"
port = get_port()
# Connect sensor with the port
device = ts_api.TSLXSensor(com_port = port)
print(device)
if device is not None:
# Disable magnometer (compass)
device.setCompassEnabled(enabled = False)
# Set calibration mode to Scale/Bias mode
device.setCalibrationMode(mode = 1)
# Begin auto calibration of gyroscope
device.beginGyroscopeAutoCalibration()
# Set filter mode to Kalman
device.setFilterMode(mode = 1)
# Fix rate
device.setStreamingTiming(interval = 0, duration = 0xFFFFFFFF, delay = 0)
# Start streaming
device.startStreaming()
device.startRecordingData()
while streaming:
# Set slots for tared quaternion and raw batch data - accel in units of g
# gyro is in rad/sec
device.setStreamingSlots(
slot0 = 'getCorrectedAccelerometerVector',
slot1 = 'getCorrectedGyroRate',
slot2 = 'getTaredOrientationAsEulerAngles')
print("==================================================")
data = device.getLatestStreamData(1)[1]
get_csv_data(data, opened, file_name)
opened = True
print(data)
print("==================================================\n")
device.stopStreaming()
print("End of session")
device.close()
return None
def calculate_position():
port = get_port()
# Connect sensor with the port
device = ts_api.TSLXSensor(com_port = port)
euler_angles = device.getTaredOrientationAsEulerAngles
print("Euler angles are as follows: " + euler_angles)
pass
# def getGyroDifference(data1, data2):
# differences = []
# for i in range(3):
# differences.append(abs(data1[i] - data2[i]))
# print("Differences between gryoscope readings: ")
# return differences
# def getAccelDifference(data1, data2):
# differences = []
# for i in range(3,6):
# differences.append(abs(data1[i] - data2[i]))
# print("Differences between acceleration readings: ")
# return differences
# def getCompDifference(data1, data2):
# differences = []
# for i in range(6,9):
# differences.append(abs(data1[i] - data2[i]))
# print("Differences between compass readings: ")
# return differences
# while device is not None:
# # Begin streaming continuous data from our sensor.
# print("==================================================")
# print("Getting the current filtered tared quaternion orientation:")
# quat = device.getTaredOrientationAsQuaternion()
# if quat is not None:
# print(quat)
# print("==================================================")
# print("Getting the current corrected data:")
# data1 = device.getAllCorrectedComponentSensorData()
# print(len(data1))
# if data1 is not None:
# print("[%f, %f, %f] --Gyro\n"
# "[%f, %f, %f] --Accel\n"
# "[%f, %f, %f] --Comp" % data1)
# print("==================================================")
# print("Getting the raw sensor data:")
# data2 = device.getAllRawComponentSensorData()
# if data2 is not None:
# print("[%f, %f, %f] --Gyro\n"
# "[%f, %f, %f] --Accel\n"
# "[%f, %f, %f] --Comp" % data2)
# print("==================================================")
# print(getGyroDifference(data1, data2))
# print(getAccelDifference(data1, data2))
# print(getCompDifference(data1, data2))
# # Delay in output for readability.
# time.sleep(5)
# # Now close the port.
# device.close()
if __name__ == '__main__':
try:
stream()
except KeyboardInterrupt:
sys.exit()