[command] Rename trigger methods (#4210)

Motivation

Feedback from 2022 showed that the Trigger API is rather confusing, mostly due to the following:
- duplicate Trigger and Button APIs were available; users were confused searching for a nonexistent difference between them.
- the when terminology was ambiguous and unclear whether it refers to the high state or specifically the rising edge.
- the Active terminology didn't unambiguously refer to the high state; it wasn't unintuitive to understand it as "when the binding is active/polled".
- whileHeld vs whenHeld was very confusing, and the difference between them wasn't obvious. The parallel Trigger verbs, whileActiveContinuously and whileActiveOnce are much less confusing.

Solution

Deprecating Button and its binding methods. The rationale for deprecating Button (and not Trigger) is because Button uses terminology that is needlessly more specific and restricting to the button use case, making the use case of arbitrary trigger conditions unintuitive.

After consideration, deprecation of Button's subclasses was decided against:

- NetworkButton (a trigger condition based on a boolean NT entry/topic) is a use case that is not necessarily intuitive for teams to implement themselves, so it is an abstraction that should be provided in the library. A parallel class for the BooleanEvent level, NetworkBooleanEvent, was also added as part of NT4. NT listeners were considered as a alternative solution, but they require attention to thread safety, and aren't interoperable with the EventLoop API.
- JoystickButton/POVButton provide abstractions around HID buttons. The new Trigger-returning factories on the HID classes are an equal (if not more concise) alternative, but there is no reason not to keep them for those who find their use preferable.

At a later date in the deprecation cycle (perhaps for 2024), when Button is removed, these subclasses should be changed to inherit directly from Trigger.

Trigger's bindings are changed to use True/False terminology, as it should be unambiguous. Each binding type has both True and False variants; for brevity, only the True variants are listed here:

- onTrue (replaces whenActive): schedule on rising edge.
- whileTrue (replaces whileActiveOnce): schedule on rising edge, cancel on falling edge.
- toggleOnTrue (replaces toggleWhenActive): on rising edge, schedule if unscheduled and cancel if scheduled.

Two binding types are completely deprecated:

- cancelWhenActive: this is a fairly niche use case which is better described as having the trigger's rising edge (Trigger.rising()) as an end condition for the command (using Command.until()).
- whileActiveContinuously: however common, this relied on the no-op behavior of scheduling an already-scheduled command. The more correct way to repeat the command if it ends before the falling edge is using Command.repeatedly/RepeatCommand or a RunCommand -- the only difference is if the command is interrupted, but that is more likely to result in two commands perpetually canceling each other than achieve the desired behavior. Manually implementing a blindly-scheduling binding like whileActiveContinuously is still possible, though might not be intuitive.

Notes

It was considered to share BooleanEvent's digital signal terminology; however, once it was decided that Trigger should not inherit from BooleanEvent (due to overload incompatibility) the common terminology was not worth the unintuitiveness stemming from users' unfamiliarity with the signal processing terms.
This commit is contained in:
Starlight220
2022-10-28 08:03:28 +03:00
committed by GitHub
parent 66157397c1
commit dcda09f90a
50 changed files with 1090 additions and 645 deletions

View File

@@ -9,6 +9,8 @@
#include <span>
#include <utility>
#include <wpi/deprecated.h>
#include "Trigger.h"
#include "frc2/command/CommandPtr.h"
@@ -28,13 +30,17 @@ class Button : public Trigger {
* Create a new button that is pressed when the given condition is true.
*
* @param isPressed Whether the button is pressed.
* @deprecated Replace with Trigger
*/
WPI_DEPRECATED("Replace with Trigger")
explicit Button(std::function<bool()> isPressed);
/**
* Create a new button that is pressed active (default constructor) - activity
* can be further determined by subclass code.
* @deprecated Replace with Trigger
*/
WPI_DEPRECATED("Replace with Trigger")
Button() = default;
/**
@@ -44,19 +50,11 @@ class Button : public Trigger {
*
* @param command The command to bind.
* @return The trigger, for chained calls.
* @deprecated Replace with Trigger::OnTrue()
*/
WPI_DEPRECATED("Replace with Trigger#OnTrue()")
Button WhenPressed(Command* command);
/**
* Binds a command to start when the button is pressed. Transfers
* command ownership to the button scheduler, so the user does not have to
* worry about lifespan.
*
* @param command The command to bind.
* @return The trigger, for chained calls.
*/
Button WhenPressed(CommandPtr&& command);
/**
* Binds a command to start when the button is pressed. Transfers
* command ownership to the button scheduler, so the user does not have to
@@ -65,9 +63,11 @@ class Button : public Trigger {
*
* @param command The command to bind.
* @return The trigger, for chained calls.
* @deprecated Replace with Trigger::OnTrue()
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
WPI_DEPRECATED("Replace with Trigger#OnTrue()")
Button WhenPressed(T&& command) {
WhenActive(std::forward<T>(command));
return *this;
@@ -78,7 +78,9 @@ class Button : public Trigger {
*
* @param toRun the runnable to execute.
* @param requirements the required subsystems.
* @deprecated Replace with Trigger::OnTrue(cmd::RunOnce())
*/
WPI_DEPRECATED("Replace with Trigger#OnTrue(cmd::RunOnce())")
Button WhenPressed(std::function<void()> toRun,
std::initializer_list<Subsystem*> requirements);
@@ -87,7 +89,9 @@ class Button : public Trigger {
*
* @param toRun the runnable to execute.
* @param requirements the required subsystems.
* @deprecated Replace with Trigger::OnTrue(cmd::RunOnce())
*/
WPI_DEPRECATED("Replace with Trigger#OnTrue(cmd::RunOnce())")
Button WhenPressed(std::function<void()> toRun,
std::span<Subsystem* const> requirements = {});
@@ -98,19 +102,11 @@ class Button : public Trigger {
*
* @param command The command to bind.
* @return The button, for chained calls.
* @deprecated Replace with Trigger::WhileTrue(command.Repeatedly())
*/
WPI_DEPRECATED("Replace with Trigger#WhileTrue(command.Repeatedly())")
Button WhileHeld(Command* command);
/**
* Binds a command to be started repeatedly while the button is pressed, and
* canceled when it is released. Transfers command ownership to the button
* scheduler, so the user does not have to worry about lifespan.
*
* @param command The command to bind.
* @return The button, for chained calls.
*/
Button WhileHeld(CommandPtr&& command);
/**
* Binds a command to be started repeatedly while the button is pressed, and
* canceled when it is released. Transfers command ownership to the button
@@ -119,9 +115,11 @@ class Button : public Trigger {
*
* @param command The command to bind.
* @return The button, for chained calls.
* @deprecated Replace with Trigger::WhileTrue(command.Repeatedly())
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
WPI_DEPRECATED("Replace with Trigger#WhileTrue(command.Repeatedly())")
Button WhileHeld(T&& command) {
WhileActiveContinous(std::forward<T>(command));
return *this;
@@ -132,7 +130,9 @@ class Button : public Trigger {
*
* @param toRun the runnable to execute.
* @param requirements the required subsystems.
* @deprecated Replace with Trigger::WhileTrue(cmd::Run())
*/
WPI_DEPRECATED("Replace with Trigger#WhileTrue(cmd::Run())")
Button WhileHeld(std::function<void()> toRun,
std::initializer_list<Subsystem*> requirements);
@@ -141,7 +141,9 @@ class Button : public Trigger {
*
* @param toRun the runnable to execute.
* @param requirements the required subsystems.
* @deprecated Replace with Trigger::WhileTrue(cmd::Run())
*/
WPI_DEPRECATED("Replace with Trigger#WhileTrue(cmd::Run())")
Button WhileHeld(std::function<void()> toRun,
std::span<Subsystem* const> requirements = {});
@@ -152,19 +154,11 @@ class Button : public Trigger {
*
* @param command The command to bind.
* @return The button, for chained calls.
* @deprecated Replace with Trigger::WhileTrue()
*/
WPI_DEPRECATED("Replace with Trigger#WhileTrue()")
Button WhenHeld(Command* command);
/**
* Binds a command to be started when the button is pressed, and canceled
* when it is released. Transfers command ownership to the button scheduler,
* so the user does not have to worry about lifespan.
*
* @param command The command to bind.
* @return The button, for chained calls.
*/
Button WhenHeld(CommandPtr&& command);
/**
* Binds a command to be started when the button is pressed, and canceled
* when it is released. Transfers command ownership to the button scheduler,
@@ -173,9 +167,11 @@ class Button : public Trigger {
*
* @param command The command to bind.
* @return The button, for chained calls.
* @deprecated Replace with Trigger::WhileTrue()
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
WPI_DEPRECATED("Replace with Trigger#WhileTrue()")
Button WhenHeld(T&& command) {
WhileActiveOnce(std::forward<T>(command));
return *this;
@@ -188,19 +184,11 @@ class Button : public Trigger {
*
* @param command The command to bind.
* @return The button, for chained calls.
* @deprecated Replace with Trigger::OnFalse()
*/
WPI_DEPRECATED("Replace with Trigger#OnFalse()")
Button WhenReleased(Command* command);
/**
* Binds a command to start when the button is pressed. Transfers
* command ownership to the button scheduler, so the user does not have to
* worry about lifespan.
*
* @param command The command to bind.
* @return The button, for chained calls.
*/
Button WhenReleased(CommandPtr&& command);
/**
* Binds a command to start when the button is pressed. Transfers
* command ownership to the button scheduler, so the user does not have to
@@ -209,9 +197,11 @@ class Button : public Trigger {
*
* @param command The command to bind.
* @return The button, for chained calls.
* @deprecated Replace with Trigger::OnFalse()
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
WPI_DEPRECATED("Replace with Trigger#OnFalse()")
Button WhenReleased(T&& command) {
WhenInactive(std::forward<T>(command));
return *this;
@@ -222,7 +212,9 @@ class Button : public Trigger {
*
* @param toRun the runnable to execute.
* @param requirements the required subsystems.
* @deprecated Replace with Trigger::OnFalse(cmd::RunOnce())
*/
WPI_DEPRECATED("Replace with Trigger#OnFalse(cmd::RunOnce())")
Button WhenReleased(std::function<void()> toRun,
std::initializer_list<Subsystem*> requirements);
@@ -231,7 +223,9 @@ class Button : public Trigger {
*
* @param toRun the runnable to execute.
* @param requirements the required subsystems.
* @deprecated Replace with Trigger::OnFalse(cmd::RunOnce())
*/
WPI_DEPRECATED("Replace with Trigger#OnFalse(cmd::RunOnce())")
Button WhenReleased(std::function<void()> toRun,
std::span<Subsystem* const> requirements = {});
@@ -242,19 +236,11 @@ class Button : public Trigger {
*
* @param command The command to bind.
* @return The button, for chained calls.
* @deprecated Replace with Trigger::ToggleOnTrue()
*/
WPI_DEPRECATED("Replace with Trigger#ToggleOnTrue()")
Button ToggleWhenPressed(Command* command);
/**
* Binds a command to start when the button is pressed, and be canceled when
* it is pessed again. Transfers command ownership to the button scheduler,
* so the user does not have to worry about lifespan.
*
* @param command The command to bind.
* @return The button, for chained calls.
*/
Button ToggleWhenPressed(CommandPtr&& command);
/**
* Binds a command to start when the button is pressed, and be canceled when
* it is pessed again. Transfers command ownership to the button scheduler,
@@ -263,9 +249,11 @@ class Button : public Trigger {
*
* @param command The command to bind.
* @return The button, for chained calls.
* @deprecated Replace with Trigger::ToggleOnTrue()
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
WPI_DEPRECATED("Replace with Trigger#ToggleOnTrue()")
Button ToggleWhenPressed(T&& command) {
ToggleWhenActive(std::forward<T>(command));
return *this;
@@ -278,7 +266,10 @@ class Button : public Trigger {
*
* @param command The command to bind.
* @return The button, for chained calls.
* @deprecated Use Rising() as a command end condition with Until() instead.
*/
WPI_DEPRECATED(
"Use Rising() as a command end condition with Until() instead.")
Button CancelWhenPressed(Command* command);
};
} // namespace frc2

View File

@@ -4,6 +4,7 @@
#pragma once
#include <frc/GenericHID.h>
#include <wpi/deprecated.h>
#include "Button.h"
@@ -24,9 +25,11 @@ class JoystickButton : public Button {
* @param joystick The joystick on which the button is located.
* @param buttonNumber The number of the button on the joystic.
*/
WPI_IGNORE_DEPRECATED
explicit JoystickButton(frc::GenericHID* joystick, int buttonNumber)
: Button([joystick, buttonNumber] {
return joystick->GetRawButton(buttonNumber);
}) {}
WPI_UNIGNORE_DEPRECATED
};
} // namespace frc2

View File

@@ -4,6 +4,7 @@
#pragma once
#include <frc/GenericHID.h>
#include <wpi/deprecated.h>
#include "Button.h"
@@ -25,9 +26,11 @@ class POVButton : public Button {
* @param angle The angle of the POV corresponding to a button press.
* @param povNumber The number of the POV on the joystick.
*/
WPI_IGNORE_DEPRECATED
POVButton(frc::GenericHID* joystick, int angle, int povNumber = 0)
: Button([joystick, angle, povNumber] {
return joystick->GetPOV(povNumber) == angle;
}) {}
WPI_UNIGNORE_DEPRECATED
};
} // namespace frc2

View File

@@ -14,6 +14,7 @@
#include <frc/event/EventLoop.h>
#include <frc/filter/Debouncer.h>
#include <units/time.h>
#include <wpi/deprecated.h>
#include "frc2/command/Command.h"
#include "frc2/command/CommandScheduler.h"
@@ -21,14 +22,14 @@
namespace frc2 {
class Command;
/**
* This class is a command-based wrapper around {@link BooleanEvent}, providing
* an easy way to link commands to inputs.
* This class is a command-based wrapper around {@link frc::BooleanEvent},
* providing an easy way to link commands to inputs.
*
* This class is provided by the NewCommands VendorDep
*
* @see Button
*/
class Trigger : public frc::BooleanEvent {
class Trigger {
public:
/**
* Creates a new trigger with the given condition determining whether it is
@@ -39,8 +40,8 @@ class Trigger : public frc::BooleanEvent {
* @param isActive returns whether or not the trigger should be active
*/
explicit Trigger(std::function<bool()> isActive)
: BooleanEvent{CommandScheduler::GetInstance().GetDefaultButtonLoop(),
std::move(isActive)} {}
: m_event{CommandScheduler::GetInstance().GetDefaultButtonLoop(),
std::move(isActive)} {}
/**
* Create a new trigger that is active when the given condition is true.
@@ -49,7 +50,7 @@ class Trigger : public frc::BooleanEvent {
* @param isActive Whether the trigger is active.
*/
Trigger(frc::EventLoop* loop, std::function<bool()> isActive)
: BooleanEvent{loop, std::move(isActive)} {}
: m_event{loop, std::move(isActive)} {}
/**
* Create a new trigger that is never active (default constructor) - activity
@@ -59,6 +60,143 @@ class Trigger : public frc::BooleanEvent {
Trigger(const Trigger& other);
/**
* Starts the given command whenever the signal rises from `false` to `true`.
*
* <p>Takes a raw pointer, and so is non-owning; users are responsible for the
* lifespan of the command.
*
* @param command the command to start
* @return this trigger, so calls can be chained
* @see #Rising()
*/
Trigger OnTrue(Command* command);
/**
* Starts the given command whenever the signal rises from `false` to `true`.
* Moves command ownership to the button scheduler.
*
* @param command The command to bind.
* @return The trigger, for chained calls.
*/
Trigger OnTrue(CommandPtr&& command);
/**
* Starts the given command whenever the signal falls from `true` to `false`.
*
* <p>Takes a raw pointer, and so is non-owning; users are responsible for the
* lifespan of the command.
*
* @param command the command to start
* @return this trigger, so calls can be chained
* @see #Falling()
*/
Trigger OnFalse(Command* command);
/**
* Starts the given command whenever the signal falls from `true` to `false`.
*
* @param command The command to bind.
* @return The trigger, for chained calls.
*/
Trigger OnFalse(CommandPtr&& command);
/**
* Starts the given command when the signal rises to `true` and cancels it
* when the signal falls to `false`.
*
* <p>Doesn't re-start the command in-between.
*
* <p>Takes a raw pointer, and so is non-owning; users are responsible for the
* lifespan of the command.
*
* @param command the command to start
* @return this trigger, so calls can be chained
*/
Trigger WhileTrue(Command* command);
/**
* Starts the given command when the signal rises to `true` and cancels it
* when the signal falls to `false`. Moves command ownership to the button
* scheduler.
*
* @param command The command to bind.
* @return The trigger, for chained calls.
*/
Trigger WhileTrue(CommandPtr&& command);
/**
* Starts the given command when the signal falls to `false` and cancels
* it when the signal rises.
*
* <p>Doesn't re-start the command in-between.
*
* <p>Takes a raw pointer, and so is non-owning; users are responsible for the
* lifespan of the command.
*
* @param command the command to start
* @return this trigger, so calls can be chained
*/
Trigger WhileFalse(Command* command);
/**
* Starts the given command when the signal falls to `false` and cancels
* it when the signal rises. Moves command ownership to the button
* scheduler.
*
* @param command The command to bind.
* @return The trigger, for chained calls.
*/
Trigger WhileFalse(CommandPtr&& command);
/**
* Toggles a command when the signal rises from `false` to the high
* state.
*
* <p>Takes a raw pointer, and so is non-owning; users are responsible for the
* lifespan of the command.
*
* @param command the command to toggle
* @return this trigger, so calls can be chained
*/
Trigger ToggleOnTrue(Command* command);
/**
* Toggles a command when the signal rises from `false` to the high
* state.
*
* <p>Takes a raw pointer, and so is non-owning; users are responsible for the
* lifespan of the command.
*
* @param command the command to toggle
* @return this trigger, so calls can be chained
*/
Trigger ToggleOnTrue(CommandPtr&& command);
/**
* Toggles a command when the signal falls from `true` to the low
* state.
*
* <p>Takes a raw pointer, and so is non-owning; users are responsible for the
* lifespan of the command.
*
* @param command the command to toggle
* @return this trigger, so calls can be chained
*/
Trigger ToggleOnFalse(Command* command);
/**
* Toggles a command when the signal falls from `true` to the low
* state.
*
* <p>Takes a raw pointer, and so is non-owning; users are responsible for the
* lifespan of the command.
*
* @param command the command to toggle
* @return this trigger, so calls can be chained
*/
Trigger ToggleOnFalse(CommandPtr&& command);
/**
* Binds a command to start when the trigger becomes active. Takes a
* raw pointer, and so is non-owning; users are responsible for the lifespan
@@ -66,18 +204,11 @@ class Trigger : public frc::BooleanEvent {
*
* @param command The command to bind.
* @return The trigger, for chained calls.
* @deprecated Use OnTrue(Command) instead
*/
WPI_DEPRECATED("Use OnTrue(Command) instead")
Trigger WhenActive(Command* command);
/**
* Binds a command to start when the trigger becomes active. Moves
* command ownership to the button scheduler.
*
* @param command The command to bind.
* @return The trigger, for chained calls.
*/
Trigger WhenActive(CommandPtr&& command);
/**
* Binds a command to start when the trigger becomes active. Transfers
* command ownership to the button scheduler, so the user does not have to
@@ -86,11 +217,13 @@ class Trigger : public frc::BooleanEvent {
*
* @param command The command to bind.
* @return The trigger, for chained calls.
* @deprecated Use OnTrue(Command) instead
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
WPI_DEPRECATED("Use OnTrue(Command) instead")
Trigger WhenActive(T&& command) {
this->Rising().IfHigh(
m_event.Rising().IfHigh(
[command = std::make_unique<std::remove_reference_t<T>>(
std::forward<T>(command))] { command->Schedule(); });
@@ -102,7 +235,11 @@ class Trigger : public frc::BooleanEvent {
*
* @param toRun the runnable to execute.
* @param requirements the required subsystems.
* @deprecated Use OnTrue(Command) instead and construct the InstantCommand
* manually
*/
WPI_DEPRECATED(
"Use OnTrue(Command) instead and construct the InstantCommand manually")
Trigger WhenActive(std::function<void()> toRun,
std::initializer_list<Subsystem*> requirements);
@@ -111,7 +248,11 @@ class Trigger : public frc::BooleanEvent {
*
* @param toRun the runnable to execute.
* @param requirements the required subsystems.
* @deprecated Use OnTrue(Command) instead and construct the InstantCommand
* manually
*/
WPI_DEPRECATED(
"Use OnTrue(Command) instead and construct the InstantCommand manually")
Trigger WhenActive(std::function<void()> toRun,
std::span<Subsystem* const> requirements = {});
@@ -122,19 +263,14 @@ class Trigger : public frc::BooleanEvent {
*
* @param command The command to bind.
* @return The trigger, for chained calls.
* @deprecated Use WhileTrue(Command) with RepeatCommand, or bind
command::Schedule with IfHigh(std::function<void()>).
*/
WPI_DEPRECATED(
"Use WhileTrue(Command) with RepeatCommand, or bind command::Schedule "
"with IfHigh(std::function<void()>).")
Trigger WhileActiveContinous(Command* command);
/**
* Binds a command to be started repeatedly while the trigger is active, and
* canceled when it becomes inactive. Moves command ownership to the button
* scheduler.
*
* @param command The command to bind.
* @return The trigger, for chained calls.
*/
Trigger WhileActiveContinous(CommandPtr&& command);
/**
* Binds a command to be started repeatedly while the trigger is active, and
* canceled when it becomes inactive. Transfers command ownership to the
@@ -143,14 +279,19 @@ class Trigger : public frc::BooleanEvent {
*
* @param command The command to bind.
* @return The trigger, for chained calls.
* @deprecated Use WhileTrue(Command) with RepeatCommand, or bind
command::Schedule with IfHigh(std::function<void()>).
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
WPI_DEPRECATED(
"Use WhileTrue(Command) with RepeatCommand, or bind command::Schedule "
"with IfHigh(std::function<void()>).")
Trigger WhileActiveContinous(T&& command) {
std::shared_ptr<T> ptr =
std::make_shared<std::remove_reference_t<T>>(std::forward<T>(command));
this->IfHigh([ptr] { ptr->Schedule(); });
this->Falling().IfHigh([ptr] { ptr->Cancel(); });
m_event.IfHigh([ptr] { ptr->Schedule(); });
m_event.Falling().IfHigh([ptr] { ptr->Cancel(); });
return *this;
}
@@ -160,7 +301,9 @@ class Trigger : public frc::BooleanEvent {
*
* @param toRun the runnable to execute.
* @param requirements the required subsystems.
* @deprecated Use WhileTrue(Command) and construct a RunCommand manually
*/
WPI_DEPRECATED("Use WhileTrue(Command) and construct a RunCommand manually")
Trigger WhileActiveContinous(std::function<void()> toRun,
std::initializer_list<Subsystem*> requirements);
@@ -169,7 +312,9 @@ class Trigger : public frc::BooleanEvent {
*
* @param toRun the runnable to execute.
* @param requirements the required subsystems.
* @deprecated Use WhileTrue(Command) and construct a RunCommand manually
*/
WPI_DEPRECATED("Use WhileTrue(Command) and construct a RunCommand manually")
Trigger WhileActiveContinous(std::function<void()> toRun,
std::span<Subsystem* const> requirements = {});
@@ -180,19 +325,11 @@ class Trigger : public frc::BooleanEvent {
*
* @param command The command to bind.
* @return The trigger, for chained calls.
* @deprecated Use WhileTrue(Command) instead.
*/
WPI_DEPRECATED("Use WhileTrue(Command) instead.")
Trigger WhileActiveOnce(Command* command);
/**
* Binds a command to be started when the trigger becomes active, and
* canceled when it becomes inactive. Moves command ownership to the button
* scheduler.
*
* @param command The command to bind.
* @return The trigger, for chained calls.
*/
Trigger WhileActiveOnce(CommandPtr&& command);
/**
* Binds a command to be started when the trigger becomes active, and
* canceled when it becomes inactive. Transfers command ownership to the
@@ -201,15 +338,17 @@ class Trigger : public frc::BooleanEvent {
*
* @param command The command to bind.
* @return The trigger, for chained calls.
* @deprecated Use WhileTrue(Command) instead.
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
WPI_DEPRECATED("Use WhileTrue(Command) instead.")
Trigger WhileActiveOnce(T&& command) {
std::shared_ptr<T> ptr =
std::make_shared<std::remove_reference_t<T>>(std::forward<T>(command));
this->Rising().IfHigh([ptr] { ptr->Schedule(); });
this->Falling().IfHigh([ptr] { ptr->Cancel(); });
m_event.Rising().IfHigh([ptr] { ptr->Schedule(); });
m_event.Falling().IfHigh([ptr] { ptr->Cancel(); });
return *this;
}
@@ -221,18 +360,11 @@ class Trigger : public frc::BooleanEvent {
*
* @param command The command to bind.
* @return The trigger, for chained calls.
* @deprecated Use OnFalse(Command) instead.
*/
WPI_DEPRECATED("Use OnFalse(Command) instead.")
Trigger WhenInactive(Command* command);
/**
* Binds a command to start when the trigger becomes inactive. Moves
* command ownership to the button scheduler.
*
* @param command The command to bind.
* @return The trigger, for chained calls.
*/
Trigger WhenInactive(CommandPtr&& command);
/**
* Binds a command to start when the trigger becomes inactive. Transfers
* command ownership to the button scheduler, so the user does not have to
@@ -241,11 +373,13 @@ class Trigger : public frc::BooleanEvent {
*
* @param command The command to bind.
* @return The trigger, for chained calls.
* @deprecated Use OnFalse(Command) instead.
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
WPI_DEPRECATED("Use OnFalse(Command) instead.")
Trigger WhenInactive(T&& command) {
this->Falling().IfHigh(
m_event.Falling().IfHigh(
[command = std::make_unique<std::remove_reference_t<T>>(
std::forward<T>(command))] { command->Schedule(); });
@@ -257,7 +391,11 @@ class Trigger : public frc::BooleanEvent {
*
* @param toRun the runnable to execute.
* @param requirements the required subsystems.
* @deprecated Use OnFalse(Command) instead and construct the InstantCommand
* manually
*/
WPI_DEPRECATED(
"Use OnFalse(Command) instead and construct the InstantCommand manually")
Trigger WhenInactive(std::function<void()> toRun,
std::initializer_list<Subsystem*> requirements);
@@ -266,7 +404,11 @@ class Trigger : public frc::BooleanEvent {
*
* @param toRun the runnable to execute.
* @param requirements the required subsystems.
* @deprecated Use OnFalse(Command) instead and construct the InstantCommand
* manually
*/
WPI_DEPRECATED(
"Use OnFalse(Command) instead and construct the InstantCommand manually")
Trigger WhenInactive(std::function<void()> toRun,
std::span<Subsystem* const> requirements = {});
@@ -277,19 +419,11 @@ class Trigger : public frc::BooleanEvent {
*
* @param command The command to bind.
* @return The trigger, for chained calls.
* @deprecated Use ToggleOnTrue(Command) instead.
*/
WPI_DEPRECATED("Use ToggleOnTrue(Command) instead.")
Trigger ToggleWhenActive(Command* command);
/**
* Binds a command to start when the trigger becomes active, and be canceled
* when it again becomes active. Moves command ownership to the button
* scheduler.
*
* @param command The command to bind.
* @return The trigger, for chained calls.
*/
Trigger ToggleWhenActive(CommandPtr&& command);
/**
* Binds a command to start when the trigger becomes active, and be canceled
* when it again becomes active. Transfers command ownership to the button
@@ -298,11 +432,13 @@ class Trigger : public frc::BooleanEvent {
*
* @param command The command to bind.
* @return The trigger, for chained calls.
* @deprecated Use ToggleOnTrue(Command) instead.
*/
template <class T, typename = std::enable_if_t<std::is_base_of_v<
Command, std::remove_reference_t<T>>>>
WPI_DEPRECATED("Use ToggleOnTrue(Command) instead.")
Trigger ToggleWhenActive(T&& command) {
this->Rising().IfHigh(
m_event.Rising().IfHigh(
[command = std::make_unique<std::remove_reference_t<T>>(
std::forward<T>(command))] {
if (!command->IsScheduled()) {
@@ -322,7 +458,10 @@ class Trigger : public frc::BooleanEvent {
*
* @param command The command to bind.
* @return The trigger, for chained calls.
* @deprecated Use Rising() as a command end condition with Until() instead.
*/
WPI_DEPRECATED(
"Use Rising() as a command end condition with Until() instead.")
Trigger CancelWhenActive(Command* command);
/**
@@ -330,14 +469,14 @@ class Trigger : public frc::BooleanEvent {
*
* @return a new event representing when this one newly changes to true.
*/
Trigger Rising() { return BooleanEvent::Rising().CastTo<Trigger>(); }
Trigger Rising() { return m_event.Rising().CastTo<Trigger>(); }
/**
* Get a new event that triggers only when this one newly changes to false.
*
* @return a new event representing when this one newly changes to false.
*/
Trigger Falling() { return BooleanEvent::Falling().CastTo<Trigger>(); }
Trigger Falling() { return m_event.Falling().CastTo<Trigger>(); }
/**
* Composes two triggers with logical AND.
@@ -345,7 +484,7 @@ class Trigger : public frc::BooleanEvent {
* @return A trigger which is active when both component triggers are active.
*/
Trigger operator&&(std::function<bool()> rhs) {
return BooleanEvent::operator&&(rhs).CastTo<Trigger>();
return m_event.operator&&(rhs).CastTo<Trigger>();
}
/**
@@ -354,7 +493,7 @@ class Trigger : public frc::BooleanEvent {
* @return A trigger which is active when either component trigger is active.
*/
Trigger operator||(std::function<bool()> rhs) {
return BooleanEvent::operator||(rhs).CastTo<Trigger>();
return m_event.operator||(rhs).CastTo<Trigger>();
}
/**
@@ -363,7 +502,7 @@ class Trigger : public frc::BooleanEvent {
* @return A trigger which is active when the component trigger is inactive,
* and vice-versa.
*/
Trigger operator!() { return BooleanEvent::operator!().CastTo<Trigger>(); }
Trigger operator!() { return m_event.operator!().CastTo<Trigger>(); }
/**
* Creates a new debounced trigger from this trigger - it will become active
@@ -376,7 +515,15 @@ class Trigger : public frc::BooleanEvent {
Trigger Debounce(units::second_t debounceTime,
frc::Debouncer::DebounceType type =
frc::Debouncer::DebounceType::kRising) {
return BooleanEvent::Debounce(debounceTime, type).CastTo<Trigger>();
return m_event.Debounce(debounceTime, type).CastTo<Trigger>();
}
/**
* Get the wrapped BooleanEvent instance.
*/
frc::BooleanEvent GetEvent() const;
private:
frc::BooleanEvent m_event;
};
} // namespace frc2