-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-grandprix.py
535 lines (446 loc) · 16 KB
/
main-grandprix.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
### BWSI 2021 Grandprix Team CocaCola
#newtest
### BWSI 2021 Grandprix Team CocaCola
import sys
from typing import Tuple
import cv2 as cv
import numpy as np
from numpy.lib.utils import _set_function_name
from numpy.testing._private.utils import jiffies
sys.path.insert(0, "../../library")
import racecar_core
import racecar_utils as rc_utils
import custom_funcs as rc_cf
from enum import IntEnum
import math
rc = racecar_core.create_racecar()
class State(IntEnum) :
green_line_follow = 0
challenge1 = 1
challenge2 = 2
challenge3 = 3
challenge4 = 4
challenge5 = 5
challenge6 = 6
challenge7 = 7
challenge8 = 8
robotState = State.green_line_follow
#VARIABLES GO HERE
#Variables for green line follow
timer = 0.0
MIN_CONTOUR_AREA = 30
# A crop window for the floor directly in front of the car
CROP_FLOOR = ((360, 0), (rc.camera.get_height(), rc.camera.get_width()))
BLUE = ((100, 200, 200), (120, 255, 255), "blue") # The HSV range for the color blue
GREEN = ((40,50,50), (80,255,255), "green")
RED = ((170,50,50), (10,255,255), "red")
ORANGE = ((15,230,230), (35,255,255,),"orange")
PURPLE = ((125,200,200),(140,255,255), "purple")
AR_ORANGE = ((10,20,20), (40,255,255),"orange")
AR_PURPLE = ((110,20,20),(140,255,255), "purple")
contour_center = None # The (pixel row, pixel column) of contour
blue_contour_center = None
contour_area = None
contours = None
green_contours = None
image = None
depth_image = None
scan = None
color_image = None
marker = None
#Challenge 2
counter = 0
center = [0,0]
hard = False
#Challenge 3
FRONT_WINDOW = (-10, 10)
FRONT_RIGHT_WINDOW = (30, 40)
FRONT_LEFT_WINDOW = (320, 330)
RIGHT_WINDOW = (65, 70)
LEFT_WINDOW = (285, 290)
class Challenge3State(IntEnum):
see_marker = 0
follow_marker = 1
search = 2
cur_state = Challenge3State.search
#Challenge 4
ar_color = None
#Challenge 6
FRONT_WINDOW_6 = (-20, 20)
detected_obstacle_time = 0.0
detected_obstacle = False
forward_dist = None
train1 = False
train2 = False
train3 = False
Stop = False
#Drive Function
speed = 0.0 # The current speed of the car
angle = 0.0 # The current angle of the car's wheels
le_max = 0.9
def start():
global robotState,timer
rc.drive.set_max_speed(0.5)
rc.drive.stop()
timer = 5
robotState = State.green_line_follow
speed = le_max
print("Team Cocacola Grand Prix Challenge!")
def update():
global speed, angle, robotState, scan, color_image, timer, depth_image, marker, contour_center, ar_color, green_contours, le_max
timer += rc.get_delta_time()
color_image = rc.camera.get_color_image()
depth_image = rc.camera.get_depth_image()
depth_image = (depth_image - 0.01) % 10000
scan = rc.lidar.get_samples()
markers = rc_utils.get_ar_markers(color_image)
#DO NOT TOUCH#######################################################
if color_image is None:
contour_center = None
contour_area = 0
else:
image = rc_utils.crop(color_image, CROP_FLOOR[0], CROP_FLOOR[1])
green_contours = rc_utils.find_contours(image, GREEN[0], GREEN[1])
contour = rc_utils.get_largest_contour(green_contours, MIN_CONTOUR_AREA)
if contour is not None:
contour_center = rc_utils.get_contour_center(contour)
contour_area = rc_utils.get_contour_area(contour)
rc_utils.draw_contour(image, contour)
rc_utils.draw_circle(image, contour_center)
else:
contour_center = None
contour_area = 0
#image = rc_utils.crop(color_image, CROP_FLOOR[0], CROP_FLOOR[1])
#contour_center = rc_cf.get_contour_info(color_image,GREEN[0], GREEN[1],CROP_FLOOR)[0]
#if contour_center is not None:
#rc_utils.draw_circle(image, contour_center)
rc.display.show_color_image(image)
#DO NOT TOUCH#######################################################
if robotState == State.green_line_follow:
green_line_follow(le_max)
if robotState == State.challenge1:
challenge1()
elif robotState ==State.challenge2:
if ar_color == "orange": challenge2(ORANGE)
elif ar_color == "purple": challenge2(PURPLE)
elif robotState == State.challenge3:
challenge3()
elif robotState == State.challenge4:
challenge4(ar_color)
elif robotState == State.challenge5:
challenge5()
elif robotState == State.challenge6:
challenge6_new()
elif robotState == State.challenge8:
challenge8_new()
for marker in markers:
id = marker.get_id()
marker.detect_colors(color_image, [BLUE, RED, GREEN,AR_ORANGE,AR_PURPLE])
color = marker.get_color()
orientation = marker.get_orientation()
marker_top, marker_left = marker.get_corners()[marker.get_orientation().value]
marker_bottom, marker_right = marker.get_corners()[(marker.get_orientation().value + 2) % 4]
ar_center = ( marker_top + (marker_bottom-marker_top)//2, marker_left + (marker_right-marker_left)//2)
ar_dis = depth_image[ar_center]
if id is not None:
# print(id)
#print(ar_center)
# print(ar_dis)
#print(color)
#print(orientation)
if id == 0 and ar_dis < 80:
robotState = State.challenge1
timer = 0
elif id == 1 and ar_dis < 100:
robotState = State.challenge2
ar_color = color
elif id == 3 and ar_dis < 300:
robotState = State.challenge4
ar_color = color
timer = 0
elif id == 199 and ar_dis < 220:
robotState = State.challenge3
timer = 0
elif id == 4 and ar_dis < 70:
robotState = State.challenge5
elif id == 5 and ar_dis < 100:
robotState = State.green_line_follow
le_max = 1
timer = 5
elif id == 6 and ar_dis < 120:
robotState = State.challenge6
timer = 5
le_max = 0.7
elif id == 8 and ar_dis < 150:
robotState = State.challenge8
le_max = 0.7
timer = 5
print(f"Max{le_max} Speed{speed}")
rc.drive.set_speed_angle(speed, angle)
def green_line_follow(max):
global contour_center,angle,speed, color_image, image, green_contours
CROP_FLOOR = ((360, 0), (rc.camera.get_height(), rc.camera.get_width()))
if contour_center is not None:
angle = rc_utils.remap_range(contour_center[1], 0, rc.camera.get_width(), -1, 1)
if timer < 4.5:
speed = 0.3
else:
max_speed = max
min_speed = 0.5
speed = math.cos(0.5 * math.pi * angle) * max_speed + min_speed
speed = rc_utils.clamp(speed, min_speed, max_speed)
def challenge1():
global scan, green_contours, image, robotState, marker, speed, angle, timer, contour_center
RIGHT_WINDOW = (32,38)
LEFT_WINDOW = (322,328)
_, right_dis = rc_utils.get_lidar_closest_point(scan, RIGHT_WINDOW)
_, left_dis = rc_utils.get_lidar_closest_point(scan, LEFT_WINDOW)
kP = 0.01
error = right_dis - left_dis
angle = kP * error
angle = rc_utils.clamp(angle, -1, 1)
max_speed = 0.6
min_speed = 0.4
speed = math.cos(0.5 * math.pi * angle) * max_speed + min_speed
speed = rc_utils.clamp(speed, -0.5, 0.6)
if contour_center is not None and timer > 5.0:
robotState = State.green_line_follow
le_max = 0.5
timer = 0.0
def challenge2(path_color):
global speed, angle
global PURPLE, ORANGE
global counter
global hard
global robotState
global le_max
CROP_FLOOR = ((300, rc.camera.get_width()//2 ), (rc.camera.get_height(),rc.camera.get_width()))
MULTIPLIER = 4
image = rc.camera.get_color_image()
cropped_image = rc_utils.crop(image, CROP_FLOOR[0], CROP_FLOOR[1])
purple_center = rc_cf.get_contour_info(image, PURPLE[0], PURPLE[1], CROP_FLOOR)
orange_center = rc_cf.get_contour_info(image, ORANGE[0], ORANGE[1], CROP_FLOOR)
purple_contours = rc_utils.find_contours(cropped_image, PURPLE[0], PURPLE[1])
orange_contours = rc_utils.find_contours(cropped_image, ORANGE[0], ORANGE[1])
color = [[],[]]
if path_color == PURPLE:
if orange_contours:
color = ORANGE
else:
color = PURPLE
elif path_color == ORANGE:
if purple_contours:
color = PURPLE
else:
color = ORANGE
center,_,_ = rc_cf.get_contour_info(image,color[0],color[1], CROP_FLOOR)
if center is not None and not hard:
angle = (center[1] - rc.camera.get_width() * 3/8 ) / rc.camera.get_width() * 4
# print(angle)
angle = rc_utils.clamp(angle, -1, 1)
rc_utils.draw_circle(cropped_image, center)
speed = 0.45
if color is not path_color:
hard = True
if hard:
angle = 0
# print(angle,counter)
speed = 0.45
counter+=rc.get_delta_time()
if counter > 1.25:
angle = 1
if counter > 2.5:
angle = 0
if counter > 3.75:
angle = -1
if counter > 5:
angle = 0
robotState = State.green_line_follow
timer = 0.0
le_max = 0.7
def challenge3():
global speed, angle
global cur_state, FRONT_LEFT_WINDOW, FRONT_RIGHT_WINDOW, depth_image, color_image,robotState, timer, contour_center
scan = rc.lidar.get_samples()
_, fr_dist = rc_utils.get_lidar_closest_point(scan, FRONT_RIGHT_WINDOW)
_, fl_dist = rc_utils.get_lidar_closest_point(scan, FRONT_LEFT_WINDOW)
markers = rc_utils.get_ar_markers(color_image)
if cur_state == Challenge3State.see_marker:
if len(markers) > 0 and rc_utils.get_pixel_average_distance(depth_image, markers[0].get_corners()[0], 11) < 120 and markers[0].get_orientation().value != 0:
cur_state = Challenge3State.follow_marker
else:
if fr_dist > 150:
angle = 0.4
elif fl_dist > 150:
angle = -0.4
else:
angle = 0
elif cur_state == Challenge3State.follow_marker:
if len(markers) > 0:
if markers[0].get_orientation().value == 1:
angle = -0.75
elif markers[0].get_orientation().value == 3:
angle = 0.75
else:
cur_state = Challenge3State.search
elif cur_state == Challenge3State.search:
if len(markers) > 0:
cur_state = Challenge3State.see_marker
else:
if fr_dist > 120 or fl_dist < 45:
angle = 0.7
elif fl_dist > 120 or fr_dist < 45:
angle = -0.7
else:
angle = 0
if contour_center is not None and timer > 2.0:
robotState = State.green_line_follow
le_max = 0.9
timer = 0.0
def challenge4(ar_color):
global speed, angle, timer, scan, contour_center, robotState
_, front_dis = rc_utils.get_lidar_closest_point(scan, (-10, 10))
speed = 1
angle = 0
print(front_dis)
print(ar_color)
if ar_color == "red" and front_dis < 300:
speed = 0
angle = 0
elif ar_color == "blue" and front_dis > 40:
speed = 0.7
angle = 0
elif ar_color == "blue" and front_dis < 40:
speed = 0
angle = 0
if contour_center is not None and timer > 5.0:
le_max = 0.7
robotState = State.green_line_follow
timer = 0.0
def challenge5():
global contour_center,angle,speed, color_image, image, green_contours, color_image, depth_image,timer
CROP_FLOOR = ((360, 0), (rc.camera.get_height(), rc.camera.get_width()))
if contour_center is not None:
angle = rc_utils.remap_range(contour_center[1], 0, rc.camera.get_width(), -1, 1)
max_speed = 0.5
min_speed = 0.4
speed = math.cos(0.5 * math.pi * angle) * max_speed + min_speed
speed = rc_utils.clamp(speed, min_speed, max_speed)
if 25 < timer < 35:
speed = 0.8
print(timer)
def update_slow():
global robotState
print(robotState)
def challenge6_new():
global speed, angle, timer
global le_max
green_line_follow(le_max)
# print(timer)
if 25 < timer < 32:
le_max = 0.7
else:
le_max = 1
def challenge8_new():
global speed, angle, timer
global le_max
green_line_follow(le_max)
# print(timer)
if timer > 28:
le_max = 1
if __name__ == "__main__":
rc.set_start_update(start, update, update_slow)
rc.go()
def challenge6():
global speed, angle, timer, detected_obstacle, detected_obstacle_time, scan, FRONT_WINDOW, forward_dist,color_image
global train1, train2, train3, Stop
global contour_center, robotState
angle = 0
right_dist = rc_utils.get_lidar_average_distance(scan, 30.0, 5.0)
left_dist = rc_utils.get_lidar_average_distance(scan, -30.0, 5.0)
forward_dist = left_dist+right_dist
print(left_dist, right_dist)
speed = 0.35
# TriggerLeft = rc.controller.Trigger.LEFT
# TriggerRight = rc.controller.Trigger.RIGHT
# speed = rc.controller.get_trigger(TriggerRight)-rc.controller.get_trigger(TriggerLeft)
if forward_dist > 400:
speed = 1
else:
speed = 0
# if not (train1 and train2 and train3):
# speed = .75
# if Stop:
# # speed = -1
# if speed > 0 and forward_dist < 160:
# if not detected_obstacle:
# detected_obstacle = True
# detected_obstacle_time = timer
# if timer - detected_obstacle_time < 0.2:
# speed = -1
# else:
# speed = 0
# elif forward_dist > 160:
# speed = 1
# detected_obstacle = False
# detected_obstacle_time = 0.0
# timer+=rc.get_delta_time()
# if contour_center is not None:
# robotState = State.green_line_follow
# timer = 0.0
def update_contour():
"""
Finds contours in the current color image and uses them to update contour_center
and contour_area
"""
global blue_contour_center
global contour_area
global contours
color_image = rc.camera.get_color_image()
CROP_FLOOR = ((300, 0), (rc.camera.get_height(), rc.camera.get_width()))
image = rc_utils.crop(color_image, CROP_FLOOR[0], CROP_FLOOR[1])
if image is None:
blue_contour_center = None
contour_area = 0
else:
# Find all of the orange contours
contours = rc_utils.find_contours(image, BLUE[0], BLUE[1])
# Select the largest contour
contour = rc_utils.get_largest_contour(contours, MIN_CONTOUR_AREA)
if contour is not None:
# Calculate contour information
blue_contour_center = rc_utils.get_contour_center(contour)
contour_area = rc_utils.get_contour_area(contour)
# Draw contour onto the image
rc_utils.draw_contour(image, contour)
rc_utils.draw_circle(image, blue_contour_center)
else:
blue_contour_center = None
contour_area = 0
# Display the image to the screen
rc.display.show_color_image(image)
def challenge8():
global blue_contour_center
global contour_area
global speed
global angle, timer, robotState, contour_center, green_contours
update_contour()
print(contour_area)
rc.drive.set_max_speed(0.52)
speed = 1.0
if blue_contour_center is not None:
if blue_contour_center[1] > rc.camera.get_width() * 2 // 3:
angle = -0.6
else:
if contour_area > 10000:
angle = -0.35
rc.drive.set_max_speed(1)
else:
angle = 6 * (blue_contour_center[1] - rc.camera.get_width() / 13) / rc.camera.get_width()
angle = rc_utils.clamp(angle, -0.32, 0.32)
else:
angle = 0
if contour_center is not None and timer > 10.0:
robotState = State.green_line_follow
timer = 0.0
return angle