forked from GO-Robot-FLL/Python-for-Spike-Prime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
1062 lines (823 loc) · 43.5 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
# LEGO type:standard slot:1 autostart
import math
from spike import PrimeHub, Motor, MotorPair, ColorSensor
from spike.control import wait_for_seconds, Timer
from hub import battery
hub = PrimeHub()
import hub as hub2
import sys
#Preperation for parallel code execution
accelerate = True
run_generator = True
runSmall = True
lastAngle = 0
oldAngle = 0
gyroValue = 0
# Create your objects here.
hub = PrimeHub()
#PID value Definition
pRegler = 0.0
iRegler = 0.0
dRegler = 0.0
pReglerLight = 0.0
iReglerLight = 0.0
dReglerLight = 0.0
"""
Initialize color Sensors
left sensor: port F
right sensor: port E
"""
colorE = ColorSensor('E') #adjust the sensor ports until they match your configuration, we recommend assigning your ports to the ones in the program for ease of use
colorF = ColorSensor('F')
smallMotorA = Motor('A')
smallMotorD = Motor('D')
#Set variables based on robot
circumference = 17.6 #circumference of the wheel powered by the robot in cm
sensordistance = 7 #distance between the two light sensors in cm. Used in Tangent alignment 6.4 in studs
cancel = False
inMain = True
class DriveBase:
def __init__(self, hub, leftMotor, rightMotor):
self.hub = hub
self.leftMotor = Motor(leftMotor)
self.rightMotor = Motor(rightMotor)
self.movement_motors = MotorPair(leftMotor, rightMotor)
def lineFollower(self, distance, startspeed, maxspeed, endspeed, sensorPort, side, addspeed = 0.2, brakeStart = 0.7 , stopMethod=None, generator = None, stop = True):
"""
This is the function used to let the robot follow a line until either the entered distance has been achieved or the other sensor of the robot senses a line.
Like all functions that drive the robot this function has linear acceleration and breaking. It also uses PID values that are automatically set depending on the
current speed of the robot (See function PIDCalculationLight)
Parameters
-------------
distance: The value tells the program the distance the robot has to drive. Type: Integer. Default: No default value
speed: The speed which the robot is supposed to start at. Type: Integer. Default: No default value
maxspeed: The highest speed at which the robot drives. Type: Integer. Default: No default value
endspeed: The speed which the robot achieves at the end of the function. Type: Integer. Default: No default value
addspeed: The percentage after which the robot reaches its maxspeed. Type: Float. Default: No default value
brakeStart: The value which we use to tell the robot after what percentage of the distance we need to slow down. Type: Float. Default: No default value
stopMethod: the Stopmethod the robot uses to stop. If no stopMethod is passed stopDistance is used instead. Default: stopDistance
generator: the generator that runs something parallel while driving. Default: No default value
stop: the boolean that determines whether the robot should stop the motors after driving or not. Default: True
"""
if cancel:
return
global run_generator, runSmall
if generator == None:
run_generator = False
#set the speed the robot starts at
speed = startspeed
#reset PID values to eliminate bugs
change = 0
old_change = 0
integral = 0
#reset the driven distance of the robot to eliminate bugs
#specifies the color sensor
colorsensor = ColorSensor(sensorPort)
#Get degrees of motors turned before robot has moved, allows for distance calculation without resetting motors
loop = True
#Going backwards is not supported on our robot due to the motors then being in front of the colour sensors and the program not working
if distance < 0:
print('ERR: distance < 0')
distance = abs(distance)
#Calculate target values for the motors to turn to
finalDistance = (distance / 17.6) * 360
#Calculate after what distance the robot has to reach max speed
accelerateDistance = finalDistance * addspeed
deccelerateDistance = finalDistance * (1 - brakeStart)
invert = 1
#Calculation of steering factor, depending on which side of the line we are on
if side == "left":
invert = 1
elif side == "right":
invert = -1
#Calculation of the start of the robot slowing down
self.left_Startvalue = self.leftMotor.get_degrees_counted()
self.right_Startvalue = self.rightMotor.get_degrees_counted()
drivenDistance = getDrivenDistance(self)
brakeStartValue = brakeStart * finalDistance
while loop:
if cancel:
print("cancel")
break
if run_generator: #run parallel code execution
next(generator)
#Checks the driven distance as an average of both motors for increased accuracy
oldDrivenDistance = drivenDistance
drivenDistance = getDrivenDistance(self)
#Calculates target value for Robot as the edge of black and white lines
old_change = change
change = colorsensor.get_reflected_light() - 50
#Steering factor calculation using PID, sets new I value
steering = (((change * pReglerLight) + (integral * iReglerLight) + (dReglerLight * (change - old_change)))) * invert
integral = change + integral
#Calculation of current speed for robot, used for acceleratiion, braking etc.
speed = speedCalculation(speed, startspeed, maxspeed, endspeed, accelerateDistance, deccelerateDistance, brakeStartValue, drivenDistance, oldDrivenDistance)
pidCalculationLight(speed)
#PID value updates
steering = max(-100, min(steering, 100))
#Driving using speed values calculated with PID and acceleration for steering, use of distance check
self.movement_motors.start_at_power(int(speed), int(steering))
if stopMethod != None:
if stopMethod.loop():
loop = False
else:
if finalDistance < drivenDistance:
break
if stop:
self.movement_motors.stop()
run_generator = True
runSmall = True
generator = 0
return
def gyroRotation(self, angle, startspeed, maxspeed, endspeed, addspeed = 0.3, brakeStart = 0.7, rotate_mode = 0, stopMethod = None, generator = None, stop = True):
"""
This is the function that we use to make the robot turn the length of a specific angle or for the robot to turn until it senses a line. Even in this function the robot
can accelerate and slow down. It also has Gyrosensor calibrations based on our experimental experience.
Parameters
-------------
angle: The angle which the robot is supposed to turn. Use negative numbers to turn counterclockwise. Type: Integer. Default value: No default value
startspeed: The speed which the robot is supposed to start at. Type: Integer. Default: No default value
maxspeed: The highest speed at which the robot drives. Type: Integer. Default: No default value
endspeed: The speed which the robot achieves at the end of the function. Type: Integer. Default: No default value
addspeed: The percentage after which the robot reaches the maxspeed. Type: Float. Default: No default value
brakeStart: The percentage after which the robot starts slowing down until it reaches endspeed. Type: Float. Default: No default value
rotate_mode: Different turning types. 0: Both motors turn, robot turns on the spot. 1: Only the outer motor turns, resulting in a corner. Type: Integer. Default: 0
stopMethod: the Stopmethod the robot uses to stop. If no stopMethod is passed stopDistance is used instead. Default: stopDistance
generator: the generator that runs something parallel while driving. Default: No default value
stop: the boolean that determines whether the robot should stop the motors after driving or not. Default: True
"""
if cancel:
return
global run_generator, runSmall
if generator == None:
run_generator = False
if rotate_mode == 0:
startspeed = abs(startspeed)
maxspeed = abs(maxspeed)
endspeed = abs(endspeed)
speed = startspeed
#set standard variables
rotatedDistance = 0
steering = 1
accelerateDistance = abs(angle * addspeed)
deccelerateDistance = abs(angle * (1 - brakeStart))
#gyro sensor calibration
angle = angle * (2400/2443) #experimental value based on 20 rotations of the robot
#Setting variables based on inputs
loop = True
gyroStartValue = getGyroValue() #Yaw angle used due to orientation of the self.hub. This might need to be changed
brakeStartValue = (angle + gyroStartValue) * brakeStart
#Inversion of steering value for turning counter clockwise
if angle < 0:
steering = -1
#Testing to see if turining is necessary, turns until loop = False
while loop:
if cancel:
break
if run_generator: #run parallel code execution
next(generator)
oldRotatedDistance = rotatedDistance
rotatedDistance = getGyroValue() #Yaw angle used due to orientation of the self.hub. This might need to be changed
speed = speedCalculation(speed, startspeed, maxspeed, endspeed, accelerateDistance, deccelerateDistance, brakeStartValue, abs(1), abs(0))
#Checking for variants
#Both Motors turn, robot moves on the spot
if rotate_mode == 0:
self.movement_motors.start_tank_at_power(int(speed) * steering, -int(speed) * steering)
#Only outer motor turns, robot has a wide turning radius
elif rotate_mode == 1:
if angle * speed > 0:
self.leftMotor.start_at_power(- int(speed))
else:
self.rightMotor.start_at_power(+ int(speed))
if stopMethod != None:
if stopMethod.loop():
loop = False
break
elif abs(angle) <= abs(rotatedDistance - gyroStartValue):
loop = False
break
#Stops movement motors for increased accuracy while stopping
if stop:
self.movement_motors.stop()
run_generator = True
runSmall = True
return # End of gyroStraightDrive
def gyroStraightDrive(self, distance, startspeed, maxspeed, endspeed, addspeed = 0.3, brakeStart = 0.7, stopMethod=None, offset = 0, generator = None, stop = True):
"""
This is the function that we use to make the robot go forwards or backwards without drifting. It can accelerate, it can slow down and there's also PID. You can set the values
in a way where you can either drive until the entered distance has been achieved or until the robot senses a line.
Parameters
-------------
distance: the distance that the robot is supposed to drive. Type: Integer. Default: No default value
speed: The speed which the robot is supposed to start at. Type: Integer. Default: No default value
maxspeed: The highest speed at which the robot drives. Type: Integer. Default: No default value
endspeed: The speed which the robot achieves at the end of the function. Type: Integer. Default: No default value
addspeed: The speed which the robot adds in order to accelerate. Type: Float. Default: 0.2
brakeStart: The value which we use to tell the robot after what percentage of the distance we need to slow down. Type: Float. Default: 0.8
port: This value tells the program whether the robot is supposed to check for a black line with the specified light snsor. Type: String. Default: 0
lightValue: This value tells the program the value the robot should stop at if port sees it. Type: Integer. Default: 0
align_variant: Tells the robot to align itself to a line if it sees one. 0: No alignment. 1: standard alignment. 2: tangent based alignment Type: Integer. Default: 0
detectLineStart: The value which we use to tell the robot after what percentage of the distance we need to look for the line to drive to. Type: Float. Default: 0
offset: The value sends the robot in a direction which is indicated by the value entered. Type: Integer. Default: 0
generator: Function executed while robot is executing gyroStraightDrive. Write the wanted function and its parameters here. Type: . Default: 0
stopMethod: the Stopmethod the robot uses to stop. If no stopMethod is passed stopDistance is used instead. Default: stopDistance
generator: the generator that runs something parallel while driving. Default: No default value
stop: the boolean that determines whether the robot should stop the motors after driving or not. Default: True
"""
if cancel:
return
global run_generator, runSmall
global pRegler, iRegler, dRegler
if generator == None:
run_generator = False
#Set starting speed of robot
speed = startspeed
#Sets PID values
change = 0
old_change = 0
integral = 0
steeringSum = 0
invert = -1
#Sets values based on user inputs
loop = True
gyroStartValue = getGyroValue()
#Error check for distance
if distance < 0:
print('ERR: distance < 0')
distance = abs(distance)
#Calulation of degrees the motors should turn to
#17.6 is wheel circumference in cm. You might need to adapt it
rotateDistance = (distance / 17.6) * 360
accelerateDistance = rotateDistance * addspeed
deccelerateDistance = rotateDistance * (1 - brakeStart)
#Inversion of target rotation value for negative values
if speed < 0:
invert = 1
#Calculation of braking point
self.left_Startvalue = self.leftMotor.get_degrees_counted()
self.right_Startvalue = self.rightMotor.get_degrees_counted()
brakeStartValue = brakeStart * rotateDistance
drivenDistance = getDrivenDistance(self)
while loop:
if cancel:
break
if run_generator: #run parallel code execution
next(generator)
#Calculation of driven distance and PID values
oldDrivenDistance = drivenDistance
drivenDistance = getDrivenDistance(self)
pidCalculation(speed)
change = getGyroValue() - gyroStartValue #yaw angle used due to orientation of the self.hub
currenSteering = (change * pRegler + integral * iRegler + dRegler * (change - old_change)) + offset + steeringSum*0.02
currenSteering = max(-100, min(currenSteering, 100))
#print("steering: " + str(currenSteering) + " gyro: " + str(change) + " integral: " + str(integral))
steeringSum += change
integral += change - old_change
old_change = change
#Calculation of speed based on acceleration and braking, calculation of steering value for robot to drive perfectly straight
speed = speedCalculation(speed, startspeed,maxspeed, endspeed, accelerateDistance, deccelerateDistance, brakeStartValue, drivenDistance, oldDrivenDistance)
self.movement_motors.start_at_power(int(speed), invert * int(currenSteering))
if stopMethod != None:
if stopMethod.loop():
loop = False
elif rotateDistance < drivenDistance:
loop = False
if stop:
self.movement_motors.stop()
run_generator = True
runSmall = True
return #End of gyroStraightDrive
def arcRotation(self, radius, angle, startspeed, maxspeed, endspeed, addspeed = 0.3, brakeStart = 0.7, stopMethod=None, generator = None, stop = True):
"""
This is the function that we use to make the robot drive a curve with a specified radius and to a given angle
Parameters
-------------
radius: the radius of the curve the robot is supposed to drive; measured from the outside edge of the casing. Type: Integer. Default: 0
angle: the angle that the robot is supposed to rotate on the curve. Type: Integer. Default: 0
speed: The speed which the robot is supposed to start at. Type: Integer. Default: No default value
maxspeed: The highest speed at which the robot drives. Type: Integer. Default: No default value
endspeed: The speed which the robot achieves at the end of the function. Type: Integer. Default: No default value
addspeed: The speed which the robot adds in order to accelerate. Type: Float. Default: 0.2
brakeStart: The value which we use to tell the robot after what percentage of the distance we need to slow down. Type: Float. Default: 0.8
stopMethod: the Stopmethod the robot uses to stop. If no stopMethod is passed stopDistance is used instead. Default: stopDistance
generator: the generator that runs something parallel while driving. Default: No default value
stop: the boolean that determines whether the robot should stop the motors after driving or not. Default: True
"""
if cancel:
print("cancel")
return
global run_generator, runSmall
if generator == None:
run_generator = False
angle = angle * (2400/2443) #gyro calibration
gyroStartValue = getGyroValue()
finalGyroValue = gyroStartValue + angle
currentAngle = gyroStartValue
accelerateDistance = abs(angle * addspeed)
deccelerateDistance = abs(angle * (1 - brakeStart))
brakeStartValue = abs(angle * brakeStart)
loop = True
#Calculating the speed ratios based on the given radius
if angle * startspeed > 0:
speed_ratio_left = (radius+14) / (radius+2) #calculate speed ratios for motors. These will need to be adapted based on your robot design
speed_ratio_right = 1
else:
speed_ratio_left = 1
speed_ratio_right = (radius+14) / (radius+2)
#Calculating the first speed to drive with
left_speed = speedCalculation(startspeed, startspeed, maxspeed, endspeed, accelerateDistance, deccelerateDistance, brakeStartValue, 1, 0)
right_speed = speedCalculation(startspeed, startspeed , maxspeed , endspeed , accelerateDistance, deccelerateDistance, brakeStartValue, 1, 0)
while loop:
#when the cancel button is pressed stop the gyrostraight drive directly
if cancel:
break
if run_generator: #run parallel code execution
next(generator)
currentAngle = getGyroValue() #Yaw angle used due to orientation of the self.hub. This might need to be changed
#Calculating the current speed the robot should drive
left_speed = speedCalculation(left_speed, startspeed, maxspeed, endspeed, accelerateDistance, deccelerateDistance, brakeStartValue, 1, 0)
right_speed = speedCalculation(right_speed, startspeed , maxspeed , endspeed , accelerateDistance, deccelerateDistance, brakeStartValue, 1, 0)
self.movement_motors.start_tank_at_power(int(left_speed* speed_ratio_left), int(right_speed* speed_ratio_right))
#if there is a stopMethod passed use it and stop the loop if it returns true otherwise check if the robot has rotated to the given angle
if stopMethod != None:
#print("stoMeth")
if stopMethod.loop():
loop = False
break
(angle / abs(angle))
if finalGyroValue * (angle / abs(angle)) < currentAngle * (angle / abs(angle)):
#print("finalGyroValue: " + str(finalGyroValue) + " rotatedDistance: " + str(currentAngle))
loop = False
break
#if stop is true then stop the motors otherwise don't stop the motor
if stop:
self.movement_motors.stop()
run_generator = True
runSmall = True
return #End of arcRotation
def resetGyroValue():
global gyroValue
hub2.motion.yaw_pitch_roll(0)
gyroValue = 0
def getGyroValue():
#this method is used to return the absolute gyro Angle and the angle returned by this method doesn't reset at 180 degree
global lastAngle
global oldAngle
global gyroValue
#gets the angle returned by the spike prime program. The problem is the default get_yaw_angle resets at 180 and -179 back to 0
angle = hub.motion_sensor.get_yaw_angle()
if angle != lastAngle:
oldAngle = lastAngle
lastAngle = angle
if angle == 179 and oldAngle == 178:
hub2.motion.yaw_pitch_roll(0)#reset
gyroValue += 179
angle = 0
if angle == -180 and oldAngle == -179:
hub2.motion.yaw_pitch_roll(0) #reset
gyroValue -= 180
angle = 0
return gyroValue + angle
def getDrivenDistance(data):
#print(str(abs(data.leftMotor.get_degrees_counted() - data.left_Startvalue)) + " .:. " + str(abs(data.rightMotor.get_degrees_counted() - data.right_Startvalue)))
drivenDistance = (
abs(data.leftMotor.get_degrees_counted() - data.left_Startvalue) +
abs(data.rightMotor.get_degrees_counted() - data.right_Startvalue)) / 2
return drivenDistance
def defaultClass(object, db):
object.db = db
object.leftMotor = db.leftMotor
object.rightMotor = db.rightMotor
object.left_Startvalue = abs(db.leftMotor.get_degrees_counted())
object.right_Startvalue = abs(db.rightMotor.get_degrees_counted())
return object
class stopMethods(): #This class has all our stopmethods for easier coding and less redundancy
class stopLine():
"""
Drive until a Line is detected
Parameters
-------------
db: the drivebase of the robot
port: Port to detect line on
lightvalue: Value of the light to detect
detectLineDistance: Distance until start detecting a line
"""
def __init__(self, db, port, lightvalue, detectLineDistance):
self = defaultClass(self, db)
self.port = port
self.detectLineDistance = (detectLineDistance / 17.6) * 360
#if lightvalue bigger 50 stop when lightvalue is higher
self.lightvalue = lightvalue
def loop(self):
drivenDistance = getDrivenDistance(self)
if abs(self.detectLineDistance) < abs(drivenDistance):
if self.lightvalue > 50:
if ColorSensor(self.port).get_reflected_light() > self.lightvalue:
return True
else:
if ColorSensor(self.port).get_reflected_light() < self.lightvalue:
return True
return False
class stopAlign():
"""
Drive until a Line is detected
Parameters
-------------
db: the drivebase of the robot
port: Port to detect line on
lightvalue: Value of the light to detect
speed: speed at which the robot searches for other line
"""
def __init__(self, db, lightvalue, speed):
self = defaultClass(self, db)
self.speed = speed
#if lightvalue bigger 50 stop when lightvalue is higher
self.lightValue = lightvalue
def loop(self):
if colorE.get_reflected_light() < self.lightValue:
self.rightMotor.stop()
#Turning robot so that other colour sensor is over line
while True:
self.leftMotor.start_at_power(-int(self.speed))
#Line detection and stopping
if colorF.get_reflected_light() < self.lightValue or cancel:
self.leftMotor.stop()
return True
#Colour sensor F sees line first
elif colorF.get_reflected_light() < self.lightValue:
self.leftMotor.stop()
#Turning robot so that other colour sensor is over line
while True:
self.rightMotor.start_at_power(int(self.speed))
#Line detection and stopping
if colorE.get_reflected_light() < self.lightValue or cancel:
self.rightMotor.stop()
return True
return False
class stopTangens():
"""
Drive until a Line is detected
Parameters
-------------
db: the drivebase of the robot
port: Port to detect line on
lightvalue: Value of the light to detect
speed: Distance until start detecting a line
"""
def __init__(self, db, lightvalue, speed):
self.count = 0
self = defaultClass(self, db)
self.speed = speed
#if lightvalue bigger 50 stop when lightvalue is higher
self.lightValue = lightvalue
self.detectedLineDistance = 0
self.invert = 1
if speed < 0:
self.invert = -1
def loop(self):
drivenDistance = getDrivenDistance(self)
if colorE.get_reflected_light() < self.lightValue:
#measuring the distance the robot has driven since it has seen the line
if(self.detectedLineDistance == 0):
self.detectedLineDistance = getDrivenDistance(self)
self.detectedPort = 'E'
elif self.detectedPort == 'F':
db.movement_motors.stop() #Stops robot with sensor F on the line
angle = math.degrees(math.atan(((drivenDistance - self.detectedLineDistance) / 360 * circumference) / sensordistance)) #Calculating angle that needs to be turned using tangent
#print("angle: " + str(angle))
db.gyroRotation(-angle, self.invert * self.speed, self.invert * self.speed, self.invert * self.speed, rotate_mode=1) #Standard gyrorotation for alignment, but inverting speed values if necessary
db.movement_motors.stop() #Stopping robot for increased reliability
return True
#Colour sensor F sees line first
elif colorF.get_reflected_light() < self.lightValue:
#measuring the distnace the robot has driven since it has seen the line
if(self.detectedLineDistance == 0):
self.detectedLineDistance = drivenDistance
self.detectedPort = 'F'
elif self.detectedPort == 'E':
db.movement_motors.stop() #Stops robot with sensor E on the line
angle = math.degrees(math.atan(((drivenDistance - self.detectedLineDistance) / 360 * circumference) / sensordistance)) #Calculation angle that needs to be turned using tangent
db.gyroRotation(angle, self.invert * self.speed, self.invert * self.speed, self.invert * self.speed, rotate_mode=1) #Standard gyrorotation for alignment, but inverting speed values if necessary
db.movement_motors.stop() #Stopping robot for increased reliablity
return True
return False
class stopDegree():
"""
Roates until a certain degree is reached
Parameters
-------------
db: the drivebase of the robot
angle: the angle to rotate
"""
def __init__(self, db, angle):
self.angle = angle * (336/360)
self.gyroStartValue = getGyroValue() #Yaw angle used due to orientation of the self.hub.
def loop(self):
rotatedDistance = getGyroValue() #Yaw angle used due to orientation of the self.hub.
if abs(self.angle) <= abs(rotatedDistance - self.gyroStartValue):
return True
else:
return False
class stopTime():
"""
Drive until a certain time is reached
Parameters
-------------
db: the drivebase of the robot
time: the time to drive
"""
def __init__(self, db, time) -> None:
self = defaultClass(self, db)
self.time = time
self.timer = Timer()
self.startTime = self.timer.now()
def loop(self):
if self.timer.now() > self.startTime + self.time:
return True
else:
return False
class stopResistance():
"""
Drive until the Robot doesn't move anymore
Parameters
-------------
db: the drivebase of the robot
restistance: the value the resistance has to be below to stop
"""
def __init__(self, db, resistance):
self = defaultClass(self, db)
self.resistance = resistance
self.timer = Timer()
self.startTime = 0
self.lower = False
self.runs = 0
def loop(self):
self.runs += 1
motion = abs(hub2.motion.accelerometer(True)[2])
if motion < self.resistance:
self.lower = True
if self.runs > 15:
if self.lower:
if self.startTime == 0:
self.startTime = self.timer.now()
if self.timer.now() > self.startTime:
return True
else:
self.lower = False
return False
def motorResistance(speed, port, resistancevalue):
"""
lets the motor stop when it hits an obstacle
"""
if abs(resistancevalue) > abs(speed):
return
if cancel:
return
if port == "A":
smallMotorA.start_at_power(speed)
while True:
old_position = smallMotorA.get_position()
wait_for_seconds(0.4)
if abs(old_position - smallMotorA.get_position())<resistancevalue or cancel:
smallMotorA.stop()
print("detected stalling")
return
elif port == "D":
smallMotorD.start_at_power(speed)
while True:
old_position = smallMotorD.get_position()
wait_for_seconds(0.4)
if abs(old_position - smallMotorD.get_position())<resistancevalue or cancel:
smallMotorD.stop()
print("detected stalling")
return
else:
print("wrong port selected. Select A or D")
return
def speedCalculation(speed, startspeed, maxspeed, endspeed, accelerateDistance, deccelerateDistance, brakeStartValue, drivenDistance, oldDrivenDistance):
"""
Used to calculate all the speeds in out programs. Done seperatly to reduce redundancy. Brakes and accelerates
Parameters
-------------
speed: The current speed the robot has
startspeed: Speed the robot starts at. Type: Integer. Default: No default value.
maxspeed: The maximum speed the robot reaches. Type: Integer. Default: No default value.
endspeed: Speed the robot aims for while braking, minimum speed at the end of the program. Type: Integer. Default: No default value.
addspeed: Percentage of the distance after which the robot reaches the maximum speed. Type: Integer. Default: No default value.
brakeStartValue: Percentage of the driven distance after which the robot starts braking. Type: Integer. Default: No default value.
drivenDistance: Calculation of the driven distance in degrees. Type: Integer. Default: No default value.
"""
addSpeedPerDegree = (maxspeed - startspeed) / accelerateDistance
subSpeedPerDegree = (maxspeed - endspeed) / deccelerateDistance
subtraction = (abs(drivenDistance) - abs(oldDrivenDistance) if abs(drivenDistance) - abs(oldDrivenDistance) >= 1 else 1) * subSpeedPerDegree
addition = (abs(drivenDistance) - abs(oldDrivenDistance) if abs(drivenDistance) - abs(oldDrivenDistance) >= 1 else 1) * addSpeedPerDegree
if abs(drivenDistance) > abs(brakeStartValue):
if abs(speed) > abs(endspeed):
speed = speed - subtraction
elif abs(speed) < abs(maxspeed):
speed = speed + addition
return speed
def breakFunction(args):
"""
Allows you to manually stop currently executing round but still stays in main.
This is much quicker and more reliable than pressing the center button.
"""
global cancel, inMain
if not inMain:
cancel = True
def pidCalculation(speed):
#golbally sets PID values based on current speed of the robot, allows for fast and accurate driving
global pRegler
global iRegler
global dRegler
#Important note: These PID values are experimental and based on our design for the robot. You will need to adjust them manually. You can also set them statically as you can see below
if speed > 0:
pRegler = -0.17 * speed + 12.83
iRegler = 12
dRegler = 1.94 * speed - 51.9
if pRegler < 3.2:
pRegler = 3.2
else:
pRegler = (11.1 * abs(speed))/(0.5 * abs(speed) -7) - 20
iRegler = 10
#iRegler = 0.02
dRegler = 1.15**(- abs(speed)+49) + 88
def pidCalculationLight(speed):
#Sets the PID values for the lineFollower based on current speed. Allows for accurate and fast driving
#Important note: these PID values are experimental and based on our design for the robot. You will need to adjust them. See above on how to do so
global pReglerLight
global dReglerLight
pReglerLight = -0.04 * speed + 4.11
dReglerLight = 0.98 * speed - 34.2
#set hard bottom for d value, as otherwise the values don't work
if dReglerLight < 5:
dReglerLight = 5
def driveMotor(rotations, speed, port):
"""
Allows you to drive a small motor in parallel to driving with gyroStraightDrive
Parameters
-------------
rotations: the rotations the motor turns
speed: the speed at which the motor turns
port: the motor used. Note: this cannot be the same motors as configured in the motor Drivebase
"""
global runSmall
global run_generator
if cancel:
runSmall = False
run_generator = False
while runSmall:
smallMotor = Motor(port)
smallMotor.set_degrees_counted(0)
loop_small = True
while loop_small:
drivenDistance = smallMotor.get_degrees_counted()
smallMotor.start_at_power(speed)
if (abs(drivenDistance) > abs(rotations) * 360):
loop_small = False
if cancel:
loop_small = False
yield
smallMotor.stop()
runSmall = False
run_generator = False
yield
hub2.motion.yaw_pitch_roll(0)
db = DriveBase(hub, 'B', 'C') #this lets us conveniently hand over our motors (B: left driver; C: right driver). This is necessary for the cancel function
def exampleOne():
#This example aims to show all the options for following a line. See the specific documentation of the function for further information.
db.lineFollower(15, 25, 35, 25, 'E', 'left') #follows the left side of a line on the E sensor for 15cm. Accelerates from speed 25 to 35 and ends on 25 again
hub.left_button.wait_until_pressed()
db.lineFollower(15, 25, 35, 25, 'E', 'left', 0.4, 0.6) #same line follower as before but with a longer acceleration and breaking period
hub.left_button.wait_until_pressed()
db.lineFollower(15, 25, 35, 25, 'E', 'left', stopMethod=stopMethods.stopLine(db, 'F', 0.7)) #same linefollower as the first, but this time stopping, when the other sensor sees a black line after at least 70% of the driven distance
hub.left_button.wait_until_pressed()
db.lineFollower(15, 25, 35, 25, 'E', 'left', stopMethod=stopMethods.stopResistance(db, 20)) #same as first linefollower, but stops when desired resistance is reached. Test the resistance value based on your robot
hub.left_button.wait_until_pressed()
generator = driveMotor(5, 100, 'A')
db.lineFollower(15, 25, 35, 25, 'E', 'left', generator=generator) #same as first linefollower, but drives while turning the A-Motor for 5 rotations
hub.left_button.wait_until_pressed()
db.lineFollower(15, 25, 35, 25, 'E', 'left', stop=False) #same as first linefollower, but does not actively brake the motors. The transistion form this action to the next is then smoother
return
def exampleTwo():
#This example aims to show all the options for turning the robot. See the specific documentation of the function for further information.
db.gyroRotation(90, 25, 35, 25) #turns the robot 90° clockwise while accelerating from speed 25 to 35 and back down to 25
hub.left_button.wait_until_pressed()
db.gyroRotation(90, 25, 35, 25, 0.4, 0.5) #same turning as in first rotation but with longer acceleration/braking phase
hub.left_button.wait_until_pressed()
db.gyroRotation(90, 25, 35, 25, rotate_mode=1) #same turn as in first rotation but this time turning using only one wheel rather than turning on the spot. Your speeds may need to be higher for this
hub.left_button.wait_until_pressed()
db.gyroRotation(90, 25, 35, 25, stopMethod=stopMethods.stopAlign(db, 25, 25)) #aligns the robot with a line in turning path
hub.left_button.wait_until_pressed()
db.gyroRotation(90, 25, 35, 25, stopMethod=stopMethods.stopLine(db, 'E', 25, 0.7)) #turns until the robot sees a line on sensor E after at least 70% of turning
hub.left_button.wait_until_pressed()
db.gyroRotation(90, 25, 35, 25, stopMethod=stopMethods.stopTangens(db, 25, 25)) #aligns the robot like stopAlign but is a bit more precise
hub.left_button.wait_until_pressed()
#remaining parameters are the same as in linefollower. Please refer to exampleOne or the documentation of the individual functions
return
def exampleThree():
#This example aims to show all the options for driving in a straight line. See the specific documentation of the function for further information.
db.gyroStraightDrive(30, 25, 35, 25) #drives in a straight line for 30cm
hub.left_button.wait_until_pressed()
db.gyroStraightDrive(30, 25, 55, 25, 0.1, 0.9) #same as first drive, but faster and with harder acceleration/braking
hub.left_button.wait_until_pressed()
db.gyroStraightDrive(30, 25, 35, 25, offset=15) #same as first drive, but aims 15° in clockwise direction as target orientation
#remaining features of code are explained in previous examples. Please refer to exampleOne, exampleTwo and additional documentation within individual functions
return
def exampleFour():
#This example aims to show all the options for turning in a large curve. See the specific documentation of the function for further information.
db.arcRotation(5, 35, 25, 30, 25) #robot drives 35° on a circle with a radius of 5cm measured from the inside edge of the robot
#remaining features of code are explained in previous examples. Please refer to exampleOne, exampleTwo and additional documentation within individual functions
return
def exampleFive():
#add your own code here
return
def exampleSix():
#add your own code here
return
class bcolors:
BATTERY = '\033[32m'
BATTERY_LOW = '\033[31m'
ENDC = '\033[0m'
pReglerLight = 1.6
iReglerLight = 0.009
dReglerLight = 16
accelerate = True
hub2.button.right.callback(breakFunction)
gyroValue = 0
#Battery voltage printout in console for monitoring charge
if battery.voltage() < 8000: #set threshold for battery level
print(bcolors.BATTERY_LOW + "battery voltage is too low: " + str(battery.voltage()) + " \n ----------------------------- \n >>>> please charge robot <<<< \n ----------------------------- \n"+ bcolors.ENDC)
else:
print(bcolors.BATTERY + "battery voltage: " + str(battery.voltage()) + bcolors.ENDC)
#User Interface in Program for competition and instant program loading
main = True
programselect = 1 #Set the attachment the selection program starts on
hub.light_matrix.write(programselect)
db.movement_motors.set_stop_action("hold") #hold motors on wait for increased reliability
while main:
cancel = False
inMain = True
#Program selection
if hub.right_button.is_pressed(): #press right button to cycle through programs. cycling back isn't supported yet, but we are working on reallocating the buttons in the file system
wait_for_seconds(0.15) #waiting prevents a single button press to be registered as multiple clicks
programselect = programselect + 1
hub.light_matrix.write(programselect) #show current selcted program
hub.speaker.beep(85, 0.1) #give audio feedback for user