WPILibC++ 2024.3.2
Trigger.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <functional>
8#include <utility>
9
11#include <frc/event/EventLoop.h>
13#include <units/time.h>
14
17
18namespace frc2 {
19class Command;
20/**
21 * This class provides an easy way to link commands to conditions.
22 *
23 * <p>It is very easy to link a button to a command. For instance, you could
24 * link the trigger button of a joystick to a "score" command.
25 *
26 * <p>Triggers can easily be composed for advanced functionality using the
27 * {@link #operator!}, {@link #operator||}, {@link #operator&&} operators.
28 *
29 * <p>This class is provided by the NewCommands VendorDep
30 */
31class Trigger {
32 public:
33 /**
34 * Creates a new trigger based on the given condition.
35 *
36 * <p>Polled by the default scheduler button loop.
37 *
38 * @param condition the condition represented by this trigger
39 */
40 explicit Trigger(std::function<bool()> condition)
41 : Trigger{CommandScheduler::GetInstance().GetDefaultButtonLoop(),
42 std::move(condition)} {}
43
44 /**
45 * Creates a new trigger based on the given condition.
46 *
47 * @param loop The loop instance that polls this trigger.
48 * @param condition the condition represented by this trigger
49 */
50 Trigger(frc::EventLoop* loop, std::function<bool()> condition)
51 : m_loop{loop}, m_condition{std::move(condition)} {}
52
53 /**
54 * Create a new trigger that is always `false`.
55 */
56 Trigger() : Trigger([] { return false; }) {}
57
58 Trigger(const Trigger& other);
59
60 /**
61 * Starts the given command whenever the condition changes from `false` to
62 * `true`.
63 *
64 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
65 * lifespan of the command.
66 *
67 * @param command the command to start
68 * @return this trigger, so calls can be chained
69 */
71
72 /**
73 * Starts the given command whenever the condition changes from `false` to
74 * `true`. Moves command ownership to the button scheduler.
75 *
76 * @param command The command to bind.
77 * @return The trigger, for chained calls.
78 */
80
81 /**
82 * Starts the given command whenever the condition changes from `true` to
83 * `false`.
84 *
85 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
86 * lifespan of the command.
87 *
88 * @param command the command to start
89 * @return this trigger, so calls can be chained
90 */
92
93 /**
94 * Starts the given command whenever the condition changes from `true` to
95 * `false`.
96 *
97 * @param command The command to bind.
98 * @return The trigger, for chained calls.
99 */
101
102 /**
103 * Starts the given command when the condition changes to `true` and cancels
104 * it when the condition changes to `false`.
105 *
106 * <p>Doesn't re-start the command if it ends while the condition is still
107 * `true`. If the command should restart, see RepeatCommand.
108 *
109 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
110 * lifespan of the command.
111 *
112 * @param command the command to start
113 * @return this trigger, so calls can be chained
114 */
116
117 /**
118 * Starts the given command when the condition changes to `true` and cancels
119 * it when the condition changes to `false`. Moves command ownership to the
120 * button scheduler.
121 *
122 * <p>Doesn't re-start the command if it ends while the condition is still
123 * `true`. If the command should restart, see RepeatCommand.
124 *
125 * @param command The command to bind.
126 * @return The trigger, for chained calls.
127 */
129
130 /**
131 * Starts the given command when the condition changes to `false` and cancels
132 * it when the condition changes to `true`.
133 *
134 * <p>Doesn't re-start the command if it ends while the condition is still
135 * `true`. If the command should restart, see RepeatCommand.
136 *
137 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
138 * lifespan of the command.
139 *
140 * @param command the command to start
141 * @return this trigger, so calls can be chained
142 */
144
145 /**
146 * Starts the given command when the condition changes to `false` and cancels
147 * it when the condition changes to `true`. Moves command ownership to the
148 * button scheduler.
149 *
150 * <p>Doesn't re-start the command if it ends while the condition is still
151 * `false`. If the command should restart, see RepeatCommand.
152 *
153 * @param command The command to bind.
154 * @return The trigger, for chained calls.
155 */
157
158 /**
159 * Toggles a command when the condition changes from `false` to `true`.
160 *
161 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
162 * lifespan of the command.
163 *
164 * @param command the command to toggle
165 * @return this trigger, so calls can be chained
166 */
168
169 /**
170 * Toggles a command when the condition changes from `false` to `true`.
171 *
172 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
173 * lifespan of the command.
174 *
175 * @param command the command to toggle
176 * @return this trigger, so calls can be chained
177 */
179
180 /**
181 * Toggles a command when the condition changes from `true` to the low
182 * state.
183 *
184 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
185 * lifespan of the command.
186 *
187 * @param command the command to toggle
188 * @return this trigger, so calls can be chained
189 */
191
192 /**
193 * Toggles a command when the condition changes from `true` to `false`.
194 *
195 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
196 * lifespan of the command.
197 *
198 * @param command the command to toggle
199 * @return this trigger, so calls can be chained
200 */
202
203 /**
204 * Composes two triggers with logical AND.
205 *
206 * @return A trigger which is active when both component triggers are active.
207 */
208 Trigger operator&&(std::function<bool()> rhs) {
209 return Trigger(m_loop, [condition = m_condition, rhs = std::move(rhs)] {
210 return condition() && rhs();
211 });
212 }
213
214 /**
215 * Composes two triggers with logical AND.
216 *
217 * @return A trigger which is active when both component triggers are active.
218 */
220 return Trigger(m_loop, [condition = m_condition, rhs] {
221 return condition() && rhs.m_condition();
222 });
223 }
224
225 /**
226 * Composes two triggers with logical OR.
227 *
228 * @return A trigger which is active when either component trigger is active.
229 */
230 Trigger operator||(std::function<bool()> rhs) {
231 return Trigger(m_loop, [condition = m_condition, rhs = std::move(rhs)] {
232 return condition() || rhs();
233 });
234 }
235
236 /**
237 * Composes two triggers with logical OR.
238 *
239 * @return A trigger which is active when either component trigger is active.
240 */
242 return Trigger(m_loop, [condition = m_condition, rhs] {
243 return condition() || rhs.m_condition();
244 });
245 }
246
247 /**
248 * Composes a trigger with logical NOT.
249 *
250 * @return A trigger which is active when the component trigger is inactive,
251 * and vice-versa.
252 */
254 return Trigger(m_loop, [condition = m_condition] { return !condition(); });
255 }
256
257 /**
258 * Creates a new debounced trigger from this trigger - it will become active
259 * when this trigger has been active for longer than the specified period.
260 *
261 * @param debounceTime The debounce period.
262 * @param type The debounce type.
263 * @return The debounced trigger.
264 */
265 Trigger Debounce(units::second_t debounceTime,
267 frc::Debouncer::DebounceType::kRising);
268
269 /**
270 * Returns the current state of this trigger.
271 * @return A bool representing the current state of the trigger.
272 */
273 bool Get() const;
274
275 private:
276 frc::EventLoop* m_loop;
277 std::function<bool()> m_condition;
278};
279} // namespace frc2
A state machine representing a complete action to be performed by the robot.
Definition: Command.h:41
A wrapper around std::unique_ptr<Command> so commands have move-only semantics.
Definition: CommandPtr.h:29
The scheduler responsible for running Commands.
Definition: CommandScheduler.h:38
This class provides an easy way to link commands to conditions.
Definition: Trigger.h:31
bool Get() const
Returns the current state of this trigger.
Trigger OnFalse(CommandPtr &&command)
Starts the given command whenever the condition changes from true to false.
Trigger()
Create a new trigger that is always false.
Definition: Trigger.h:56
Trigger operator&&(std::function< bool()> rhs)
Composes two triggers with logical AND.
Definition: Trigger.h:208
Trigger WhileTrue(CommandPtr &&command)
Starts the given command when the condition changes to true and cancels it when the condition changes...
Trigger operator!()
Composes a trigger with logical NOT.
Definition: Trigger.h:253
Trigger WhileFalse(Command *command)
Starts the given command when the condition changes to false and cancels it when the condition change...
Trigger OnFalse(Command *command)
Starts the given command whenever the condition changes from true to false.
Trigger operator&&(Trigger rhs)
Composes two triggers with logical AND.
Definition: Trigger.h:219
Trigger Debounce(units::second_t debounceTime, frc::Debouncer::DebounceType type=frc::Debouncer::DebounceType::kRising)
Creates a new debounced trigger from this trigger - it will become active when this trigger has been ...
Trigger OnTrue(CommandPtr &&command)
Starts the given command whenever the condition changes from false to true.
Trigger OnTrue(Command *command)
Starts the given command whenever the condition changes from false to true.
Trigger WhileFalse(CommandPtr &&command)
Starts the given command when the condition changes to false and cancels it when the condition change...
Trigger(const Trigger &other)
Trigger ToggleOnTrue(Command *command)
Toggles a command when the condition changes from false to true.
Trigger operator||(Trigger rhs)
Composes two triggers with logical OR.
Definition: Trigger.h:241
Trigger operator||(std::function< bool()> rhs)
Composes two triggers with logical OR.
Definition: Trigger.h:230
Trigger ToggleOnFalse(CommandPtr &&command)
Toggles a command when the condition changes from true to false.
Trigger WhileTrue(Command *command)
Starts the given command when the condition changes to true and cancels it when the condition changes...
Trigger ToggleOnTrue(CommandPtr &&command)
Toggles a command when the condition changes from false to true.
Trigger(frc::EventLoop *loop, std::function< bool()> condition)
Creates a new trigger based on the given condition.
Definition: Trigger.h:50
Trigger(std::function< bool()> condition)
Creates a new trigger based on the given condition.
Definition: Trigger.h:40
Trigger ToggleOnFalse(Command *command)
Toggles a command when the condition changes from true to the low state.
DebounceType
Type of debouncing to perform.
Definition: Debouncer.h:23
A declarative way to bind a set of actions to a loop and execute them when the loop is polled.
Definition: EventLoop.h:15
type
Definition: core.h:556
Definition: TrapezoidProfileSubsystem.h:12
Definition: array.h:89