forked from cnst01/OBRSolver2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathground1.0.py
172 lines (155 loc) · 5.14 KB
/
ground1.0.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
from pybricks.hubs import PrimeHub
from pybricks.pupdevices import Motor, ColorSensor, UltrasonicSensor, ForceSensor
from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
from pybricks.robotics import DriveBase
from pybricks.tools import wait, StopWatch
class MotorPair:
def __init__(self, port1, port2):
self.motor1 = Motor(port1)
self.motor2 = Motor(port2)
self.timer = StopWatch()
def move_angle(self,amount,speed1,speed2, timeout = 1000):
self.motor1.reset_angle(0)
self.motor2.reset_angle(0)
self.timer.reset()
while abs(self.motor1.angle()) < amount or self.timer.time() < timeout:
while abs(self.motor2.angle()) < amount or self.timer.time() < timeout:
self.motor1.run(speed1)
self.motor2.run(-(speed2))
self.motor1.stop()
self.motor2.stop()
return "succeded"
def move_tank(self,amount, speed1, speed2):
self.motor1.run(speed1)
self.motor2.run(-(speed2))
wait(amount)
self.motor1.stop()
self.motor2.stop()
def start_tank(self, speed1, speed2):
self.motor1.run(speed1)
self.motor2.run(-(speed2))
def stop_tank(self):
self.motor1.run(0)
self.motor2.run(0)
def updateLog():
global logs
global name
global move_side
global log
logs = [name,move_side,log]
def axis_correction(set_point_c = 40, set_point_s = 35 ):
global corner
global name
global move_side
global log
if corner == 3:
motors.stop_tank()
corner = 0
if sd.reflection() < set_point_s:
while sd.reflection() < set_point_s:
motors.start_tank(-200,50)
move_side = 'right'
else:
while sd.reflection() < set_point_s:
motors.start_tank(50,-200)
move_side = 'left'
name = "axis correction **Suave**"
log = 'succeded'
else:
if sd.reflection() > set_point_c:
while sd.reflection() > set_point_c:
motors.start_tank(300,-50)
move_side = 'left'
corner += 1
else:
while se.reflection() > set_point_c:
motors.start_tank(-50,300)
move_side = 'right'
corner += 1
name = "axis correction **Corner**"
log = 'succeded'
updateLog()
return [name, move_side, log]
def proportionalAlign(errorE,errorD, kP):
global name
global move_side
global log
name ='proportionalAlign'
move_side = ''
log='failed'
leftMotorSpd = errorE * kP * 4.7
rightMotorSpd = errorD * kP * 4.7
motors.start_tank(leftMotorSpd,rightMotorSpd)
diff_l_r = leftMotorSpd - rightMotorSpd
if diff_l_r > 0:
move_side = 'right'
else:
move_side = 'left'
log = 'succeded'
updateLog()
return [name, move_side, log]
# def intersectionSolver():
def checkGreen():
ve = se.color() == 'green'
vd = se.color() == 'green'
if ve and vd:
return True
else:
if ve:
return True
if vd:
return True
return False
hub = PrimeHub()
setPoint = 50
# defining motors
motors = MotorPair(Port.A,Port.B)
# defining sensors
u2 = UltrasonicSensor(Port.E).distance()
sc = ColorSensor(Port.D)
sd = ColorSensor(Port.C)
se = ColorSensor(Port.F)
# defining the log list
name = 'Beginning run'
move_side = 'None'
log = 'succeded'
logs = [name,move_side,log]
corner = 0
if __name__ == "__main__":
while 1:
se_value = se.reflection()
sd_value = sd.reflection()
sc_value = sc.reflection()
errord = se_value - setPoint
errore = sd_value - setPoint
if se_value > 60 and sd_value > 60:
proportionalAlign(errore,errord,0.8)
else:
if checkGreen():
print('-------- Green founded --------')
else:
if se_value > 80 and sd_value > 80 and sc_value > 80:
if logs[0] == 'proportionalAlign':
print('------- Possible Gap ---------')
else:
print("xxxxxxxxx Recovery xxxxxxxxx")
else:
if se_value < 30 and sd_value < 30:
print("------ crossing line -----")
motors.move_tank(10, 200, 200)
se_value = se.reflection()
sd_value = sd.reflection()
sc_value = sc.reflection()
errord = se_value - setPoint
errore = sd_value - setPoint
if errord > 40 and errore > 40:
proportionalAlign(errore,errord,0.8)
else:
print('back until see black')
print('Axis Correction')
axis_correction()
elif se_value < 30 or sd_value < 30:
print('Axis Correction\n\n\n')
axis_correction()
print(errore,errord)
print(logs)