-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1021 lines (828 loc) · 26.8 KB
/
main.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 pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import (Motor, ColorSensor, GyroSensor, InfraredSensor)
from pybricks.nxtdevices import ColorSensor as nxtColorSensor
from pybricks.parameters import Port, Stop, Direction, Button, Color
from pybricks.tools import wait, StopWatch, DataLog
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile, ImageFile
from pybricks.iodevices import Ev3devSensor
import math, time
import sys
from helper import *
from pid import *
# declare global variables
Houses = [[], [], []]
numYellow = 4
numBlue = 4
numGreen = 4
numSurplus = 4
surplus = None
extraCol = None
# initialise ev3
ev3 = EV3Brick()
clock = StopWatch()
# initialise motors
frontClaw = FrontClaw(Port.A)
backClaw = BackClaw(Port.D)
leftMotor = Motor(Port.B, positive_direction = Direction.COUNTERCLOCKWISE)
rightMotor = Motor(Port.C)
# unlock speed limit
leftMotor.control.limits(1500)
rightMotor.control.limits(1500)
# initialise sensors
ev3Col = Ev3devSensor(Port.S1)
ev3ColSensor = ColorSensor(Port.S1)
gyro = GyroSensor(Port.S2)
colLeft = ColorSensor(Port.S3)
colRight = ColorSensor(Port.S4)
base = Base(leftMotor, rightMotor, colLeft, colRight, frontClaw, backClaw)
# set up defaults for PID functions
# old: 0.16, 0.0001, 17
LineTrack = PID_LineTrack(base, 0.21, 0.0013, 12, 45)
GyroStraight = PID_GyroStraight(base, 1.2, 0.005, 20, gyro)
GyroStraightDeg = PID_GyroStraightDegrees(base, 1.2, 0.005, 20, gyro)
GyroTurn = PID_GyroTurn(base, 0.9, 0.015, 5, gyro)
#GyroTurn = PID_GyroTurn(base, 1, 0, 0)
# battery alert
print(ev3.battery.voltage())
if ev3.battery.voltage() <= 8050:
print('LOW BATTERY')
ev3.speaker.beep()
#sys.exit()
def calibrate_gyro():
ev3.speaker.beep()
gyro = GyroSensor(Port.S2)
_ = gyro.speed()
while gyro.angle() != 0:
wait(1)
def print_degrees():
while True:
print(rightMotor.angle())
def debug_LineSquare():
GyroStraight.move(-50, lambda: colLeft.color() != Color.WHITE)
base.hold()
start = clock.time()
PID_LineSquare(base, direction = -1)
print(clock.time() - start)
gyro.reset_angle(0)
base.hold()
base.reset()
GyroStraight.move(50, lambda: rightMotor.angle() < 200)
base.stop()
wait(1000)
def debug_GyroTurn():
while True:
GyroTurn.turn(89)
base.reset()
GyroStraightDeg.move(50, 200)
base.hold()
wait(100)
def scanHouseEV3(house, target = 300):
# initialise pid for gyrostraight
kp, ki, kd = GyroStraight.kp, GyroStraight.ki, GyroStraight.kd
gyroPID = PID(kp, ki, kd)
speed = 80
maxSpeed = 80
minSpeed = 30
rate = 2 * maxSpeed / (target * 0.04)
base.reset()
deccel = False
while colRight.color() != Color.BLACK and colLeft.color() != Color.BLACK:
detected = False
gyroPID.update(gyro.angle(), kp, ki, kd)
angle = base.rightMotor.angle()
if abs(abs(angle) - abs(target)) <= 80 * maxSpeed / 40:
deccel = True
if abs(speed) > minSpeed:
speed = (abs(speed) - rate) * speed/abs(speed)
if speed < minSpeed:
speed = minSpeed
elif not deccel:
speed += rate
if speed > maxSpeed:
speed = maxSpeed
base.run(speed - gyroPID.correction, speed + gyroPID.correction)
r, g, b = ev3Col.read('RGB-RAW')
if r + g + b >= 20:
detected = True
if r - b >= 3 and r - g >= 3:
house.append(Color.YELLOW)
elif b - r >= 3 and b - g >= 3:
house.append(Color.BLUE)
elif g - r >= 3 and g - b >= 3:
house.append(Color.GREEN)
else:
detected = False
# once an indicator has been detected, move until nothing is detected
if detected:
while r + g + b > 15:
r, g, b = ev3Col.read('RGB-RAW')
gyroPID.update(gyro.angle(), kp, ki, kd)
angle = base.rightMotor.angle()
if abs(abs(angle) - abs(target)) <= 100 * maxSpeed / 40:
if abs(speed) > minSpeed:
speed = (abs(speed) - rate) * speed/abs(speed)
if speed < minSpeed:
speed = minSpeed
base.run(speed - gyroPID.correction, speed + gyroPID.correction)
detected = False
print(house)
def checkSurplus(degrees):
# reverse for certain amount of degrees to check if surplus is present
speed = -40
kp, ki, kd = GyroStraight.kp, GyroStraight.ki, GyroStraight.kd
gyroPID = PID(kp, ki, kd)
base.reset()
detected = False
while rightMotor.angle() >= degrees:
# r, g, b = ev3Col.read('RGB-RAW')
gyroPID.update(gyro.angle(), kp, ki, kd)
base.run(speed - gyroPID.correction, speed + gyroPID.correction)
if ev3ColSensor.reflection() > 0:
detected = True
base.hold()
return detected
def collectSurplus(degrees, col):
if col == Color.BLUE:
base.reset()
GyroStraightDeg.move(-40, -100)
base.hold()
PID_SingleMotorTurn(base, gyro, 180, 1, 0.7)
# start opening claw
frontClaw.openUp(wait = False)
base.reset()
LineTrack.move(colLeft, 80, lambda: colRight.color() != Color.BLACK, target = 1200)
base.hold()
base.reset()
GyroStraightDeg.move(-50, -120)
base.hold()
GyroTurn.turn(-89)
base.reset()
GyroStraightDeg.move(-20, -20)
base.hold()
else:
base.reset()
frontClaw.openUp()
GyroStraightDeg.move(-60, -160)
base.hold()
GyroTurn.turn(89)
base.reset()
if col == Color.YELLOW:
GyroStraightDeg.move(-50, -120)
base.hold()
PID_LineSquare(base, direction = -1)
gyro.reset_angle(0)
base.reset()
#move forward to collect surplus
GyroStraightDeg.move(85, degrees)
base.hold()
# grab front surplus using claw
frontClaw.goUp()
base.reset()
GyroStraightDeg.move(60, 420)
base.hold()
frontClaw.goDown()
base.reset()
if col == Color.YELLOW:
GyroStraightDeg.move(-80, -(degrees + 300))
base.hold()
GyroTurn.turn(89)
elif col == Color.GREEN:
GyroStraightDeg.move(-80, -(degrees + 370))
base.hold()
elif col == Color.BLUE:
GyroStraightDeg.move(-80, -790)
base.hold()
def collectGreen():
backClaw.run_time(100, 1000, wait = False)
base.reset()
LineTrack.move(colRight, 50, lambda: colLeft.color() != Color.BLACK, side = -1)
curr = rightMotor.angle()
LineTrack.move(colRight, 40, lambda: rightMotor.angle() < 215 + curr, side = -1, target = 200 + curr, reset_I = False)
base.hold()
gyro.reset_angle(0)
backClaw.run_angle(50, -192, wait = False)
GyroTurn.turn(-89)
GyroStraight.move(-40, lambda: colRight.color() != Color.WHITE)
GyroStraight.move(-40, lambda: colRight.color() != Color.BLACK)
base.hold()
# reset claw again
backClaw.run_time(100, 1000, wait = False)
# linetrack and turn to grab other 2 green
base.reset()
GyroStraightDeg.move(60, 165)
base.hold()
GyroTurn.turn(89)
base.reset()
LineTrack.move(colRight, 50, lambda: rightMotor.angle() < 355, side = -1, target = 355)
base.hold()
GyroTurn.turn(-89)
backClaw.run_target(60, -192)
base.reset()
GyroStraightDeg.move(-40, -35)
base.hold()
backClaw.run_angle(50, 47)
GyroStraightDeg.move(-40, -50)
base.hold()
# cap speed of turns after grabbing green to stop them from jerking
GyroTurn.maxSpeed = 40
def depositHouse(house, time, houseNum):
# TO DO
# add variable degree for cases when bot starts closer to house
global numGreen, numBlue, numYellow, numSurplus, surplus, extraCol
clawDeposit = False
catchmentDeposit = False
if time == 1:
# first visit to house deposits surplus and green
numCol = numGreen
RingCol = Color.GREEN
tmp = 1
if len(house) == 1 or (surplus in house and houseNum == 2 and surplus == Color.BLUE):
tmp = 1
if len(house) == 1 and house[0] == surplus and houseNum == 2 and surplus == Color.BLUE:
tmp = 2
if houseNum == 1:
GyroTurn.turn(-89)
else:
GyroTurn.turn(89)
if numSurplus == 4 and tmp == 1:
catchmentDeposit = True
# lift claw, move forward then reverse to deposit surplus from within catchment area
if houseNum == 1:
frontClaw.goUp(wait = False, full = True)
else:
frontClaw.goUp(full = True)
GyroStraight.move(60, lambda: colLeft.color() != Color.RED)
base.reset()
GyroStraightDeg.move(60, 180)
base.hold()
numSurplus -= 2
elif numSurplus == 2:
clawDeposit = True
# deposit surplus from claw
frontClaw.openSmall(wait = False)
base.run_time(60, 500)
base.hold()
numSurplus = 0
elif tmp == 2:
# deposit all
clawDeposit = True
frontClaw.openUp()
base.run_time(80, 600)
base.hold()
numSurplus = 0
else:
# second visit to house deposits yellow and blue
numCol = numBlue
RingCol = Color.BLUE
if Color.YELLOW in house:
# check number of yellow in house
tmp = 1
if len(house) == 2:
if house[0] == Color.YELLOW and house[1] == Color.YELLOW:
tmp = 2
if houseNum == 1:
GyroTurn.turn(-89)
else:
GyroTurn.turn(89)
if tmp == 2:
# deposit all yellow
clawDeposit = True
frontClaw.openUp()
base.run_time(80, 600)
base.hold()
numYellow = 0
elif tmp == 1 and numYellow == 2 and extraCol != Color.YELLOW:
clawDeposit = True
# deposit yellow from claw if 2 yellow left and yellow is not in battery area
base.run_time(80, 400)
base.hold()
frontClaw.openSmall()
numYellow = 0
else:
# deposit yellow from catchment area
catchmentDeposit = True
base.reset()
if (houseNum == 1) or time == 2:
frontClaw.goUp(wait = False, full = True)
else:
frontClaw.goUp(full = True)
GyroStraight.move(60, lambda: colLeft.color() != Color.RED)
base.reset()
GyroStraightDeg.move(70, 80)
base.hold()
numYellow -= 2
# exit if 4 energy has been deposited
if RingCol not in house or (time == 1 and tmp == 2):
if houseNum == 1 or houseNum == 2:
base.reset()
if clawDeposit:
GyroStraightDeg.move(-70, -410)
else:
GyroStraightDeg.move(-70, -580)
base.hold()
if houseNum == 1:
GyroTurn.turn(-89)
elif houseNum == 2:
GyroTurn.turn(89)
else:
GyroStraightDeg.move(-80, -200)
base.hold()
GyroTurn.turn(180)
if houseNum == 2 and numSurplus == 2 and time == 1:
# lower claw if not house 1
frontClaw.goDown(wait = False)
#frontClaw.dc(dir = -1, speed = 20)
else:
# green/blue to be deposited
tmp = 1
# check number of green/blue indicators for house
if len(house) == 2:
if house[0] == RingCol and house[1] == RingCol:
tmp = 2
if clawDeposit:
base.reset()
GyroStraightDeg.move(-50, -200)
base.hold()
if houseNum == 3:
GyroTurn.turn(180)
else:
GyroTurn.turn(-180)
elif catchmentDeposit:
base.reset()
GyroStraightDeg.move(-50, -250)
base.hold()
if houseNum == 3:
GyroTurn.turn(180)
else:
GyroTurn.turn(-180)
else:
if houseNum == 1:
GyroTurn.turn(89)
else:
GyroTurn.turn(-89)
base.reset()
if catchmentDeposit:
pass
elif clawDeposit:
GyroStraightDeg.move(-40, -30)
elif time == 1 and houseNum == 1:
GyroStraightDeg.move(-50, -300)
elif time == 2 and (houseNum == 1 or houseNum == 2):
GyroStraightDeg.move(-50, -330)
elif houseNum == 3 and time == 1:
GyroStraightDeg.move(-40, -50)
base.hold()
deg = 105
backClaw.run_target(-30, -deg)
base.reset()
# go more if last set of ring blocks to be deposited
if (numCol == 4 and tmp == 2) or numCol == 2:
GyroStraightDeg.move(40, 170)
base.hold()
if RingCol == Color.GREEN:
numGreen = 0
else:
numBlue = 0
base.reset()
else:
GyroStraightDeg.move(30, 50)
if RingCol == Color.GREEN:
numGreen -= 2
else:
numBlue -= 2
base.hold()
backClaw.run_target(30, deg)
if (numCol == 4 and tmp == 2) or numCol == 2:
backClaw.run_time(100, 1000, wait = False)
base.reset()
GyroStraightDeg.move(-80, -350)
base.hold()
base.reset()
GyroStraightDeg.move(80, 350)
base.hold()
if houseNum == 1 or houseNum == 2:
base.reset()
if not clawDeposit:
GyroStraight.move(40, lambda: colLeft.reflection() > 30 or colRight.reflection() > 30)
curr = rightMotor.angle()
GyroStraightDeg.move(50, 145 + curr)
base.hold()
if houseNum == 1:
GyroTurn.turn(89)
else:
GyroTurn.turn(-87)
# if no more ring blocks are on the bot, reset the maximum turn speed
if numCol == 0:
GyroTurn.maxSpeed = 100
def collectBlue():
GyroTurn.turn(135)
base.reset()
backClaw.run_time(100, 1200, wait = False)
GyroStraightDeg.move(90, 1210)
base.hold()
GyroTurn.turn(-45)
base.reset()
LineTrack.move(colRight, 60, lambda: rightMotor.angle() < 600, side = -1)
LineTrack.move(colRight, 30, lambda: colLeft.color() != Color.BLACK, side = -1, reset_I = False)
base.hold()
wait(50)
gyro.reset_angle(0)
PID_SingleMotorTurn(base, gyro, -89, 1, 0)
wait(10)
base.reset()
GyroStraightDeg.move(40, 30)
base.hold()
PID_LineSquare(base, direction = -1)
gyro.reset_angle(0)
backClaw.run_target(-40, -225, wait = False)
base.reset()
GyroStraightDeg.move(80, 640)
base.hold()
GyroTurn.turn(-89, precision = True)
base.reset()
GyroStraightDeg.move(-40, -195, minSpeed = 10)
base.hold()
backClaw.run_target(-100, -20)
wait(10)
GyroStraightDeg.move(-10, -204)
base.hold()
backClaw.run_time(50, 1200, wait = False)
base.reset()
GyroStraightDeg.move(50, 204)
base.hold()
# collect next 2
GyroTurn.turn(89)
GyroStraight.move(50, lambda: colRight.color() != Color.BLACK)
curr = rightMotor.angle()
GyroStraightDeg.move(60, 255 + curr)
base.hold()
GyroTurn.turn(-89)
backClaw.run_target(-30, -225)
base.reset()
GyroStraight.move(-20, lambda: rightMotor.angle() > -120)
base.hold()
backClaw.run_target(15, 75)
GyroTurn.maxSpeed = 40
base.reset()
GyroStraightDeg.move(90, 1110)
base.hold()
def collectYellow():
# line track to intersection
base.reset()
frontClaw.solar(wait = False)
LineTrack.move(colLeft, 70, lambda: rightMotor.angle() < 700)
LineTrack.move(colLeft, 40, lambda: colRight.color() != Color.BLACK, reset_I = False)
curr = rightMotor.angle()
LineTrack.move(colLeft, 40, lambda: rightMotor.angle() < curr + 105, reset_I = False)
base.hold()
GyroTurn.turn(89)
# push solar panels
frontClaw.hold()
base.reset()
LineTrack.move(colRight, 30, lambda: rightMotor.angle() < 550, threshold = 40)
base.hold()
gyro.reset_angle(0)
wait(10)
GyroStraight.move(30, lambda: colLeft.color() != Color.BLACK and colRight.color() != Color.BLACK)
GyroStraight.move(30, lambda: colLeft.color() != Color.WHITE and colRight.color() != Color.WHITE)
curr = rightMotor.angle()
GyroStraight.move(30, lambda: rightMotor.angle() < 80 + curr)
base.hold()
frontClaw.run_target(60, 570)
frontClaw.run_target(100, 200)
frontClaw.hold()
base.reset()
GyroStraight.move(-30, lambda: rightMotor.angle() > -18)
base.hold()
# track to first 2 yellow and grab with claw
GyroTurn.turn(-89)
frontClaw.openUp(wait = False)
base.reset()
LineTrack.move(colRight, 55, lambda: rightMotor.angle() < 500, side = -1)
GyroStraightDeg.move(60, 620)
base.hold()
GyroTurn.turn(89)
base.reset()
GyroStraightDeg.move(40, 40)
base.hold()
frontClaw.goDown(speed = 100)
base.reset()
GyroStraightDeg.move(-40, -45)
base.hold()
# collect next 2 in catchment area
GyroTurn.turn(89)
base.reset()
frontClaw.goUp(wait = False)
backClaw.run_time(100, 1200, wait = False)
LineTrack.move(colLeft, 60, lambda: colRight.color() != Color.BLACK)
curr = rightMotor.angle()
LineTrack.move(colLeft, 60, lambda: rightMotor.angle() < 600 + curr, reset_I = False)
#gyro.reset_angle(0)
GyroStraightDeg.move(60, 730 + curr)
base.hold()
GyroTurn.turn(-89)
base.reset()
GyroStraightDeg.move(40, 215 )
base.hold()
frontClaw.goDown()
base.reset()
def depositBatteryFront(numCube):
base.reset()
if numCube == 4:
GyroStraightDeg.move(50, 140)
else:
GyroStraightDeg.move(50, 225)
base.hold()
wait(50)
frontClaw.goDown(-50, -300)
if numCube == 4:
GyroStraight.move(-10, lambda: colRight.color() != Color.BLACK and colLeft.color() != Color.BLACK)
else:
GyroStraight.move(-30, lambda: colRight.color() != Color.BLACK and colLeft.color() != Color.BLACK)
base.hold()
def depositBatteryBack(time):
global numBlue
PID_SingleMotorTurn(base, gyro, 179, 0.6, 1, maxSpeed = 50)
base.reset()
if time == 2 and numBlue == 4:
GyroStraightDeg.move(-40, -105, minSpeed = 20)
base.hold()
backClaw.run_target(-100, -100)
base.reset()
GyroStraightDeg.move(40, 85)
base.hold()
backClaw.run_target(50, 100)
base.reset()
GyroStraightDeg.move(-30, -15)
base.hold()
else:
GyroStraightDeg.move(-40, -175, minSpeed = 10)
base.hold()
backClaw.run_target(-100, -100)
base.reset()
GyroStraightDeg.move(40, 140)
base.hold()
def depositBattery(time, extraCol):
global numSurplus, numYellow, numBlue
base.reset()
if (time == 1 and extraCol != Color.GREEN and numSurplus == 0 and surplus != Color.GREEN):
frontClaw.goUp(wait = False)
base.reset()
LineTrack.move(colLeft, 70, lambda: rightMotor.angle() < 400)
LineTrack.move(colLeft, 30, lambda: colRight.color() != Color.BLACK, reset_I = False)
base.hold()
base.reset()
GyroStraightDeg.move(-40, -70)
base.hold()
PID_SingleMotorTurn(base, gyro, 89, 1, 0)
else:
if (extraCol == Color.YELLOW and time == 2) or (numSurplus != 0 and time == 1):
# raise claw if not raised at house 3
if numYellow == 4:
frontClaw.goUp(wait = False)
else:
# otherwise lower claw if raised at house 3
if numYellow == 2:
frontClaw.goDown(wait = False)
LineTrack.move(colLeft, 60, lambda: rightMotor.angle() < 400)
LineTrack.move(colLeft, 30, lambda: colRight.color() != Color.BLACK, reset_I = False)
base.hold()
if time == 1:
if numSurplus != 0:
depositBatteryFront(numSurplus)
if extraCol == Color.GREEN or (surplus == Color.GREEN and numSurplus == 0):
depositBatteryBack(time)
GyroTurn.turn(-89)
else:
# single motor turn to avoid hitting wall of battery area
base.reset()
GyroStraightDeg.move(-40, -65)
base.hold()
PID_SingleMotorTurn(base, gyro, 89, 1, 0)
else:
if extraCol == Color.YELLOW:
depositBatteryFront(numYellow)
numYellow -= 2
if extraCol == Color.BLUE or (surplus == Color.BLUE and numSurplus == 0):
depositBatteryBack(time)
numBlue -= 2
else:
base.reset()
GyroStraightDeg.move(-40, -65)
base.hold()
def getExtra():
cols = {Color.YELLOW: 0, Color.GREEN: 0, Color.BLUE: 0}
for house in Houses:
for col in house:
cols[col] += 1
for key in cols:
if cols[key] == 1:
return key
def checkHouse1():
base.reset()
GyroStraight.move(85, lambda: rightMotor.angle() < 200)
LineTrack.move(colRight, 80, lambda: colLeft.color() != Color.BLACK, side = -1)
curr = rightMotor.angle()
GyroStraightDeg.move(60, 320 + curr)
base.hold()
GyroTurn.turn(-89)
base.run_time(-100, 500)
gyro.reset_angle(0)
scanHouseEV3(Houses[0], target = 250)
base.hold()
PID_SingleMotorTurn(base, gyro, -179, 0.06, 1)
def returnHouse1():
global surplus
# if surplus == Color.YELLOW:
# base.reset()
# GyroStraightDeg.move(50, 30)
# base.hold()
if surplus == Color.BLUE:
GyroTurn.turn(-89)
else:
GyroTurn.turn(-89)
base.reset()
LineTrack.move(colRight, 85, lambda: colLeft.color() != Color.BLACK, side = -1)
curr = rightMotor.angle()
LineTrack.move(colRight, 85, lambda: rightMotor.angle() < 1250 + curr, side = -1, target = 1100 + curr, reset_I = False)
base.hold()
depositHouse(Houses[0], 1, 1)
# return to house 2 intersection
LineTrack.move(colRight, 80, lambda: colLeft.color() != Color.BLACK, side = -1)
LineTrack.move(colRight, 80, lambda: colLeft.color() != Color.WHITE, side = -1, reset_I = False)
def checkHouse2():
# scan house 2
if surplus == Color.BLUE and Color.GREEN not in Houses[0] and len(Houses[0]) != 1:
# if house 1 has nothing to be deposited, go to house 2 directly from blue surplus area
PID_SingleMotorTurn(base, gyro, 89, 0, 1)
base.reset()
GyroStraightDeg.move(-40, -50)
base.hold()
else:
if numSurplus == 4:
# only raise claw if it hasnt been raised at house 1
frontClaw.goUp(speed = 30, wait = False)
if surplus == Color.GREEN and Color.GREEN not in Houses[0] and len(Houses[0]) == 2:
LineTrack.move(colRight, 40, lambda: colLeft.color() != Color.BLACK, side = -1)
else:
LineTrack.move(colRight, 70, lambda: colLeft.color() != Color.BLACK, side = -1, reset_I = False)
curr = rightMotor.angle()
LineTrack.move(colRight, 40, lambda: rightMotor.angle() < 350 + curr, side = -1, target = 350 + curr, reset_I = False)
base.hold()
gyro.reset_angle(0)
wait(10)
PID_AngleOffSet(base, gyro, 77)
frontClaw.goDown()
scanHouseEV3(Houses[1], target = 250)
curr = rightMotor.angle()
GyroStraightDeg.move(50, 110 + curr)
base.hold()
# deposit at house 2
if Color.GREEN in Houses[1] or len(Houses[1]) == 1 or (surplus in Houses[1] and surplus != Color.YELLOW) :
depositHouse(Houses[1], 1, 2)
else:
PID_SingleMotorTurn(base, gyro, -180, 0.05, 1)
def checkHouse3():
# move to house 3
base.reset()
LineTrack.move(colLeft, 65, lambda: rightMotor.angle() < 500)
LineTrack.move(colLeft, 40, lambda: colRight.color() != Color.BLACK, reset_I = False)
curr = rightMotor.angle()
LineTrack.move(colLeft, 40, lambda: rightMotor.angle() < 105 + curr, reset_I = False)
base.hold()
GyroTurn.turn(89)
base.reset()
LineTrack.move(colRight, 60, lambda: rightMotor.angle() < 805)
base.hold()
GyroTurn.turn(-89)
base.reset()
GyroStraightDeg.move(-40, -60)
base.hold()
# scan house 3
PID_LineSquare(base, direction = -1)
gyro.reset_angle(0)
base.reset()
GyroStraightDeg.move(-85, -300)
base.hold()
base.reset()
scanHouseEV3(Houses[2])
curr = rightMotor.angle()
GyroStraightDeg.move(50, 150 + curr)
base.hold()
# deposit at house 3
if Color.GREEN in Houses[2] or len(Houses[2]) == 1:
depositHouse(Houses[2], 1, 3)
else:
GyroTurn.turn(-89)
GyroTurn.maxSpeed = 100
def returnBase():
curr = rightMotor.angle()
if Color.BLUE in Houses[0] or Color.YELLOW in Houses[0]:
LineTrack.move(colLeft, 80, lambda: rightMotor.angle() < 880 + curr, target = 880 + curr)
base.hold()
depositHouse(Houses[0], 2, 1)
frontClaw.goUp(wait = False, load = False)
backClaw.run_time(100, 500, wait = False)
base.run_time(-100, 500)
gyro.reset_angle(0)
GyroTurn.turn(89)
else:
frontClaw.goUp(wait = False, load = False)
backClaw.run_time(100, 500, wait = False)
LineTrack.move(colLeft, 80, lambda: colRight.color() != Color.BLACK)
base.hold()
GyroTurn.turn(-90)
leftMotor.run_time(CorrectSpeed(-92), 2000, wait = False)
rightMotor.run_time(CorrectSpeed(-100), 2000)
base.hold()
def main():
global surplus
global extraCol
base.reset()
checkHouse1()
# if yellow surplus is present, collect it
# move toward green energy
if checkSurplus(-180):
surplus = Color.YELLOW
collectSurplus(192, Color.YELLOW)
else:
PID_SingleMotorTurn(base, gyro, 179, 0.75, 1)
# collect green energy
collectGreen()
# collect green surplus if present, else go collect blue surplus
if surplus is None:
GyroTurn.turn(-89)
if checkSurplus(-155):
surplus = Color.GREEN
collectSurplus(25, Color.GREEN)
else:
surplus = Color.BLUE
collectSurplus(418, Color.BLUE)
# check whether to deposit in house 1
if Color.GREEN in Houses[0] or len(Houses[0]) == 1:
returnHouse1()
else:
# turn to face house 2 from green surplus area if not blue surplus
if surplus == Color.GREEN:
PID_SingleMotorTurn(base, gyro, 89, 1, 0.3)
elif surplus == Color.YELLOW:
base.reset()
GyroStraightDeg.move(50, 100)
base.hold()
GyroTurn.turn(89)
print(numSurplus)
checkHouse2()
if numSurplus == 0:
frontClaw.goUp(wait = False)
checkHouse3()
# based on houses, determine which energy is extra
extraCol = getExtra()
# always deposit two surplus into battery storage from claw, deposit any remaining green
depositBattery(1, extraCol)
# collect yellow and blue energy
collectYellow()
collectBlue()
# deposit at house 3 again
if Color.YELLOW in Houses[2] or Color.BLUE in Houses[2]:
# add condition to turn based on whether blue is in the house
depositHouse(Houses[2], 2, 3)
else:
GyroTurn.turn(-89)
depositBattery(2, extraCol)
# go back to house 2 if needed
if (Color.YELLOW in Houses[1]) or (Color.BLUE in Houses[1] and surplus != Color.BLUE):
if extraCol == Color.BLUE:
GyroTurn.turn(89)
else:
PID_SingleMotorTurn(base, gyro, -89, 0, 1)
base.reset()
LineTrack.move(colRight, 70, lambda: rightMotor.angle() < 750, side = -1, target = 700, accel = True)
base.hold()
depositHouse(Houses[1], 2, 2)
base.reset()
LineTrack.move(colLeft, 80, lambda: colRight.color() != Color.BLACK, accel = True)
LineTrack.move(colLeft, 80, lambda: colRight.color() != Color.WHITE, reset_I = False)
LineTrack.move(colLeft, 80, lambda: colRight.color() != Color.BLACK, reset_I = False)
LineTrack.move(colLeft, 80, lambda: colRight.color() != Color.WHITE, reset_I = False)
else:
if extraCol == Color.BLUE or (surplus == Color.BLUE and numSurplus == 0):
GyroTurn.turn(-89)