1
1
package frc .robot .subsystems .cellmech ;
2
2
3
+ import edu .wpi .first .wpilibj .Timer ;
3
4
import edu .wpi .first .wpilibj2 .command .SubsystemBase ;
4
5
import frc .lib5k .components .motors .TalonHelper ;
5
6
import frc .lib5k .components .motors .motorsensors .TalonEncoder ;
@@ -18,7 +19,7 @@ public class Hopper extends SubsystemBase {
18
19
public static Hopper s_instance = null ;
19
20
private RobotLogger logger = RobotLogger .getInstance ();
20
21
21
- private OI m_OI = OI .getInstance ();
22
+ private OI m_OI = OI .getInstance ();
22
23
23
24
/** Motor that moves hopper belt up and down */
24
25
private SimTalon m_hopperBelt ;
@@ -74,6 +75,9 @@ private enum SystemState {
74
75
/** amount of cells currently in hopper */
75
76
private int m_cellCount = 0 ;
76
77
78
+ // Timer for reset action
79
+ private Timer m_resetTimer ;
80
+
77
81
private Hopper () {
78
82
// Construct motor controller
79
83
m_hopperBelt = new SimTalon (RobotConstants .Hopper .HOPPER_BELT_MOTOR );
@@ -104,11 +108,11 @@ private Hopper() {
104
108
m_lineTopLastValue = false ;
105
109
106
110
// cache array so I don't have to type a bunch of stuff
107
- int [][] rumbles = RobotConstants .Hopper .HOPPER_DONE_RUMBLE_SEQUENCE ;
111
+ int [][] rumbles = RobotConstants .Hopper .HOPPER_DONE_RUMBLE_SEQUENCE ;
108
112
109
113
// find length of array and create array
110
114
int arrayLength = 0 ;
111
- for (int i = 0 ; i < rumbles .length ; i ++) {
115
+ for (int i = 0 ; i < rumbles .length ; i ++) {
112
116
arrayLength += rumbles [i ][1 ];
113
117
}
114
118
m_rumbleSequence = new int [arrayLength ];
@@ -117,14 +121,18 @@ private Hopper() {
117
121
m_rumbleCounter = arrayLength ;
118
122
119
123
// parse 2d array of value,duration pairs into a 1d array of values
120
- int i = 0 ;
121
- for (int y = 0 ; y < rumbles .length ; y ++) {
122
- for (int x = 0 ; x < rumbles [y ][1 ]; x ++) {
124
+ int i = 0 ;
125
+ for (int y = 0 ; y < rumbles .length ; y ++) {
126
+ for (int x = 0 ; x < rumbles [y ][1 ]; x ++) {
123
127
m_rumbleSequence [i ] = rumbles [y ][0 ];
124
128
i ++;
125
129
}
126
130
}
127
131
132
+ // Set up the reset timer
133
+ m_resetTimer = new Timer ();
134
+ m_resetTimer .reset ();
135
+
128
136
// Add children
129
137
addChild ("Belt" , m_hopperBelt );
130
138
addChild ("Bottom Limit" , m_lineBottom );
@@ -148,13 +156,14 @@ public static Hopper getInstance() {
148
156
149
157
@ Override
150
158
public void periodic () {
151
- if (m_rumbleCounter < m_rumbleSequence .length ) {
152
- m_OI .rumbleOperator ((double )m_rumbleSequence [m_rumbleCounter ]);
159
+ if (m_rumbleCounter < m_rumbleSequence .length ) {
160
+ m_OI .rumbleOperator ((double ) m_rumbleSequence [m_rumbleCounter ]);
153
161
System .out .println (m_rumbleSequence [m_rumbleCounter ]);
154
162
m_rumbleCounter ++;
155
163
}
156
164
157
- if (m_systemState == SystemState .INTAKING || m_systemState == SystemState .INTAKEREADY || m_systemState == SystemState .UNJAM || m_systemState == SystemState .SHOOTING ) {
165
+ if (m_systemState == SystemState .INTAKING || m_systemState == SystemState .INTAKEREADY
166
+ || m_systemState == SystemState .UNJAM || m_systemState == SystemState .SHOOTING ) {
158
167
// Count cells
159
168
160
169
// cache values of line break sensors
@@ -256,11 +265,11 @@ private void handleIntakeReady(boolean newState) {
256
265
257
266
}
258
267
// increase counter when cell in range, reset to 0 when out of range
259
- if (m_lineBottom .get ()) {
268
+ if (m_lineBottom .get ()) {
260
269
m_intakeDelayCounter ++;
261
270
262
271
// if count reaches the desired amount of delay, starting intaking the cell
263
- if (m_intakeDelayCounter >= RobotConstants .Hopper .CYCLES_BEFORE_INTAKE ) {
272
+ if (m_intakeDelayCounter >= RobotConstants .Hopper .CYCLES_BEFORE_INTAKE ) {
264
273
m_systemState = SystemState .INTAKING ;
265
274
m_intakeDelayCounter = 0 ;
266
275
}
@@ -293,8 +302,8 @@ private void handleIntaking(boolean newState) {
293
302
m_systemState = SystemState .INTAKEREADY ;
294
303
}
295
304
296
- // if no cells in hopper, only rely on sensors
297
- if (m_cellCount > 0 ) {
305
+ // if no cells in hopper, only rely on sensors
306
+ if (m_cellCount > 0 ) {
298
307
// if belt has gone 12 inches, stop tying and set state to ready to intake
299
308
if (m_ticksAtStartOfIntake - m_hopperEncoder .getTicks () >= 41583 ) {
300
309
m_systemState = SystemState .INTAKEREADY ;
@@ -349,12 +358,22 @@ private void handleMoveToBottom(boolean newState) {
349
358
// Start belt
350
359
setBeltSpeed (-0.5 );
351
360
361
+ // Reset the timer
362
+ m_resetTimer .reset ();
363
+ m_resetTimer .start ();
364
+
352
365
}
353
366
354
367
// when a cell reaches the bottom, stop
355
368
if (m_lineBottom .get ()) {
356
369
m_systemState = SystemState .MOVEUPONEPLACE ;
357
370
}
371
+
372
+ // If our timer runs out, reset the counters, and go idle
373
+ if (m_resetTimer .hasPeriodPassed (RobotConstants .Hopper .RESET_TIMEOUT_SECONDS )) {
374
+ forceCellCount (0 );
375
+ m_systemState = SystemState .IDLE ;
376
+ }
358
377
}
359
378
360
379
/**
@@ -455,7 +474,7 @@ public boolean topLineBreakState() {
455
474
*/
456
475
public void interruptShooting () {
457
476
logger .log ("Hopper" , "Shooting interrupt requested" );
458
- if (m_cellCount > 0 ) {
477
+ if (m_cellCount > 0 ) {
459
478
m_systemState = SystemState .MOVETOBOTTOM ;
460
479
}
461
480
}
0 commit comments