WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
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#include <wpi/FunctionExtras.h>
15
18
19namespace frc2 {
20class Command;
21/**
22 * This class provides an easy way to link commands to conditions.
23 *
24 * <p>It is very easy to link a button to a command. For instance, you could
25 * link the trigger button of a joystick to a "score" command.
26 *
27 * <p>Triggers can easily be composed for advanced functionality using the
28 * {@link #operator!}, {@link #operator||}, {@link #operator&&} operators.
29 *
30 * <p>This class is provided by the NewCommands VendorDep
31 */
32class Trigger {
33 public:
34 /**
35 * Creates a new trigger based on the given condition.
36 *
37 * <p>Polled by the default scheduler button loop.
38 *
39 * @param condition the condition represented by this trigger
40 */
41 explicit Trigger(std::function<bool()> condition)
42 : Trigger{CommandScheduler::GetInstance().GetDefaultButtonLoop(),
43 std::move(condition)} {}
44
45 /**
46 * Creates a new trigger based on the given condition.
47 *
48 * @param loop The loop instance that polls this trigger.
49 * @param condition the condition represented by this trigger
50 */
51 Trigger(frc::EventLoop* loop, std::function<bool()> condition)
52 : m_loop{loop}, m_condition{std::move(condition)} {}
53
54 /**
55 * Create a new trigger that is always `false`.
56 */
57 Trigger() : Trigger([] { return false; }) {}
58
59 Trigger(const Trigger& other);
60
61 /**
62 * Starts the command when the condition changes.
63 *
64 * @param command the command to start
65 * @return this trigger, so calls can be chained
66 */
68
69 /**
70 * Starts the command when the condition changes. Moves command ownership to
71 * the button scheduler.
72 *
73 * @param command the command to start
74 * @return this trigger, so calls can be chained
75 */
77
78 /**
79 * Starts the given command whenever the condition changes from `false` to
80 * `true`.
81 *
82 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
83 * lifespan of the command.
84 *
85 * @param command the command to start
86 * @return this trigger, so calls can be chained
87 */
89
90 /**
91 * Starts the given command whenever the condition changes from `false` to
92 * `true`. Moves command ownership to the button scheduler.
93 *
94 * @param command The command to bind.
95 * @return The trigger, for chained calls.
96 */
98
99 /**
100 * Starts the given command whenever the condition changes from `true` to
101 * `false`.
102 *
103 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
104 * lifespan of the command.
105 *
106 * @param command the command to start
107 * @return this trigger, so calls can be chained
108 */
110
111 /**
112 * Starts the given command whenever the condition changes from `true` to
113 * `false`.
114 *
115 * @param command The command to bind.
116 * @return The trigger, for chained calls.
117 */
119
120 /**
121 * Starts the given command when the condition changes to `true` and cancels
122 * it when the condition changes to `false`.
123 *
124 * <p>Doesn't re-start the command if it ends while the condition is still
125 * `true`. If the command should restart, see RepeatCommand.
126 *
127 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
128 * lifespan of the command.
129 *
130 * @param command the command to start
131 * @return this trigger, so calls can be chained
132 */
134
135 /**
136 * Starts the given command when the condition changes to `true` and cancels
137 * it when the condition changes to `false`. Moves command ownership to the
138 * button scheduler.
139 *
140 * <p>Doesn't re-start the command if it ends while the condition is still
141 * `true`. If the command should restart, see RepeatCommand.
142 *
143 * @param command The command to bind.
144 * @return The trigger, for chained calls.
145 */
147
148 /**
149 * Starts the given command when the condition changes to `false` and cancels
150 * it when the condition changes to `true`.
151 *
152 * <p>Doesn't re-start the command if it ends while the condition is still
153 * `true`. If the command should restart, see RepeatCommand.
154 *
155 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
156 * lifespan of the command.
157 *
158 * @param command the command to start
159 * @return this trigger, so calls can be chained
160 */
162
163 /**
164 * Starts the given command when the condition changes to `false` and cancels
165 * it when the condition changes to `true`. Moves command ownership to the
166 * button scheduler.
167 *
168 * <p>Doesn't re-start the command if it ends while the condition is still
169 * `false`. If the command should restart, see RepeatCommand.
170 *
171 * @param command The command to bind.
172 * @return The trigger, for chained calls.
173 */
175
176 /**
177 * Toggles a command when the condition changes from `false` to `true`.
178 *
179 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
180 * lifespan of the command.
181 *
182 * @param command the command to toggle
183 * @return this trigger, so calls can be chained
184 */
186
187 /**
188 * Toggles a command when the condition changes from `false` to `true`.
189 *
190 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
191 * lifespan of the command.
192 *
193 * @param command the command to toggle
194 * @return this trigger, so calls can be chained
195 */
197
198 /**
199 * Toggles a command when the condition changes from `true` to the low
200 * state.
201 *
202 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
203 * lifespan of the command.
204 *
205 * @param command the command to toggle
206 * @return this trigger, so calls can be chained
207 */
209
210 /**
211 * Toggles a command when the condition changes from `true` to `false`.
212 *
213 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
214 * lifespan of the command.
215 *
216 * @param command the command to toggle
217 * @return this trigger, so calls can be chained
218 */
220
221 /**
222 * Composes two triggers with logical AND.
223 *
224 * @return A trigger which is active when both component triggers are active.
225 */
226 Trigger operator&&(std::function<bool()> rhs) {
227 return Trigger(m_loop, [condition = m_condition, rhs = std::move(rhs)] {
228 return condition() && rhs();
229 });
230 }
231
232 /**
233 * Composes two triggers with logical AND.
234 *
235 * @return A trigger which is active when both component triggers are active.
236 */
238 return Trigger(m_loop, [condition = m_condition, rhs] {
239 return condition() && rhs.m_condition();
240 });
241 }
242
243 /**
244 * Composes two triggers with logical OR.
245 *
246 * @return A trigger which is active when either component trigger is active.
247 */
248 Trigger operator||(std::function<bool()> rhs) {
249 return Trigger(m_loop, [condition = m_condition, rhs = std::move(rhs)] {
250 return condition() || rhs();
251 });
252 }
253
254 /**
255 * Composes two triggers with logical OR.
256 *
257 * @return A trigger which is active when either component trigger is active.
258 */
260 return Trigger(m_loop, [condition = m_condition, rhs] {
261 return condition() || rhs.m_condition();
262 });
263 }
264
265 /**
266 * Composes a trigger with logical NOT.
267 *
268 * @return A trigger which is active when the component trigger is inactive,
269 * and vice-versa.
270 */
272 return Trigger(m_loop, [condition = m_condition] { return !condition(); });
273 }
274
275 /**
276 * Creates a new debounced trigger from this trigger - it will become active
277 * when this trigger has been active for longer than the specified period.
278 *
279 * @param debounceTime The debounce period.
280 * @param type The debounce type.
281 * @return The debounced trigger.
282 */
283 Trigger Debounce(units::second_t debounceTime,
286
287 /**
288 * Returns the current state of this trigger.
289 *
290 * @return A bool representing the current state of the trigger.
291 */
292 bool Get() const;
293
294 private:
295 /**
296 * Adds a binding to the EventLoop.
297 *
298 * @param body The body of the binding to add.
299 */
300 void AddBinding(wpi::unique_function<void(bool, bool)>&& body);
301
302 frc::EventLoop* m_loop;
303 std::function<bool()> m_condition;
304};
305} // namespace frc2
This file provides a collection of function (or more generally, callable) type erasure utilities supp...
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:28
The scheduler responsible for running Commands.
Definition CommandScheduler.h:38
This class provides an easy way to link commands to conditions.
Definition Trigger.h:32
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:57
Trigger operator&&(std::function< bool()> rhs)
Composes two triggers with logical AND.
Definition Trigger.h:226
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:271
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:237
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 OnChange(Command *command)
Starts the command when the condition changes.
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:259
Trigger operator||(std::function< bool()> rhs)
Composes two triggers with logical OR.
Definition Trigger.h:248
Trigger OnChange(CommandPtr &&command)
Starts the command when the condition changes.
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:51
Trigger(std::function< bool()> condition)
Creates a new trigger based on the given condition.
Definition Trigger.h:41
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
@ kRising
Rising edge.
Definition Debouncer.h:25
A declarative way to bind a set of actions to a loop and execute them when the loop is polled.
Definition EventLoop.h:15
unique_function is a type-erasing functor similar to std::function.
Definition FunctionExtras.h:57
Definition FunctionalCommand.h:13
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280