-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFlywheel_Master.c
757 lines (603 loc) · 20 KB
/
Flywheel_Master.c
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
#pragma config(Motor, port1, frontLeftWheel, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port2, backLeftWheel, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port3, middleLeftWheel, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port4, middleRightWheel, tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor, port5, frontRightWheel, tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor, port6, backRightWheel, tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor, port7, Intake, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port8, Indexer, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port9, Flywheel1, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port10, Flywheel2, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port11, Flywheel3, tmotorVex393_HBridge, openLoop, reversed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
/*---------------------------------------------------------------------------*/
/* */
/* Description: Competition template for VEX EDR */
/* */
/*---------------------------------------------------------------------------*/
// This code is for the VEX cortex platform
#pragma platform(VEX2)
// Select Download method as "competition"
#pragma competitionControl(Competition)
//Main competition background code...do not modify!
#include "Vex_Competition_Includes.c"
#define min(a, b) (a) < (b) ? (a) : (b)
#define max(a, b) (a) < (b) ? (b) : (a)
// Global variables
const int dev = -10; // Global deviation
int con = -1; // Conventional direction
bool mat; // Are we on competition mats?
int friction = 15; // Have to account for different surface from competition mats
bool stop_intake; // For stopping ball-intake
bool holding;
bool flags = 1; // For when we're on the flags side during autonomous
bool red = 1; // For when we're on the red side during autonomous
/*---------------------------------------------------------------------------*/
/* Pre-Autonomous Functions */
/* */
/* You may want to perform some actions before the competition starts. */
/* Do them in the following function. You must return from this function */
/* or the autonomous and usercontrol tasks will not be started. This */
/* function is only called once after the cortex has been powered on and */
/* not every time that the robot is disabled. */
/*---------------------------------------------------------------------------*/
void pre_auton () {
// Set bStopTasksBetweenModes to false if you want to keep user created tasks
// running between Autonomous and Driver controlled modes. You will need to
// manage all user created tasks if set to false.
bStopTasksBetweenModes = true;
// Set bDisplayCompetitionStatusOnLcd to false if you don't want the LCD
// used by the competition include file, for example, you might want
// to display your team name on the LCD in this function.
// bDisplayCompetitionStatusOnLcd = false;
// All activities that occur before the competition starts
// Example: clearing encoders, setting servo positions, ...
return;
}
// Global helper functions
// For stopping the drive
void StopDrive () {
motor[frontLeftWheel] = motor[frontRightWheel] = motor[backLeftWheel] = motor[backRightWheel] = motor[middleLeftWheel] = motor[middleRightWheel] = 0;
}
// For stopping the shooter
void StopShoot () {
motor[Flywheel1] = motor[Flywheel2] = 0;
motor[Flywheel3] = 0;
}
// For stopping the claw flipper
void StopDescorer () {
motor[Descorer] = 0;
}
// For stopping the ball intake
void StopBallIntake () {
motor[Intake] = 0;
}
void StopBallIndexer () {
motor[Indexer] = 0;
}
// Drive forward
void DriveF (int amount, int time) {
if (mat) time += friction;
motor[frontLeftWheel] = amount;
motor[frontRightWheel] = amount;
motor[backLeftWheel] = amount;
motor[backRightWheel] = amount;
motor[middleLeftWheel] = amount;
motor[middleRightWheel] = amount;
if (time != 0) wait1Msec(time);
}
// Drive backwards
void DriveB (int amount, int time) {
if (mat) time += friction;
motor[frontLeftWheel] = -amount;
motor[frontRightWheel] = -amount;
motor[backLeftWheel] = -amount;
motor[backRightWheel] = -amount;
motor[middleLeftWheel] = -amount;
motor[middleRightWheel] = -amount;
if (time != 0) wait1Msec(time);
}
// Turn right
void TurnR (int amount, int time) {
if (mat) time += friction;
motor[frontRightWheel] = amount;
motor[middleRightWheel] = amount;
motor[backRightWheel] = amount;
if (time != 0) wait1Msec(time);
}
// Turn left
void TurnL (int amount, int time) {
if (mat) time += friction;
motor[frontLeftWheel] = amount;
motor[middleLeftWheel] = amount;
motor[backLeftWheel] = amount;
if (time != 0) wait1Msec(time);
}
void Turn90L () {
// 344 = competition mat
TurnL(90, 314);
}
void Turn90R () {
// 344 = competition mat
TurnR(90, 314);
}
// For taking ball in
task BallIntake1 () {
motor[Intake] = -100 * con;
}
// For taking ball out
task BallIntake2 () {
motor[Intake] = 100 * con;
}
// Turns both the indexer and loader one way
task Indexer1 () {
motor[Indexer] = 100 * -con;
}
// Turns both the indexer and loader the other way
task Indexer2 () {
motor[Indexer] = -100 * -con;
}
// Turns both the intake and indexer and loader
task Loader1 () {
motor[Intake] = motor[Indexer] = 100 * -con;
}
task turnFlywheelOn () {
motor[Flywheel1] = motor[Flywheel2] = 127 * -con;
motor[Flywheel3] = -127 * -con;
}
// For securing center and low flag points
// Starting position is facing flags
// Shoot middle flag and push low flag
void TwoFlags () {
startTask(turnFlywheelOn);
wait1Msec(5000);
DriveF(100, 600); // Now prepare to hit middle flag
StopDrive();
// 2nd ball is much lower (right under the indexer)
startTask(Indexer1);
wait1Msec(3000);
stopTask(Indexer1);
StopBallIndexer();
stopTask(turnFlywheelOn);
StopBallIndexer();
DriveF(100,1500) ; //Drive into low flag
}
// Auto shoots the balls onto the top 2 flags
void autoShoot () {
startTask(turnFlywheelOn);
wait1Msec(3000); // MAY NEED TO CHANGE THE TIME VALUE
// Shoot 1st ball
startTask(Indexer1);
wait1Msec(1500);
stopTask(Indexer1);
StopBallIndexer();
// wait1Msec(500); // WAIT BEFORE DRIVING FORWARD
// MAY NEED TO CHANGE THE TIME VALUE
DriveF(100, 500); // Now prepare to hit middle flag
StopDrive();
// 2nd ball is much lower (right under the indexer)
startTask(Indexer1);
wait1Msec(2500); // MAY NEED TO CHANGE THE TIME VALUE
stopTask(Indexer1);
StopBallIndexer();
stopTask(turnFlywheelOn);
}
// Rotate the claw to flip the object
void DescorerUp (int amount, int time) {
motor[Descorer] = amount * con;
if (time != 0) wait1Msec(time);
}
void DescorerDown (int amount, int time) {
motor[Descorer] = amount * -con;
if (time != 0) wait1Msec(time);
}
/*---------------------------------------------------------------------------*/
/* */
/* Autonomous Task */
/* */
/* This task is used to control your robot during the autonomous phase of */
/* a VEX Competition. */
/* */
/* You must modify the code to add your own robot specific commands here. */
/*---------------------------------------------------------------------------*/
// For when we are on the side with no flags during autonomous
void autoWithFlags (bool red) {
int a,b,c;//arguement variables for red and no red based on red
// Red side
if (red) {
// First start intake
startTask(BallIntake1);
// Drive up to get the ball underneath the cap
a = 1000;
DriveF(100, a);
StopDrive();
stopTask(BallIntake1);
b = 500;
wait1Msec(b);
// Drive back and rotate 90 degrees CCW
c = 700;
DriveB(100, c);
StopDrive();
Turn90L();
StopDrive();
/* Can alternatively use autoShoot() instead of code below */
autoShoot();
// Shoot the ball onto the top flag
// Stop flywheel and drive up then stop drive
// Turn on flywheel to shoot middle flag
// Turn off flywheel and drive straight to toggle low flag
DriveF(100, b);
StopDrive();
// Drive back and turn 90 degrees CW
c=250;
DriveB(100, c);
StopDrive();
Turn90R();
StopDrive();
// Flip over the cap/drive forwards simultaneously
startTask(BallIntake2);
DriveF(100, 300);
StopDrive();
stopTask(BallIntake2);
// Drive back and turn 90 degrees CW
DriveB(100, 250);
StopDrive();
Turn90R();
StopDrive();
// Drive forwards to get onto the platform
DriveF(100, 200);
StopDrive();
// Turn 90 degrees CCW and drive onto the platform
Turn90L();
StopDrive();
DriveF(100, 1000);
StopDrive();
}
// Blue side (left and right are flipped)
else {
// First start intake
startTask(BallIntake1);
// Drive up to get the ball underneath the cap
DriveF(100, a);
StopDrive();
stopTask(BallIntake1);
wait1Msec(b);
// Drive back and rotate 90 degrees CW
DriveB(100, c);
StopDrive();
Turn90R();
StopDrive();
/* Can alternatively use autoShoot() instead of code below */
autoShoot();
// Shoot the ball onto the top flag
// Stop flywheel and drive up then stop drive
// Turn on flywheel to shoot middle flag
// Turn off flywheel and drive straight to toggle low flag
DriveF(100, 700);
StopDrive();
// Drive back and turn 90 degrees CCW
DriveB(100, 250);
StopDrive();
Turn90L();
StopDrive();
// Flip over the cap/drive forwards simultaneously
startTask(BallIntake2);
DriveF(100, 300);
StopDrive();
stopTask(BallIntake2);
// Drive back and turn 90 degrees CCW
DriveB(100, 250);
StopDrive();
Turn90L();
StopDrive();
// Drive forwards to get onto the platform
DriveF(100, 200);
StopDrive();
// Turn 90 degrees CW and drive onto the platform
Turn90R();
StopDrive();
DriveF(100, 1000);
StopDrive();
}
}
// For when we are on the side with flags during autonomous
void autoNoFlags (bool red) {
// Red side
if (red) {
// Drive straight to align with the opponent's cap
DriveF(100, 100);
StopDrive();
// Turn 90 degrees CCW and drive forwards, while turning on ball intake, to flip over the cap
Turn90L();
StopDrive();
startTask(BallIntake2);
DriveF(100, 500);
stopTask(BallIntake2);
// Now drive back and turn 90 degrees CCW
DriveB(100, 400);
StopDrive();
Turn90L();
StopDrive();
// Drive forwards and turn 90 degrees CW to align with the platform
DriveF(100, 100);
StopDrive();
Turn90R();
StopDrive();
// Now drive forwards to get onto the platform
DriveF(100, 1000);
StopDrive();
}
// Blue side (left and right are flipped)
else {
// Drive straight to align with the opponent's cap
DriveF(100, 100);
StopDrive();
// Turn 90 degrees CW and drive forwards, while turning on ball intake, to flip over the cap
Turn90R();
StopDrive();
startTask(BallIntake2);
DriveF(100, 500);
stopTask(BallIntake2);
// Now drive back and turn 90 degrees CW
DriveB(100, 400);
StopDrive();
Turn90R();
StopDrive();
// Drive forwards and turn 90 degrees CCW to align with the platform
DriveF(100, 100);
StopDrive();
Turn90L();
StopDrive();
// Now drive forwards to get onto the platform
DriveF(100, 1000);
StopDrive();
}
}
// Used after all 3 flags are toggled - goes back and flips the opponent's cap
void autoCapFlipper () {
if (red) {
// Drive back and turn 90 degrees CW
DriveB(100, 500); // MAY NEED TO CHANGE THE TIME VALUE
StopDrive();
Turn90R();
StopDrive();
// Flip over the cap/drive forwards simultaneously
startTask(BallIntake2);
DriveF(100, 1000); // MAY NEED TO CHANGE THE TIME VALUE
StopDrive();
stopTask(BallIntake2);
}
else {
// Drive back and turn 90 degrees CCW
DriveB(100, 500); // MAY NEED TO CHANGE THE TIME VALUE
StopDrive();
Turn90L();
StopDrive();
// Flip over the cap/drive forwards simultaneously
startTask(BallIntake2);
DriveF(100, 1000); // MAY NEED TO CHANGE THE TIME VALUE
StopDrive();
stopTask(BallIntake2);
}
}
void ThreeFlags () {
if (red) {
// First start intake
startTask(BallIntake1);
// Drive up to get the ball underneath the cap
DriveF(100, 1500); // MAY NEED TO CHANGE THE TIME VALUE
StopDrive();
stopTask(BallIntake1);
// wait1Msec(500);
// Drive back and rotate 90 degrees CCW
DriveB(100, 1000); // MAY NEED TO CHANGE THE TIME VALUE
StopDrive();
Turn90L();
StopDrive();
/* Can alternatively use autoShoot() instead of code below */
autoShoot();
// Shoot the ball onto the top flag
// Stop flywheel and drive up then stop drive
// Turn on flywheel to shoot middle flag
// Turn off flywheel and drive straight to toggle low flag
DriveF(100, 1000); // MAY NEED TO CHANGE THE TIME VALUE
StopDrive();
// Now flip over opponent's cap
autoCapFlipper();
}
else {
// First start intake
startTask(BallIntake1);
// Drive up to get the ball underneath the cap
DriveF(100, 1500); // MAY NEED TO CHANGE THE TIME VALUE
StopDrive();
stopTask(BallIntake1);
// wait1Msec(500);
// Drive back and rotate 90 degrees CW
DriveB(100, 1000); // MAY NEED TO CHANGE THE TIME VALUE
StopDrive();
Turn90R();
StopDrive();
/* Can alternatively use autoShoot() instead of code below */
autoShoot();
// Shoot the ball onto the top flag
// Stop flywheel and drive up then stop drive
// Turn on flywheel to shoot middle flag
// Turn off flywheel and drive straight to toggle low flag
DriveF(100, 1000); // MAY NEED TO CHANGE THE TIME VALUE
StopDrive();
// Now flip over opponent's cap
autoCapFlipper();
}
}
task autonomous () {
// ..........................................................................
// Insert user code here.
// ..........................................................................
// Still needs TESTING
if (flags) {
// TwoFlags();
ThreeFlags();
}
else {
autoNoFlags(red);
}
}
/*---------------------------------------------------------------------------*/
/* */
/* User Control Task */
/* */
/* This task is used to control your robot during the user control phase of */
/* a VEX Competition. */
/* */
/* You must modify the code to add your own robot specific commands here. */
/*---------------------------------------------------------------------------*/
int facing = 1;
task usercontrol () {
// User control code here, inside the loop
while (1) {
/*
// FORWARD AND BACKWARD
int tmp = 0;
double backDev;
if (vexRT[Ch2] > 20) {
tmp = min(vexRT[Ch2], 110);
backDev = 1;
}
else if (vexRT[Ch2] < -20) {
tmp = max(vexRT[Ch2], -110);
backDev = 0.978;
}
motor[DriveLeft1] = tmp * con*backDev;
motor[DriveRight1] = 0.9*tmp * con*backDev;
motor[DriveRight2] = 0.9*tmp * con*backDev;
motor[DriveLeft2] = tmp * con*backDev;
// Left axle turn
if (vexRT[Btn5U]) {
motor[DriveLeft1] = vexRT[Btn5U] * 90 * con;
motor[DriveRight1] = vexRT[Btn5U] * -90 * con;
motor[DriveRight2] = vexRT[Btn5U] * -90 * con;
motor[DriveLeft2] = vexRT[Btn5U] * 90 * con;
}
// Right axle turn
else if (vexRT[Btn6U]) {
motor[DriveLeft1] = vexRT[Btn6U] * 90 * -con;
motor[DriveRight1] = vexRT[Btn6U] * -90 * -con;
motor[DriveRight2] = vexRT[Btn6U] * -90 * -con;
motor[DriveLeft2] = vexRT[Btn6U] * 90 * -con;
}
// Turn left sensitive
if (vexRT[Btn7L]) {
motor[DriveLeft1] = 40 * con;
motor[DriveRight1] = 0;
motor[DriveRight2] = 0;
motor[DriveLeft2] = 40 * con;
}
// Turn right sensitive
else if (vexRT[Btn7R]) {
motor[DriveLeft1] = 0;
motor[DriveRight1] = -40 * -con;
motor[DriveRight2] = -40 * -con;
motor[DriveLeft2] = 0;
}
*/
if (facing == 1) {
if (vexRT[Ch2] && vexRT[Ch3]) //WHEELS CONTROL (JOYSTICKS){
motor[frontLeftWheel] = vexRT[Ch3];
motor[frontRightWheel] = vexRT[Ch2];
motor[backLeftWheel] = vexRT[Ch3];
motor[backRightWheel] = vexRT[Ch2];
motor[middleLeftWheel] = vexRT[Ch3];
motor[middleRightWheel] = vexRT[Ch2];
}
else if (vexRT[Ch3]) //LEFT SIDE
{
motor[frontLeftWheel] = vexRT[Ch3];
motor[middleLeftWheel] = vexRT[Ch3];
motor[backLeftWheel] = vexRT[Ch3];
}
else if (vexRT[Ch2]) //RIGHT SIDE
{
motor[frontRightWheel] = vexRT[Ch2];
motor[middleRightWheel] = vexRT[Ch3];
motor[backRightWheel] = vexRT[Ch2];
}
else {
StopDrive();
}
} else if (facing == 2)
{
if (vexRT[Ch2] && vexRT[Ch3]) //WHEELS CONTROL (JOYSTICKS)
{
motor[frontLeftWheel] = -vexRT[Ch3];
motor[frontRightWheel] = -vexRT[Ch2];
motor[backLeftWheel] = -vexRT[Ch3];
motor[backRightWheel] = -vexRT[Ch2];
motor[middleLeftWheel] = -vexRT[Ch3];
motor[middleRightWheel] = -vexRT[Ch2];
}
else if (vexRT[Ch3]) //LEFT SIDE
{
motor[frontLeftWheel] = vexRT[Ch3];
motor[middleLeftWheel] = vexRT[Ch3];
motor[backLeftWheel] = vexRT[Ch3];
}
else if (vexRT[Ch2]) //RIGHT SIDE
{
motor[frontRightWheel] = vexRT[Ch2];
motor[middleRightWheel] = vexRT[Ch3];
motor[backRightWheel] = vexRT[Ch2];
}
else {
StopDrive();
}
}
// Turn on the flywheel
if (vexRT[Btn8U]) {
int res = vexRT[Btn8U];
motor[Flywheel1] = motor[Flywheel2] = res * 127 * -con;
motor[Flywheel3] = res * -127 * -con;
}
else {
StopShoot();
}
// Turn off the flywheel
if (vexRT[Btn8D]) {
StopShoot();
}
// Ball intake (CW) working
if (vexRT[Btn6D]) {
motor[Intake] = vexRT[Btn6D] * -100 * con;
}
// Ball intake (CCW) working
else if (vexRT[Btn5D]) {
motor[Intake] = vexRT[Btn5D] * 100 * con;
}
else {
StopBallIntake();
}
// Implement indexer and (maybe) 3rd intake here...
if (vexRT[Btn7U]) {
motor[Indexer] = vexRT[Btn7U] * -100 * con;
}
else if (vexRT[Btn7D]) {
motor[Indexer] = vexRT[Btn7D] * 100 * con;
}
else {
StopBallIndexer();
}
// For auto shoot (top 2 flags)
if (vexRT[Btn8L]) {
autoShoot();
}
// For turning 90 degrees CW
if (vexRT[Btn8R]) {
Turn90R();
}
if (vexRT[Btn5U]) {
facing = 1;
}
else if (vexRT[Btn6U]) {
facing = 2;
}
}