-
Notifications
You must be signed in to change notification settings - Fork 1
/
TEST_API.py
335 lines (277 loc) · 11.6 KB
/
TEST_API.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import serial
import sys
import time
"""
Terabee API for use with path_planning.
"""
#sys.path.append('/home/cornellcup/c1c0-main/c1c0-movement/c1c0-movement/Locomotion') #Might need to be resolved
import R2Protocol2 as r2p
ser = None
#If wanting to run TEST_API main function to debug, set to 1
TESTING = 1
LIDAR_DATA_POINTS = 360
LIDAR_DATA_LEN = LIDAR_DATA_POINTS * 4
TERABEE_DATA_POINTS = 8
TERABEE_DATA_LEN = TERABEE_DATA_POINTS * 2
IMU_DATA_POINTS = 3
IMU_DATA_LEN = IMU_DATA_POINTS * 2
# variables for indexing
ENCODING_BYTES = 16
ENCODING_BYTES_TOTAL = ENCODING_BYTES * 5 # 16 EACH FOR LIDAR, IMU, AND 3 TERABEES
TOTAL_BYTES = LIDAR_DATA_LEN + TERABEE_DATA_LEN*3 + IMU_DATA_LEN + ENCODING_BYTES
terabee_array_1 = []
terabee_array_2 = []
terabee_array_3 = []
ldr_array = []
imu_array = []
def init_serial(port, baud):
"""
Opens serial port for LIDAR communication
port should be a string linux port: Ex dev/ttyTHS1
Baud is int the data rate, commonly multiples of 9600
For using all all three types of sensors, baud should be 115200
"""
global ser, startseq, endseq
ser = serial.Serial(port, baud)
return ser
def close_serial():
"""
Closes serial port for sensor communication
"""
global ser
ser.close()
def get_array(array_type):
"""
Description: Returns the specified sensor array using the given parameter
Parameters: String array_type
Returns: Array
"""
if array_type == "TB1":
return terabee_array_1
elif array_type == "TB2":
return terabee_array_2
elif array_type == "TB3":
return terabee_array_3
elif array_type == "LDR":
return ldr_array
elif array_type == "IMU":
return imu_array
else:
print("array_type must be one of 'TB1', 'TB2', 'TB3', 'LDR', IMUG', or 'IMUA'" )
def decode_arrays():
"""
Description: Checks to see if the mtype bit is a valid type and then
calls the function to decode it based on the type indicated.
Functions using API must call this to update global arrays
Returns: Nothing
"""
global ser, terabee_array_1, terabee_array_2, terabee_array_3, ldr_array
global imu_array
good_data = False
# print("GET ARRAY FUNCTION")
while(not good_data):
terabee_array_1 = []
terabee_array_2 = []
terabee_array_3 = []
ldr_array = []
imu_array = []
# print("IN LOOP")
ser_msg = ser.read_until(b"\xd2\xe2\xf2")
ser.reset_input_buffer()
#print(ser_msg)
# ~ time.sleep(0.001)
# print("GOT MESSAGE")
mtype, data, status = r2p.decode(ser_msg)
# print("TYPE: " + str(mtype))
# print("")
# print("Data: " + str(data))
# print("")
# print("Status: " + str(status))
#print("TYPE: \n" + str(mtype))
#print("DATA:" + str(data))
if (mtype == b'IR\x00\x00'):
decode_from_ir(data)
good_data = True
elif (mtype == b'IR2\x00'):
decode_from_ir2(data)
good_data = True
elif (mtype == b'IR3\x00'):
decode_from_ir3(data)
good_data = True
elif (mtype == b'LDR\x00'):
decode_from_ldr(data)
good_data = True
elif (mtype == b'IMU\x00'):
decode_from_imu(data)
good_data = True
elif (mtype == b'SENS'):
decode_from_sens(data)
good_data = True
else:
# ~ print("NO GOOD")
ser.reset_input_buffer()
time.sleep(0.01)
def decode_from_sens(data):
terabee1_data = data[:TERABEE_DATA_LEN]
terabee2_data = data[TERABEE_DATA_LEN:TERABEE_DATA_LEN + TERABEE_DATA_LEN]
terabee3_data = data[TERABEE_DATA_LEN*2:TERABEE_DATA_LEN*2 + TERABEE_DATA_LEN]
ldr_data = data[TERABEE_DATA_LEN*3:TERABEE_DATA_LEN*3 + LIDAR_DATA_LEN]
imu_data = data[TERABEE_DATA_LEN*3 + LIDAR_DATA_LEN :TOTAL_BYTES]
terabee_array_append(terabee1_data, terabee_array_1)
terabee_array_append(terabee2_data, terabee_array_2)
terabee_array_append(terabee3_data, terabee_array_3)
lidar_tuple_array_append(ldr_data, ldr_array)
imu_array_append(imu_data, imu_array)
def decode_from_ir(data):
"""
Description: Function that decodes the data if the received mtype
is that corresponding to Terabee 1.
Returns: Nothing
"""
terabee1_data = data[:TERABEE_DATA_LEN]
terabee2_data = data[TERABEE_DATA_LEN + ENCODING_BYTES:TERABEE_DATA_LEN + ENCODING_BYTES + TERABEE_DATA_LEN]
terabee3_data = data[TERABEE_DATA_LEN*2 + ENCODING_BYTES*2:TERABEE_DATA_LEN*2 + ENCODING_BYTES*2 + TERABEE_DATA_LEN]
ldr_data = data[TERABEE_DATA_LEN*3 + ENCODING_BYTES*3:TERABEE_DATA_LEN*3 + ENCODING_BYTES*3 + LIDAR_DATA_LEN]
imu_data = data[TERABEE_DATA_LEN*3 + LIDAR_DATA_LEN + ENCODING_BYTES*4:TOTAL_BYTES]
terabee_array_append(terabee1_data, terabee_array_1)
terabee_array_append(terabee2_data, terabee_array_2)
terabee_array_append(terabee3_data, terabee_array_3)
lidar_tuple_array_append(ldr_data, ldr_array)
imu_array_append(imu_data, imu_array)
def decode_from_ir2(data):
"""
Description: Function that decodes the data if the received mtype
is that corresponding to Terabee 2.
Returns: Nothing
"""
terabee2_data = data[:TERABEE_DATA_LEN]
terabee3_data = data[TERABEE_DATA_LEN + ENCODING_BYTES:TERABEE_DATA_LEN + ENCODING_BYTES + TERABEE_DATA_LEN]
ldr_data = data[TERABEE_DATA_LEN*2 + ENCODING_BYTES*2:TERABEE_DATA_LEN*2 + ENCODING_BYTES*2 + LIDAR_DATA_LEN]
imu_data = data[TERABEE_DATA_LEN*2 + LIDAR_DATA_LEN + ENCODING_BYTES*3:
TERABEE_DATA_LEN*2 + LIDAR_DATA_LEN + ENCODING_BYTES*3 + IMU_DATA_LEN]
terabee1_data = data[TERABEE_DATA_LEN*2 + LIDAR_DATA_LEN + ENCODING_BYTES*4 + IMU_DATA_LEN:TOTAL_BYTES]
terabee_array_append(terabee1_data, terabee_array_1)
terabee_array_append(terabee2_data, terabee_array_2)
terabee_array_append(terabee3_data, terabee_array_3)
lidar_tuple_array_append(ldr_data, ldr_array)
imu_array_append(imu_data, imu_array)
def decode_from_ir3(data):
"""
Description: Function that decodes the data if the received mtype
is that corresponding to Terabee 3.
Returns: Nothing
"""
terabee3_data = data[:TERABEE_DATA_LEN]
ldr_data = data[TERABEE_DATA_LEN + ENCODING_BYTES:TERABEE_DATA_LEN + ENCODING_BYTES + LIDAR_DATA_LEN]
imu_data = data[TERABEE_DATA_LEN + LIDAR_DATA_LEN + ENCODING_BYTES*2:
TERABEE_DATA_LEN + LIDAR_DATA_LEN + ENCODING_BYTES*2 + IMU_DATA_LEN]
terabee1_data = data[TERABEE_DATA_LEN + LIDAR_DATA_LEN + IMU_DATA_LEN + ENCODING_BYTES*3:
TERABEE_DATA_LEN + LIDAR_DATA_LEN + IMU_DATA_LEN + ENCODING_BYTES*3 + TERABEE_DATA_LEN]
terabee2_data = data[TERABEE_DATA_LEN*2 + LIDAR_DATA_LEN + IMU_DATA_LEN + ENCODING_BYTES*4:
TOTAL_BYTES]
terabee_array_append(terabee1_data, terabee_array_1)
terabee_array_append(terabee2_data, terabee_array_2)
terabee_array_append(terabee3_data, terabee_array_3)
lidar_tuple_array_append(ldr_data, ldr_array)
imu_array_append(imu_data, imu_array)
def decode_from_ldr(data):
"""
Description: Function that decodes the data if the received mtype
is that corresponding to LIDAR.
Returns: Nothing
"""
ldr_data = data[:LIDAR_DATA_LEN]
imu_data = data[LIDAR_DATA_LEN + ENCODING_BYTES:LIDAR_DATA_LEN + ENCODING_BYTES + IMU_DATA_LEN]
terabee1_data = data[LIDAR_DATA_LEN + IMU_DATA_LEN + ENCODING_BYTES*2 :
LIDAR_DATA_LEN + IMU_DATA_LEN + ENCODING_BYTES*2 + TERABEE_DATA_LEN]
terabee2_data = data[LIDAR_DATA_LEN + IMU_DATA_LEN + TERABEE_DATA_LEN + ENCODING_BYTES*3:
LIDAR_DATA_LEN + IMU_DATA_LEN + TERABEE_DATA_LEN + ENCODING_BYTES*3 + TERABEE_DATA_LEN]
terabee3_data = data[LIDAR_DATA_LEN + IMU_DATA_LEN + TERABEE_DATA_LEN*2 + ENCODING_BYTES*4:]
terabee_array_append(terabee1_data, terabee_array_1)
terabee_array_append(terabee2_data, terabee_array_2)
terabee_array_append(terabee3_data, terabee_array_3)
lidar_tuple_array_append(ldr_data, ldr_array)
imu_array_append(imu_data, imu_array)
def decode_from_imu(data):
"""
Description: Function that decodes the data if the received mtype
is that corresponding to IMU.
Parameters:
Returns: Nothing
"""
imu_data = data[:IMU_DATA_LEN]
terabee1_data = data[IMU_DATA_LEN + ENCODING_BYTES:IMU_DATA_LEN + ENCODING_BYTES + TERABEE_DATA_LEN]
terabee2_data = data[IMU_DATA_LEN + TERABEE_DATA_LEN + ENCODING_BYTES*2:
IMU_DATA_LEN + TERABEE_DATA_LEN + ENCODING_BYTES*2 + TERABEE_DATA_LEN]
terabee3_data = data[IMU_DATA_LEN + TERABEE_DATA_LEN*2 + ENCODING_BYTES*3:
IMU_DATA_LEN + TERABEE_DATA_LEN*3 + ENCODING_BYTES*3:]
ldr_data = data[IMU_DATA_LEN + TERABEE_DATA_LEN*3 + ENCODING_BYTES*4:]
terabee_array_append(terabee1_data, terabee_array_1)
terabee_array_append(terabee2_data, terabee_array_2)
terabee_array_append(terabee3_data, terabee_array_3)
lidar_tuple_array_append(ldr_data, ldr_array)
imu_array_append(imu_data, imu_array)
def imu_array_append(data, target_array):
"""
Description: Copies the data received into the corresponding IMU array.
Parameters:
bytes Array data - the data to be placed in the target array
target_array - the corresponding array for data to be copied to
"""
for i in range(0, 6, 2):
target_array.append(int.from_bytes(data[i:i+2],
byteorder = 'big', signed = True))
def terabee_array_append(data, target_array):
"""
Description: Copies the data received into the corresponding Terabee array.
Parameters:
bytes Array data - the data to be placed in the target array
target_array - the corresponding array for data to be copied to
"""
for i in range(0, 16, 2):
value = (data[i]<<8) | data[i+1]
target_array.append(value)
def lidar_tuple_array_append(data, target_array):
"""
Description: Copies the data received into the corresponding LDR array.
Parameters:
bytes Array data - the data to be placed in the target array
target_array - the corresponding array for data to be copied to
"""
for i in range(0, LIDAR_DATA_LEN, 4):
angle_msbs = data[i]
angle_lsbs = data[i+1]
distance_msbs = data[i+2]
distance_lsbs = data[i+3]
angle = angle_msbs<<8 | angle_lsbs
distance = distance_msbs<<8 | distance_lsbs
target_array.append((angle,distance))
def sensor_token():
"""
Sends a downstream message to request sensor data packet.
"""
req = r2p.encode(b'SNSR', token.to_bytes(1, 'big'))
ser.write(req)
if __name__ == '__main__':
if(TESTING):
init_serial('/dev/ttyTHS1', 115200)
ser.reset_input_buffer()
print("STARTED")
try:
while True:
if ser.in_waiting:
print("Getting data")
decode_arrays()
ldr = get_array('LDR')
tb1 = get_array('TB1')
tb2 = get_array('TB2')
tb3 = get_array('TB3')
imu = get_array('IMU')
print(tb1)
print(imu)
# ~ else:
# ~ print("NOT GOT")
# ~ time.sleep(1)
ser.close()
except KeyboardInterrupt:
ser.close()