[examples] Move triggers to subsystem fields (#6318)

This commit is contained in:
DeltaDizzy
2024-01-28 01:47:06 -06:00
committed by GitHub
parent 177132fa2a
commit 53ebb6679e
5 changed files with 18 additions and 22 deletions

View File

@@ -46,11 +46,12 @@ public class RapidReactCommandBot {
*/
public void configureBindings() {
// Automatically run the storage motor whenever the ball storage is not full,
// and turn it off whenever it fills.
new Trigger(m_storage::isFull).whileFalse(m_storage.runCommand());
// and turn it off whenever it fills. Uses subsystem-hosted trigger to
// improve readability and make inter-subsystem communication easier.
m_storage.hasCargo.whileFalse(m_storage.runCommand());
// Automatically disable and retract the intake whenever the ball storage is full.
new Trigger(m_storage::isFull).onTrue(m_intake.retractCommand());
m_storage.hasCargo.onTrue(m_intake.retractCommand());
// Control the drive with split-stick arcade controls
m_drive.setDefaultCommand(

View File

@@ -4,28 +4,29 @@
package edu.wpi.first.wpilibj.examples.rapidreactcommandbot.subsystems;
import static edu.wpi.first.wpilibj.examples.rapidreactcommandbot.Constants.StorageConstants;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.examples.rapidreactcommandbot.Constants.StorageConstants;
import edu.wpi.first.wpilibj.motorcontrol.PWMSparkMax;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import edu.wpi.first.wpilibj2.command.button.Trigger;
public class Storage extends SubsystemBase {
private final PWMSparkMax m_motor = new PWMSparkMax(StorageConstants.kMotorPort);
private final DigitalInput m_ballSensor = new DigitalInput(StorageConstants.kBallSensorPort);
// Expose trigger from subsystem to improve readability and ease
// inter-subsystem communications
/** Whether the ball storage is full. */
@SuppressWarnings("checkstyle:MemberName")
public final Trigger hasCargo = new Trigger(m_ballSensor::get);
/** Create a new Storage subsystem. */
public Storage() {
// Set default command to turn off the storage motor and then idle
setDefaultCommand(runOnce(m_motor::disable).andThen(run(() -> {})).withName("Idle"));
}
/** Whether the ball storage is full. */
public boolean isFull() {
return m_ballSensor.get();
}
/** Returns a command that runs the storage motor indefinitely. */
public Command runCommand() {
return run(() -> m_motor.set(1)).withName("run");