-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWorlds8.c
422 lines (349 loc) · 11.1 KB
/
Worlds8.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
#pragma config(I2C_Usage, I2C1, i2cSensors)
#pragma config(Sensor, in7, indexLow, sensorAnalog)
#pragma config(Sensor, in8, powerExpander, sensorAnalog)
#pragma config(Sensor, dgtl1, flywheelEncoder, sensorQuadEncoder)
#pragma config(Sensor, dgtl3, indexHigh, sensorTouch)
#pragma config(Sensor, dgtl9, upToSpeed, sensorLEDtoVCC)
#pragma config(Sensor, dgtl10, encoderTest, sensorTouch)
#pragma config(Sensor, dgtl11, tune, sensorTouch)
#pragma config(Sensor, dgtl12, debug, sensorTouch)
#pragma config(Sensor, I2C_1, , sensorQuadEncoderOnI2CPort, , AutoAssign )
#pragma config(Sensor, I2C_2, , sensorQuadEncoderOnI2CPort, , AutoAssign )
#pragma config(Motor, port1, flywheel3, tmotorVex393TurboSpeed_HBridge, openLoop)
#pragma config(Motor, port2, indexer, tmotorVex393TurboSpeed_MC29, openLoop)
#pragma config(Motor, port3, flywheel2, tmotorVex393TurboSpeed_MC29, openLoop, reversed)
#pragma config(Motor, port4, flywheel1, tmotorVex393HighSpeed_MC29, openLoop)
#pragma config(Motor, port5, rightWheel2, tmotorVex393TurboSpeed_MC29, openLoop, reversed)
#pragma config(Motor, port6, leftWheel13, tmotorVex393HighSpeed_MC29, openLoop, encoderPort, I2C_2)
#pragma config(Motor, port7, flywheel4, tmotorVex393HighSpeed_MC29, openLoop, reversed)
#pragma config(Motor, port8, leftWheel2, tmotorVex393TurboSpeed_MC29, openLoop)
#pragma config(Motor, port9, rightWheel13, tmotorVex393TurboSpeed_MC29, openLoop, reversed, encoderPort, I2C_1)
#pragma config(Motor, port10, intake, tmotorVex393HighSpeed_HBridge, openLoop, reversed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
#pragma platform(VEX)
//Competition Control and Duration Settings
#pragma competitionControl(Competition)
#pragma autonomousDuration(0)
#pragma userControlDuration(60)
#pragma systemFile // eliminates warning for "unreferenced" functions
#include "Vex_Competition_Includes.c" //Main competition background code...do not modify!
bool debugMode = true;
bool debugDrivebaseActive = false;
bool debugFlywheelActive = false;
//Stores the differient speeds for the velocity states of the robot
typedef struct {
int velocity;
int velocityThreshold;
int highSpeed;
int lowSpeed;
int ramp;
int wait;
float kP;
bool velocityShot;
} flywheelShot;
flywheelShot longShot, midShot, pipeShot, holdShot;
flywheelShot currentShot;
void flywheelShots() {
longShot.velocity = 5300;
longShot.highSpeed = 100;
longShot.lowSpeed = 50;
longShot.ramp = 0;
longShot.wait = 300; //prevents double shooting
longShot.kP = 0.05;
longShot.velocityShot = true;
longShot.velocityThreshold = 30;
midShot.velocity = 4330;
midShot.highSpeed = 100;
midShot.lowSpeed = 40;
midShot.ramp = 0;
midShot.wait = 0;
midShot.kP = 0.04;
midShot.velocityShot = false;
pipeShot.velocity = 3800;
pipeShot.highSpeed = 100;
pipeShot.lowSpeed = 40;
pipeShot.ramp = 0;
pipeShot.wait = 0;
pipeShot.kP = 0.05;
pipeShot.velocityShot = false;
holdShot.velocity = 300;
holdShot.highSpeed = 90;
holdShot.lowSpeed = 45;
holdShot.ramp = 45;
holdShot.wait = 0;
holdShot.velocityShot = false;
}
int flywheelVelocity;
int flywheelVelocityUpdateFrequency = 25;
int flywheelReverseStartThreshold = 10;
int flywheelSlowDownVelocity = 4000;
int flywheelControlUpdateFrequency = flywheelVelocityUpdateFrequency;
int intakeMoveUpTime = 200;
int intakeMoveDownTime = 250;
int intakeShootVelocityThreshold = 50;
int intakeLightThreshold = 2500; //higher is more sensitive
bool intakeAutonomousIntake;
bool intakeAutonomousIndexer;
bool intakeAutonomousShoot;
int autonomousChoice = -1;
#include "JonLib/Drivebase.h"
#include "JonLib/PID.h"
#include "autonomous.h"
#warning "setLeftWheelSpeed"
void setLeftWheelSpeed ( int speed ) {
motor[leftWheel13] = speed;
motor[leftWheel2] = speed;
}
//Sets the speed of the wheels on the right side of the robot
#warning "setRightWheelSpeed"
void setRightWheelSpeed ( int speed ) {
motor[rightWheel13] = speed;
motor[rightWheel2] = speed;
}
#warning "setFlywheel"
void setFlywheel ( int speed ) {
motor[flywheel1] = speed;
motor[flywheel2] = speed;
motor[flywheel3] = speed;
motor[flywheel4] = speed;
}
//Logarithmic drivebase control
#warning "logDrive"
void logDrive () {
setWheelSpeed(
abs(vexRT(Ch3))*vexRT(Ch3)/127,
(abs(vexRT(Ch2))*vexRT(Ch2)/127)>100?100:abs(vexRT(Ch2))*vexRT(Ch2)/127);
}
#warning "flywheelVelocityCalculation"
task flywheelVelocityCalculation() {
long lastTime;
while(true){
lastTime = nSysTime;
delay(flywheelVelocityUpdateFrequency);
flywheelVelocity = (SensorValue[flywheelEncoder]*1000)/(nSysTime-lastTime);
SensorValue[flywheelEncoder] = 0;
}
}
void flywheelLCD () {
clearLCDLine(0);
displayLCDNumber(0,1,flywheelVelocity);
displayLCDNumber(0,5,currentShot.velocity);
displayLCDNumber(0,10,motor[flywheel4]);
}
void flywheelLED() {
if(motor[flywheel4]>=100)
SensorValue[upToSpeed] = 1;
else
SensorValue[upToSpeed] = 0;
}
void flywheelRampUp (int target) {
while(motor[flywheel4] < target) {
motor[flywheel4]+=2;
motor[flywheel3]+=2;
motor[flywheel2]+=2;
motor[flywheel1]+=2;
wait1Msec(35);
}
}
task jonflywheelControl () {
float kP = 0.02;
setFlywheel(20);
flywheelRampUp (currentShot.lowSpeed);
int flywheelSpeed;
while(true) {
flywheelSpeed = motor[flywheel4] + (currentShot.velocity-flywheelVelocity)*kP;
clearLCDLine(0);
displayLCDNumber(0,1,flywheelSpeed);
displayLCDNumber(0,5,flywheelVelocity);
displayLCDNumber(0,10,(currentShot.velocity-flywheelVelocity)*kP);
flywheelSpeed = flywheelSpeed>100?100:flywheelSpeed;
flywheelSpeed = flywheelSpeed<0?0:flywheelSpeed;
setFlywheel(flywheelSpeed);
}
}
#warning "flywheelControl"
task flywheelControl() {
setFlywheel(20);
flywheelRampUp (currentShot.lowSpeed);
int flywheelSpeedA, flywheelSpeedB;
while(true) {
flywheelSpeedA = currentShot.highSpeed + (currentShot.velocity-flywheelVelocity) * currentShot.kP;
flywheelSpeedB = currentShot.lowSpeed + (currentShot.velocity-flywheelVelocity) * currentShot.kP;
flywheelSpeedA = flywheelSpeedA>100?100:flywheelSpeedA;
flywheelSpeedB = flywheelSpeedB>100?100:flywheelSpeedB;
flywheelSpeedA = flywheelSpeedA<0?0:flywheelSpeedA;
flywheelSpeedB = flywheelSpeedB<0?0:flywheelSpeedB;
if(flywheelVelocity < currentShot.velocity+currentShot.ramp) {
setFlywheel(flywheelSpeedA);
} else {
setFlywheel(flywheelSpeedB);
}
if(debugMode) {
flywheelLCD();
flywheelLED();
clearLCDLine(1);
displayLCDNumber(1,1,flywheelSpeedA);
displayLCDNumber(1,5,flywheelSpeedB);
}
delay(flywheelControlUpdateFrequency);
}
}
#warning "startFlywheel"
void startFlywheel (flywheelShot shot) {
currentShot.velocity = shot.velocity;
currentShot.highSpeed = shot.highSpeed;
currentShot.lowSpeed = shot.lowSpeed;
currentShot.ramp = shot.ramp;
currentShot.wait = shot.wait;
currentShot.kP = shot.kP;
currentShot.velocityShot = shot.velocityShot;
currentShot.velocityThreshold = shot.velocityThreshold;
if(flywheelVelocity >= 0)
startTask(flywheelControl, kHighPriority);
else
setFlywheel(0);
}
void startFlywheel (int targetVelocity, int lowSpeed, int highSpeed, int rampThreshold, int waitTime = 0, float kP = 0.07, bool velocityShot = false, int velocityThreshold = 50) {
flywheelShot tempShot;
tempShot.velocity = targetVelocity;
tempShot.lowSpeed = lowSpeed;
tempShot.highSpeed = highSpeed;
tempShot.ramp = rampThreshold;
tempShot.wait = waitTime;
tempShot.kP = kP;
tempShot.velocityShot = velocityShot;
tempShot.velocityThreshold = velocityThreshold;
startFlywheel(tempShot);
}
#warning "stopFlywheel"
void stopFlywheel () {
stopTask(flywheelControl);
setFlywheel(0);
}
#warning "intakeControl"
task intakeControl () {
while(true) {
motor[intake] = ((vexRT(Btn5U)||intakeAutonomousIntake)-vexRT(Btn5D))*127;
//Move ball from high limit switch to low limit switch
if(vexRT(Btn6D) && SensorValue[indexHigh]) {
motor[indexer] = -127;
delay(intakeMoveDownTime);
}
//Shooting control
if (vexRT(Btn6U) || intakeAutonomousShoot) {
if((currentShot.velocityShot?abs(currentShot.velocity-flywheelVelocity)<currentShot.velocityThreshold:true) && flywheelVelocity>intakeShootVelocityThreshold && time1[T1]>currentShot.wait) {
writeDebugStreamLine("%d", flywheelVelocity);
motor[indexer] = 127;
while(SensorValue[indexHigh] && (vexRT(Btn6U)||intakeAutonomousShoot)) { delay(25); }
clearTimer(T1);
}
else {
motor[indexer] = (SensorValue[indexHigh])?-5:80;
}
}
//Move ball down even if there is a sensor we want
else if (vexRT(Btn5D))
motor[indexer] = -127;
//Stop ball if ball is at a sensor
else if(SensorValue[indexLow]<intakeLightThreshold && !SensorValue[indexHigh]) {
motor[indexer] = 70;
clearTimer(T2);
while(time1[T2] < intakeMoveUpTime && !SensorValue[indexHigh]) { delay(20); }
motor[indexer] = 0;
}
else
motor[indexer] = 0;
delay(25);
}
}
task reverseFlywheel () {
while(true) {
if(vexRT(Btn7L) && flywheelVelocity > flywheelReverseStartThreshold) {
stopFlywheel();
clearLCDLine(1);
while(flywheelVelocity>0) {
setFlywheel(flywheelVelocity>flywheelSlowDownVelocity?0:-pow(abs((flywheelVelocity/1000)-flywheelSlowDownVelocity/1000),1.3));
clearLCDLine(0);
displayLCDNumber(0,0,flywheelVelocity);
displayLCDNumber(0,10,motor[flywheel1]);
delay(25);
}
}
else {
while(vexRT(Btn7L)) {
stopFlywheel();
setFlywheel(-127);
delay(25);
}
if(flywheelVelocity < 0 && !debugFlywheelActive) {
setFlywheel(0);
}
}
delay(25);
}
}
#include "LCD.c"
#warning "init"
void init() {
playTone(700,10);
clearDebugStream();
intakeAutonomousIndexer = false;
intakeAutonomousIntake = false;
intakeAutonomousShoot = false;
//Startup modes
if(!debugMode)
debugMode = (bool) SensorValue[debug];
intakeAutonomousIndexer = false;
intakeAutonomousIntake = false;
intakeAutonomousIndexer = false;
flywheelShots();
startTask(intakeControl, kHighPriority);
startTask(flywheelVelocityCalculation, kHighPriority);
startTask(reverseFlywheel);
startTask(LCD);
}
void pre_auton() {
init();
bStopTasksBetweenModes = true;
}
#include "autonomousPrograms.h"
task autonomous() {
switch (autonomousChoice) {
case 0: fourBalls(); break;
case 1: rSCurveAuto(); break;
case 2: rAngleShotAuto(); break;
case 3: rFourCross(); break;
case 4: lSCurveAuto(); break;
case 5: lAngleShotAuto(); break;
case 6: lFourCross(); break;
}
}
task usercontrol() {
init();
while (true) {
if(!debugDrivebaseActive)
logDrive();
if(vexRT(Btn8U)) {
startFlywheel(pipeShot);
while(vexRT(Btn8U)) { delay(10); }
}
else if(vexRT(Btn8R)) {
startFlywheel(midShot);
while(vexRT(Btn8R)) { delay(10); }
}
else if(vexRT(Btn8L)) {
startFlywheel(longShot);
while(vexRT(Btn8L)) { delay(10); }
}
else if(vexRT(Btn7D)) {
//startFlywheel(holdShot);
startFlywheel(200, 30, 60, 10, 0);
while(vexRT(Btn7D)) { delay(10); }
}
// for debugging - consider removing
else if(vexRT(Btn7U))
setFlywheel(60);
else if(vexRT(Btn8D))
stopFlywheel();
}
}