-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntakeSubsystem.java
More file actions
103 lines (87 loc) · 3.45 KB
/
IntakeSubsystem.java
File metadata and controls
103 lines (87 loc) · 3.45 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
package frc.robot.subsystems;
import com.revrobotics.PersistMode;
import com.revrobotics.ResetMode;
import com.revrobotics.spark.SparkLowLevel;
import com.revrobotics.spark.SparkMax;
import com.revrobotics.spark.config.SparkBaseConfig;
import com.revrobotics.spark.config.SparkMaxConfig;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.Robot;
import frc.robot.commands.intake.SpinIntake;
import frc.robot.constants.Constants;
import frc.robot.constants.GameConstants;
import frc.robot.constants.enums.DeploymentState;
import frc.robot.utils.diag.DiagSparkMaxEncoder;
import frc.robot.utils.logging.input.DigitalInputLoggableInputs;
import frc.robot.utils.logging.input.MotorLoggableInputs;
import frc.robot.utils.logging.io.motor.MockSparkMaxIo;
import frc.robot.utils.logging.io.motor.RealSparkMaxIo;
import frc.robot.utils.logging.io.motor.SimSparkMaxIo;
import frc.robot.utils.logging.io.motor.SparkMaxIo;
import frc.robot.utils.simulation.MotorSimulator;
import frc.robot.utils.simulation.RobotVisualizer;
import org.littletonrobotics.junction.Logger;
import static java.lang.Math.abs;
public class IntakeSubsystem extends SubsystemBase {
public static final String LOGGING_NAME = "IntakeSubsystem";
private final SparkMaxIo io;
private boolean intakeStalled = false;
private boolean intaking = false;
private Timer stallTimer = new Timer();
public IntakeSubsystem(SparkMaxIo io) {
this.io = io;
}
public void setSpeed(double speed) {
io.set(speed);
intaking = true;
}
public void stopMotors() {
io.stopMotor();
intaking = false;
intakeStalled = false;
}
@Override
public void periodic() {
io.periodic();
if (intaking && abs(io.getVelocity())<Constants.INTAKE_STALL_SPEED && stallTimer.hasElapsed(Constants.INTAKE_STALL_TIME)) {
intakeStalled = true;
} else {
intakeStalled = false;
}
}
public static SparkMaxIo createMockIo() {
return new MockSparkMaxIo(LOGGING_NAME, MotorLoggableInputs.allMetrics());
}
public static SparkMaxIo createRealIo() {
SparkMax motor = createMotor();
Robot.getDiagnostics()
.addDiagnosable(
new DiagSparkMaxEncoder(
"Intake Roller", "Encoder", GameConstants.INTAKE_ROLLER_DIAGS_ENCODER, motor));
return new RealSparkMaxIo(LOGGING_NAME, motor, MotorLoggableInputs.allMetrics());
}
public static SparkMaxIo createSimIo(RobotVisualizer visualizer) {
SparkMax motor = createMotor();
return new SimSparkMaxIo(LOGGING_NAME, motor, MotorLoggableInputs.allMetrics(),
new MotorSimulator(motor, visualizer.getIntakeLigament()));
}
private static SparkMax createMotor() {
SparkMax motor = new SparkMax(Constants.INTAKE_MOTOR_ID, SparkLowLevel.MotorType.kBrushless);
SparkMaxConfig motorConfig = new SparkMaxConfig();
motorConfig.idleMode(SparkBaseConfig.IdleMode.kBrake);
motorConfig.smartCurrentLimit(Constants.NEO_CURRENT_LIMIT);
motor.configure(
motorConfig,
ResetMode.kResetSafeParameters,
PersistMode.kPersistParameters);
return motor;
}
public boolean getIntakeStalled() {
return intakeStalled;
}
public void startIntaking() {
if (!intaking)
stallTimer.reset();
}
}