Skip to content
This repository was archived by the owner on Nov 27, 2020. It is now read-only.

Commit b7d94c0

Browse files
committed
Merge branch 'intake-auto-reset' into intake-fixes
2 parents bb0615e + 429c730 commit b7d94c0

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

src/main/java/frc/robot/RobotConstants.java

+3
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ public static class Intake {
232232
*/
233233
public static class Hopper {
234234

235+
// Reset Timer
236+
public static final double RESET_TIMEOUT_SECONDS = 4.0;
237+
235238
// Motor
236239
public static final int HOPPER_BELT_MOTOR = 12;
237240

src/main/java/frc/robot/subsystems/cellmech/Hopper.java

+33-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package frc.robot.subsystems.cellmech;
22

3+
import edu.wpi.first.wpilibj.Timer;
34
import edu.wpi.first.wpilibj2.command.SubsystemBase;
45
import frc.lib5k.components.motors.TalonHelper;
56
import frc.lib5k.components.motors.motorsensors.TalonEncoder;
@@ -18,7 +19,7 @@ public class Hopper extends SubsystemBase {
1819
public static Hopper s_instance = null;
1920
private RobotLogger logger = RobotLogger.getInstance();
2021

21-
private OI m_OI = OI.getInstance();
22+
private OI m_OI = OI.getInstance();
2223

2324
/** Motor that moves hopper belt up and down */
2425
private SimTalon m_hopperBelt;
@@ -74,6 +75,9 @@ private enum SystemState {
7475
/** amount of cells currently in hopper */
7576
private int m_cellCount = 0;
7677

78+
// Timer for reset action
79+
private Timer m_resetTimer;
80+
7781
private Hopper() {
7882
// Construct motor controller
7983
m_hopperBelt = new SimTalon(RobotConstants.Hopper.HOPPER_BELT_MOTOR);
@@ -104,11 +108,11 @@ private Hopper() {
104108
m_lineTopLastValue = false;
105109

106110
// 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;
108112

109113
// find length of array and create array
110114
int arrayLength = 0;
111-
for(int i = 0; i < rumbles.length; i++) {
115+
for (int i = 0; i < rumbles.length; i++) {
112116
arrayLength += rumbles[i][1];
113117
}
114118
m_rumbleSequence = new int[arrayLength];
@@ -117,14 +121,18 @@ private Hopper() {
117121
m_rumbleCounter = arrayLength;
118122

119123
// 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++) {
123127
m_rumbleSequence[i] = rumbles[y][0];
124128
i++;
125129
}
126130
}
127131

132+
// Set up the reset timer
133+
m_resetTimer = new Timer();
134+
m_resetTimer.reset();
135+
128136
// Add children
129137
addChild("Belt", m_hopperBelt);
130138
addChild("Bottom Limit", m_lineBottom);
@@ -148,13 +156,14 @@ public static Hopper getInstance() {
148156

149157
@Override
150158
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]);
153161
System.out.println(m_rumbleSequence[m_rumbleCounter]);
154162
m_rumbleCounter++;
155163
}
156164

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) {
158167
// Count cells
159168

160169
// cache values of line break sensors
@@ -256,11 +265,11 @@ private void handleIntakeReady(boolean newState) {
256265

257266
}
258267
// increase counter when cell in range, reset to 0 when out of range
259-
if(m_lineBottom.get()) {
268+
if (m_lineBottom.get()) {
260269
m_intakeDelayCounter++;
261270

262271
// 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) {
264273
m_systemState = SystemState.INTAKING;
265274
m_intakeDelayCounter = 0;
266275
}
@@ -293,8 +302,8 @@ private void handleIntaking(boolean newState) {
293302
m_systemState = SystemState.INTAKEREADY;
294303
}
295304

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) {
298307
// if belt has gone 12 inches, stop tying and set state to ready to intake
299308
if (m_ticksAtStartOfIntake - m_hopperEncoder.getTicks() >= 41583) {
300309
m_systemState = SystemState.INTAKEREADY;
@@ -349,12 +358,22 @@ private void handleMoveToBottom(boolean newState) {
349358
// Start belt
350359
setBeltSpeed(-0.5);
351360

361+
// Reset the timer
362+
m_resetTimer.reset();
363+
m_resetTimer.start();
364+
352365
}
353366

354367
// when a cell reaches the bottom, stop
355368
if (m_lineBottom.get()) {
356369
m_systemState = SystemState.MOVEUPONEPLACE;
357370
}
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+
}
358377
}
359378

360379
/**
@@ -455,7 +474,7 @@ public boolean topLineBreakState() {
455474
*/
456475
public void interruptShooting() {
457476
logger.log("Hopper", "Shooting interrupt requested");
458-
if(m_cellCount > 0) {
477+
if (m_cellCount > 0) {
459478
m_systemState = SystemState.MOVETOBOTTOM;
460479
}
461480
}

0 commit comments

Comments
 (0)