-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonDecode.java
More file actions
344 lines (291 loc) · 9.57 KB
/
CommonDecode.java
File metadata and controls
344 lines (291 loc) · 9.57 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
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
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.hardware.DcMotorEx;
import com.qualcomm.robotcore.util.ElapsedTime;
import com.qualcomm.robotcore.hardware.HardwareMap;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.hardware.DcMotor;
public class CommonDecode {
private HardwareMap hardwareMap = null;
private Servo ballDrop = null;
private Servo rotator = null;
private DcMotorEx shooter = null;
private DcMotorEx intake = null;
enum ColorOrder { GPP, PGP, PPG }
enum ShooterState { Off, SpinningUp, On }
enum ShootAllBallsState {
Off,
StartSpinning,
RotateTo1,
RotateTo2,
RotateTo3,
StopSpinning,
Done }
// states
private ColorOrder colorOrder = ColorOrder.GPP;
private int currentBallSlot = 1; // 1, 2 or 3
private ShooterState shooterState = ShooterState.Off;
private ShootAllBallsState shootAllBallsState = ShootAllBallsState.Off;
private double shootAllBallsDelay = 0.0;
private ElapsedTime shootAllBallsTimer = new ElapsedTime();
private ElapsedTime spinningUpTime = new ElapsedTime();
// constants
private final double intakePower = 1.0;
private final double retrieveVelocity = 725.0;
private final double shootVelocity = 1650.0;
private final double ballDropDown = 0.3;
private final double ballDropUp = 0.85;
private final double ballSlot1 = 1.0;
private final double ballSlot2 = 0.53;
private final double ballSlot3 = 0.08;
public void initialize (HardwareMap hardwareMap)
{
this.hardwareMap = hardwareMap;
ballDrop = hardwareMap.get(Servo.class, "balldrop");
rotator = hardwareMap.get(Servo.class, "rotator");
shooter = hardwareMap.get(DcMotorEx.class, "shooter");
intake = hardwareMap.get(DcMotorEx.class, "intake");
shooter.setDirection(DcMotor.Direction.FORWARD);
intake.setDirection(DcMotor.Direction.FORWARD);
// raise the ball drop
setBallDropDown (false);
// start in slot 1
rotateToSlot (1);
}
public int getCurrentBallSlot ()
{
return currentBallSlot;
}
public ShootAllBallsState getShootAllBallsState ()
{
return shootAllBallsState;
}
public void checkShooterState ()
{
double rotateDelay = 0;
// if finished spinning up, drop the ball
if (shooterState == ShooterState.SpinningUp &&
spinningUpTime.seconds () > 1.0)
{
shooterState = ShooterState.On;
setBallDropDown (true);
}
if (shootAllBallsTimer.seconds () > shootAllBallsDelay)
{
shootAllBallsTimer.reset ();
switch (shootAllBallsState)
{
case Off:
break;
case StartSpinning:
shootOn (true);
switch (colorOrder)
{
case GPP:
rotateDelay = rotateToSlot (1);
shootAllBallsState = ShootAllBallsState.RotateTo2;
break;
case PGP:
rotateDelay = rotateToSlot (2);
shootAllBallsState = ShootAllBallsState.RotateTo1;
break;
case PPG:
rotateDelay = rotateToSlot (3);
shootAllBallsState = ShootAllBallsState.RotateTo2;
break;
}
shootAllBallsDelay = 2.0;
break;
case RotateTo1:
rotateDelay = rotateToSlot (1);
switch (colorOrder)
{
case GPP:
shootAllBallsState = ShootAllBallsState.RotateTo2;
break;
case PGP:
shootAllBallsState = ShootAllBallsState.RotateTo3;
break;
case PPG:
shootAllBallsState = ShootAllBallsState.StopSpinning;
break;
}
shootAllBallsDelay = 1.0 + rotateDelay;
break;
case RotateTo2:
rotateDelay = rotateToSlot (2);
switch (colorOrder)
{
case GPP:
shootAllBallsState = ShootAllBallsState.RotateTo3;
break;
case PGP:
shootAllBallsState = ShootAllBallsState.RotateTo1;
break;
case PPG:
shootAllBallsState = ShootAllBallsState.RotateTo1;
break;
}
shootAllBallsDelay = 1.0 + rotateDelay;
break;
case RotateTo3:
rotateDelay = rotateToSlot (3);
switch (colorOrder)
{
case GPP:
shootAllBallsState = ShootAllBallsState.StopSpinning;
break;
case PGP:
shootAllBallsState = ShootAllBallsState.StopSpinning;
break;
case PPG:
shootAllBallsState = ShootAllBallsState.RotateTo2;
break;
}
shootAllBallsDelay = 1.0 + rotateDelay;
break;
case StopSpinning:
rotateToSlot (1);
shootOn (false);
shootAllBallsState = ShootAllBallsState.Done;
shootAllBallsDelay = 1.0;
break;
case Done:
shootAllBallsState = ShootAllBallsState.Off;
shootAllBallsDelay = 0.0;
break;
}
}
}
public void setBallDropDown (boolean state)
{
if (state == true)
{
ballDrop.setPosition (ballDropDown);
}
else
{
ballDrop.setPosition (ballDropUp);
}
}
public void setColorOrder (ColorOrder colorOrder)
{
this.colorOrder = colorOrder;
}
public void setIntakeOn (boolean state)
{
if (state == true)
{
intake.setPower(intakePower);
}
else // turn off motor
{
intake.setVelocity(0.0);
}
}
public void setIntakeReverse (boolean state)
{
if (state == true)
{
intake.setPower(-intakePower);
}
else // turn off motor
{
intake.setVelocity(0.0);
}
}
public void setShooterOn (boolean state)
{
if (state == true)
{
shooter.setVelocity(shootVelocity);
// if it is already on, no change in state
if (shooterState == ShooterState.Off)
{
shooterState = ShooterState.SpinningUp;
spinningUpTime.reset ();
}
}
else // turn off motor
{
shooter.setVelocity(0.0);
shooterState = ShooterState.Off;
}
}
public void setShooterRetrieveOn (boolean state)
{
if (state == true)
{
shooter.setVelocity(retrieveVelocity);
}
else // turn off motor
{
shooter.setVelocity(0.0);
}
// all retrieve states are not shooting
shooterState = ShooterState.Off;
}
// returns time to rotate from current slot to new slot
public double rotateToSlot (int slot)
{
final double ONE_SLOT_DELAY = 0.750; // 750 milliseconds
double rotateDelay = Math.abs(slot - currentBallSlot) * ONE_SLOT_DELAY;
switch (slot)
{
case 1:
rotator.setPosition (ballSlot1);
break;
case 2:
rotator.setPosition (ballSlot2);
break;
case 3:
rotator.setPosition (ballSlot3);
break;
}
currentBallSlot = slot;
return rotateDelay;
}
public void rotateCW ()
{
if (currentBallSlot < 3)
{
rotateToSlot (currentBallSlot + 1);
}
}
public void rotateCCW ()
{
if (currentBallSlot > 1)
{
rotateToSlot (currentBallSlot - 1);
}
}
public void retrieveOn (boolean state)
{
setShooterRetrieveOn (state);
setIntakeOn (state);
// always pull up the ball drop
setBallDropDown (false);
}
public void shootOn (boolean state)
{
setShooterOn (state);
setIntakeOn (state);
if (state == false)
{
// pull up the ball drop when done shooting
setBallDropDown (false);
}
}
public void shootAllBalls (boolean state)
{
if (state == true)
{
shootAllBallsState = ShootAllBallsState.StartSpinning;
}
else
{
// stop immediately
shootAllBallsState = ShootAllBallsState.StopSpinning;
shootAllBallsDelay = 0.0;
}
}
}