-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRHSCompetetive.c
456 lines (382 loc) · 14.9 KB
/
CRHSCompetetive.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
#pragma config(I2C_Usage, I2C1, i2cSensors)
#pragma config(Sensor, dgtl1, rightEncoder, sensorQuadEncoder)
#pragma config(Sensor, dgtl3, leftEncoder, sensorQuadEncoder)
#pragma config(Sensor, dgtl5, blueSide1, sensorDigitalIn)
#pragma config(Sensor, dgtl6, blueSide2, sensorDigitalIn)
#pragma config(Sensor, dgtl7, blueBack1, sensorDigitalIn)
#pragma config(Sensor, dgtl8, blueBack2, sensorDigitalIn)
#pragma config(Sensor, dgtl9, redSide1, sensorDigitalIn)
#pragma config(Sensor, dgtl10, redSide2, sensorDigitalIn)
#pragma config(Sensor, dgtl11, redBack1, sensorDigitalIn)
#pragma config(Sensor, dgtl12, redBack2, sensorNone)
#pragma config(Sensor, I2C_1, , sensorQuadEncoderOnI2CPort, , AutoAssign )
#pragma config(Sensor, I2C_2, , sensorQuadEncoderOnI2CPort, , AutoAssign )
#pragma config(Motor, port1, armMotor, tmotorVex393_HBridge, openLoop, encoderPort, I2C_1)
#pragma config(Motor, port2, clawMotor, tmotorVex393_MC29, openLoop, encoderPort, I2C_2)
#pragma config(Motor, port3, backRight, tmotorVex269_MC29, openLoop, reversed, encoderPort, dgtl1)
#pragma config(Motor, port4, backLeft, tmotorVex269_MC29, openLoop, encoderPort, dgtl3)
#pragma config(Motor, port5, frontRight, tmotorVex269_MC29, openLoop, reversed)
#pragma config(Motor, port6, frontLeft, tmotorVex269_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
// Beware if you enable PID control...... It hasn't always ended well for the unexperienced programmer
#pragma platform(VEX)
//Competition Control and Duration Settings
#pragma competitionControl(Competition)
#pragma autonomousDuration(15)
#pragma userControlDuration(105)
//This sets the time for doing certain moves if the encoders aren't working
//float feet10 = 7130; //7.13 seconds
//float completeTurn = 4680; //4.68 seconds
//29.85 is how many inches needed to turn for a 180
//float rotationsRight = distanceRight / circumference;
//float degreesRight = rotationsRight * 360;
//int encoderCountsRight = degreesRight * 4; // The encoder counts in quarter degrees.
// This is so we can find out how many times the left wheels have to turn to go the specefied distance
#include "Vex_Competition_Includes.c" //Main competition background code...do not modify!
/////////////////////////////////////////////////////////////////////////////////////////
//
// Pre-Autonomous Functions
//
// You may want to perform some actions before the competition starts. Do them in the
// following function.
//
/////////////////////////////////////////////////////////////////////////////////////////
void pre_auton()
{
// Set bStopTasksBetweenModes to false if you want to keep user created tasks running between
// Autonomous and Tele-Op modes. You will need to manage all user created tasks if set to false.
bStopTasksBetweenModes = true;
// All activities that occur before the competition starts
}
/////////////////////////////////////////////////////////////////////////////////////////
//
// 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.
//
/////////////////////////////////////////////////////////////////////////////////////////
//Declaring an array to make setting motor speed in autonomous easier
string motors[] = {frontRight, frontLeft, backRight, backLeft};
// 5.125 makes us innacurate, how about 5.25? (Check this later down the road)
float distanceRight, distanceLeft, rotationsRight, rotationsLeft, degreesRight, degreesLeft, ratio, diameter = 5.125, circumference = PI * diameter;
int encoderCountsRight = 0, encoderCountsLeft = 0;
void ForwardForDistance(int speed, float targetDistance) //The arguments are sorted alphabetically. Make speed negative to reverse and change the wait to abs to make sure it works
{
//float rotations = targetDistance / circumference; On torque mode (stock), there are 627 counts to a revolution, high speed there is 392, and turbo speed has 261
//float degrees = rotations * 360;
//float encoderCounts = degrees * 4; // 24 / 18.375 is 1.30612244898
//Auto-correction
//while(nMotorEncoder[frontRight] < encoderCounts && nMotorEncoder[frontLeft] < encoderCounts)
//{
// if (nMotorEncoder[frontRight] > nMotorEncoder[frontLeft])
// {
// motor[frontRight] = speed / 2;
// motor[backRight] = speed / 2;
// }
// else if(nMotorEncoder[frontRight] < nMotorEncoder[frontLeft])
// {
// motor[frontLeft] = speed / 2; // These are negative because we are still turning
// motor[backLeft] = speed / 2;
// }
// else if (nMotorEncoder[frontRight] == nMotorEncoder[frontLeft])
// {
// motor[frontRight] = speed;
// motor[frontLeft] = speed;
// motor[backRight] = speed;
// motor[backLeft] = speed;
// }
//}
float motorEncoderRight = targetDistance, motorEncoderLeft = targetDistance; //600 = 1 foot
//while (nMotorEncoder(frontRight) < motorEncoderRight && nMotorEncoder(frontLeft) < motorEncoderLeft)
//{
// motor[frontRight] = speed;
// motor[frontLeft] = speed;
// motor[backRight] = speed;
// motor[backLeft] = speed;
//}
//Could this work???
while (nMotorEncoder(frontRight) < motorEncoderRight && nMotorEncoder(frontLeft) < motorEncoderLeft)
{
for (int i = 0; i <= sizeof(motors); i++)
motor[motors[i]] = speed;
}
}
//Could this work as well???
void Turn(float degreesToTurn, int speed)
{
ratio = 180 / degreesToTurn;
distanceRight = 29.85 / ratio;
rotationsRight = distanceRight / circumference;
degreesRight = rotationsRight * 360;
encoderCountsRight = degreesRight * 4;
encoderCountsLeft = encoderCountsRight * -1;
if (degreesToTurn < 0)
{
motor[frontRight] = speed;
motor[frontLeft] = speed * -1;
motor[backRight] = speed;
motor[backLeft] = speed * -1;
while (nMotorEncoder[frontRight] < encoderCountsRight && nMotorEncoder[frontLeft] < encoderCountsLeft)
{
if (abs(nMotorEncoder[frontRight]) > abs(nMotorEncoder[frontLeft]))
{
motor[frontRight] = speed / 2;
motor[backRight] = speed / 2;
}
else if (abs(nMotorEncoder[frontRight]) < abs(nMotorEncoder[frontLeft]))
{
motor[frontLeft] = speed / -2;
motor[backLeft] = speed / -2;
}
else if (nMotorEncoder[frontRight] == nMotorEncoder[frontLeft])
{
motor[frontRight] = speed;
motor[frontLeft] = speed;
motor[backRight] = speed;
motor[backLeft] = speed;
}
}
}
else if (degreesToTurn > 0)
{
motor[frontRight] = speed * -1;
motor[frontLeft] = speed;
motor[backRight] = speed * -1;
motor[backLeft] = speed;
while (nMotorEncoder[frontRight] < encoderCountsRight && nMotorEncoder[frontLeft] < encoderCountsLeft)
{
if (abs(nMotorEncoder[frontRight]) > abs(nMotorEncoder[frontLeft]))
{
//Since one value is negative and one is positive, abs(absolute value) makes it so that we can make sure the robot turns evenly
motor[frontRight] = speed / -2;
motor[backRight] = speed / -2;
}
else if (abs(nMotorEncoder[frontRight]) < abs(nMotorEncoder[frontLeft]))
{
motor[frontLeft] = speed / 2; // These are negative because we are still turning
motor[backLeft] = speed / 2;
}
else if (nMotorEncoder[frontRight] == nMotorEncoder[frontLeft])
{
motor[frontRight] = speed;
motor[frontLeft] = speed;
motor[backRight] = speed;
motor[backLeft] = speed;
}
}
}
}
/*void TurnLeft(float degreesToTurn, int speed)
{
//float rotationsRight = distanceRight / circumference; On torque mode (stock), there are 627 counts to a revolution, high speed there is 392, and
// turbo speed has 261
//float degreesRight = rotationsRight * 627.2;
//float encoderCountsRight = degreesRight * 4; // 24 / 18.375 is 1.30612244898
////float rotationsLeft = distanceLeft / circumference;
//float rotationsLeft = rotationsRight * -1; // This is the same as calculating the rotations directly
//float degreesLeft = rotationsLeft * 627.2;
//float encoderCountsLeft = degreesLeft * 4;
ratio = 180 / degreesToTurn; // We find the ratio of degrees to turn to doing a 180 turn
distanceLeft = 29.85 //ratio; This then compares the distance to turn of a 180 to the ratio made above to make the distance the robot has to turn
rotationsLeft = distanceLeft / circumference;
degreesLeft = rotationsLeft * 360;
encoderCountsLeft = degreesLeft * 4; // The encoder counts in quarter degrees.
encoderCountsRight = encoderCountsLeft * -1; // This is the same as calculating the counts directly, but saves time :)
motor[frontRight] = speed * -1;
motor[frontLeft] = speed;
motor[backRight] = speed * -1;
motor[backLeft] = speed;
while(nMotorEncoder[frontRight] < encoderCountsRight && nMotorEncoder[frontLeft] < encoderCountsLeft)
{
if (abs(nMotorEncoder[frontRight]) > abs(nMotorEncoder[frontLeft]))
{ Since one value is negative and one is positive, abs (absolute value) makes it so that we can make sure the robot
turns evenly
motor[frontRight] = speed / -2;
motor[backRight] = speed / -2;
}
else if(abs(nMotorEncoder[frontRight]) < abs(nMotorEncoder[frontLeft]))
{
motor[frontLeft] = speed / 2; // These are negative because we are still turning
motor[backLeft] = speed / 2;
}
else if (nMotorEncoder[frontRight] == nMotorEncoder[frontLeft])
{
motor[frontRight] = speed;
motor[frontLeft] = speed;
motor[backRight] = speed;
motor[backLeft] = speed;
}
}
}
void TurnRight(float degreesToTurn, int speed)
{
ratio = 180 / degreesToTurn;
distanceRight = 29.85 / ratio;
rotationsRight = distanceRight / circumference;
degreesRight = rotationsRight * 360;
encoderCountsRight = degreesRight * 4;
encoderCountsLeft = encoderCountsRight * -1;
resetMotorEncoder(frontRight);
resetMotorEncoder(frontLeft);
motor[frontRight] = speed;
motor[frontLeft] = speed * -1;
motor[backRight] = speed;
motor[backLeft] = speed * -1;
while(nMotorEncoder[frontRight] < encoderCountsRight && nMotorEncoder[frontLeft] < encoderCountsLeft)
{
if (abs(nMotorEncoder[frontRight]) > abs(nMotorEncoder[frontLeft]))
{
motor[frontRight] = speed / 2;
motor[backRight] = speed / 2;
}
else if(abs(nMotorEncoder[frontRight]) < abs(nMotorEncoder[frontLeft]))
{
motor[frontLeft] = speed / -2;
motor[backLeft] = speed / -2;
}
else if (nMotorEncoder[frontRight] == nMotorEncoder[frontLeft])
{
motor[frontRight] = speed;
motor[frontLeft] = speed;
motor[backRight] = speed;
motor[backLeft] = speed;
}
}
}*/
void Wait(float timeToWait)
{
wait1Msec(timeToWait * 1000);
}
void Stop()
{
motor[frontRight] = 0;
motor[frontLeft] = 0;
motor[backRight] = 0;
motor[backLeft] = 0;
}
task autonomous()
{
// These 'if' statements check to see if there is a jumper in the selected port. If there is a jumper in that port, the sensor returns a false value that portion of code will be ran.
//float rotationsRight = distanceRight / circumference; // On torque mode (stock), there are 627 counts to a revolution, high speed there is 392, and
// turbo speed has 261
//float degreesRight = rotationsRight * 627.2;
//float encoderCountsRight = degreesRight * 4; // 24 / 18.375 is 1.30612244898
//float rotationsLeft = distanceLeft / circumference;
//float degreesLeft = rotationsLeft * 627.2;
//float encoderCountsLeft = degreesLeft * 4;
if (!SensorBoolean[blueSide1])
{
}
else if (!SensorBoolean[blueSide2])
{
}
else if (!SensorBoolean[blueBack1])
{
}
else if (!SensorBoolean[blueBack2])
{
}
else if (!SensorBoolean[redSide1])
{
}
else if (!SensorBoolean[redSide2])
{
}
else if (!SensorBoolean[redBack1])
{
}
else if (!SensorBoolean[redBack2])
{
}
else
{
}
}
/////////////////////////////////////////////////////////////////////////////////////////
//
// 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 _threshold = 20;
task usercontrol()
{
while (true)
{
// This is the main execution loop for the user control program. Each time through the loop
// your program should update motor + servo values based on feedback from the joysticks.
// Based on what wheels are installed, change the program between regular and mecanum wheels
/*frontRightValue = 0;
backRightValue = 0;
frontLeftValue = 0;
backLeftValue = 0;
if (abs(vexRT[Ch2]) > 10)
{
frontRightValue = vexRT[Ch2];
backRightValue = vexRT[Ch2];
}
if (abs(vexRT[Ch3]) > 10)
{
frontLeftValue = vexRT[Ch3];
backLeftValue = vexRT[Ch3];
}
if (abs(vexRT[Ch2Xmtr2]) > 10)
{
frontRightValue = vexRT[Ch2Xmtr2];
backRightValue = vexRT[Ch2Xmtr2];
}
if (abs(vexRT[Ch3Xmtr2]) > 10)
{
frontLeftValue = vexRT[Ch3Xmtr2];
backLeftValue = vexRT[Ch3Xmtr2];
}
motor[frontRight] = frontRightValue;
motor[backRight] = backRightValue;
motor[frontLeft] = frontLeftValue;
motor[backLeft] = backLeftValue;*/
// Under here is the code for mecanum wheels
//Create "deadzone" variables. Adjust threshold value to increase/decrease deadzone
int X2 = 0, Y1 = 0, X1 = 0, threshold = 15, armMovement = 0, clawMovement = 0;
//Create "deadzone" for Y1/Ch3
if (abs(vexRT[Ch3]) > threshold)
Y1 = vexRT[Ch3];
else if (abs(vexRT[Ch3Xmtr2]) > threshold)
Y1 = vexRT[Ch3Xmtr2];
else
Y1 = 0;
//Create "deadzone" for X1/Ch4
if (abs(vexRT[Ch4]) > threshold)
X1 = vexRT[Ch4];
else if (abs(vexRT[Ch4Xmtr2]) > threshold)
X1 = vexRT[Ch4Xmtr2];
else
X1 = 0;
//Create "deadzone" for X2/Ch1
if (abs(vexRT[Ch1]) > threshold)
X2 = vexRT[Ch1];
else if (abs(vexRT[Ch1Xmtr2]) > threshold)
X2 = vexRT[Ch1Xmtr2];
else
X2 = 0;
//Arm Controls
if (vexRT[Btn5D] == 1 || vexRT[Btn5DXmtr2] == 1)
armMovement = 127;
else if (vexRT[Btn5U] == 1 || vexRT[Btn5UXmtr2] == 1)
armMovement = -127;
//Claw controls
if (vexRT[Btn6D] == 1 || vexRT[Btn6DXmtr2] == 1)
clawMovement = 127;
else if (vexRT[Btn6U] == 1 || vexRT[Btn6UXmtr2] == 1)
clawMovement = -127;
//Remote Control Commands
motor[frontRight] = Y1 - X2 - X1;
motor[backRight] = Y1 - X2 + X1;
motor[frontLeft] = Y1 + X2 + X1;
motor[backLeft] = Y1 + X2 - X1;
motor[armMotor] = armMovement;
motor[clawMotor] = clawMovement;
}
}