This repository has been archived by the owner on Jun 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
439 lines (362 loc) · 15.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
#region VEXcode Generated Robot Configuration
from math import *
import random
from vexcode_vrc import *
from vexcode_vrc.events import get_Task_func
# constructors
drivetrain = Drivetrain()
brain = Brain()
bottom_distance = Distance("BottomDistance", 18)
roller_optical = Optical("RollerOptical", 2)
gps = GPS("GPS", 3)
intake_motor_group = Motor("IntakeMotorGroup", 10)
bottom_line_tracker = LineTracker("BottomLineTracker", 22)
middle_line_tracker = LineTracker("MiddleLineTracker", 23)
top_line_tracker = LineTracker("TopLineTracker", 24)
#endregion VEXcode Generated Robot Configuration
#region constants
BLUE_HIGH_GOAL_COORDINATE = [-1307, -1307]
RED_HIGH_GOAL_COORDINATE = [ 1307, 1307]
#endregion constants
#region helper functions
def format_angle(a):
"""Map an arbitary angle to -180 to 180 degrees format"""
# check whether the angle is positive
sign = 1
if a < 0:
sign = -1
else:
sign = 1
positive_a = abs(a)
# eliminate coterminal angles greater than 360 degrees
mod = positive_a % 360
if (mod <= 180):
return sign * mod
else:
return sign * (mod - 360)
#endregion helper functions
#region class definitions
class Coordinates:
def __init__(self, x, y, theta=0):
self.x = x
self.y = y
self.theta = theta
class Chassis:
__instance__ = None
def __init__(self):
if Chassis.__instance__ is None:
Chassis.__instance__ = self
else:
raise Exception("You cannot create another Chassis class")
def face_angle(self, angle):
"""Orients the robot to a specific absolute orientation"""
drivetrain.set_turn_velocity(100,PERCENT)
position = Coordinates(gps.x_position(MM), gps.y_position(MM), gps.heading())
# calculates the angle of rotation
target_angle = format_angle(angle - position.theta)
drivetrain.turn_for(RIGHT, target_angle, DEGREES)
def face_coordinate(self, x, y, aiming, offset):
"""Rotates the robot to face a specific coordinate on the field"""
drivetrain.set_turn_velocity(100,PERCENT)
position = Coordinates(gps.x_position(MM), gps.y_position(MM), gps.heading())
# relative coordinate difference between the point and the robot's position
x_dist = x - position.x
y_dist = y - position.y
# absolute orientation to face a point
target_angle = 90 - atan2(y_dist, x_dist) * 180 / pi
if aiming:
# face the coordinate backwards
drivetrain.turn_for(RIGHT, float(format_angle(180 + target_angle - position.theta + offset)), DEGREES)
else:
# face the coordinate
drivetrain.turn_for(RIGHT, float(format_angle(target_angle - position.theta + offset)), DEGREES)
def move_to_point(self, x, y):
"""Moves the robot to a specific coordinate on the field"""
drivetrain.set_drive_velocity(100, PERCENT)
position = Coordinates(gps.x_position(MM), gps.y_position(MM), gps.heading())
# relative coordinate difference between the point and the robot's position
x_dist = x - position.x
y_dist = y - position.y
# calculate the distance to the point
dist = sqrt(x_dist**2 + y_dist**2)
# absolute orientation to face a point
target_angle = 90 - atan2(y_dist, x_dist) * 180 / pi
# face the coordinate
drivetrain.turn_for(RIGHT, float(format_angle(target_angle - position.theta)), DEGREES)
# move to the point
drivetrain.drive_for(FORWARD, dist, MM)
def move_to_point_backwards(self, x, y):
"""Moves the robot backwards to a specific coordinate on the field"""
drivetrain.set_drive_velocity(100, PERCENT)
position = Coordinates(gps.x_position(MM), gps.y_position(MM), gps.heading())
# relative coordinate difference between the point and the robot's position
x_dist = x - position.x
y_dist = y - position.y
# distance to the point
dist = sqrt(x_dist**2 + y_dist**2)
# absolute orientation to face a point
target_angle = 90 - atan2(y_dist, x_dist) * 180 / pi
# face the coordinate backwards
drivetrain.turn_for(RIGHT, float(format_angle(180 + target_angle - position.theta)), DEGREES)
# move backwards to the point
drivetrain.drive_for(REVERSE, dist, MM)
class Roller:
__instance__ = None
def __init__(self):
if Roller.__instance__ is None:
Roller.__instance__ = self
else:
raise Exception("You cannot create another Roller class")
def score(self):
"""Score the roller in front of the robot"""
drivetrain.drive(REVERSE)
# Once the roller is detected, drive closer to make contact
while not roller_optical.is_near_object():
wait(5, MSEC)
drivetrain.stop()
drivetrain.drive_for(REVERSE, 35, MM)
# Spin the intake to score the roller
intake_motor_group.set_velocity(100, PERCENT)
intake_motor_group.spin_for(FORWARD, 34, DEGREES)
class Robot:
__instance__ = None
def __init__(self):
if Robot.__instance__ is None:
self.chassis = Chassis()
self.roller = Roller()
Robot.__instance__ = self
else:
raise Exception("You cannot create another Robot class")
@staticmethod
def face_angle(angle):
"""Orients the robot to a specific absolute orientation"""
Robot.__instance__.chassis.face_angle(angle)
@staticmethod
def face_coordinate(x, y, aiming = False, offset = 0):
"""Rotates the robot to face a specific coordinate on the field"""
Robot.__instance__.chassis.face_coordinate(x, y, aiming, offset)
@staticmethod
def move_to_point(x, y):
"""Moves the robot to a specific coordinate on the field"""
Robot.__instance__.chassis.move_to_point(x, y)
@staticmethod
def move_to_point_backwards(x, y):
"""Moves the robot backwards to a specific coordinate on the field"""
Robot.__instance__.chassis.move_to_point_backwards(x, y)
@staticmethod
def score_roller():
"""Score the roller in front of the robot"""
Robot.__instance__.roller.score()
@staticmethod
def shoot_disk(degrees):
"""Rotate the intake to shoot disks"""
intake_motor_group.spin_for(REVERSE, degrees, DEGREES)
intake_motor_group.spin(REVERSE)
# wait until all disks are shot
while top_line_tracker.reflectivity(PERCENT) > 80 or \
middle_line_tracker.reflectivity(PERCENT) > 80 or \
bottom_line_tracker.reflectivity(PERCENT) > 80:
wait(5, MSEC)
intake_motor_group.stop()
#endregion class definitions
# ----------------------------------------------------------------------------
#
# Project: 14683A VR skills
# Description: The source code for 14683A's VR skills
#
# Starting Position: A
# Preload: 2
#
# ----------------------------------------------------------------------------
robot = Robot()
def main():
drivetrain.set_drive_velocity(100, PERCENT)
drivetrain.set_turn_velocity(100,PERCENT)
intake_motor_group.set_velocity(100, PERCENT)
# ------------------------------------------------------------------------
# Blue half field route
# ------------------------------------------------------------------------
# shoot out first three disks
robot.face_coordinate(BLUE_HIGH_GOAL_COORDINATE[0], BLUE_HIGH_GOAL_COORDINATE[1], aiming=True, offset=-2)
intake_motor_group.spin(REVERSE)
robot.move_to_point(-1450, 1260)
robot.face_coordinate(BLUE_HIGH_GOAL_COORDINATE[0], BLUE_HIGH_GOAL_COORDINATE[1], aiming=True, offset=-2)
robot.shoot_disk(degrees=110)
# score one roller
robot.face_angle(90)
robot.score_roller()
# score one roller
drivetrain.drive_for(FORWARD, 20, MM)
robot.move_to_point(-900, 1475)
robot.face_angle(180)
robot.score_roller()
# intake three stacked disks (move at the same time)
robot.move_to_point(-900, 1120)
drivetrain.set_drive_velocity(17, PERCENT)
drivetrain.drive(FORWARD)
intake_motor_group.spin_for(REVERSE, 100, DEGREES)
drivetrain.stop()
drivetrain.set_drive_velocity(100, PERCENT)
# shoot out three disks (move at the same time)
intake_motor_group.set_velocity(30, PERCENT)
intake_motor_group.spin(REVERSE)
robot.face_coordinate(RED_HIGH_GOAL_COORDINATE[0], RED_HIGH_GOAL_COORDINATE[1], aiming=True, offset=-4.6)
intake_motor_group.set_velocity(90, PERCENT)
drivetrain.set_drive_velocity(40, PERCENT)
drivetrain.drive_for(REVERSE, 400, MM)
intake_motor_group.stop()
intake_motor_group.set_velocity(100, PERCENT)
# intake and shoot out three disks (move at the same time)
intake_motor_group.set_velocity(80, PERCENT)
intake_motor_group.spin(REVERSE)
robot.move_to_point(298, 290)
intake_motor_group.set_velocity(1.5, PERCENT)
robot.face_coordinate(BLUE_HIGH_GOAL_COORDINATE[0], BLUE_HIGH_GOAL_COORDINATE[1], aiming=True, offset=-2)
intake_motor_group.set_velocity(90, PERCENT)
drivetrain.set_drive_velocity(19, PERCENT)
drivetrain.drive(FORWARD)
robot.shoot_disk(degrees=95)
drivetrain.stop()
drivetrain.set_drive_velocity(100, PERCENT)
intake_motor_group.set_velocity(100, PERCENT)
# intake and shoot out three blue low goal disks
intake_motor_group.spin(REVERSE)
robot.move_to_point(450, 500)
robot.move_to_point(440, 700)
robot.move_to_point(450, 900)
robot.move_to_point(460, 1000)
intake_motor_group.stop()
robot.move_to_point_backwards(420, 380)
intake_motor_group.set_velocity(95, PERCENT)
robot.face_coordinate(BLUE_HIGH_GOAL_COORDINATE[0], BLUE_HIGH_GOAL_COORDINATE[1], aiming=True, offset=-1)
robot.shoot_disk(degrees=100)
# intake three blue low goal disks
intake_motor_group.set_velocity(90, PERCENT)
intake_motor_group.spin(REVERSE)
robot.move_to_point(520, 400)
robot.move_to_point(750, 440)
robot.move_to_point(950, 440)
robot.face_angle(80)
wait(50,MSEC)
intake_motor_group.set_velocity(1.5, PERCENT)
# shoot out three blue low goal disks and seven match load
robot.move_to_point(1538, 212)
robot.face_coordinate(RED_HIGH_GOAL_COORDINATE[0], RED_HIGH_GOAL_COORDINATE[1], aiming=True, offset=7.2)
intake_motor_group.set_velocity(100, PERCENT)
robot.shoot_disk(degrees=398)
# intake three stacked disks (move at the same time)
robot.move_to_point(1125, -153)
drivetrain.set_drive_velocity(6, PERCENT)
drivetrain.drive(FORWARD)
intake_motor_group.spin_for(REVERSE, 85, DEGREES)
drivetrain.stop()
drivetrain.set_drive_velocity(100, PERCENT)
intake_motor_group.stop()
# shoot out six disks (move at the same time)
robot.face_angle(191)
intake_motor_group.set_velocity(92, PERCENT)
intake_motor_group.spin(REVERSE)
robot.move_to_point(928, -694)
drivetrain.set_drive_velocity(16, PERCENT)
drivetrain.drive(FORWARD)
robot.shoot_disk(degrees=180)
drivetrain.stop()
drivetrain.set_drive_velocity(100, PERCENT)
intake_motor_group.set_velocity(100, PERCENT)
# intake and shoot out two disks
intake_motor_group.spin(REVERSE)
robot.move_to_point(1342, -1342)
robot.move_to_point_backwards(1402, -1175)
robot.face_coordinate(RED_HIGH_GOAL_COORDINATE[0], RED_HIGH_GOAL_COORDINATE[1], aiming=True, offset=-1.5)
robot.shoot_disk(degrees=50)
intake_motor_group.stop()
# score two rollers
robot.face_angle(270)
robot.score_roller()
drivetrain.drive_for(FORWARD, 100, MM)
robot.move_to_point_backwards(903, -1435)
robot.face_angle(0)
robot.score_roller()
# ------------------------------------------------------------------------
# Red half field route
# ------------------------------------------------------------------------
# intake three disks
drivetrain.drive_for(FORWARD, 50, MM)
intake_motor_group.set_velocity(70, PERCENT)
intake_motor_group.spin(REVERSE)
robot.move_to_point(-308, -335)
# shoot out three disks (move at the same time)
intake_motor_group.set_velocity(1.5, PERCENT)
robot.face_coordinate(RED_HIGH_GOAL_COORDINATE[0], RED_HIGH_GOAL_COORDINATE[1], aiming=True, offset=-2)
intake_motor_group.set_velocity(95, PERCENT)
drivetrain.set_drive_velocity(13, PERCENT)
drivetrain.drive(FORWARD)
robot.shoot_disk(degrees=100)
drivetrain.stop()
drivetrain.set_drive_velocity(100, PERCENT)
intake_motor_group.set_velocity(100, PERCENT)
# intake three red low goal disks
intake_motor_group.spin(REVERSE)
robot.move_to_point(-458, -480)
robot.move_to_point(-480, -680)
robot.move_to_point(-480, -1000)
wait(20, MSEC)
# shoot out three red low goal disks (move at the same time)
intake_motor_group.set_velocity(1, PERCENT)
robot.move_to_point_backwards(-450, -400)
robot.face_coordinate(RED_HIGH_GOAL_COORDINATE[0], RED_HIGH_GOAL_COORDINATE[1], aiming=True)
intake_motor_group.set_velocity(98, PERCENT)
drivetrain.set_drive_velocity(1, PERCENT)
drivetrain.drive(FORWARD)
robot.shoot_disk(degrees=110)
drivetrain.stop()
drivetrain.set_drive_velocity(100, PERCENT)
# intake three red low goal disks
intake_motor_group.spin(REVERSE)
robot.move_to_point(-508, -420)
robot.move_to_point(-708, -440)
robot.move_to_point(-965, -440)
wait(50, MSEC)
# shoot out three red low goal disks and seven match load
intake_motor_group.set_velocity(0.5, PERCENT)
robot.move_to_point(-1540, -208)
robot.face_coordinate(BLUE_HIGH_GOAL_COORDINATE[0], BLUE_HIGH_GOAL_COORDINATE[1], aiming=True, offset=2.5)
intake_motor_group.set_velocity(100, PERCENT)
robot.shoot_disk(degrees=392)
# intake and shoot out three stacked disks
robot.move_to_point(-1126, 145)
intake_motor_group.spin_for(REVERSE, 120, DEGREES)
robot.move_to_point(-1173.7350199733687, 279.46071904127825);
robot.face_coordinate(BLUE_HIGH_GOAL_COORDINATE[0], BLUE_HIGH_GOAL_COORDINATE[1], aiming=True, offset=-2)
intake_motor_group.set_velocity(95, PERCENT)
intake_motor_group.spin(REVERSE)
drivetrain.drive_for(FORWARD, 850, MM)
intake_motor_group.stop()
# ------------------------------------------------------------------------
# Middle field route
# ------------------------------------------------------------------------
# intake three disks
robot.move_to_point(-877.2636484687084, 870.7833875511361)
intake_motor_group.set_velocity(30, PERCENT)
intake_motor_group.spin(REVERSE)
drivetrain.drive_for(FORWARD, 300, MM)
intake_motor_group.set_velocity(100, PERCENT)
drivetrain.drive_for(FORWARD, 610, MM)
# move to the middle of the field
intake_motor_group.stop()
drivetrain.drive_for(FORWARD, 540, MM)
intake_motor_group.set_velocity(0.1, PERCENT)
intake_motor_group.spin(REVERSE)
# shoot out three disks
robot.face_coordinate(BLUE_HIGH_GOAL_COORDINATE[0], BLUE_HIGH_GOAL_COORDINATE[1], aiming=True, offset=1)
intake_motor_group.set_velocity(95, PERCENT)
robot.shoot_disk(degrees=100)
# intake two disks
intake_motor_group.set_velocity(100, PERCENT)
intake_motor_group.spin(REVERSE)
robot.move_to_point(608, -608)
# shoot out two disks
robot.face_coordinate(BLUE_HIGH_GOAL_COORDINATE[0], BLUE_HIGH_GOAL_COORDINATE[1], aiming=True, offset=1)
intake_motor_group.set_velocity(95, PERCENT)
intake_motor_group.spin(REVERSE)
vr_thread(main)