-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2016JPD23.c
More file actions
292 lines (247 loc) · 6.94 KB
/
2016JPD23.c
File metadata and controls
292 lines (247 loc) · 6.94 KB
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
#pragma config(I2C_Usage, I2C1, i2cSensors)
#pragma config(Sensor, dgtl12, ballHigh, sensorTouch)
#pragma config(Sensor, I2C_1, , sensorQuadEncoderOnI2CPort, , AutoAssign)
#pragma config(Motor, port1, feeder, tmotorVex393TurboSpeed_HBridge, openLoop)
#pragma config(Motor, port2, LUflywheel, tmotorVex393TurboSpeed_MC29, openLoop, reversed)
#pragma config(Motor, port3, LDflywheel, tmotorVex393TurboSpeed_MC29, openLoop, reversed)
#pragma config(Motor, port4, LBMdrive, tmotorVex393TurboSpeed_MC29, openLoop)
#pragma config(Motor, port5, LFdrive, tmotorVex393TurboSpeed_MC29, openLoop, reversed)
#pragma config(Motor, port6, RBMdrive, tmotorVex393TurboSpeed_MC29, openLoop)
#pragma config(Motor, port7, RFdrive, tmotorVex393TurboSpeed_MC29, openLoop)
#pragma config(Motor, port8, RUflywheel, tmotorVex393TurboSpeed_MC29, openLoop, encoderPort, I2C_1)
#pragma config(Motor, port9, RDflywheel, tmotorVex393TurboSpeed_MC29, openLoop)
#pragma config(Motor, port10, intake1, tmotorVex393TurboSpeed_HBridge, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
#pragma platform(VEX)
//Competition Control and Duration Settings
#pragma competitionControl(Competition)
#pragma autonomousDuration(20)
#pragma userControlDuration(120)
#include "Vex_Competition_Includes.c" //Main competition background code...do not modify!
int motorSpeed = 0;
bool loadFireMode = false;
float velocity;
int waitTime = 680; //WAS 550
int waitTimeP = 100;
int fireDistance = 75; // power needed to shoot full court WAS 73
task flywheelVelocity(){
int position1, position2;
while(true){
position1 = nMotorEncoder(RDflywheel);
wait1Msec(200);
position2 = nMotorEncoder(RDflywheel);
wait1Msec(200);
velocity = (position2 - position1);
}
}
float flywheelSetpoint = 130;
task flywheelP(){
float error;
float lastError = 0;
float flywheelKp = .2;
float flywheelKi;
float flywheelKd;
float integral = 0, derivative;
startTask(flywheelVelocity);
while(true){
error = velocity - flywheelSetpoint;
if( flywheelKi != 0 ) {
if( abs(error) < 50 )
integral = integral + error;
else
integral = 0;
}
else
integral = 0;
derivative = error - lastError;
lastError = error;
motorSpeed += (flywheelKp*error + flywheelKi*integral + flywheelKd*derivative);
if(motorSpeed > 127)
motorSpeed = 127;
else if (motorSpeed < 80)
motorSpeed = 80;
motor[LDflywheel] = motorSpeed;
motor[LUflywheel] = motorSpeed;
motor[RDflywheel] = motorSpeed;
motor[RUflywheel] = motorSpeed;
wait1Msec(35);
}
}
void flywheelSpeed (int speed) {
while(speed != motorSpeed) {
if (speed > 127) //If we give a speed that's too high, discard it
speed = 127;
else if (speed < 0) //If we give a speed that would make the wheels spin negatively, set it to 0
speed = 0;
else if (speed < motorSpeed) //If the wheels are too fast, lower the speed by 1 per x msec
motorSpeed -= 1;
else if (speed > motorSpeed) //If the wheels are going too slow, raise the speed by 1 per x msec
motorSpeed += 1;
else //catch any exceptions
motorSpeed = speed;
//set the speeds of the wheels
motor[LUflywheel] = motorSpeed;
motor[LDflywheel] = motorSpeed;
motor[RUflywheel] = motorSpeed;
motor[RDflywheel] = motorSpeed;
wait1Msec(50); //x msec delay
}
}
task shooter(){
//loop it
while(true){
if (vexRT(Btn6U)) //Slow speed
flywheelSpeed(55);
else if (vexRT(Btn6D)) //Fast speed
flywheelSpeed(50);
else if(!loadFireMode && vexRT(Btn8L))
flywheelSpeed(50); //idle speed for flywheel
wait1Msec(25); //25msec delay
}
}
task drive(){
//Drive loop
while(true){
motor[LFdrive] = vexRT(Ch2);
motor[LBMdrive] = vexRT(Ch2);
motor[RFdrive] = vexRT(Ch3);
motor[RBMdrive] = vexRT(Ch3);
wait1Msec(25);
}
}
int shooterSpeed = 94;
task loadFireSpeedControl() {
while (true) {
if(vexRT(Btn7R)) {
shooterSpeed++;
wait1Msec(300);
}
if(vexRT(Btn7L)) {
shooterSpeed--;
wait1Msec(300);
}
wait1Msec(25);
}
}
task loadFire() {
int timesShot = 0;
flywheelSpeed(fireDistance);
startTask(loadFireSpeedControl);
while(true){
clearTimer(T1); //Clear the timer
while(!SensorValue(ballHigh)) //Get a ball into the top posision
motor[feeder] = 127;
while(SensorValue(ballHigh)) { //When there is a ball in the top position...
if(time1(T1) < waitTime ) //If we still need to wait more,
motor[feeder] = 0; //don't shoot.
else //If we've waited enough time,
motor[feeder] = 127; //shoot.
if(timesShot != 0) {
motor[LUflywheel] = shooterSpeed;
motor[LDflywheel] = shooterSpeed;
motor[RUflywheel] = shooterSpeed;
motor[RDflywheel] = shooterSpeed;
}
timesShot++;
wait1Msec(200); //Wait for the ball to fully leave the intake
}
wait1Msec(25); //25msec delay
}
}
task loadFirePipe() {
int timesShot = 0;
flywheelSpeed(60);
while(true){
clearTimer(T3); //Clear the timer
while(!SensorValue(ballHigh)) //Get a ball into the top posision
motor[feeder] = 127;
while(SensorValue(ballHigh)) { //When there is a ball in the top position...
if(time1(T3) < waitTimeP ) //If we still need to wait more,
motor[feeder] = 0; //don't shoot.
else //If we've waited enough time,
motor[feeder] = 127; //shoot.
if(timesShot != 0) {
motor[LUflywheel] = 80;
motor[LDflywheel] = 80;
motor[RUflywheel] = 80;
motor[RDflywheel] = 80;
}
timesShot++;
wait1Msec(200); //Wait for the ball to fully leave the intake
}
wait1Msec(25); //25msec delay
}
}
task intake() {
int swap = 0;
while(true){
//Begin shooting balls
if(vexRT(Btn7D)){
startTask(loadFirePipe);
loadFireMode = true;
swap = 1;
}
//Stop shooting balls
else if(vexRT(Btn7U)){
stopTask(loadFirePipe);
loadFireMode = false;
motor[feeder] = 0;
swap = 0;
}
//Begin shooting balls
else if(vexRT(Btn8D)){
startTask(loadFire);
loadFireMode = true;
swap = 1;
}
//Stop shooting balls
else if(vexRT(Btn8U)){
stopTask(loadFire);
loadFireMode = false;
motor[feeder] = 0;
swap = 0;
}
//Kills everything if we stop firing
else if(swap == 0){
motor[intake1] = 0;
motor[feeder] = 0;
}
//Rake in balls
if(vexRT(Btn5U)){
motor[intake1] = 127;
}
//Manual feeder control
if(vexRT(Btn5D)){
motor[feeder] = 127;
}
if(vexRT(Btn8R)){
motor[feeder] = -127;
motor[intake1] = -127;
}
wait1Msec(25); //25msec delay
}
}
void pre_auton()
{
bStopTasksBetweenModes = true;
}
task autonomous()
{
flywheelSpeed(78); //gets it to fireDistance
wait1Msec(3500);
for(int i = 4; i>0; i--) {
motor[feeder] = 127;
motor[intake1] = 127;
wait1Msec(600);
motor[feeder] = 0;
motor[intake1] = 0;
wait1Msec(700);
}
flywheelSpeed(0);
}
task usercontrol()
{
startTask(shooter);
startTask(drive);
startTask(intake);
}