generated from SciBorgs/Hydrogen
-
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 #4 from SciBorgs/hopper
Hopper
- Loading branch information
Showing
6 changed files
with
145 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
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,68 @@ | ||
package org.sciborgs1155.robot.hopper; | ||
|
||
import edu.wpi.first.wpilibj2.command.Command; | ||
import edu.wpi.first.wpilibj2.command.SubsystemBase; | ||
import edu.wpi.first.wpilibj2.command.button.Trigger; | ||
import org.sciborgs1155.robot.Robot; | ||
|
||
public class Hopper extends SubsystemBase implements AutoCloseable { | ||
/** Creates a real or non-existent hopper based on {@link Robot#isReal()}. */ | ||
public static Hopper create() { | ||
return Robot.isReal() ? new Hopper(new RealHopper()) : Hopper.none(); | ||
} | ||
|
||
/** Creates a non-existent hopper. */ | ||
public static Hopper none() { | ||
return new Hopper(new NoHopper()); | ||
} | ||
|
||
private final HopperIO hardware; | ||
public final Trigger beambreakTrigger; | ||
|
||
public Hopper(HopperIO hardware) { | ||
this.hardware = hardware; | ||
this.beambreakTrigger = new Trigger(hardware::beambreak); | ||
} | ||
|
||
/** | ||
* Runs the motors of the hopper. | ||
* | ||
* @param power The power to set the hoppers motors to. | ||
* @return A command to set the power of the hopper motors. | ||
*/ | ||
public Command run(double power) { | ||
return runOnce(() -> hardware.setPower(power)); | ||
} | ||
|
||
/** | ||
* Intakes corals from human player. | ||
* | ||
* @return A command to intake corals. | ||
*/ | ||
public Command intake() { | ||
return run(HopperConstants.INTAKE_POWER); // more logic later | ||
} | ||
|
||
/** | ||
* Outtake corals. | ||
* | ||
* @return A command to outtake corals. | ||
*/ | ||
public Command outtake() { | ||
return run(-HopperConstants.INTAKE_POWER); | ||
} | ||
|
||
/** | ||
* Stops the hopper. | ||
* | ||
* @return A command to stop the hopper. | ||
*/ | ||
public Command stop() { | ||
return run(0); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
hardware.close(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/org/sciborgs1155/robot/hopper/HopperConstants.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,5 @@ | ||
package org.sciborgs1155.robot.hopper; | ||
|
||
public class HopperConstants { | ||
public static final double INTAKE_POWER = 0.9; | ||
} |
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,7 @@ | ||
package org.sciborgs1155.robot.hopper; | ||
|
||
public interface HopperIO extends AutoCloseable { | ||
void setPower(double power); | ||
|
||
boolean beambreak(); | ||
} |
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,14 @@ | ||
package org.sciborgs1155.robot.hopper; | ||
|
||
public class NoHopper implements HopperIO { | ||
@Override | ||
public void setPower(double power) {} | ||
|
||
@Override | ||
public boolean beambreak() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void close() {} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/org/sciborgs1155/robot/hopper/RealHopper.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,45 @@ | ||
package org.sciborgs1155.robot.hopper; | ||
|
||
import com.revrobotics.spark.SparkBase.PersistMode; | ||
import com.revrobotics.spark.SparkBase.ResetMode; | ||
import com.revrobotics.spark.SparkLowLevel.MotorType; | ||
import com.revrobotics.spark.SparkMax; | ||
import com.revrobotics.spark.config.SparkBaseConfig; | ||
import com.revrobotics.spark.config.SparkMaxConfig; | ||
import edu.wpi.first.wpilibj.DigitalInput; | ||
import org.sciborgs1155.robot.Ports.Hopper; | ||
|
||
public class RealHopper implements HopperIO { | ||
private final SparkMax leftMotor = new SparkMax(Hopper.LEFT_MOTOR, MotorType.kBrushless); | ||
private final SparkMax rightMotor = new SparkMax(Hopper.RIGHT_MOTOR, MotorType.kBrushless); | ||
private final DigitalInput beambreak = new DigitalInput(Hopper.BEAMBREAK); | ||
|
||
public RealHopper() { | ||
SparkBaseConfig config = new SparkMaxConfig(); // prob more configs to do | ||
config.inverted(true); | ||
|
||
rightMotor.configure( | ||
config, | ||
ResetMode.kResetSafeParameters, | ||
PersistMode.kPersistParameters); // one of the motors are inverted so that the motors can | ||
// intake/outtake right | ||
} | ||
|
||
@Override | ||
public void setPower(double power) { | ||
leftMotor.set(power); | ||
rightMotor.set(power); | ||
} | ||
|
||
@Override | ||
public boolean beambreak() { | ||
return beambreak.get(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
leftMotor.close(); | ||
rightMotor.close(); | ||
beambreak.close(); | ||
} | ||
} |