-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMicromouseCode.py
More file actions
1331 lines (1188 loc) · 48.6 KB
/
Copy pathMicromouseCode.py
File metadata and controls
1331 lines (1188 loc) · 48.6 KB
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
import RPi.GPIO as GPIO
import time
import math
import pigpio
import rotary_encoder
from rotary_encoder import decoder
from pid_c import PID
import numpy as np
import smbus
import sys
sys.path.insert(0, '/home/someone/Micromouse/VL53L0X_rasp_python/python')
import VL53L0X
import threading
global left
global right
global front
global desiredEncL
global desiredEncR
#sys.path.insert(0, '/home/someone/Micromouse/MinIMU-9-v5')
#from MinIMU_v5_pi import MinIMU_v5_pi
#gyro
# sys.path.insert(0, '/home/someone/Micromouse/mpu6050/mpu')
# from gyro import mpu6050
# gyroscope = mpu6050(0x68)
#IMU = MinIMU_v5_pi()
#Laser rage finder sensors, turning them on and off
sensor1_shutdown = 20 #left
sensor2_shutdown = 16 #middle
sensor3_shutdown = 21 #right
#the actual names of the sensors
tof1 = VL53L0X.VL53L0X(address=0x2B)
tof2 = VL53L0X.VL53L0X(address=0x2D)
tof3 = VL53L0X.VL53L0X(address=0x2E)
#sampling time (ms)
dt_target = 0.05
#motor speed control pid
Ps = 1.2
Is = 2
Ds = 0
#motor turning pid
Pt = 0.14 #0.12
It = 0.0001 #0.1
Dt = 0 #0.008
#motor move pid
Pm = 0.1 #0.12, 0.1
Im = 0.01 #0.1
Dm = 0 #0.0067
#motor sprint pid
Pv = 0.02 #0.12
Iv = 0.0014 #0.1
Dv = 0 #0.0067
#motor adjusting pid
Pa = 0.20 #0.25
Ia = 0.001
Da = 0
#motor hitting wall pid
Pw = 0.25
Iw = 0.01
Dw = 0
#more stuff for motor setup
output_L = 0
output_R = 0
thetaprev_L = 0
thetaprev_R = 0
taupid = 0.1
wcurr_L = 0
wcurr_R = 0
pos_L = 0
pos_R = 0
way_L = 0
way_R = 0
desiredEncL = 0
desiredEncR = 0
#sets up the PID
pid_L = PID(dt_target, Ps, Is, Ds, 100, -100, tau=taupid)
pid_R = PID(dt_target, Ps, Is, Ds, 100, -100, tau=taupid)
#pins for motor control
pin1 = 22
pin2 = 23
pin3 = 24
pin4 = 25
#for PWM
frequency = 100
#turning on GPIO pins and choosing the naming setup
pi = pigpio.pi()
GPIO.setmode(GPIO.BCM)
#Switch Pin
toggle = 26
def setup():
#IMU.trackYaw()
#IMU.trackAngle()
print('Starting program...')
#motor setup
GPIO.setup(pin1, GPIO.OUT)
GPIO.setup(pin2, GPIO.OUT)
GPIO.setup(pin3, GPIO.OUT)
GPIO.setup(pin4, GPIO.OUT)
GPIO.output(pin1, GPIO.LOW)
GPIO.output(pin2, GPIO.LOW)
GPIO.output(pin3, GPIO.LOW)
GPIO.output(pin4, GPIO.LOW)
global pwm1
global pwm2
global pwm3
global pwm4
pwm1 = GPIO.PWM(pin1, frequency)
pwm2 = GPIO.PWM(pin2, frequency)
pwm3 = GPIO.PWM(pin3, frequency)
pwm4 = GPIO.PWM(pin4, frequency)
pwm1.start(0)
pwm2.start(0)
pwm3.start(0)
pwm4.start(0)
#laser range finder setup
GPIO.setup(sensor1_shutdown, GPIO.OUT)
GPIO.setup(sensor2_shutdown, GPIO.OUT)
GPIO.setup(sensor3_shutdown, GPIO.OUT)
GPIO.output(sensor1_shutdown, GPIO.LOW)
GPIO.output(sensor2_shutdown, GPIO.LOW)
GPIO.output(sensor3_shutdown, GPIO.LOW)
#activating the laser range finders
time.sleep(0.5)
GPIO.output(sensor1_shutdown, GPIO.HIGH)
time.sleep(0.5)
tof1.start_ranging(VL53L0X.VL53L0X_BETTER_ACCURACY_MODE)
GPIO.output(sensor2_shutdown, GPIO.HIGH)
time.sleep(0.5)
tof2.start_ranging(VL53L0X.VL53L0X_BETTER_ACCURACY_MODE)
GPIO.output(sensor3_shutdown, GPIO.HIGH)
time.sleep(0.5)
tof3.start_ranging(VL53L0X.VL53L0X_BETTER_ACCURACY_MODE)
#starting the motor encoder
global decoder_L
global decoder_R
decoder_L = rotary_encoder.decoder(pi, 5, 6, callback_L)
decoder_R = rotary_encoder.decoder(pi, 12, 13, callback_R)
#Switch tings
GPIO.setup(toggle, GPIO.IN, pull_up_down = GPIO.PUD_UP)
def turnRight():
global pos_L, pos_R
resetMotor()
#setup PID for turning (dt is time stamp, Dt is derivative term)
turn_pid_L = PID(dt_target, Pt, It, Dt, 35, -35, tau=taupid)
turn_pid_R = PID(dt_target, Pt, It, Dt, 35, -35, tau=taupid)
#sets target values for the encoder
desiredEncL = pos_L + 220
desiredEncR = pos_R - 220
#timer stuff
prev_time = time.perf_counter() - dt_target
while True:
#more timer stuff
curr_time = time.perf_counter()
dt = curr_time - prev_time
prev_time = curr_time
#print(dt)
#determines the speed of the motor
outputL = turn_pid_L.control(desiredEncL, pos_L)
outputR = turn_pid_R.control(desiredEncR, pos_R)
#print(outputL)
#print(outputR)
motorMove(outputL, outputR, dt)
#motorMove(25, 25, dt)
time.sleep(dt_target)
#exits loop once desired turn is achieved
if(abs(desiredEncL - pos_L) < 6 and abs(pos_R - desiredEncR) < 6):
resetMotor()
#print("Broke out of while loop")
break
def turnLeft():
global pos_L, pos_R
resetMotor()
#setup PID for turning (dt is time stamp, Dt is derivative term)
turn_pid_L = PID(dt_target, Pt, It, Dt, 35, -35, tau=taupid)
turn_pid_R = PID(dt_target, Pt, It, Dt, 35, -35, tau=taupid)
#sets target values for the encoder
desiredEncL = pos_L - 220
desiredEncR = pos_R + 220
#timer stuff
prev_time = time.perf_counter() - dt_target
while True:
#more timer stuff
curr_time = time.perf_counter()
dt = curr_time - prev_time
prev_time = curr_time
#print(dt)
#determines the speed of the motor
outputL = turn_pid_L.control(desiredEncL, pos_L)
outputR = turn_pid_R.control(desiredEncR, pos_R)
#print(outputL)
#print(outputR)
motorMove(outputL, outputR, dt)
#motorMove(25, 25, dt)
time.sleep(dt_target)
#exits loop once desired turn is achieved
if(abs(desiredEncL - pos_L) < 6 and abs(pos_R - desiredEncR) < 6):
resetMotor()
#print("Broke out of while loop")
break
def turnVar(encChangeL, encChangeR):
global pos_L, pos_R
resetMotor()
#setup PID for turning (dt is time stamp, Dt is derivative term)
turn_pid_L = PID(dt_target, Pa, Ia, Da, 35, -35, tau=taupid)
turn_pid_R = PID(dt_target, Pa, Ia, Da, 35, -35, tau=taupid)
#sets target values for the encoder
desiredEncL = pos_L - encChangeL
desiredEncR = pos_R - encChangeR
#timer stuff
prev_time = time.perf_counter() - dt_target
while True:
#more timer stuff
curr_time = time.perf_counter()
dt = curr_time - prev_time
prev_time = curr_time
#print(dt)
#determines the speed of the motor
outputL = turn_pid_L.control(desiredEncL, pos_L)
outputR = turn_pid_R.control(desiredEncR, pos_R)
#print(outputL)
#print(outputR)
motorMove(outputL, outputR, dt)
#motorMove(25, 25, dt)
time.sleep(dt_target)
#exits loop once desired turn is achieved
if(abs(desiredEncL - pos_L) < 5 and abs(pos_R - desiredEncR) < 5):
resetMotor()
#print("Broke out of while loop")
break
def forward1():
global pos_L, pos_R
global desiredEncL
global desiredEndR
resetMotor()
#setup PID for motor moving
move_pid_L = PID(dt_target, Pm, Im, Dm, 35, -35, tau=taupid)
move_pid_R = PID(dt_target, Pm, Im, Dm, 35, -35, tau=taupid)
#sets target values for the encoder
desiredEncL = pos_L + 640
desiredEncR = pos_R + 640
#timer stuff
prev_time = time.perf_counter() - dt_target
while True:
EncL = int(mouse1.autoAdjustL(desiredEncL))
EncR = int(mouse1.autoAdjustR(desiredEncR))
desiredEncL += EncL
desiredEncR += EncR
desiredEncL -= EncR
desiredEncR -= EncL
#more timer stuff
curr_time = time.perf_counter()
dt = curr_time - prev_time
prev_time = curr_time
#print(dt)
#determines the speed of the motor
outputL = move_pid_L.control(desiredEncL, pos_L)
outputR = move_pid_R.control(desiredEncR, pos_R)
#print(outputL)
#print(outputR)
motorMove(outputL, outputR, dt)
#motorMove(25, 25, dt)
time.sleep(dt_target)
#exits loop once desired turn is achieved
if((abs(desiredEncL - pos_L) < 6 and abs(pos_R - desiredEncR) < 6)): #or front <= 65
resetMotor()
#print("Broke out of while loop")
break
def forwardVar(cells):
global pos_L, pos_R
global desiredEncL
global desiredEndR
resetMotor()
#setup PID for motor moving
sprint_pid_L = PID(dt_target, Pv, Iv, Dv, 35, -35, tau=taupid)
sprint_pid_R = PID(dt_target, Pv, Iv, Dv, 35, -35, tau=taupid)
#sets target values for the encoder
desiredEncL = pos_L + 637 * cells
desiredEncR = pos_R + 637 * cells
EncL = int(mouse1.autoAdjustL(desiredEncL))
EncR = int(mouse1.autoAdjustR(desiredEncR))
desiredEncL += EncL
desiredEncR += EncR
desiredEncL -= EncR
desiredEncR -= EncL
#timer stuff
prev_time = time.perf_counter() - dt_target
while True:
#more timer stuff
curr_time = time.perf_counter()
dt = curr_time - prev_time
prev_time = curr_time
#print(dt)
#determines the speed of the motor
outputL = sprint_pid_L.control(desiredEncL, pos_L)
outputR = sprint_pid_R.control(desiredEncR, pos_R)
#print(outputL)
#print(outputR)
motorMove(outputL, outputR, dt)
#motorMove(25, 25, dt)
time.sleep(dt_target)
#exits loop once desired turn is achieved
if((abs(desiredEncL - pos_L) < 6 and abs(pos_R - desiredEncR) < 6)): #or front <= 65
resetMotor()
#print("Broke out of while loop")
break
def hitWall(encChangeL, encChangeR):
global pos_L, pos_R
resetMotor()
#setup PID for turning (dt is time stamp, Dt is derivative term)
turn_pid_L = PID(dt_target, Pw, Iw, Dw, 35, -35, tau=taupid)
turn_pid_R = PID(dt_target, Pw, Iw, Dw, 35, -35, tau=taupid)
#sets target values for the encoder
desiredEncL = pos_L - encChangeL
desiredEncR = pos_R - encChangeR
#timer stuff
prev_time = time.perf_counter() - dt_target
while True:
#more timer stuff
curr_time = time.perf_counter()
dt = curr_time - prev_time
prev_time = curr_time
#print(dt)
#determines the speed of the motor
outputL = turn_pid_L.control(desiredEncL, pos_L)
outputR = turn_pid_R.control(desiredEncR, pos_R)
#print(outputL)
#print(outputR)
motorMove(outputL, outputR, dt)
#motorMove(25, 25, dt)
time.sleep(dt_target)
#exits loop once desired turn is achieved
if(abs(desiredEncL - pos_L) < 10 and abs(pos_R - desiredEncR) < 10):
resetMotor()
#print("Broke out of while loop")
break
def motorMove(leftMotorSpeed, rightMotorSpeed, dt):
#print("m")
global thetaprev_L, thetaprev_R
global pos_L, pos_R
global pid_L, pid_R
thetacurr_L = pos_L
thetacurr_R = pos_R
dthetal = thetacurr_L - thetaprev_L
dthetar = thetacurr_R - thetaprev_R
#pi * D dtheta/450 / dt/1000
#mm/sec
wcurr_L = (np.pi * 3.8) * (dthetal / 450) / (dt)
wcurr_R = (np.pi * 3.8) * (dthetar / 450) / (dt)
thetaprev_L = thetacurr_L
thetaprev_R = thetacurr_R
print(wcurr_L)
#print(SpeedL - wcurr_L)
#print(wcurr_R)
#print(SpeedR - wcurr_R)
#determines how much to change motor speed in order for the actual speed to be equal to the desired speed
output_L = pid_L.controlSpeed(leftMotorSpeed, wcurr_L)
output_R = pid_R.controlSpeed(rightMotorSpeed, wcurr_R)
#print(output_L)
#print(output_R)
#changes the motor speed
if(output_L > 0):
pwm2.ChangeDutyCycle(0)
pwm1.ChangeDutyCycle(output_L)
elif(output_L < 0):
pwm1.ChangeDutyCycle(0)
pwm2.ChangeDutyCycle(-output_L)
else:
pwm1.ChangeDutyCycle(0)
pwm2.ChangeDutyCycle(0)
if(output_R > 0):
pwm4.ChangeDutyCycle(0)
pwm3.ChangeDutyCycle(output_R)
elif(output_R < 0):
pwm3.ChangeDutyCycle(0)
pwm4.ChangeDutyCycle(-output_R)
else:
pwm3.ChangeDutyCycle(0)
pwm4.ChangeDutyCycle(0)
def resetMotor():
#print("motors reset")
#stop all motors
pwm1.ChangeDutyCycle(0)
pwm2.ChangeDutyCycle(0)
pwm3.ChangeDutyCycle(0)
pwm4.ChangeDutyCycle(0)
global thetaprev_L, thetaprev_R
global pos_L, pos_R
global pid_L, pid_R
#reset variable for motor speed calc
thetaprev_L = 0
thetaprev_R = 0
#reset encoder values (not rly necessary)
pos_L = 0
pos_R = 0
#reset PID for motor speed
pid_L = PID(dt_target, Ps, Is, Ds, 100, -100, tau=taupid)
pid_R = PID(dt_target, Ps, Is, Ds, 100, -100, tau=taupid)
#functions for getting the encoder values
def callback_L(way_L):
global pos_L
pos_L += way_L
#print("L={}".format(pos_L))
def callback_R(way_R):
global pos_R
pos_R += way_R
#print("R={}".format(pos_R))
#function to get the values from the laser range sensors
def tof():
global left
global right
global front
timing = tof1.get_timing()
if (timing < 20000):
timing = 20000
left = tof1.get_distance()
front = tof2.get_distance()
right = tof3.get_distance()
time.sleep(timing/1000000.00)
#stop everything
def destroy():
pwm1.stop()
pwm2.stop()
pwm3.stop()
pwm4.stop()
tof1.stop_ranging()
tof2.stop_ranging()
tof3.stop_ranging()
decoder_L.cancel()
decoder_R.cancel()
GPIO.cleanup()
pi.stop()
global override
override = False
class Cell:
def __init__(self, cols, rows): # constructor
self.cols = cols
self.rows = rows
self.value = 99
self.wallN = False
self.wallS = False
self.wallE = False
self.wallW = False
self.onBestPath = False
def setOnBestPath(self): # makes something on the best path
self.onBestPath = True
def setWall(self, direction): # makes a wall at specified direction
if direction == "N":
self.wallN = True
if direction == "S":
self.wallS = True
if direction == "E":
self.wallE = True
if direction == "W":
self.wallW = True
def updateWalls(self): # updates the walls of the cell with the walls of adjacent cells
if self.cols < cols - 1:
if array_2d[self.rows][self.cols + 1].wallW:
self.wallE = True
if self.cols > 0:
if array_2d[self.rows][self.cols - 1].wallE:
self.wallW = True
if self.rows < rows - 1:
if array_2d[self.rows + 1][self.cols].wallN:
self.wallS = True
if self.rows > 0:
if array_2d[self.rows - 1][self.cols].wallS:
self.wallN = True
def setVal(self, value): # sets the value(cells from center) of the cell
self.value = value
def updateVal(self): # updates the value of the cell with the value of adjacent cells
if self.cols < cols - 1:
if array_2d[self.rows][self.cols + 1].value + 1 < self.value and not self.wallE:
self.value = array_2d[self.rows][self.cols + 1].value + 1
"""
if the cell to the right of the cell's value + 1 is less than the
current cell's value, then it sets the current cell's value to the
right cell's value + 1
"""
if self.cols > 0:
if array_2d[self.rows][self.cols - 1].value + 1 < self.value and not self.wallW:
self.value = array_2d[self.rows][self.cols - 1].value + 1
if self.rows < rows - 1:
if array_2d[self.rows + 1][self.cols].value + 1 < self.value and not self.wallS:
self.value = array_2d[self.rows + 1][self.cols].value + 1
if self.rows > 0:
if array_2d[self.rows - 1][self.cols].value + 1 < self.value and not self.wallN:
self.value = array_2d[self.rows - 1][self.cols].value + 1
# setting up base values and the array itself
# rows = 16
# cols = 16
rows, cols = 8, 8
array_2d = [[Cell(j, i) for j in range(cols)] for i in range(rows)]
# array_2d[8][8].setval(0)
array_2d[4][4].setVal(0)
def printArrayVals(): # prints the array with the values of the cells accounting for if it is on the best path or not and if there are walls in a specified direction
print("\n\n")
for i in range (rows):
print("[ ", end = "")
for j in range (cols):
if(array_2d[i][j].value < 10):
if(array_2d[i][j].wallE and j < 15):
if(array_2d[i][j].wallS and i < 15):
if(array_2d[i][j].onBestPath):
print("\x1b[35m" + "\x1B[4m" + str(array_2d[i][j].value) + "\x1B[0m", end = "\x1B[4m" + " " + "\x1B[0m" + "|")
else:
print("\x1B[4m" + str(array_2d[i][j].value) + "\x1B[0m", end = "\x1B[4m" + " " + "\x1B[0m" + "|")
else:
if(array_2d[i][j].onBestPath):
print("\x1b[35m" + str(array_2d[i][j].value) + "\x1B[0m", end = " |")
else:
print(array_2d[i][j].value, end = " |")
else:
if(array_2d[i][j].wallS and i < 15):
if(array_2d[i][j].onBestPath):
print("\x1b[35m" + "\x1B[4m" + str(array_2d[i][j].value) + "\x1B[0m", end = "\x1B[4m" + " " + "\x1B[0m" + " ")
else:
print("\x1B[4m" + str(array_2d[i][j].value) + "\x1B[0m", end = "\x1B[4m" + " " + "\x1B[0m" + " ")
else:
if(array_2d[i][j].onBestPath):
print("\x1b[35m" + str(array_2d[i][j].value) + "\x1B[0m", end = " ")
else:
print(array_2d[i][j].value, end = " ")
else:
if(array_2d[i][j].wallE and j < 15):
if(array_2d[i][j].wallS and i < 15):
if(array_2d[i][j].onBestPath):
print("\x1b[35m" + "\x1B[4m" + str(array_2d[i][j].value) + "\x1B[0m", end = "|")
else:
print("\x1B[4m" + str(array_2d[i][j].value) + "\x1B[0m", end = "|")
else:
if(array_2d[i][j].onBestPath):
print("\x1b[35m" + str(array_2d[i][j].value) + "\x1B[0m", end = "|")
else:
print(array_2d[i][j].value, end = "|")
else:
if(array_2d[i][j].wallS and i < 15):
if(array_2d[i][j].onBestPath):
print("\x1b[35m" + "\x1B[4m" + str(array_2d[i][j].value) + "\x1B[0m", end = " ")
else:
print("\x1B[4m" + str(array_2d[i][j].value) + "\x1B[0m", end = " ")
else:
if(array_2d[i][j].onBestPath):
print("\x1b[35m" + str(array_2d[i][j].value) + "\x1B[0m", end = " ")
else:
print(array_2d[i][j].value, end = " ")
print ("]")
print("^^^")
def findDistance(self): # finds the distnce of the furthest cell in a a straight line that is on the best path and moves the mouse to that cell
if(self.direction == "E"):
val = 0
for i in range (1, cols):
if(self.col < cols - i and self.maze[self.row][self.col + i].onBestPath and not self.maze[self.row][self.col + (i - 1)].wallE):
val += 1
else:
break
if(val == 1 and diag):
print("diag")
diagonal(self)
else:
self.moveForward(val)
if(self.direction == "W"):
val = 0
for i in range (1, cols):
if(self.col > i - 1 and self.maze[self.row][self.col - i].onBestPath and not self.maze[self.row][self.col - (i - 1)].wallW):
val += 1
else:
break
if(val == 1 and diag):
print("diag")
diagonal(self)
else:
self.moveForward(val)
if(self.direction == "N"):
val = 0
for i in range (1, rows):
if(self.row > i - i and self.maze[self.row - i][self.col].onBestPath and not self.maze[self.row - (i - 1)][self.col].wallN):
val += 1
else:
break
if(val == 1 and diag):
print("diag")
diagonal(self)
else:
self.moveForward(val)
if(self.direction == "S"):
val = 0
for i in range (1, rows):
if(self.row < rows - i and self.maze[self.row + i][self.col].onBestPath and not self.maze[self.row + (i - 1)][self.col].wallS):
val += 1
else:
break
if(val == 1 and diag):
print("diag")
diagonal(self)
else:
self.moveForward(val)
def findDistanceDiag(self): # finds the distnce of the furthest cell in a a diagonal line that is on the best path and moves the mouse to that cell
print(self.direction)
if(self.direction == "E"):
val = 0
for i in range (1, cols):
if(self.col < cols - i and self.maze[self.row][self.col + i].onBestPath and not self.maze[self.row][self.col + (i - 1)].wallE):
val += 1
else:
turn45()
break
if(val == 1 and diag):
print("diag (fake)")
return True
else:
return False
if(self.direction == "W"):
val = 0
for i in range (1, cols):
if(self.col > i - 1 and self.maze[self.row][self.col - i].onBestPath and not self.maze[self.row][self.col - (i - 1)].wallW):
val += 1
else:
turn45()
break
if(val == 1 and diag):
print("diag (fake)")
return True
else:
return False
if(self.direction == "N"):
val = 0
for i in range (1, rows):
if(self.row > i - i and self.maze[self.row - i][self.col].onBestPath and not self.maze[self.row - (i - 1)][self.col].wallN):
val += 1
else:
turn45()
break
if(val == 1 and diag):
print("diag (fake)")
return True
else:
return False
if(self.direction == "S"):
val = 0
for i in range (1, rows):
if(self.row < rows - i and self.maze[self.row + i][self.col].onBestPath and not self.maze[self.row + (i - 1)][self.col].wallS):
val += 1
else:
turn45()
break
if(val == 1 and diag):
print("diag (fake)")
return True
else:
return False
def diagonal(self): # moves the mouse diagonally to the end of the diagonal line
next = True
while(next):
next = mouse1.followBestPath(False)
def updateArrayVals(): # updates the values of the cells in the array accounting for new walls
for s in range (99):
for i in range (rows):
for j in range (cols):
array_2d[i][j].updateVal()
def resetArrayVals(): # resets the values of the cells in the array (called in conjunction with updateArrayVals())
global override
for i in range (rows):
for j in range (cols):
array_2d[i][j].setVal(99)
if(not override):
# array_2d[8][8].setVal(0)
array_2d[4][4].setVal(0)
else:
array_2d[0][0].setVal(0)
def updateArrayWalls(): # updates the walls of the cells in the array
for i in range (rows):
for j in range (cols):
array_2d[i][j].updateWalls()
def findBestPath(rowS, colS): # finds the best path from the mouse's current position to the center using the cell values(does not account for diagonal turns being slightly faster despite more distance)
resetBestPath()
rowsC = rowS
colsC = colS
currentCell = array_2d[rowsC][colsC]
for i in range (99):
currentCell.setOnBestPath()
if(colsC < cols - 1):
if(array_2d[rowsC][colsC + 1].value == currentCell.value - 1 and not currentCell.wallE):
currentCell = array_2d[rowsC][colsC + 1]
if(colsC > 0):
if(array_2d[rowsC][colsC - 1].value == currentCell.value - 1 and not currentCell.wallW):
currentCell = array_2d[rowsC][colsC - 1]
if(rowsC < rows - 1):
if(array_2d[rowsC + 1][colsC].value == currentCell.value - 1 and not currentCell.wallS):
currentCell = array_2d[rowsC + 1][colsC]
if(rowsC > 0):
if(array_2d[rowsC - 1][colsC].value == currentCell.value - 1 and not currentCell.wallN):
currentCell = array_2d[rowsC - 1][colsC]
rowsC = currentCell.rows
colsC = currentCell.cols
def resetBestPath(): # resets the best path of the cells in the array (called in conjunction with findBestPath())
for i in range (rows):
for j in range (cols):
array_2d[i][j].onBestPath = False
class Mouse: # defines the class of mouse and its base values and methods
def __init__(self, maze):
self.direction = "E"
self.row = 0
self.col = 0
self.wallN = False
self.wallS = False
self.wallE = False
self.wallW = False
self.wallF = False
self.wallR = False
self.wallL = False
self.maze = maze
self.currentCell = self.maze[self.row][self.col]
def turn90(self): # turns the mouse 90 degrees to the right
turnRight()
if(self.direction == "N"):
self.direction = "E"
return
if(self.direction == "E"):
self.direction = "S"
return
if(self.direction == "S"):
self.direction = "W"
return
if(self.direction == "W"):
self.direction = "N"
return
def turnNeg90(self): # turns the mouse 90 degrees to the left
turnLeft()
if(self.direction == "N"):
self.direction = "W"
return
if(self.direction == "W"):
self.direction = "S"
return
if(self.direction == "S"):
self.direction = "E"
return
if(self.direction == "E"):
self.direction = "N"
return
def moveForward1(self): # moves the mouse forward a specified amount of cells
forward1()
if(self.direction == "N"):
self.row -= 1
if(self.direction == "S"):
self.row += 1
if(self.direction == "E"):
self.col += 1
if(self.direction == "W"):
self.col -= 1
self.currentCell = self.maze[self.row][self.col]
def moveForward(self, amount): # moves the mouse forward a specified amount of cells
forwardVar(amount)
if(self.direction == "N"):
self.row -= amount
if(self.direction == "S"):
self.row += amount
if(self.direction == "E"):
self.col += amount
if(self.direction == "W"):
self.col -= amount
self.currentCell = self.maze[self.row][self.col]
def movePosForward(self, amount): # moves the mouse's position forward a specified amount of cells (used for diagonal movement)
if(self.direction == "N"):
self.row -= amount
if(self.direction == "S"):
self.row += amount
if(self.direction == "E"):
self.col += amount
if(self.direction == "W"):
self.col -= amount
self.currentCell = self.maze[self.row][self.col]
def detectWalls(self): # detects the walls around the mouse
global left
global right
global front
tof()
if(right < 150):
self.wallR = True
if(left < 150):
self.wallL = True
if(front < 150):
self.wallF = True
mouse1.updateWalls()
self.wallF = False
self.wallR = False
self.wallL = False
def updateWalls(self): # updates the walls of the cell the mouse is on with the walls the mouse detects
if(self.wallF):
self.currentCell.setWall(self.direction)
updateArrayWalls()
resetArrayVals()
updateArrayVals()
findBestPath(self.row, self.col)
if(self.wallR):
if(self.direction == "N"):
self.currentCell.setWall("E")
if(self.direction == "W"):
self.currentCell.setWall("N")
if(self.direction == "S"):
self.currentCell.setWall("W")
if(self.direction == "E"):
self.currentCell.setWall("S")
updateArrayWalls()
resetArrayVals()
updateArrayVals()
findBestPath(self.row, self.col)
if(self.wallL):
if(self.direction == "N"):
self.currentCell.setWall("W")
if(self.direction == "W"):
self.currentCell.setWall("S")
if(self.direction == "S"):
self.currentCell.setWall("E")
if(self.direction == "E"):
self.currentCell.setWall("N")
updateArrayWalls()
resetArrayVals()
updateArrayVals()
findBestPath(self.row, self.col)
def autoAdjustL(self, EncL):
if(left < 50 and EncL < 630):
return "1"
return "0"
def autoAdjustR(self, EncR):
if(right < 50 and EncR < 630):
return "1"
return "0"
def autoAdjustOnWall(self):
if(((left > 70 and left < 120) or left < 60) and ((right > 75 and right < 120) or right < 50)):
turnVar((left - 64)/2, (right - 61)/2)
def hitDaWall(self):
if(front < 90 and front > 30):
turnVar(-80, -80)
def followBestPath(self, real): # follows the best path from the mouse's current position to the center using the cell values(has different modes for mapping and diagonal movement)
findBestPath(self.row, self.col)
mouse1.detectWalls()
updateArrayWalls()
if(real):
self.hitDaWall()
if(mapping):
if(self.direction == "E"):
if(self.col < cols - 1 and self.maze[self.row][self.col + 1].onBestPath and not self.maze[self.row][self.col].wallE):
self.autoAdjustOnWall()
self.moveForward1()
return
elif(self.row < rows - 1 and self.maze[self.row + 1][self.col].onBestPath and not self.maze[self.row][self.col].wallS):
self.turn90()
self.moveForward1()
return
elif(self.row > 0 and self.maze[self.row - 1][self.col].onBestPath and not self.maze[self.row][self.col].wallN):
self.turnNeg90()
self.moveForward1()
return
elif(self.col > 0 and self.maze[self.row][self.col - 1].onBestPath and not self.maze[self.row][self.col].wallW):
if(left < 61):
self.turn90()
time.sleep(0.5)
self.turn90()
else:
self.turnNeg90()
time.sleep(0.5)
self.turnNeg90()
self.moveForward1()
return
else:
findBestPath(self.row, self.col)
if(self.direction == "W"):
if(self.col > 0 and self.maze[self.row][self.col - 1].onBestPath and not self.maze[self.row][self.col].wallW):
self.autoAdjustOnWall()
self.moveForward1()
return
elif(self.row < rows - 1 and self.maze[self.row + 1][self.col].onBestPath and not self.maze[self.row][self.col].wallS):
self.turnNeg90()