-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from Robocubs/indexer-subsystem
Indexer-subsystem
- Loading branch information
Showing
5 changed files
with
192 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/com/team1701/robot/commands/IndexCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.team1701.robot.commands; | ||
|
||
import java.util.function.BooleanSupplier; | ||
|
||
import com.team1701.robot.subsystems.indexer.Indexer; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
|
||
public class IndexCommand extends Command { | ||
private final Indexer mIndexer; | ||
private final BooleanSupplier mShouldLoad; | ||
|
||
public IndexCommand(Indexer indexer, BooleanSupplier shouldLoad) { | ||
mIndexer = indexer; | ||
mShouldLoad = shouldLoad; | ||
addRequirements(indexer); | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
if (mIndexer.noteIsLoaded() || !mShouldLoad.getAsBoolean()) { | ||
mIndexer.stop(); | ||
} else { | ||
mIndexer.setForwardLoad(); | ||
} | ||
} | ||
|
||
@Override | ||
public void end(boolean interrupted) { | ||
mIndexer.stop(); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/com/team1701/robot/subsystems/indexer/Indexer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.team1701.robot.subsystems.indexer; | ||
|
||
import com.team1701.lib.drivers.digitalinputs.DigitalIO; | ||
import com.team1701.lib.drivers.digitalinputs.DigitalInputsAutoLogged; | ||
import com.team1701.lib.drivers.motors.MotorIO; | ||
import com.team1701.lib.drivers.motors.MotorIOSim; | ||
import com.team1701.lib.drivers.motors.MotorInputsAutoLogged; | ||
import com.team1701.robot.Constants; | ||
import edu.wpi.first.math.system.plant.DCMotor; | ||
import edu.wpi.first.wpilibj2.command.SubsystemBase; | ||
import org.littletonrobotics.junction.AutoLogOutput; | ||
import org.littletonrobotics.junction.Logger; | ||
|
||
public class Indexer extends SubsystemBase { | ||
private final MotorIO mIndexerMotorIO; | ||
private final MotorInputsAutoLogged mIndexerMotorInputsAutoLogged = new MotorInputsAutoLogged(); | ||
private final DigitalIO mEntranceSensor; | ||
private final DigitalInputsAutoLogged mEntranceSensorInputs = new DigitalInputsAutoLogged(); | ||
private final DigitalIO mExitSensor; | ||
private final DigitalInputsAutoLogged mExitSensorInputs = new DigitalInputsAutoLogged(); | ||
|
||
@AutoLogOutput(key = "Indexer/Motor/DemandRadiansPerSecond") | ||
private double mDemandRadiansPerSecond; | ||
|
||
public Indexer(MotorIO motor, DigitalIO entranceSensor, DigitalIO exitSensor) { | ||
mEntranceSensor = entranceSensor; | ||
mExitSensor = exitSensor; | ||
mIndexerMotorIO = motor; | ||
} | ||
|
||
public static MotorIOSim createSim(DCMotor IndexerMotor) { | ||
return new MotorIOSim(IndexerMotor, Constants.Indexer.kIndexerReduction, 0.025, Constants.kLoopPeriodSeconds); | ||
} | ||
|
||
@Override | ||
public void periodic() { | ||
mIndexerMotorIO.updateInputs(mIndexerMotorInputsAutoLogged); | ||
mEntranceSensor.updateInputs(mEntranceSensorInputs); | ||
mExitSensor.updateInputs(mExitSensorInputs); | ||
Logger.processInputs("Indexer/EntranceSensor", mEntranceSensorInputs); | ||
Logger.processInputs("Indexer/Motor", mIndexerMotorInputsAutoLogged); | ||
Logger.processInputs("Indexer/ExitSensor", mExitSensorInputs); | ||
} | ||
|
||
public boolean noteIsLoaded() { | ||
return mExitSensorInputs.blocked; | ||
} | ||
|
||
public void setForwardLoad() { | ||
mIndexerMotorIO.setPercentOutput(Constants.Indexer.kIndexerLoadPercent); | ||
} | ||
|
||
public void setForwardShoot() { | ||
mIndexerMotorIO.setPercentOutput(Constants.Indexer.kIndexerFeedPercent); | ||
} | ||
|
||
public void setReverse() { | ||
mIndexerMotorIO.setPercentOutput(-Constants.Indexer.kIndexerFeedPercent); | ||
} | ||
|
||
public void stop() { | ||
mIndexerMotorIO.setPercentOutput(0); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/com/team1701/robot/subsystems/indexer/IndexerMotorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.team1701.robot.subsystems.indexer; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import com.revrobotics.CANSparkFlex; | ||
import com.revrobotics.CANSparkLowLevel.MotorType; | ||
import com.revrobotics.REVLibError; | ||
import com.team1701.lib.alerts.REVAlert; | ||
import com.team1701.lib.drivers.motors.MotorIOSparkFlex; | ||
import com.team1701.robot.Constants; | ||
|
||
public class IndexerMotorFactory { | ||
public static MotorIOSparkFlex createDriveMotorIOSparkFlex(int deviceId) { | ||
var motor = new CANSparkFlex(deviceId, MotorType.kBrushless); | ||
var encoder = motor.getEncoder(); | ||
var controller = motor.getPIDController(); | ||
var errorAlert = new REVAlert(motor, deviceId); | ||
|
||
motor.setCANTimeout(200); | ||
|
||
configureWithRetry(() -> motor.restoreFactoryDefaults(), errorAlert); | ||
|
||
configureWithRetry(() -> motor.setSmartCurrentLimit(20), errorAlert); | ||
configureWithRetry(() -> motor.enableVoltageCompensation(12), errorAlert); | ||
|
||
configureWithRetry(() -> encoder.setPosition(0), errorAlert); | ||
configureWithRetry(() -> encoder.setMeasurementPeriod(10), errorAlert); | ||
configureWithRetry(() -> encoder.setAverageDepth(2), errorAlert); | ||
|
||
configureWithRetry(() -> controller.setP(Constants.Indexer.kIndexerKp.get()), errorAlert); | ||
configureWithRetry(() -> controller.setD(Constants.Indexer.kIndexerKd.get()), errorAlert); | ||
configureWithRetry(() -> controller.setFF(Constants.Indexer.kIndexerKff.get()), errorAlert); | ||
|
||
configureWithRetry(() -> motor.burnFlash(), errorAlert); | ||
|
||
motor.setCANTimeout(0); | ||
|
||
return new MotorIOSparkFlex(motor, Constants.Shooter.kShooterReduction); | ||
} | ||
|
||
private static void configureWithRetry(Supplier<REVLibError> config, REVAlert failureAlert) { | ||
REVLibError error = REVLibError.kUnknown; | ||
for (var i = 0; i < 4; i++) { | ||
error = config.get(); | ||
if (error == REVLibError.kOk) { | ||
return; | ||
} | ||
} | ||
|
||
failureAlert.enable(error); | ||
} | ||
} |