-
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.
- Loading branch information
Showing
5 changed files
with
216 additions
and
0 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
32 changes: 32 additions & 0 deletions
32
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,32 @@ | ||
package com.team1701.robot.commands; | ||
|
||
import edu.wpi.first.wpilibj2.command.Command; | ||
import com.team1701.robot.subsystems.indexer.IndexerIO; | ||
import com.team1701.robot.subsystems.indexer.Indexer.IndexerIOSensors; | ||
//import com.team1701.robot.subsystems.elevator.*; | ||
//import com.team1701.robot.subsystems.intake.*; | ||
|
||
|
||
public class IndexCommand extends Command { | ||
private boolean shouldLoad; | ||
private boolean enterSensor = IndexerIOSensors.entranceSensorBlocked; | ||
private boolean exitSensor = IndexerIOSensors.exitSensorBlocked; | ||
//private boolean elevatorReady = elevator is down | ||
//private boolean intakeSensor = intake has piece | ||
public static void loadPiece( | ||
boolean shouldLoad, | ||
boolean enterSensor, | ||
boolean exitSensor, | ||
boolean intakeSensor, | ||
boolean elevatorReady | ||
) { | ||
if(intakeSensor && !exitSensor && shouldLoad) { | ||
return LoadPiece.Index; | ||
} else if (exitSensor && shouldLoad && elevatorReady) { | ||
return LoadPiece.Load; | ||
} else { | ||
//do nothing? | ||
} | ||
} | ||
|
||
} |
96 changes: 96 additions & 0 deletions
96
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,96 @@ | ||
package com.team1701.robot.subsystems.indexer; | ||
|
||
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.wpilibj.DigitalInput; | ||
import edu.wpi.first.wpilibj2.command.SubsystemBase; | ||
import org.littletonrobotics.junction.AutoLogOutput; | ||
import org.littletonrobotics.junction.Logger; | ||
|
||
public class Indexer extends SubsystemBase { | ||
private MotorIO mIndexerMotorIO; | ||
private final MotorInputsAutoLogged mIndexerMotorInputsAutoLogged = new MotorInputsAutoLogged(); | ||
private final IndexerInputsAutoLogged mIndexerInputsAutoLogged = new IndexerInputsAutoLogged(); | ||
private final IndexerIO.IndexerInputs mIndexerInputs = new IndexerIO.IndexerInputs(); | ||
private DigitalInput mEntranceSensor; | ||
private DigitalInput mExitSensor; | ||
private IndexerIO indexerIO = new IndexerIOSensors(); | ||
|
||
@AutoLogOutput(key = "Indexer/Motor/DemandRadiansPerSecond") | ||
private double mDemandRadiansPerSecond; | ||
|
||
public Indexer(MotorIO motor) { | ||
mEntranceSensor = new DigitalInput(Constants.Indexer.kIndexerEntranceSensorPort); | ||
mExitSensor = new DigitalInput(Constants.Indexer.kIndexerExitSensorPort); | ||
mIndexerMotorIO = motor; | ||
setPID( | ||
Constants.Indexer.kIndexerKff.get(), | ||
Constants.Indexer.kIndexerKp.get(), | ||
0, | ||
Constants.Indexer.kIndexerKd.get()); | ||
mIndexerMotorIO.setBrakeMode(false); | ||
|
||
} | ||
|
||
public static MotorIOSim createSim(DCMotor IndexerMotor) { | ||
return new MotorIOSim(IndexerMotor, Constants.Indexer.kIndexerReduction, 0.025, Constants.kLoopPeriodSeconds); | ||
} | ||
|
||
@Override | ||
public void periodic() { | ||
var hash = hashCode(); | ||
mIndexerMotorIO.updateInputs(mIndexerMotorInputsAutoLogged); | ||
Logger.processInputs("Indexer/Motor", mIndexerMotorInputsAutoLogged); | ||
if (Constants.Indexer.kIndexerKff.hasChanged(hash) | ||
|| Constants.Indexer.kIndexerKp.hasChanged(hash) | ||
|| Constants.Indexer.kIndexerKd.hasChanged(hash)) { | ||
setPID( | ||
Constants.Indexer.kIndexerKff.get(), | ||
Constants.Indexer.kIndexerKp.get(), | ||
0, | ||
Constants.Indexer.kIndexerKd.get()); | ||
} | ||
indexerIO.updateInputs(mIndexerInputsAutoLogged); | ||
} | ||
|
||
public void setPID(double ff, double p, double i, double d) { | ||
mIndexerMotorIO.setPID(ff, p, i, d); | ||
} | ||
|
||
public void setSpeed(double radiansPerSecond) { | ||
mIndexerMotorIO.setVelocityControl(radiansPerSecond); | ||
} | ||
|
||
public void loadIndexer() { | ||
if(mIndexerInputs.entranceSensorBlocked == true) { | ||
mIndexerMotorIO.setPercentOutput(Constants.Indexer.kIndexerLoadPercent); | ||
} else if(mIndexerInputs.exitSensorBlocked == true) { | ||
mIndexerMotorIO.setPercentOutput(0); | ||
} | ||
} | ||
|
||
public void setForward() { | ||
mIndexerMotorIO.setPercentOutput(Constants.Indexer.kIndexerFeedPercent); | ||
} | ||
|
||
public void setReverse() { | ||
mIndexerMotorIO.setPercentOutput(-Constants.Indexer.kIndexerFeedPercent); | ||
} | ||
|
||
public void stop () { | ||
mIndexerMotorIO.setPercentOutput(0); | ||
} | ||
|
||
|
||
public class IndexerIOSensors implements IndexerIO { | ||
|
||
@Override | ||
public void updateInputs(IndexerInputs inputs) { | ||
inputs.entranceSensorBlocked = mEntranceSensor.get(); | ||
inputs.exitSensorBlocked = mExitSensor.get(); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/team1701/robot/subsystems/indexer/IndexerIO.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,20 @@ | ||
package com.team1701.robot.subsystems.indexer; | ||
|
||
import org.littletonrobotics.junction.AutoLog; | ||
|
||
import edu.wpi.first.wpilibj.DigitalInput; | ||
|
||
public interface IndexerIO{ | ||
@AutoLog | ||
public static class IndexerInputs { | ||
public boolean entranceSensorBlocked; | ||
public boolean exitSensorBlocked; | ||
|
||
} | ||
boolean entranceSensorBlocked = false; | ||
boolean exitSensorBlocked = false; | ||
public default void updateInputs(IndexerInputs inputs) { | ||
|
||
} | ||
|
||
} |
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(80), 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); | ||
} | ||
} |