-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerTest.py
547 lines (464 loc) · 19.7 KB
/
SerTest.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
"""!
Robohatlib (2022-2823-01)
Copyright © 2023 Vrije Universiteit Amsterdam
Electronica-Beta-VU
A. Denker (a.denker@vu.nl)
"""
from __future__ import absolute_import, division, print_function, unicode_literals
try:
from robohatlib.Robohat import Robohat
from robohatlib import RobohatConstants
from robohatlib.hal.assemblyboard.PwmPlug import PwmPlug
from robohatlib import RobohatConfig
from robohatlib.hal.assemblyboard.ServoAssemblyConfig import ServoAssemblyConfig
from robohatlib.hal.assemblyboard.servo.ServoData import ServoData
from robohatlib.hal.datastructure.Color import Color
from robohatlib.hal.datastructure.ExpanderDirection import ExpanderDir
from robohatlib.hal.datastructure.ExpanderStatus import ExpanderStatus
from robohatlib.driver_ll.datastructs.IOStatus import IOStatus
from testlib import TestConfig
import sys
import os
import time
except ImportError:
print("Failed to import all dependencies for SerTestClass")
raise
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
SUPER_USER_MAX_MOVE = 170
SUPER_USER_MIN_MOVE = 10
NORMAL_USER_MAX_MOVE = 160
NORMAL_USER_MIN_MOVE = 20
# --------------------------------------------------------------------------------------
class SerTestClass:
"""!
Our example class.
The Robohat class will be created.
The Robohat class will be initialized
"""
def __init__(self):
"""!
Constructor of this test class
"""
print("################################################")
print("Starting robohat servo test routine")
self.__running = True
self.__robohat = Robohat(TestConfig.SERVOASSEMBLY_1_CONFIG, TestConfig.SERVOASSEMBLY_2_CONFIG, TestConfig.TOPBOARD_ID_SWITCH)
self.__robohat.init(TestConfig.SERVOBOARD_1_DATAS_LIST, TestConfig.SERVOBOARD_2_DATAS_LIST)
self.__robohat.do_buzzer_beep()
self.__i_am_super_user = False
self.__robohat.start_servo_drivers()
print("\n")
if len(sys.argv) > 1:
self.__i_am_super_user = True
print("You are a superuser, warning")
self.__limit_min = SUPER_USER_MIN_MOVE
self.__limit_max = SUPER_USER_MAX_MOVE
else:
self.__limit_min = NORMAL_USER_MIN_MOVE
self.__limit_max = NORMAL_USER_MAX_MOVE
print("limits are from " + str(self.__limit_min) + " till " + str(self.__limit_max) )
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
def start(self) -> None:
"""!
Starts the infinite loop which scans the keyboard
@return: None
"""
self.__ser_test_help()
while self.__running is True:
inp = input()
self.__process_commands(inp)
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# noinspection PyMethodMayBeStatic
def __ser_test_help(self) -> None:
"""!
Will print available commands
@return: None
"""
print("\n")
print("1 show all connected servos")
print("2 calibrate servos")
print("3 read out the angles of the servos")
print("4 move servos to " + str(self.__limit_min) + " °")
print("5 move servos to 90 °")
print("6 move servos to " + str(self.__limit_max) + " °")
print("7 topboard OUTPUT test ")
print("8 assembly boards OUTPUT test ")
print("l for led test ")
print("v show voltages of all servo adcs")
print("t toggle SERVO update mode between DIRECT or PERIODICALLY")
print("b sounds the buzzer")
print("c test camera")
print("\n")
print("x for exit")
print("\n")
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
def __process_commands(self, _command: str) -> None:
"""!
Will handle console request
@param _command: console input
@return: None
"""
if _command == "1":
self.__servo_show_connected()
elif _command == "v":
self.__servo_show_adc_voltages()
elif _command == "2":
self.__servo_calibrate()
elif _command == "3":
self.__servo_read_angle()
elif _command == "4":
self.__servo_move(self.__limit_min)
elif _command == "5":
self.__servo_move(90.0)
elif _command == "6":
self.__servo_move(self.__limit_max)
elif _command == "7":
self.__topboard_running_light()
elif _command == "8":
self.__assembly_boards_running_light()
elif _command == "l":
self.__led_test()
elif _command == "t":
self.__toggle_direct_update_mode()
elif _command == "b":
self.__sound_the_buzzer()
elif _command == "c":
self.__test_the_camera()
# --------------------------------------------------------------------------------------
elif _command == "x":
self.exit_program()
else:
self.__ser_test_help()
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
def __servo_show_connected(self) -> None:
angles = self.__robohat.get_servo_multiple_angles()
servo_counter = 0
for servo_nr in range(0, len(angles)):
pos = angles[servo_nr]
if pos != -1:
print("Servo " + str(servo_nr) + " is connected")
servo_counter = servo_counter + 1
if servo_counter == 0:
print("No servos are found")
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
def __servo_show_adc_voltages(self) -> None:
"""!
Shows the servo adc voltages
@return: None
"""
voltages = self.__robohat.get_servo_adc_multiple_channels()
for servo_nr in range(0, len(voltages)):
voltage = voltages[servo_nr]
print("Servo " + str(servo_nr) + ": " + str(voltage) + " V")
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
def __servo_calibrate(self) -> None:
"""!
Will calibrate the adc of servo 0
@return: None
"""
print("Going to calibrate the servos. Please be patient...")
self.__robohat.do_servo_fit_formula_readout_vs_angle_multiple_servos(self.__limit_min, self.__limit_max)
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
def __servo_read_angle(self) -> None:
"""!
Will read the angle of servo 0
@return: None
"""
angles = self.__robohat.get_servo_multiple_angles()
for i in range(0, len(angles)):
pos = angles[i]
if pos == -1:
print("Servo " + str(i) + " is not connected")
else:
print("Servo " + str(i) + " position is " + str(pos) + " °")
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
def __servo_move(self, _degree: float) -> None:
"""!
Will read the angle of servo 0
@paramL _degree wanted degree of servo
@return: None
"""
time_to_servo = self.__robohat.get_servo_us_time(_degree)
self.__robohat.set_servo_multiple_angles(
[
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
_degree,
]
)
print(
"Angles of the servos should be: "
+ str(_degree)
+ "°"
+ " time: "
+ str(int(time_to_servo))
+ " uS"
)
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
def __toggle_direct_update_mode(self) -> None:
"""!
Toggles the servo update mode
@return: None
"""
mode = self.__robohat.get_servo_is_direct_mode()
if mode is False:
print("setting SERVO update mode to DIRECT")
self.__robohat.set_servo_direct_mode(True)
else:
print("setting SERVO update mode to PERIODICALLY")
self.__robohat.set_servo_direct_mode(False)
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
def __topboard_running_light(self) -> None:
"""!
Sets the IO of the topboard, in sequence. When leds are attached you will get a running light
@return: None
"""
print("Starting topboard OUTPUT test")
for pin_nr_dir in range(8):
self.__robohat.set_topboard_io_expander_direction(
pin_nr_dir, ExpanderDir.OUTPUT
)
pin_high_nr = 0
for loop_counter in range(3):
for led_counter in range(8):
for pin_nr in range(8):
self.__robohat.set_topboard_io_expander_output(
pin_nr, ExpanderStatus.LOW
)
self.__robohat.set_topboard_io_expander_output(
pin_high_nr, ExpanderStatus.HIGH
)
pin_high_nr = pin_high_nr + 1
if pin_high_nr >= 8:
pin_high_nr = 0
print("--> " + str(loop_counter) + " LED GP:" + str(led_counter))
time.sleep(1)
for pin_nr in range(8):
self.__robohat.set_topboard_io_expander_output(pin_nr, ExpanderStatus.LOW)
print("Ready topboard OUTPUT test")
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
def __assembly_boards_running_light(self) -> None:
"""
Sets the IO of the attached assembly boards, in sequence. When leds are attached you will get a running light
@return: None
"""
print("Starting assembly boards OUTPUT test")
for pin_nr_dir in range(4, 7):
if (
self.__robohat.get_assemblyboard_is_connected(PwmPlug.PWMPLUG_P3)
is True
):
self.__robohat.set_servo_io_expander_direction(
PwmPlug.PWMPLUG_P3, pin_nr_dir, ExpanderDir.OUTPUT
)
if (
self.__robohat.get_assemblyboard_is_connected(PwmPlug.PWMPLUG_P4)
is True
):
self.__robohat.set_servo_io_expander_direction(
PwmPlug.PWMPLUG_P4, pin_nr_dir, ExpanderDir.OUTPUT
)
pin_high_nr = 4
for loop_counter in range(3):
for led_counter in range(4, 7):
for pin_nr in range(4, 7):
if (
self.__robohat.get_assemblyboard_is_connected(
PwmPlug.PWMPLUG_P3
)
is True
):
self.__robohat.set_servo_io_expander_output(
PwmPlug.PWMPLUG_P3, pin_nr, ExpanderStatus.LOW
)
if (
self.__robohat.get_assemblyboard_is_connected(
PwmPlug.PWMPLUG_P4
)
is True
):
self.__robohat.set_servo_io_expander_output(
PwmPlug.PWMPLUG_P4, pin_nr, ExpanderStatus.LOW
)
if (
self.__robohat.get_assemblyboard_is_connected(PwmPlug.PWMPLUG_P3)
is True
):
self.__robohat.set_servo_io_expander_output(
PwmPlug.PWMPLUG_P3, pin_high_nr, ExpanderStatus.HIGH
)
if (
self.__robohat.get_assemblyboard_is_connected(PwmPlug.PWMPLUG_P4)
is True
):
self.__robohat.set_servo_io_expander_output(
PwmPlug.PWMPLUG_P4, pin_high_nr, ExpanderStatus.HIGH
)
pin_high_nr = pin_high_nr + 1
if pin_high_nr >= 7:
pin_high_nr = 4
if (
self.__robohat.get_assemblyboard_is_connected(PwmPlug.PWMPLUG_P3)
is True
):
print(
"Plug P3: --> "
+ str(loop_counter)
+ " LED GP:"
+ str(led_counter)
)
if (
self.__robohat.get_assemblyboard_is_connected(PwmPlug.PWMPLUG_P4)
is True
):
print(
"Plug P4: --> "
+ str(loop_counter)
+ " LED GP:"
+ str(led_counter)
)
time.sleep(1)
for pin_nr in range(4, 7):
if (
self.__robohat.get_assemblyboard_is_connected(PwmPlug.PWMPLUG_P3)
is True
):
self.__robohat.set_servo_io_expander_output(
PwmPlug.PWMPLUG_P3, pin_nr, ExpanderStatus.LOW
)
if (
self.__robohat.get_assemblyboard_is_connected(PwmPlug.PWMPLUG_P4)
is True
):
self.__robohat.set_servo_io_expander_output(
PwmPlug.PWMPLUG_P4, pin_nr, ExpanderStatus.LOW
)
print("Ready assembly boards OUTPUT test")
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
def __led_test(self) -> None:
"""!
Do a LED test
@return: None
"""
print("Starting LED test")
self.__robohat.set_led_color(Color.RED)
print("LED: RED")
time.sleep(1)
self.__robohat.set_led_color(Color.GREEN)
print("LED: GREEN")
time.sleep(1)
self.__robohat.set_led_color(Color.YELLOW)
print("LED: YELLOW")
time.sleep(1)
self.__robohat.set_led_color(Color.BLUE)
print("LED: BLUE")
time.sleep(1)
self.__robohat.set_led_color(Color.WHITE)
print("Ready LED test")
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
def __sound_the_buzzer(self) -> None:
"""!
Make some sound with the buzzer
@return: None
"""
self.__robohat.do_buzzer_random()
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
def __test_the_camera(self) -> None:
"""!
Test the camera
@return: None
"""
self.__robohat.test_camera()
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
def exit_program(self) -> None:
"""!
Should exit this program
@return: None
"""
print("Exiting this program")
self.__robohat.exit_program()
self.__running = False
sys.exit(0)
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
def main():
"""!
Start of our servo test program
"""
ser_test = SerTestClass() # creates the class Example
try:
ser_test.start() # starts the example
except KeyboardInterrupt: # catch 'CTR-C to get a graceful exit'
print("Interrupted")
try:
sys.exit(130)
except SystemExit:
ser_test.exit_program() # graceful exit
print("Exit")
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
if __name__ == "__main__":
main()