forked from cnst01/OBRSolver2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathground_rescue_obj_detect_10.py
435 lines (410 loc) · 15.2 KB
/
ground_rescue_obj_detect_10.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
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
hub = PrimeHub()
yaw = hub.imu
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 = 75 ):
global corner
global name
global move_side
global log
if name != "axis correction **Corner**":
corner = 0
if corner == 3:
motors.stop_tank()
if sd.reflection() < set_point_s:
while sd.reflection() < set_point_s:
u_value = u2.distance()
motors.start_tank(-150,0)
move_side = 'right'
if se.reflection() < set_point_s :
while se.reflection() < set_point_s:
u_value = u2.distance()
motors.start_tank(0,-150)
move_side = 'left'
corner = 0
name = "axis correction **Suave**"
log = 'succeded'
else:
if sd.reflection() > set_point_c:
while sd.reflection() > set_point_c:
u_value = u2.distance()
motors.start_tank(300,-50)
move_side = 'left'
corner += 1
else:
while se.reflection() > set_point_c:
u_value = u2.distance()
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 ='proportional align'
move_side = ''
log='failed'
leftMotorSpd = 50 + errorE * kP * 4.7
rightMotorSpd = 50 + 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(valores):
print(valores)
if valores[0] == True and valores[1] == True:
print('dar voltinha')
else:
if valores[0] == True:
print('esquerdinha')
motors.stop_tank()
wait(1000)
motors.start_tank(300,0)
wait(1000)
motors.stop_tank()
move_side = 'left'
else:
print('direitinha')
motors.stop_tank()
wait(1000)
motors.start_tank(0,300)
wait(1000)
motors.stop_tank()
move_side = 'right'
name = intersectionSolver
log = 'succeded'
updateLog()
def recoveryTask():
global logs
global name
global move_side
global log
if logs[0] == "proportionalAlign":
timeout = 0
while se.reflection() > 20 and sc.reflection() > 20 and sd.reflection() > 20 and timeout < 3000 :
motors.start_tank(-200,-200)
wait(10)
timeout += 10
motors.stop_tank()
if se.reflection() < 20 or sc.reflection() < 20 or sd.reflection() < 20:
name = "recovery task"
move_side = move_side
log = "succeded"
updateLog()
return "succeded"
else:
name = "recovery task"
move_side = move_side
log = "failed"
updateLog()
return "succeded"
if logs[0] == "axis correction" or logs[0] == "intersection solver":
if logs[0] == "intersection solver":
if logs[1] == "right":
logs[1] = "left"
if logs[1] == "left":
logs[1] = "right"
yaw.reset_heading()
if logs[1] == "right":
motors.start_tank(-200,200)
while yaw.heading() >= -90:
if se.reflection() < 20 or sd.reflection() < 20:
name = "recovery task"
move_side = "left"
log = "succeded"
updateLog()
return "succeded"
else:
timeout = 0
while se.reflection() > 20 and sc.reflection() > 20 and sd.reflection() > 20 and timeout < 3000 :
motors.start_tank(-200,-200)
wait(10)
timeout += 10
motors.stop_tank()
if se.reflection() < 20 or sc.reflection() < 20 or sd.reflection() < 20:
name = "recovery task"
move_side = move_side
log = "succeded"
updateLog()
return "succeded"
else:
name = "recovery task"
move_side = move_side
log = "failed"
updateLog()
return "succeded"
yaw.reset_heading()
motors.start_tank(200,-200)
while yaw.heading() <= 90:
if se.reflection() < 20 or sd.reflection() < 20:
name = "recovery task"
move_side = "right"
log = "succeded"
updateLog()
return "succeded"
else:
timeout = 0
while se.reflection() > 20 and sc.reflection() > 20 and sd.reflection() > 20 and timeout < 3000 :
motors.start_tank(-200,-200)
wait(10)
timeout += 10
motors.stop_tank()
if se.reflection() < 20 or sc.reflection() < 20 or sd.reflection() < 20:
name = "recovery task"
move_side = move_side
log = "succeded"
updateLog()
return "succeded"
else:
name = "recovery task"
move_side = move_side
log = "failed"
updateLog()
return "succeded"
if logs[1] == "left":
motors.start_tank(200,-200)
while yaw.heading() <= 90:
if se.reflection() < 20 or sd.reflection() < 20:
name = "recovery task"
move_side = "right"
log = "succeded"
updateLog()
return "succeded"
else:
timeout = 0
while se.reflection() > 20 and sc.reflection() > 20 and sd.reflection() > 20 and timeout < 3000 :
motors.start_tank(-200,-200)
wait(10)
timeout += 10
motors.stop_tank()
if se.reflection() < 20 or sc.reflection() < 20 or sd.reflection() < 20:
name = "recovery task"
move_side = move_side
log = "succeded"
updateLog()
return "succeded"
else:
name = "recovery task"
move_side = move_side
log = "failed"
updateLog()
return "succeded"
yaw.reset_heading()
motors.start_tank(-200,200)
while yaw.heading() >= -90:
if se.reflection() < 20 or sd.reflection() < 20:
name = "recovery task"
move_side = "left"
log = "succeded"
updateLog()
return "succeded"
else:
timeout = 0
while se.reflection() > 20 and sc.reflection() > 20 and sd.reflection() > 20 and timeout < 3000 :
motors.start_tank(-200,-200)
wait(10)
timeout += 10
motors.stop_tank()
if se.reflection() < 20 or sc.reflection() < 20 or sd.reflection() < 20:
name = "recovery task"
move_side = move_side
log = "succeded"
updateLog()
return "succeded"
else:
name = "recovery task"
move_side = move_side
log = "failed"
updateLog()
return "succeded"
def checkGreen(values):
sensor_d = sd.hsv()
sensor_e = se.hsv()
direita = False
esquerda = False
# print(sensor)
# if sensor.h < values[0][0] or sensor.h > values[1][0]:
# return False
# if sensor.s < values[0][1] or sensor.s > values[1][1]:
# return False
# if sensor.v < values[0][2] or sensor.v > values[1][2]:
# return False
if sensor_d.h > values[0][0] and sensor_d.h < values[1][0]:
if sensor_d.s > values[0][1] and sensor_d.s < values[1][1]:
if sensor_d.v > values[0][2] and sensor_d.v < values[1][2]:
direita = True
if sensor_e.h > values[0][0] and sensor_e.h < values[1][0]:
if sensor_e.s > values[0][1] and sensor_e.s < values[1][1]:
if sensor_e.v > values[0][2] and sensor_e.v < values[1][2]:
esquerda = True
return [esquerda, direita]
def desviarObs(lado = 'right'):
if lado == 'right':
motors.move_tank(800, -400, 400)
motors.stop_tank()
while se.reflection() > 80 or sd.reflection() > 800 :
motors.start_tank(350, 150)
motors.stop_tank()
wait(1000)
return True
elif lado == 'left':
motors.move_tank(800, 400, -400)
motors.stop_tank()
while se.reflection() > 80 or sd.reflection() > 80 :
motors.start_tank(150, 350)
motors.stop_tank()
wait(1000)
return True
return False
def checarResgate(u_value):
global logs
global name
global move_side
global log
if u_value > 900 and u_value < 930:
motors.stop_tank()
hub.speaker.beep()
motors.move_tank(3000,250,250)
motors.move_tank(1500,-250,250)
motors.move_tank(1000,250,250)
wait(2000)
u_value = u2.distance()
print(u_value)
#Mover motor esquerdo para frente fazendo com q o robo gire 90 graus
if u_value > 500 and u_value < 800:
hub.speaker.beep()
hub.display.pixel(4,4)
print("resgate!!!!")
return True
else:
motors.move_tank(1000,-250,-250)
motors.move_tank(1500,250,-250)
motors.move_tank(2250,-250,-250)
elif u_value < 100:
hub.speaker.beep()
motors.stop_tank
move_side = 'right'
if desviarObs(move_side) == True:
name = "desvio Obs"
log = 'succeded'
updateLog()
return True
return False
hub = PrimeHub()
setPoint = 50
darkest = ""
# defining motors
motors = MotorPair(Port.A,Port.B)
# defining sensors
u2 = UltrasonicSensor(Port.E)
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
green_values = [[163, 66, 57], [168, 87, 92]]
if __name__ == "__main__":
while True:
u_value = u2.distance()
while checarResgate(u_value) == False:
print(logs,corner)
hub.display.pixel(2,1)
u_value = u2.distance()
se_value = se.reflection()
sd_value = sd.reflection()
sc_value = sc.reflection()
errord = se_value - setPoint
errore = sd_value - setPoint
if se_value > 50 and sd_value > 50 and sc_value < 45:
print(sc_value)
proportionalAlign(errore,errord,1.2)
else:
valores_verdes = checkGreen(green_values)
if valores_verdes[0] != False or valores_verdes[1] != False:
print('-------- Green founded --------')
intersectionSolver(valores_verdes)
if se_value > 80 and sd_value > 80 and sc_value > 80:
if logs[0] == 'proportional align':
print('------- Possible Gap ---------')
else:
recoveryTask()
else:
motors.stop_tank()
if se_value > sd_value:
darkest = "direita"
if sd_value > se_value:
darkest = "esquerda"
if se_value < 45 and sd_value < 45:
print("------ crossing line ------")
motors.move_tank(2000, 200, 200)
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 and sc_value < 45:
proportionalAlign(errore,errord,0.8)
else:
print('back until see black')
motors.move_tank(2000, -200, -200)
print('Axis Correction')
axis_correction()
else:
if darkest == "esquerda":
print('Axis Correction')
axis_correction()
if darkest == "direita":
print('Axis Correction')
axis_correction()
msg = str(se.reflection()) + ',' + str(sc.reflection()) + ',' + str(sd.reflection())
print(msg)