-
Notifications
You must be signed in to change notification settings - Fork 0
/
launcher.py
executable file
·1158 lines (1007 loc) · 41.9 KB
/
launcher.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import os
import sys
import logging
import time
import threading
import RPi.GPIO as GPIO
import core
import rc
import speed
import wall_follower
import rainbow
import tof_calibrate
import whitebalance
from PIL import Image
# import VL53L0X
from enum import Enum
try:
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict
# import smbus
from lib_oled96 import ssd1306
from approxeng.input.selectbinder import ControllerResource
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
class Mode(Enum):
# Enum class for robot mode/challenge.
MODE_NONE = 0
MODE_POWER = 1
MODE_REBOOT = 2
MODE_RC = 3
MODE_MAZE = 4
MODE_SPEED = 5
MODE_SPEED_LINEAR = 6
MODE_RAINBOW = 7
MODE_TOF_CALIBRATE = 8
MODE_KILL_PROCESS = 9
MODE_WHITE_CALIBRATE = 10
MODE_WIPE_RAINBOW_MEMORY = 11
class launcher:
def __init__(self):
# Initialise controller and bind now
self.controller = None
self.current_batt = None
# Initialise GPIO
self.GPIO = GPIO
self.GPIO.setwarnings(False)
self.GPIO.setmode(self.GPIO.BCM)
# Instantiate CORE / Chassis module and store in the launcher.
self.core = core.Core(self.GPIO)
self.challenge = None
self.challenge_thread = None
# Shutting down status
self.shutting_down = False
self.killed = False
# Mode/Challenge Dictionary
self.menu_list = OrderedDict((
(Mode.MODE_POWER, "Power Off"),
(Mode.MODE_REBOOT, "Reboot"),
(Mode.MODE_KILL_PROCESS, "Kill Process"),
(Mode.MODE_RC, "RC"),
(Mode.MODE_MAZE, "Maze"),
(Mode.MODE_SPEED, "Speed"),
# (Mode.MODE_SPEED_LINEAR, "Linear Speed"),
(Mode.MODE_RAINBOW, "Rainbow"),
(Mode.MODE_WIPE_RAINBOW_MEMORY, "Wipe Rainbow Memory"),
(Mode.MODE_WHITE_CALIBRATE, "Camera Calibrate"),
(Mode.MODE_TOF_CALIBRATE, "TOF Calibrate")
))
self.current_mode = Mode.MODE_NONE
self.menu_mode = Mode.MODE_RC
# Create oled object
# Note: Set to None if you need to disable screen
try:
self.oled = ssd1306(self.core.i2cbus)
except:
print("Failed to get OLED")
self.oled = None
def stop_threads(self):
""" Single point of call to stop any RC or Challenge Threads """
if self.challenge:
try:
self.challenge.stop()
finally:
self.challenge = None
self.challenge_thread = None
logging.info("Stopping Challenge Thread")
else:
logging.info("No Challenge Thread")
# Reset current mode index
self.current_mode = Mode.MODE_NONE
# Safety setting
self.core.enable_motors(False)
# Show state on OLED display
self.show_menu()
def get_mode_name(self, mode):
""" Return appropriate mode name """
mode_name = ""
if mode != Mode.MODE_NONE:
mode_name = self.menu_list[mode]
return mode_name
def get_next_mode(self, mode):
""" Find the previous menu item """
# mode_index = self.menu_list.index(mode)
# next_index = mode_index + 1
# if next_index >= len(self.menu_list):
# next_index = 0 # Wrapped round to end
# return list(self.menu_list.items())[next_index]
index = 0
count = 0
for x in self.menu_list.keys():
if x == mode:
index = count
count = count + 1
list_keys = list(self.menu_list.keys())
if index + 1 >= len(list_keys):
index = -1 # Loop back to start of list
return list_keys[index + 1]
def get_previous_mode(self, mode):
""" Find the previous menu item """
# mode_index = self.menu_list.index(mode)
# previous_index = mode_index - 1
# if previous_index < 0:
# previous_index = len(self.menu_list) - 1 # Wrapped round to end
# return list(self.menu_list.items())[previous_index]
index = 0
count = 0
for x in self.menu_list.keys():
if x == mode:
index = count
count = count + 1
list_keys = list(self.menu_list.keys())
if index - 1 < 0:
index = len(list_keys) # Loop back to end of list
return list_keys[index - 1]
def show_message(self, message):
""" Show state on OLED display """
if self.oled is not None:
self.oled.cls() # Clear Screen
self.oled.canvas.text((10, 10), message, fill=1)
# Now show the mesasge on the screen
self.oled.display()
def show_mode(self):
""" Display current menu item. """
if self.oled is not None:
# Clear Screen
self.oled.cls()
# Get current mode name and display it.
mode_name = self.get_mode_name(self.current_mode)
self.oled.canvas.text((10, 10), 'Mode: ' + mode_name, fill=1)
# Now show the mesasge on the screen
self.oled.display()
def menu_item_pressed(self):
""" Current menu item pressed. Do something """
if self.menu_mode == Mode.MODE_POWER:
logging.info("Power Off")
self.power_off()
elif self.menu_mode == Mode.MODE_REBOOT:
logging.info("Reboot")
self.reboot()
elif self.menu_mode == Mode.MODE_KILL_PROCESS:
logging.info("Kill Process")
self.kill_process()
elif self.menu_mode == Mode.MODE_RC:
logging.info("RC Mode")
self.start_rc_mode()
elif self.menu_mode == Mode.MODE_SPEED:
logging.info("Speed Mode")
self.start_speed_mode(False)
elif self.menu_mode == Mode.MODE_SPEED_LINEAR:
logging.info("Linear Speed Mode")
self.start_speed_mode(True)
elif self.menu_mode == Mode.MODE_MAZE:
self.start_maze_mode()
logging.info("Maze Mode")
elif self.menu_mode == Mode.MODE_RAINBOW:
self.start_rainbow_mode()
logging.info("Rainbow Mode")
elif self.menu_mode == Mode.MODE_TOF_CALIBRATE:
self.start_tof_calibrate_mode()
logging.info("TOF Calibrate Mode")
elif self.menu_mode == Mode.MODE_WHITE_CALIBRATE:
self.start_white_calibrate_mode()
logging.info("White Calibrate Mode")
elif self.menu_mode == Mode.MODE_WIPE_RAINBOW_MEMORY:
self.start_clear_rainbow_memory()
logging.info("Wipe rainbow memory Mode")
def menu_up(self):
self.menu_mode = self.get_previous_mode(self.menu_mode)
self.show_menu()
def menu_down(self):
self.menu_mode = self.get_next_mode(self.menu_mode)
self.show_menu()
def draw_maze(self, x_offset, y_offset):
""" Draw the maze icon """
self.oled.canvas.line(
(x_offset + 45.0, y_offset + 0.0 * -1.0,
x_offset + 45.0, y_offset + 24.22 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 45.0, y_offset + 24.22 * -1.0,
x_offset + 0.0, y_offset + 24.22 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 0.0, y_offset + 24.22 * -1.0,
x_offset + 0.0, y_offset + 0.0 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 0.0, y_offset + 0.0 * -1.0,
x_offset + 29.18, y_offset + 0.0 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 37.059, y_offset + 0.0 * -1.0,
x_offset + 37.059, y_offset + 16.279 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 37.059, y_offset + 16.279 * -1.0,
x_offset + 29.118, y_offset + 16.279 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 29.118, y_offset + 16.279 * -1.0,
x_offset + 29.118, y_offset + 7.941 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 29.118, y_offset + 7.941 * -1.0,
x_offset + 7.941, y_offset + 7.941 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 7.941, y_offset + 7.941 * -1.0,
x_offset + 7.941, y_offset + 16.279 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 18.529, y_offset + 24.22 * -1.0,
x_offset + 18.529, y_offset + 16.279 * -1.0),
fill=1)
def draw_speed_test(self, x_offset, y_offset):
""" Draw the maze icon """
self.oled.canvas.line(
(x_offset + 0.0, y_offset + 0.0 * -1.0,
x_offset + 9.477, y_offset + 0.0 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 9.477, y_offset + 0.0 * -1.0,
x_offset + 19.395, y_offset + 7.001 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 19.395, y_offset + 7.001 * -1.0,
x_offset + 25.605, y_offset + 7.001 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 25.605, y_offset + 7.001 * -1.0,
x_offset + 35.523, y_offset + 0.0 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 35.523, y_offset + 0.0 * -1.0,
x_offset + 45.0, y_offset + 0.0 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 0.0, y_offset + 7.001 * -1.0,
x_offset + 5.625, y_offset + 7.001 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 5.625, y_offset + 7.001 * -1.0,
x_offset + 16.875, y_offset + 14.942 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 16.875, y_offset + 14.942 * -1.0,
x_offset + 28.125, y_offset + 14.942 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 28.125, y_offset + 14.942 * -1.0,
x_offset + 39.375, y_offset + 7.001 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 39.375, y_offset + 7.001 * -1.0,
x_offset + 45.0, y_offset + 7.001 * -1.0),
fill=1)
def draw_rainbow(self, x_offset, y_offset):
""" Draw the maze icon """
self.oled.canvas.line(
(x_offset + 0.0, y_offset + 0.0 * -1.0,
x_offset + 0.0, y_offset + 24.219 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 0.0, y_offset + 24.219 * -1.0,
x_offset + 24.219, y_offset + 24.219 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 24.219, y_offset + 24.219 * -1.0,
x_offset + 24.219, y_offset + 0.0 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 24.219, y_offset + 0.0 * -1.0,
x_offset + 0.0, y_offset + 0.0 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 10.322, y_offset + 9.147 * -1.0,
x_offset + 10.322, y_offset + 15.071 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 10.322, y_offset + 15.071 * -1.0,
x_offset + 13.897, y_offset + 15.071 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 13.897, y_offset + 15.071 * -1.0,
x_offset + 13.897, y_offset + 9.147 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 13.897, y_offset + 9.147 * -1.0,
x_offset + 10.322, y_offset + 9.147 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 4.274, y_offset + 0.0 * -1.0,
x_offset + 0.0, y_offset + 4.273 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 0.0, y_offset + 19.945 * -1.0,
x_offset + 4.274, y_offset + 24.219 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 19.945, y_offset + 24.219 * -1.0,
x_offset + 24.219, y_offset + 19.945 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 24.219, y_offset + 4.273 * -1.0,
x_offset + 19.945, y_offset + 0.0 * -1.0),
fill=1)
def draw_rc(self, x_offset, y_offset):
""" Draw the RC icon """
self.oled.canvas.line(
(x_offset + 0.0, y_offset + 0.0 * -1.0,
x_offset + 9.477, y_offset + 14.947 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 9.477, y_offset + 14.947 * -1.0,
x_offset + 35.523, y_offset + 14.942 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 35.523, y_offset + 14.942 * -1.0,
x_offset + 45.0, y_offset + 0.0 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 45.0, y_offset + 0.0 * -1.0,
x_offset + 35.523, y_offset + 0.0 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 35.523, y_offset + 0.0 * -1.0,
x_offset + 25.605, y_offset + 7.001 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 25.605, y_offset + 7.001 * -1.0,
x_offset + 19.477, y_offset + 7.001 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 19.477, y_offset + 7.001 * -1.0,
x_offset + 9.477, y_offset + 0.0 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 9.477, y_offset + 0.0 * -1.0,
x_offset + 0.0, y_offset + 0.0 * -1.0),
fill=1)
radius = 1.5
self.oled.canvas.ellipse(
(x_offset + (16.895 - radius), y_offset + (9.501 + radius) * -1.0,
x_offset + (16.895 + radius), y_offset + (9.501 - radius) * -1.0),
outline=1,
fill=0)
self.oled.canvas.ellipse(
(x_offset + (28.105 - radius), y_offset + (9.501 + radius) * -1.0,
x_offset + (28.105 + radius), y_offset + (9.501 - radius) * -1.0),
outline=1,
fill=0)
line_len = 1.5
self.oled.canvas.line(
(x_offset + (12.44 - line_len), y_offset + 12.65 * -1.0,
x_offset + (12.44 + line_len), y_offset + 12.65 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 12.44, y_offset + (12.65 - line_len) * -1.0,
x_offset + 12.44, y_offset + (12.65 + line_len) * -1.0),
fill=1)
def draw_tof_calib(self, x_offset, y_offset):
""" Draw the TOF calibration icon """
self.oled.canvas.line(
(x_offset + 0.0, y_offset + 0.0 * -1.0,
x_offset + 45.0, y_offset + 0.0 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 22.5, y_offset + 0.0 * -1.0,
x_offset + 22.5, y_offset + 12.11 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 3.117, y_offset + -3.131 * -1.0,
x_offset + 0.0, y_offset + 0.0 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 0.0, y_offset + 0.0 * -1.0,
x_offset + 3.117, y_offset + 3.131 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 19.368, y_offset + 8.993 * -1.0,
x_offset + 22.5, y_offset + 12.110 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 22.5, y_offset + 12.110 * -1.0,
x_offset + 25.631, y_offset + 8.993 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 41.883, y_offset + 3.131 * -1.0,
x_offset + 45.0, y_offset + 0.0 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 45.0, y_offset + 0.0 * -1.0,
x_offset + 41.883, y_offset + -3.131 * -1.0),
fill=1)
def draw_cmd(self, x_offset, y_offset):
""" Draw the maze icon """
self.oled.canvas.line(
(x_offset + 0.0, y_offset + 0.0 * -1.0,
x_offset + 0.0, y_offset + 24.219 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 0.0, y_offset + 24.219 * -1.0,
x_offset + 24.219, y_offset + 24.219 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 24.219, y_offset + 24.219 * -1.0,
x_offset + 24.219, y_offset + 0.0 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 24.219, y_offset + 0.0 * -1.0,
x_offset + 0.0, y_offset + 0.0 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 0.0, y_offset + 19.945 * -1.0,
x_offset + 24.219, y_offset + 19.945 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 4.5, y_offset + 15.445 * -1.0,
x_offset + 12.109, y_offset + 9.972 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 12.109, y_offset + 9.972 * -1.0,
x_offset + 4.5, y_offset + 4.5 * -1.0),
fill=1)
def draw_pwr(self, x_offset, y_offset):
""" Draw the maze icon """
radius = 7.2
self.oled.canvas.ellipse(
(x_offset + (12.109 - radius), y_offset + (12.109 + radius) * -1.0,
x_offset + (12.109 + radius), y_offset + (12.109 - radius) * -1.0),
outline=1,
fill=0)
self.oled.canvas.rectangle(
(x_offset + 7.811, y_offset + 20.0 * -1.0,
x_offset + 16.408, y_offset + 12.109 * -1.0),
outline=0, fill=0)
self.oled.canvas.line(
(x_offset + 12.109, y_offset + 21.959 * -1.0,
x_offset + 12.109, y_offset + 15.460 * -1.0),
fill=1)
def draw_reboot(self, x_offset, y_offset):
""" Draw the maze icon """
radius = 7.2
self.oled.canvas.ellipse(
(x_offset + (12.109 - radius), y_offset + (12.109 + radius) * -1.0,
x_offset + (12.109 + radius), y_offset + (12.109 - radius) * -1.0),
outline=1,
fill=0)
self.oled.canvas.rectangle(
(x_offset + 7.811, y_offset + 20.0 * -1.0,
x_offset + 20.0, y_offset + 12.109 * -1.0),
outline=0, fill=0)
self.oled.canvas.line(
(x_offset + 12.109, y_offset + 21.959 * -1.0,
x_offset + 12.109, y_offset + 15.460 * -1.0),
fill=1)
self.oled.canvas.line(
(x_offset + 16.309, y_offset + 12.109 * -1.0,
x_offset + 19.309, y_offset + 15.109 * -1.0
),
fill=1)
self.oled.canvas.line(
(x_offset + 19.309, y_offset + 15.109 * -1.0,
x_offset + 22.309, y_offset + 12.109 * -1.0),
fill=1)
def draw_white_balance(self, x_offset, y_offset):
# 2x triangles indicating menu direction
self.oled.canvas.polygon(
((x_offset + 0.0, y_offset + 0.0 * -1.0),
(x_offset + 0.0, y_offset + 45.0 * -1.0),
(x_offset + 45.0, y_offset + 45.0 * -1.0),
(x_offset + 0.0, y_offset + 0.0 * -1.0)),
outline=1,
fill=0)
self.oled.canvas.polygon(
((x_offset + 0.0, y_offset + 0.0 * -1.0),
(x_offset + 45.0, y_offset + 0.0 * -1.0),
(x_offset + 45.0, y_offset + 45.0 * -1.0),
(x_offset + 0.0, y_offset + 0.0 * -1.0)),
outline=1,
fill=1)
def draw_bin(self, x_offset, y_offset):
# 2x triangles indicating menu direction
self.oled.canvas.polygon(
((x_offset + 11.25, y_offset + 0.0 * -1.0),
(x_offset + 33.75, y_offset + 0.0 * -1.0),
(x_offset + 40.0, y_offset + 25.0 * -1.0),
(x_offset + 5.0, y_offset + 25.0 * -1.0),
(x_offset + 11.25, y_offset + 0.0 * -1.0)),
outline=1,
fill=1)
self.oled.canvas.polygon(
((x_offset + 5.0, y_offset + 32.0 * -1.0),
(x_offset + 40.0, y_offset + 32.0 * -1.0),
(x_offset + 40.0, y_offset + 35.0 * -1.0),
(x_offset + 33.75, y_offset + 35.0 * -1.0),
(x_offset + 33.75, y_offset + 40.0 * -1.0),
(x_offset + 11.25, y_offset + 40.0 * -1.0),
(x_offset + 11.25, y_offset + 35.0 * -1.0),
(x_offset + 5.0, y_offset + 35.0 * -1.0),
(x_offset + 5.0, y_offset + 32.0 * -1.0)),
outline=1,
fill=1)
def draw_icon_rect(self):
self.oled.canvas.line(
(39.0, 12.0,
89.0, 12.0),
fill=1)
self.oled.canvas.line(
(89.0, 12.0,
89.0, 62.0),
fill=1)
self.oled.canvas.line(
(89.0, 62.0,
39.0, 62.0),
fill=1)
self.oled.canvas.line(
(39.0, 62.0,
39.0, 12.0),
fill=1)
def show_menu_header(self, header_y, current_mode_name):
""" Draw the menu header. """
if self.oled is not None:
self.oled.cls()
# Display Bot name and header information
self.oled.canvas.text(
(10, header_y),
'TITO 5: ' + current_mode_name,
fill=1)
# Line underneath header
self.oled.canvas.line(
(0, 9, self.oled.width - 1, 9),
fill=1)
if self.current_batt:
fill_rect = 0
if self.current_batt >= 0.2:
fill_rect = 1
self.oled.canvas.rectangle(
(self.oled.width - 5, 8, self.oled.width - 2, 0),
outline=1,
fill=fill_rect)
fill_rect = 0
if self.current_batt >= 0.4:
fill_rect = 1
self.oled.canvas.rectangle(
(self.oled.width - 10, 8, self.oled.width - 7, 0),
outline=1,
fill=fill_rect)
fill_rect = 0
if self.current_batt >= 0.6:
fill_rect = 1
self.oled.canvas.rectangle(
(self.oled.width - 15, 8, self.oled.width - 12, 0),
outline=1,
fill=fill_rect)
fill_rect = 0
if self.current_batt >= 0.8:
fill_rect = 1
self.oled.canvas.rectangle(
(self.oled.width - 20, 8, self.oled.width - 17, 0),
outline=1,
fill=fill_rect)
else:
pass # No controller yet
def show_menu(self):
""" Display menu. """
# Display current menu item to prompt for when no OLED attached
mode_name = self.get_mode_name(self.menu_mode)
print(mode_name)
if self.current_batt is not None:
print(self.current_batt)
# Clear Screen
if self.oled is not None:
header_y = 0
previous_y = 20
current_y = 30
next_y = 40
# Get next and previous list items
previous_mode = self.get_previous_mode(self.menu_mode)
next_mode = self.get_next_mode(self.menu_mode)
# Get mode names and display them.
current_mode_name = self.get_mode_name(self.current_mode)
mode_name_up = self.get_mode_name(previous_mode)
mode_name_down = self.get_mode_name(next_mode)
self.show_menu_header(header_y, current_mode_name)
icon_drawn = True
if self.menu_mode == Mode.MODE_MAZE:
# Draw Maze logo
self.draw_icon_rect()
x_offset = 64.0 - 45.0 / 2.0
y_offset = 35.0 + 24.22 / 2.0
self.draw_maze(x_offset, y_offset)
elif self.menu_mode == Mode.MODE_SPEED:
# Draw Speed logo
self.draw_icon_rect()
x_offset = 64.0 - 45.0 / 2.0
y_offset = 35.0 + 14.942 / 2.0
self.draw_speed_test(x_offset, y_offset)
elif self.menu_mode == Mode.MODE_RAINBOW:
# Draw Rainbow logo
self.draw_icon_rect()
x_offset = 64.0 - 24.219 / 2.0
y_offset = 35.0 + 24.219 / 2.0
self.draw_rainbow(x_offset, y_offset)
elif self.menu_mode == Mode.MODE_RC:
# Draw RC logo
self.draw_icon_rect()
x_offset = 64.0 - 45.0 / 2.0
y_offset = 35.0 + 14.942 / 2.0
self.draw_rc(x_offset, y_offset)
elif self.menu_mode == Mode.MODE_TOF_CALIBRATE:
# Draw tof calibrate logo
self.draw_icon_rect()
x_offset = 64.0 - 45.0 / 2.0
y_offset = (35.0 + 3.131) + (12.110 + 3.131) / 2.0
self.draw_tof_calib(x_offset, y_offset)
elif self.menu_mode == Mode.MODE_KILL_PROCESS:
# Draw CMD logo
self.draw_icon_rect()
x_offset = 64.0 - 24.219 / 2.0
y_offset = 35.0 + 24.219 / 2.0
self.draw_cmd(x_offset, y_offset)
elif self.menu_mode == Mode.MODE_POWER:
# Draw Power off logo
self.draw_icon_rect()
x_offset = 64.0 - 24.219 / 2.0
y_offset = 35.0 + 24.219 / 2.0
self.draw_pwr(x_offset, y_offset)
elif self.menu_mode == Mode.MODE_REBOOT:
# Draw Reboot logo
self.draw_icon_rect()
x_offset = 64.0 - 24.219 / 2.0
y_offset = 35.0 + 24.219 / 2.0
self.draw_reboot(x_offset, y_offset)
elif self.menu_mode == Mode.MODE_WHITE_CALIBRATE:
# Draw white balance logo
self.draw_icon_rect()
x_offset = 64.0 - 45.0 / 2.0
y_offset = 35.0 + 45.0 / 2.0
self.draw_white_balance(x_offset, y_offset)
elif self.menu_mode == Mode.MODE_WIPE_RAINBOW_MEMORY:
# Draw white balance logo
self.draw_icon_rect()
x_offset = 64.0 - 45.0 / 2.0
y_offset = 35.0 + 45.0 / 2.0
self.draw_bin(x_offset, y_offset)
else:
# Draw rect around current selection.
# NOTE: Has to be done BEFORE text below
icon_drawn = False
self.oled.canvas.rectangle(
(10, current_y, self.oled.width - 1, current_y + 10),
outline=1,
fill=0)
# show current mode as well as one mode either side
self.oled.canvas.text(
(15, previous_y),
'Mode: ' + mode_name_up,
fill=1)
self.oled.canvas.text(
(15, current_y),
'Mode: ' + mode_name,
fill=1)
self.oled.canvas.text(
(15, next_y),
'Mode: ' + mode_name_down,
fill=1)
# 2x triangles indicating menu direction
self.oled.canvas.polygon(
((1, previous_y + 9),
(5, previous_y + 1),
(9, previous_y + 9),
(1, previous_y + 9)),
outline=1,
fill=0)
self.oled.canvas.polygon(
((1, next_y + 1),
(5, next_y + 9),
(9, next_y + 1),
(1, next_y + 1)),
outline=1,
fill=0)
if icon_drawn:
# 2x triangles indicating menu direction
self.oled.canvas.polygon(
((10.0, 30.0),
(15.0, 20.0),
(20.0, 30.0),
(10.0, 30.0)),
outline=1,
fill=0)
self.oled.canvas.polygon(
((10.0, 40.0),
(15.0, 50.0),
(20.0, 40.0),
(10.0, 40.0)),
outline=1,
fill=0)
# Right arrow to indicate how to enter challenge
self.oled.canvas.polygon(
((100.0, 30.0),
(110.0, 35.0),
(100.0, 40.0),
(100.0, 30.0)),
outline=1,
fill=0)
# Now show the mesasge on the screen
self.oled.display()
def power_off(self):
""" Power down the pi """
self.stop_threads()
if self.oled is not None:
self.oled.cls() # Clear Screen
self.oled.canvas.text((10, 10), 'Powering off...', fill=1)
# Now show the mesasge on the screen
self.oled.display()
# Call system OS to shut down the Pi
logging.info("Shutting Down Pi")
os.system("sudo shutdown -h now")
def reboot(self):
""" Power down the pi """
self.stop_threads()
if self.oled is not None:
self.oled.cls() # Clear Screen
self.oled.canvas.text((10, 10), 'Rebooting...', fill=1)
# Now show the mesasge on the screen
self.oled.display()
# Call system OS to shut down the Pi
logging.info("Rebooting Pi")
os.system("sudo reboot")
def kill_process(self):
""" Power down the pi """
self.stop()
if self.oled is not None:
self.oled.cls() # Clear Screen
self.oled.canvas.text((10, 10), 'Killing Process...', fill=1)
# Now show the mesasge on the screen
self.oled.display()
# Stop running this python module
logging.info("Exiting Process")
quit()
def start_speed_mode(self, linear):
# Kill any previous Challenge / RC mode
self.stop_threads()
# Set Wiimote LED to RC Mode index
if linear:
self.current_mode = Mode.MODE_SPEED_LINEAR
style = "LINEAR"
else:
self.current_mode = Mode.MODE_SPEED
style = None # Default to whatever.
# Set sensible speed
self.core.set_speed_factor(0.35)
# Inform user we are about to start RC mode
logging.info("Entering into SPEED Mode")
self.challenge = speed.Speed(self.core, self.oled, style)
# Create and start a new thread
# running the remote control script
logging.info("Starting SPEED Thread")
self.challenge_thread = threading.Thread(
target=self.challenge.run)
self.challenge_thread.start()
logging.info("SPEED Thread Running")
def start_rc_mode(self):
# Kill any previous Challenge / RC mode
self.stop_threads()
# Set Wiimote LED to RC Mode index
self.current_mode = Mode.MODE_RC
# Set maximum power for RC
self.core.set_speed_factor(1.0)
# Inform user we are about to start RC mode
logging.info("Entering into RC Mode")
self.challenge = rc.rc(self.core, self.controller, self.oled)
# Create and start a new thread
# running the remote control script
logging.info("Starting RC Thread")
self.challenge_thread = threading.Thread(
target=self.challenge.run)
self.challenge_thread.start()
logging.info("RC Thread Running")
def start_maze_mode(self):
# Kill any previous Challenge / RC mode, yada yada as above
self.stop_threads()
self.current_mode = Mode.MODE_MAZE
self.core.set_speed_factor(0.4)
logging.info("Entering into Maze mode")
self.challenge = wall_follower.WallFollower(self.core, self.oled)
logging.info("Starting maze thread")
self.challenge_thread = threading.Thread(
target=self.challenge.run)
self.challenge_thread.start()
logging.info("Maze thread running")
def start_rainbow_mode(self):
# Kill any previous Challenge / RC mode, yada yada as above
self.stop_threads()
self.current_mode = Mode.MODE_RAINBOW
self.core.set_speed_factor(0.4)
logging.info("Entering into Rainbow mode")
self.challenge = rainbow.Rainbow(self.core, self.oled)
logging.info("Starting Rainbow thread")
self.challenge_thread = threading.Thread(
target=self.challenge.run)
self.challenge_thread.start()
logging.info("Rainbow thread running")
def start_tof_calibrate_mode(self):
# Kill any previous Challenge / RC mode, yada yada as above
self.stop_threads()
self.current_mode = Mode.MODE_TOF_CALIBRATE
logging.info("Entering into TOF Calibrate mode")
self.challenge = tof_calibrate.Tof_Calibrate(self.core, self.oled)
logging.info("Starting TOF Calibrate thread")
self.challenge_thread = threading.Thread(
target=self.challenge.run)
self.challenge_thread.start()
logging.info("TOF Calibrate thread running")
def start_white_calibrate_mode(self):
# Kill any previous Challenge / White balance calibration, yada yada as above
self.stop_threads()
self.current_mode = Mode.MODE_WHITE_CALIBRATE
logging.info("Running white Calibrate mode")
wb = whitebalance.WhiteBalance()
wb.run()
self.current_mode = Mode.MODE_WHITE_CALIBRATE
logging.info("White Calibrate mode finished")
def start_clear_rainbow_memory(self):
# Kill any previous Challenge / wipe rainbow stored RGB order, yada yada as above
self.stop_threads()
self.current_mode = Mode.MODE_WIPE_RAINBOW_MEMORY
logging.info("Running white Calibrate mode")
f = open('arenacolours.txt', 'r+')
f.truncate(0) # need '0' when using r+
self.current_mode = Mode.MODE_WIPE_RAINBOW_MEMORY
logging.info("White Calibrate mode finished")
def stop(self):
""" Stop the entire program safely. """
launcher.controller = None
launcher.stop_threads() # This will set neutral for us.
print("Clearing up")
launcher.core.cleanup()
launcher.GPIO.cleanup()
def run(self):
""" Main Running loop controling bot mode and menu state """
# Show state on OLED display
self.show_message('Booting...')
self.show_message('Initialising Bluetooth...')
start = time.time()
# Never stop looking for controller.
while not self.killed:
try:
if self.oled is not None:
# Show state on OLED display
self.oled.cls() # Clear screen
self.oled.canvas.text(