-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_hardware.py
323 lines (290 loc) · 14.1 KB
/
test_hardware.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
import time
import unittest
from threading import Thread
import os
import serial
from utils import util
import config
class Linkage(unittest.TestCase):
encoder_pos = [0, 0, 0, 0, 0, 0]
continue_serial_connection_flag = True
def test_mechanical(self):
res = util.upload_firmware('./firmware/hardware/linkage encoder')
self.assertEqual(res, 0, msg='failed to upload firmware')
time.sleep(1)
self.continue_serial_connection_flag = True
serial_connection_thread = Thread(target=self.handle_serial_connection)
serial_connection_thread.start()
print("move the upperhandle to the default position [ENTER]")
input()
start = self.encoder_pos
print("move the upperhandle to the rightmost position [ENTER]")
input()
mid = self.encoder_pos
print("move the upperhandle to the start position [ENTER]")
input()
end = self.encoder_pos
print(start)
print(mid)
print(end)
self.assertLess(abs(start[0] - end[0]), 500, "start and end position are not aligning")
self.assertLess(abs(start[1] - end[1]), 500, "start and end position are not aligning")
self.assertTrue(3500 < abs(start[0] - mid[0]) < 4500, "the left encoder of the upper handle didn't move far enough")
self.assertTrue(3500 < abs(start[1] - mid[1]) < 4500, "the right encoder of the upper handle didn't move far enough")
self.continue_serial_connection_flag = False
def test_encoder(self):
res = util.upload_firmware('./firmware/hardware/linkage encoder')
self.assertEqual(res, 0, msg='failed to upload firmware')
time.sleep(1)
# move serial connection handling to other thread
serial_connection_thread = Thread(target=self.handle_serial_connection)
serial_connection_thread.start()
time.sleep(3)
# self.assertEqual(len(self.encoder_pos), 4, msg="getting no data from connection thread")
start_position = self.encoder_pos
display_interval = 7500
display_step_size = 200
try:
while serial_connection_thread.is_alive():
rel_pos = [self.encoder_pos[i] - start_position[i] for i in range(len(self.encoder_pos))]
# print("Move the handles to test the encoders")
print(rel_pos)
# print(" " * int(display_interval / display_step_size), "|")
# for j in range(4): # four main encoders
# b = True
# for i in range(-display_interval, display_interval, display_step_size):
# if rel_pos[j] < i and b:
# b = False
# print("#", end="")
# else:
# print(" ", end="")
# if b:
# print("#", end="")
# print()
# print()
# for j in range(4, 6): # two endeffector encoders
# print("#" * (rel_pos[j] // 4))
# print()
# print("Press CTRL + C to continue")
# os.system('cls' if os.name == 'nt' else 'clear')
time.sleep(0.05)
except KeyboardInterrupt:
self.continue_serial_connection_flag = False
def test_motor(self):
res = util.upload_firmware('./firmware/hardware/linkage motor')
self.assertEqual(res, 0, msg='failed to upload firmware')
time.sleep(1)
print("Press any key to continue")
input()
def test_sync(self):
res = util.upload_firmware('./firmware/hardware/linkage sync')
self.assertEqual(res, 0, msg='failed to upload firmware')
time.sleep(1)
# with serial.Serial(config.COM_PORT, 9600, timeout=1) as ser:
# while True:
# message = b'dpmotor100234;'
# ser.write(message)
# res = ser.read(1)
# print(res)
def handle_serial_connection(self):
print("Connecting...")
with serial.Serial(config.COM_PORT, 115200, timeout=1, parity=serial.PARITY_EVEN) as ser:
time.sleep(1)
self.assertNotEqual(ser.inWaiting(), 0,
msg="could not establish serial connection... try restarting the panto")
uint_overflow_correction = [0, 0, 0, 0]
while self.continue_serial_connection_flag:
if ser.inWaiting() > 0:
r = str(ser.readline()).split("dptest")
if len(r) != 2:
print(r, " has wrong serial format - skipping")
continue
new_encoder_pos = [int(y) for y in [x.split(",")[:-1] for x in r][1]]
#print(new_encoder_pos)
# correcting the uint overflow -> 16383 (14bit max) jump to 0
for i in range(4):
if self.encoder_pos[i] - uint_overflow_correction[i] - new_encoder_pos[i] > 10000:
uint_overflow_correction[i] += 16383
if self.encoder_pos[i] - uint_overflow_correction[i] - new_encoder_pos[i] < -10000:
uint_overflow_correction[i] -= 16383
new_encoder_pos[i] += uint_overflow_correction[i]
for i in range(4, 6):
# 136 steps for the endeffector encoders
new_encoder_pos[i] = abs(new_encoder_pos[i] % (136 * 2))
self.encoder_pos = new_encoder_pos
# print(self.encoder_pos)
else:
time.sleep(0.01)
class EndEffector(unittest.TestCase):
def test_encoder(self):
res = util.upload_firmware('./firmware/04 encoder read')
self.assertEqual(res, 0, msg='failed to upload firmware')
time.sleep(1)
# move serial connection handling to other thread
serial_connection_thread = Thread(target=self.handle_serial_connection)
serial_connection_thread.start()
time.sleep(3)
# self.assertEqual(len(self.encoder_pos), 4, msg="getting no data from connection thread")
start_position = self.encoder_pos
display_interval = 7500
display_step_size = 200
try:
while serial_connection_thread.is_alive():
rel_pos = [self.encoder_pos[i] - start_position[i] for i in range(len(self.encoder_pos))]
print("Move the handles to test the encoders")
print(rel_pos)
print(" " * int(display_interval / display_step_size), "|")
# for j in range(4): # four main encoders
# b = True
# for i in range(-display_interval, display_interval, display_step_size):
# if rel_pos[j] < i and b:
# b = False
# print("#", end="")
# else:
# print(" ", end="")
# if b:
# print("#", end="")
# print()
# print()
# for j in range(4, 6): # two endeffector encoders
# print("#" * (rel_pos[j] // 4))
# print()
print("Press CTRL + C to continue")
time.sleep(0.05)
os.system('cls' if os.name == 'nt' else 'clear')
except KeyboardInterrupt:
self.continue_serial_connection_flag = False
def test_motor(self):
res = util.upload_firmware('./firmware/03 motor')
self.assertEqual(res, 0, msg='failed to upload firmware')
time.sleep(1)
print("Press any key to continue")
input()
def test_sync(self):
res = util.upload_firmware('./firmware/hardware/linkage sync')
self.assertEqual(res, 0, msg='failed to upload firmware')
time.sleep(1)
# with serial.Serial(config.COM_PORT, 9600, timeout=1) as ser:
# while True:
# message = b'dpmotor100234;'
# ser.write(message)
#res = ser.read(1)
#print(res)
def handle_serial_connection(self):
print("Connecting...")
with serial.Serial(config.COM_PORT, 115200, timeout=1, parity=serial.PARITY_EVEN) as ser:
time.sleep(1)
self.assertNotEqual(ser.inWaiting(), 0, msg="could not establish serial connection... try restarting the panto")
uint_overflow_correction = [0,0,0,0]
while self.continue_serial_connection_flag:
if ser.inWaiting() > 0:
r = str(ser.readline()).split("dptest")
if len(r) != 2:
print(r, " has wrong serial format - skipping")
continue
new_encoder_pos = [int(y) for y in [x.split(",")[:-1] for x in r][1]]
print(new_encoder_pos)
# correcting the uint overflow -> 16383 (14bit max) jump to 0
for i in range(4):
if self.encoder_pos[i] -uint_overflow_correction[i] - new_encoder_pos[i] > 10000:
uint_overflow_correction[i] += 16383
if self.encoder_pos[i] - uint_overflow_correction[i] - new_encoder_pos[i] < -10000:
uint_overflow_correction[i] -= 16383
new_encoder_pos[i] += uint_overflow_correction[i]
for i in range(4, 6):
# 136 steps for the endeffector encoders
new_encoder_pos[i] = abs(new_encoder_pos[i] % (136 * 2))
self.encoder_pos = new_encoder_pos
#print(self.encoder_pos)
else:
time.sleep(0.01)
class HardwareTest(unittest.TestCase):
encoder_pos = [0, 0, 0, 0]
continue_serial_connection_flag = True
def test_motor(self):
res = util.upload_firmware('./firmware/03 motor')
self.assertEqual(res, 0, msg='failed to upload firmware')
time.sleep(1)
print("Press any key to continue")
input()
def handle_serial_connection(self):
print("Connecting...")
with serial.Serial(config.COM_PORT, 9600, timeout=1, parity=serial.PARITY_EVEN) as ser:
time.sleep(1)
self.assertNotEqual(ser.inWaiting(), 0, msg="could not establish serial connection... try restarting the panto")
uint_overflow_correction = [0,0,0,0]
while self.continue_serial_connection_flag:
if ser.inWaiting() > 0:
r = str(ser.readline()).split("dptest")
if len(r) != 2:
print(r, " has wrong serial format - skipping")
continue
new_encoder_pos = [int(y) for y in [x.split(",")[:-1] for x in r][1]]
print(new_encoder_pos)
# correcting the uint overflow -> 16383 (14bit max) jump to 0
for i in range(4):
if self.encoder_pos[i] -uint_overflow_correction[i] - new_encoder_pos[i] > 10000:
uint_overflow_correction[i] += 16383
if self.encoder_pos[i] - uint_overflow_correction[i] - new_encoder_pos[i] < -10000:
uint_overflow_correction[i] -= 16383
new_encoder_pos[i] += uint_overflow_correction[i]
for i in range(4, 6):
# 136 steps for the endeffector encoders
new_encoder_pos[i] = abs(new_encoder_pos[i] % (136 * 2))
self.encoder_pos = new_encoder_pos
#print(self.encoder_pos)
else:
time.sleep(0.01)
def test_encoder(self):
res = util.upload_firmware('./firmware/04 encoder read')
self.assertEqual(res, 0, msg='failed to upload firmware')
time.sleep(1)
# move serial connection handling to other thread
serial_connection_thread = Thread(target=self.handle_serial_connection)
serial_connection_thread.start()
time.sleep(3)
#self.assertEqual(len(self.encoder_pos), 4, msg="getting no data from connection thread")
start_position = self.encoder_pos
display_interval = 7500
display_step_size = 200
try:
while serial_connection_thread.is_alive():
rel_pos = [self.encoder_pos[i] - start_position[i] for i in range(len(self.encoder_pos))]
print("Move the handles to test the encoders")
print(rel_pos)
print(" " * int(display_interval / display_step_size), "|")
# for j in range(4): # four main encoders
# b = True
# for i in range(-display_interval, display_interval, display_step_size):
# if rel_pos[j] < i and b:
# b = False
# print("#", end="")
# else:
# print(" ", end="")
# if b:
# print("#", end="")
# print()
# print()
# for j in range(4, 6): # two endeffector encoders
# print("#" * (rel_pos[j] // 4))
# print()
print("Press CTRL + C to continue")
time.sleep(0.05)
os.system('cls' if os.name == 'nt' else 'clear')
except KeyboardInterrupt:
self.continue_serial_connection_flag = False
def test_sync(self):
res = util.upload_firmware('./firmware/05 move handles in sync')
self.assertEqual(res, 0, msg='failed to upload firmware')
time.sleep(1)
# with serial.Serial(config.COM_PORT, 9600, timeout=1) as ser:
# while True:
# message = b'dpmotor100234;'
# ser.write(message)
#res = ser.read(1)
#print(res)
# if __name__ == "__main__":
# h = HardwareTest()
# #h.test_encoder()
# #h.test_motor()
# h.test_sync()