-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMaster.c
471 lines (430 loc) · 14.4 KB
/
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
/*
NOTICE: Version without sensors
Date: Jan 12th, 2018
*/
#pragma config(Sensor, in1, Poten, sensorPotentiometer)
#pragma config(Motor, port2, DriveLeft1, tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor, port3, DriveLeft2, tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor, port4, DriveRight1, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port5, DriveRight2, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port6, Claw, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port7, Shooter, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port8, Intake1, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port9, Intake2, tmotorVex393_MC29, openLoop)
//*!!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 stop_intake; // For stopping ball-intake
bool holding;
bool flags; // For when we're on the flags side during autonomous
bool red; // 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[DriveLeft1] = motor[DriveRight1] = motor[DriveRight2] = motor[DriveLeft2] = 0;
}
// For stopping the claw flipper
void StopClaw () {
motor[Claw] = 0;
}
// For stopping the shooter
void StopShoot () {
motor[Shooter] = 0;
}
// For stopping the ball intake
void StopBallIntake () {
motor[Intake1] = motor[Intake2] = 0;
}
// Drive forward
void DriveF (int amount, int time) {
int tmp = amount;
motor[DriveLeft1] = tmp * -con;
motor[DriveRight1] = tmp * -con - dev;
motor[DriveRight2] = tmp * -con - dev;
motor[DriveLeft2] = tmp * -con;
if (time != 0) wait1Msec(time);
}
// Drive backwards
void DriveB (int amount, int time) {
int tmp = -amount;
motor[DriveLeft1] = tmp * -con;
motor[DriveRight1] = tmp * -con - dev;
motor[DriveRight2] = tmp * -con - dev;
motor[DriveLeft2] = tmp * -con;
if (time != 0) wait1Msec(time);
}
// Turn right
void TurnR (int amount, int time) {
motor[DriveLeft1] = amount * -con;
motor[DriveRight1] = -amount * -con;
motor[DriveRight2] = -amount * -con;
motor[DriveLeft2] = amount * -con;
if (time != 0) wait1Msec(time);
}
// Turn left
void TurnL (int amount, int time) {
motor[DriveLeft1] = amount * con;
motor[DriveRight1] = -amount * con;
motor[DriveRight2] = -amount * con;
motor[DriveLeft2] = amount * con;
if (time != 0) wait1Msec(time);
}
// Rotate the claw to flip the object
void ClawUp (int amount, int time) {
motor[Claw] = amount * con;
if (time != 0) wait1Msec(time);
}
void ClawDown (int amount, int time) {
motor[Claw] = amount * -con;
if (time != 0) wait1Msec(time);
}
// For taking ball in - need to lower the catapult to load the balls in
void BallIntake1 (int amount, int time) {
motor[Intake1] = -amount * con;
motor[Intake2] = -amount * -con;
// HoldShoot(3000);
if (time != 0) wait1Msec(time);
}
// For taking ball out - need to lower the catapult to load the balls in
void BallIntake2 (int amount, int time) {
motor[Intake1] = amount * con;
motor[Intake2] = amount * -con;
// HoldShoot(3000);
if (time != 0) wait1Msec(time);
}
// Should hold the catapult in place
task HoldShoot () {
// BallIntake1(127, 0);
motor[Shooter] = 127;
wait1Msec(1200);
motor[Shooter] = 30;
wait1Msec(2500); // Wait to lock in place
/*
motor[Shooter] = 127;
wait1Msec(700);
motor[Shooter] = 0;
*/
}
// For shooting the ball up - automate it for now
void ShootUp (int amount) {
motor[Shooter] = -amount * -con;
wait1Msec(1500);
motor[Shooter] = -amount * -con;
wait1Msec(700);
motor[Shooter] = 0;
// if (time != 0) wait1Msec(time);
}
// For shooting the ball down - automate it for now
void ShootDown (int amount) {
motor[Shooter] = amount * -con;
wait1Msec(1500);
motor[Shooter] = amount * -con;
wait1Msec(700);
motor[Shooter] = 0;
// if (time != 0) wait1Msec(time);
}
// Ball intake method as a task
task auto_intake () {
motor[Intake1] = -127 * con;
motor[Intake2] = -127 * -con;
}
// Used along with ball intake to run simultaneously during autonomous
task lock_catapult () {
motor[Shooter] = 127;
wait1Msec(1200);
motor[Shooter] = 30;
wait1Msec(2500); // Wait to lock in place
}
/*---------------------------------------------------------------------------*/
/* */
/* 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. */
/*---------------------------------------------------------------------------*/
// Autonomous Functions
// For when we are on the side with no flags during autonomous
void autoNoFlags (bool red) {
// Red side
if (red) {
// Flip opponent's cap over first
DriveB(127, 2500);
StopDrive();
TurnL(127, 1100);
StopDrive();
DriveF(127, 1500);
StopDrive();
TurnR(127, 1100);
StopDrive();
DriveB(127, 2200);
StopDrive();
ClawUp(127, 1100); // Make claw touch the floor
StopClaw();
DriveB(127, 200);
StopDrive();
ClawDown(127, 1100); // Make claw go up to flip cap
StopClaw();
TurnR(127, 1400);
StopDrive();
// Try running intake and locking at same time
startTask(auto_intake);
// BallIntake1(127, 0);
DriveF(127, 2000);
StopDrive();
startTask(lock_catapult);
// StopBallIntake();
wait1Msec(1000);
StopTask(auto_intake);
StopTask(lock_catapult);
}
// Blue side (left and right are flipped)
else {
// Flip opponent's cap over first
DriveB(127, 2500);
StopDrive();
TurnR(127, 1100);
StopDrive();
DriveF(127, 1500);
StopDrive();
TurnL(127, 1100);
StopDrive();
DriveB(127, 2200);
StopDrive();
ClawUp(127, 1100); // Make claw touch the floor
StopClaw();
DriveB(127, 200);
StopDrive();
ClawDown(127, 1100); // Make claw go up to flip cap
StopClaw();
TurnL(127, 1400);
StopDrive();
// Try running intake and locking at same time
startTask(auto_intake);
// BallIntake1(127, 0);
DriveF(127, 2000);
StopDrive();
startTask(lock_catapult);
// StopBallIntake();
wait1Msec(1000);
StopTask(auto_intake);
StopTask(lock_catapult);
}
}
// For when we are on the side with flags during autonomous
void autoWithFlags (bool red) {
// Red side
if (red) {
// Shoot the ball at second/third flag
TurnR(127, 1100);
StopDrive();
DriveF(127, 500);
StopDrive();
ShootDown(127)
// Now drive forward to hit lowest flag (with the back bar)
TurnR(127, 2400); // Turn around 180 degrees
StopDrive();
DriveB(127, 2000);
StopDrive();
// Now flip the cap over
DriveF(127, 500);
StopDrive();
TurnR(127, 1100); // Turn 90 degrees
StopDrive();
DriveF(127, 500);
StopDrive();
// Get claw down
ClawUp(127, 1100);
StopClaw();
DriveB(127, 600);
StopDrive();
// Flip cap
ClawDown(127, 1100);
StopClaw();
}
// Blue side (left and right are flipped)
else {
// Shoot the ball at second/third flag
TurnL(127, 1100);
StopDrive();
DriveF(127, 500);
StopDrive();
ShootDown(127)
// Now drive forward to hit lowest flag (with the back bar)
TurnL(127, 2400);
StopDrive();
DriveB(127, 2000);
StopDrive();
// Now flip the cap over
DriveF(127, 500);
StopDrive();
TurnL(127, 1100); // Turn 90 degrees
StopDrive();
DriveF(127, 500);
StopDrive();
// Get claw down
ClawUp(127, 1100);
StopClaw();
DriveB(127, 600);
StopDrive();
// Flip cap
ClawDown(127, 1100);
StopClaw();
}
}
task autonomous () {
// ..........................................................................
// Insert user code here.
// ..........................................................................
// Still needs TESTING
if (flags) {
autoWithFlags(red);
}
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 potValue = SensorValue(Poten); //top initial = 249
int desired = 2400; // UPDATE VALUE
task usercontrol () {
// User control code here, inside the loop
while (1) {
// FORWARD AND BACKWARD
int tmp;
if (vexRT[Ch2] > 0) {
tmp = min(vexRT[Ch2], 127);
}
else if (vexRT[Ch2] < 0) {
tmp = max(vexRT[Ch2], -127);
}
motor[DriveLeft1] = tmp * -con;
motor[DriveRight1] = tmp * -con - dev;
motor[DriveRight2] = tmp * -con - dev;
motor[DriveLeft2] = tmp * -con;
// Left axle turn
if (vexRT[Btn5U]) {
motor[DriveLeft1] = vexRT[Btn5U] * 127 * con;
motor[DriveRight1] = vexRT[Btn5U] * -127 * con;
motor[DriveRight2] = vexRT[Btn5U] * -127 * con;
motor[DriveLeft2] = vexRT[Btn5U] * 127 * con;
}
// Right axle turn
if (vexRT[Btn6U]) {
motor[DriveLeft1] = vexRT[Btn6U] * 127 * -con;
motor[DriveRight1] = vexRT[Btn6U] * -127 * -con;
motor[DriveRight2] = vexRT[Btn6U] * -127 * -con;
motor[DriveLeft2] = vexRT[Btn6U] * 127 * -con;
}
// Claw angle rotation (to go down) working
if (vexRT[Btn7U]) {
motor[Claw] = vexRT[Btn7U] * 127 * con;
}
// Claw angle rotation (to go up) not working
else if (vexRT[Btn7L]) {
motor[Claw] = vexRT[Btn7L] * 127 * -con;
}
else {
motor[Claw] = 0;
}
// Need to make sure catapult arm stays in place for ball intake
// Shooter working CCW (make sure it goes back down again after shooting up)
if (vexRT[Btn8L]) {
int res = vexRT[Btn8L];
motor[Shooter] = res * 127 * -con;
wait1Msec(1500);
motor[Shooter] = 127 * -con;
wait1Msec(700);
motor[Shooter] = 0;
// motor[Shooter] = 0;
}
// For locking shooter in place - used for ball intake (need to fix)
// (Locking and ball intake are to be separate!!!)
else if (vexRT[Btn8R]) {
// HoldShoot(3000);
StartTask(HoldShoot);
holding = 1;
// StopTask(HoldShoot);
}
/* Manual buttons for shooting */
// Make shooter go up
else if (vexRT[Btn8U]) {
motor[Shooter] = -127 * -con;
}
// Make shooter go down
else if (vexRt[Btn8D]) {
motor[Shooter] = 127 * -con;
}
else {
// Should hopefully make holdshoot function work
if (!holding) {
motor[Shooter] = 0;
}
else {
holding = 0;
}
}
// Ball intake (CW) working
if (vexRT[Btn6D]) {
motor[Intake1] = vexRT[Btn6D] * -127 * con;
motor[Intake2] = vexRT[Btn6D] * -127 * -con;
// HoldShoot(3000);
stop_intake = 1;
}
// Ball intake (CCW) working
else if (vexRT[Btn5D]) {
motor[Intake1] = vexRT[Btn5D] * 127 * con;
motor[Intake2] = vexRT[Btn5D] * 127 * -con;
// HoldShoot(3000);
stop_intake = 1;
}
else {
StopBallIntake();
}
}
}