-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVEX.c
488 lines (444 loc) · 13.2 KB
/
VEX.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
#pragma config(I2C_Usage, I2C1, i2cSensors)
#pragma config(Sensor, in1, mobileGoalAngle, sensorPotentiometer)
#pragma config(Sensor, in2, armAngle, sensorPotentiometer)
#pragma config(Sensor, dgtl1, tankDriveSignal, sensorLEDtoVCC)
#pragma config(Sensor, dgtl12, hasMobileGoal, sensorTouch)
#pragma config(Sensor, I2C_1, driveEncoderLeft, sensorQuadEncoderOnI2CPort, , AutoAssign )
#pragma config(Sensor, I2C_2, driveEncoderRight, sensorQuadEncoderOnI2CPort, , AutoAssign )
#pragma config(Motor, port1, armRight, tmotorVex393_HBridge, openLoop, reversed)
#pragma config(Motor, port2, goalRight, tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor, port4, coneMotor, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port5, leftInteriorMotor, tmotorVex393_MC29, openLoop, reversed, encoderPort, I2C_1)
#pragma config(Motor, port6, leftExteriorMotors, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port7, rightExteriorMotors, tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor, port8, rightInteriorMotor, tmotorVex393_MC29, openLoop, encoderPort, I2C_2)
#pragma config(Motor, port9, goalLeft, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port10, armLeft, tmotorVex393_HBridge, 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"
/*---------------------------------------------------------------------------*/
/* 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. */
/*---------------------------------------------------------------------------*/
#define DEADZONE 30
#define ARM_SPEED 100
#define ROLLER_SPEED 100
#define GOAL_SPEED 100
//function declarations
void drive( bool isTank );
void moveArm(bool up, bool down, bool autoHoldHeight, int currentSpeed);
void rollRoller( bool up, bool down );
void moveGoal( bool up, bool down );
void holdArm(int overrideAngle, int currentSpeed);
//task declarations
task autoDropMobile();
task autoDropFixed();
task autoLoadHumanPlayerStation();
//globals
int GOAL_START_ANGLE;
int ARM_CURRENT_ANGLE;
int holdAngle;
bool autoLiftRunning;
bool armDownDecreased;
bool armUpDecreased;
bool armDownFound;
bool armUpFound;
//globals for PID
int armUpPower;
int armDownPower;
//button encoding
const int LIFT_UP_BTN = Btn6U,
LIFT_DOWN_BTN = Btn6D,
CONE_UP_BTN = Btn8L,
CONE_DOWN_BTN = Btn8D,
GOAL_UP_BTN = Btn8U,
GOAL_DOWN_BTN = Btn8R,
AUTO_LIFT_MOBILE = Btn5U,
AUTO_LIFT_FIXED = Btn5D,
AUTO_HOLD_HEIGHT_ON = Btn7D,
AUTO_HOLD_HEIGHT_OFF = Btn7R;
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;
holdAngle = 1600;
}
/*---------------------------------------------------------------------------*/
/* */
/* 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. */
/*---------------------------------------------------------------------------*/
task autonomous()
{
bool robotInPosition = false;
clearTimer(T1);
//Robot drives forward
resetMotorEncoder(rightInteriorMotor);
resetMotorEncoder(leftInteriorMotor);
while(!robotInPosition)
{
if(time1[T1] < 750)
{
rollRoller(true, false);
}
if(time1[T1] > 1000 && time1[T1] < 1750)
{
motor[leftExteriorMotors] = motor[leftInteriorMotor] = motor[rightExteriorMotors] = motor[rightInteriorMotor] = -50;
}
else if(time1[T1] < 2500)
{
motor[leftExteriorMotors] = motor[leftInteriorMotor] = motor[rightExteriorMotors] = motor[rightInteriorMotor] = 50;
}
else if(time1[T1] < 3500)
{
motor[leftExteriorMotors] = motor[leftInteriorMotor] = motor[rightExteriorMotors] = motor[rightInteriorMotor] = -50;
}
else
{
motor[leftExteriorMotors] = motor[leftInteriorMotor] = motor[rightExteriorMotors] = motor[rightInteriorMotor] = 0;
}
if(SensorValue[armAngle] < 1600)
{
motor[armLeft] = motor[armRight] = 100;
}
else
{
motor[armLeft] = motor[armRight] = 0;
}
if(time1[T1] > 3500)
{
robotInPosition = true;
}
}
sleep(1000);
startTask(autoDropFixed);
}
/*---------------------------------------------------------------------------*/
/* */
/* User Control Task */
/* */
/* This task is used to control your robot during the user control phase of */
/* a VEX Competition. */
task usercontrol()
{
GOAL_START_ANGLE = SensorValue[mobileGoalAngle];
ARM_CURRENT_ANGLE = SensorValue[armAngle];
bool isTank = true;
bool autoHoldHeight = true;
int armPositionNew = SensorValue[armAngle];
int armPositionOld = armPositionNew;
int armRotationalSpeed;
int tenMSPoll;
autoLiftRunning = false;
armDownDecreased = false;
armUpDecreased = false;
armDownFound = false;
armUpFound = false;
//clears timer2
clearTimer(T2);
while ( true ) {
//drive function
drive( isTank );
//manual arm control
if(!autoLiftRunning)
{
moveArm( vexRT[LIFT_UP_BTN] == 1, vexRT[LIFT_DOWN_BTN] == 1, autoHoldHeight, armRotationalSpeed);
}
//manual roller control
rollRoller( vexRT[CONE_UP_BTN] == 1, vexRT[CONE_DOWN_BTN] == 1 ); // ( up, down )
//manual goal control
moveGoal( vexRT[GOAL_UP_BTN] == 1, vexRT[GOAL_DOWN_BTN] == 1 ); // ( up, down )
//Auto Lift and Drop Activation
if( vexRT[AUTO_LIFT_MOBILE])
{
startTask( autoDropMobile );
}
if(vexRT[AUTO_LIFT_FIXED])
{
startTask( autoDropFixed );
}
//turns on and off auto hold height
if(vexRT[AUTO_HOLD_HEIGHT_ON])
{
autoHoldHeight = true;
}
if(vexRT[AUTO_HOLD_HEIGHT_OFF])
{
autoHoldHeight = false;
}
//run every 10ms
if(time1[T2] - tenMSPoll > 10)
{
tenMSPoll = time1[T2];
armPositionOld = armPositionNew;
armPositionNew = SensorValue[armAngle];
armRotationalSpeed = armPositionNew - armPositionOld;
}
if(vexRT[Btn7L])
{
startTask(autonomous);
}
}
}
//task to automatically pick up cone from human player station
//sets arm to appropriate height
//requires robot to start at correct distance and orientation
task autoLoadHumanPlayerStation()
{
clearTimer(T1);
bool coneLoaded = false;
int clearHPS;
int pickupHeight;
while(!coneLoaded)
{
if(time1[T1] < 1000)
{
holdArm(clearHPS,0);
}
else if(time1[T1] < 2000)
{
holdArm(pickupHeight, 0);
rollRoller(true, false);
}
else
{
coneLoaded = true;
}
}
startTask(autoDropMobile);
}
//raises arm to mobile goal drop point and drops cone
task autoDropMobile(){
int motorSpeed;
bool isDropped = false;
autoLiftRunning = true;
const int ARM_DROP_ANGLE_MOBILE = 1650;
//raise at full speed to 1200
while(SensorValue[armAngle] < 1200)
{
motor[armLeft] = motor[armRight] = 127;
}
while(SensorValue[armAngle] < ARM_DROP_ANGLE_MOBILE)
{
float speedMultiplier = (SensorValue[armAngle] - 1200)/ (ARM_DROP_ANGLE_MOBILE - 1200);
speedMultiplier = speedMultiplier * 127;
motorSpeed = 127 - (int) speedMultiplier;
motor[armLeft] = motor[armRight] = motorSpeed;
}
motor[armLeft] = motor[armRight] = 0;
clearTimer(T1);
while(!isDropped)
{
if( SensorValue[armAngle] > ARM_DROP_ANGLE_MOBILE + 50 )
{
motor[armLeft] = motor[armRight] = -30;
}
else if ( SensorValue[armAngle] < ARM_DROP_ANGLE_MOBILE -50)
{
motor[armLeft] = motor[armRight] = 30;
}
else
{
motor[armLeft] = motor[armRight] = 0;
}
if (time1[T1] > 1000)
{
motor[coneMotor] = -ROLLER_SPEED;
}
if(time1[T1] > 2000)
{
motor[coneMotor] = 0;
isDropped = true;
}
}
autoLiftRunning = false;
}
//drops a cone on the fixed goal
task autoDropFixed()
{
int motorSpeed;
bool isDropped = false;
autoLiftRunning = true;
const int ARM_DROP_ANGLE_FIXED = 2150;
while(SensorValue[armAngle] < 1600)
{
motor[armLeft] = motor[armRight] = 127;
}
while(SensorValue[armAngle] < ARM_DROP_ANGLE_FIXED)
{
float speedMultiplier = (SensorValue[armAngle] - 1200)/ (ARM_DROP_ANGLE_FIXED - 1200);
speedMultiplier = speedMultiplier * 127;
motorSpeed = 127 - (int) speedMultiplier;
motor[armLeft] = motor[armRight] = motorSpeed;
}
clearTimer(T1);
while(!isDropped)
{
if( SensorValue[armAngle] > ARM_DROP_ANGLE_FIXED + 50 )
{
motor[armLeft] = motor[armRight] = -30;
}
else if ( SensorValue[armAngle] < ARM_DROP_ANGLE_FIXED -50)
{
motor[armLeft] = motor[armRight] = 30;
}
else
{
motor[armLeft] = motor[armRight] = 0;
}
if (time1[T1] > 1000)
{
motor[coneMotor] = -ROLLER_SPEED;
}
if(time1[T1] > 2000)
{
motor[coneMotor] = 0;
isDropped = true;
}
}
autoLiftRunning = false;
}
//drive code for the robot
//@param isTank: bool indicating if tank or arcade drive
void drive( bool isTank ) {
if ( isTank ) {
int leftSpeed = vexRT[Ch3];
int rightSpeed = vexRT[Ch2];
if ( abs( leftSpeed ) <= DEADZONE ) {
leftSpeed = 0;
}
if ( abs( rightSpeed ) <= DEADZONE ) {
rightSpeed = 0;
}
motor[rightInteriorMotor] = rightSpeed;
motor[leftExteriorMotors] = leftSpeed;
motor[rightExteriorMotors] = rightSpeed;
motor[leftInteriorMotor] = leftSpeed;
} else {
motor[leftExteriorMotors] = motor[rightInteriorMotor] = (vexRT[Ch3] - vexRT[Ch4]) / 2; // (y + x)/2
motor[rightExteriorMotors] = motor[leftInteriorMotor] = (vexRT[Ch3] + vexRT[Ch4]) / 2; // (y - x)/2
}
}
void holdArm(int overrideAngle, int currentSpeed)
{
if(overrideAngle != 0)
{
holdAngle = overrideAngle;
}
if (SensorValue[armAngle] > holdAngle + 50)
{
if(!armDownFound)
{
if(currentSpeed > 0)
{
armDownPower--;
armDownDecreased = true;
}
if(currentSpeed < 1)
{
armDownPower++;
if(armDownDecreased)
{
armDownFound = true;
}
}
}
motor[armRight] = motor[armLeft] = -30; //armDownPower;
}
if(SensorValue[armAngle] < holdAngle - 50)
{
if(!armUpFound)
{
if(currentSpeed < 0)
{
armUpPower++;
if(armUpDecreased)
{
armUpFound = true;
}
}
if(currentSpeed > -1)
{
armUpPower--;
armUpDecreased = true;
}
}
motor[armRight] = motor[armLeft] = 50; //armUpPower;
}
else
{
motor[armRight] = motor[armLeft] = 0;
}
}
//controls the movement of the arm
void moveArm( bool up, bool down, bool autoHoldHeight, int currentSpeed) {
if ( up ) {
motor[armRight] = motor[armLeft] = ARM_SPEED;
holdAngle = SensorValue[armAngle];
armUpPower = 50;
armDownPower = -30;
armDownDecreased = false;
armUpDecreased = false;
armDownFound = false;
armUpFound = false;
}
else if ( down ) {
motor[armRight] = motor[armLeft] = -ARM_SPEED;
holdAngle = SensorValue[armAngle];
armUpPower = 50;
armDownPower = -30;
armDownDecreased = false;
armUpDecreased = false;
armDownFound = false;
armUpFound = false;
}
else if (autoHoldHeight){
holdArm(0, currentSpeed);
}
else{
motor[armRight] = motor[armLeft] = 0;
}
}
void rollRoller( bool up, bool down ) {
if ( up ) {
motor[coneMotor] = ROLLER_SPEED;
} else if ( down ) {
motor[coneMotor] = -ROLLER_SPEED;
} else {
motor[coneMotor] = 0;
}
}
void moveGoal( bool up, bool down ) {
if ( up ) {
motor[goalRight] = motor[goalLeft] = GOAL_SPEED;
} else if ( down ) {
motor[goalRight] = motor[goalLeft] = -GOAL_SPEED;
} else {
motor[goalRight] = motor[goalLeft] = 0;
}
}