diff --git a/wpilibcExamples/src/main/cpp/examples/RapidReactCommandBot/cpp/RapidReactCommandBot.cpp b/wpilibcExamples/src/main/cpp/examples/RapidReactCommandBot/cpp/RapidReactCommandBot.cpp index 1b46affa21..653aad3ce5 100644 --- a/wpilibcExamples/src/main/cpp/examples/RapidReactCommandBot/cpp/RapidReactCommandBot.cpp +++ b/wpilibcExamples/src/main/cpp/examples/RapidReactCommandBot/cpp/RapidReactCommandBot.cpp @@ -12,16 +12,13 @@ void RapidReactCommandBot::ConfigureBindings() { // Automatically run the storage motor whenever the ball storage is not full, - // and turn it off whenever it fills. - frc2::Trigger([this] { - return 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. - frc2::Trigger([this] { - return 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(m_drive.ArcadeDriveCommand( diff --git a/wpilibcExamples/src/main/cpp/examples/RapidReactCommandBot/cpp/subsystems/Storage.cpp b/wpilibcExamples/src/main/cpp/examples/RapidReactCommandBot/cpp/subsystems/Storage.cpp index 3e9dc6203b..59c2a02351 100644 --- a/wpilibcExamples/src/main/cpp/examples/RapidReactCommandBot/cpp/subsystems/Storage.cpp +++ b/wpilibcExamples/src/main/cpp/examples/RapidReactCommandBot/cpp/subsystems/Storage.cpp @@ -12,7 +12,3 @@ Storage::Storage() { frc2::CommandPtr Storage::RunCommand() { return Run([this] { m_motor.Set(1.0); }).WithName("Run"); } - -bool Storage::IsFull() const { - return m_ballSensor.Get(); -} diff --git a/wpilibcExamples/src/main/cpp/examples/RapidReactCommandBot/include/subsystems/Storage.h b/wpilibcExamples/src/main/cpp/examples/RapidReactCommandBot/include/subsystems/Storage.h index 58694b3035..ec99118d60 100644 --- a/wpilibcExamples/src/main/cpp/examples/RapidReactCommandBot/include/subsystems/Storage.h +++ b/wpilibcExamples/src/main/cpp/examples/RapidReactCommandBot/include/subsystems/Storage.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "Constants.h" @@ -19,7 +20,7 @@ class Storage : frc2::SubsystemBase { frc2::CommandPtr RunCommand(); /** Whether the ball storage is full. */ - bool IsFull() const; + frc2::Trigger HasCargo{[this] { return m_ballSensor.Get(); }}; private: frc::PWMSparkMax m_motor{StorageConstants::kMotorPort}; diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/rapidreactcommandbot/RapidReactCommandBot.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/rapidreactcommandbot/RapidReactCommandBot.java index 12d7f662c6..88b1913aa0 100644 --- a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/rapidreactcommandbot/RapidReactCommandBot.java +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/rapidreactcommandbot/RapidReactCommandBot.java @@ -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( diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/rapidreactcommandbot/subsystems/Storage.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/rapidreactcommandbot/subsystems/Storage.java index dac61a43fc..f3812b1d89 100644 --- a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/rapidreactcommandbot/subsystems/Storage.java +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/rapidreactcommandbot/subsystems/Storage.java @@ -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");