WPILibC++ 2024.3.2-109-g8c420fa
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 command when the condition changes.
62 *
63 * @param command the command to start
64 * @return this trigger, so calls can be chained
65 */
67
68 /**
69 * Starts the command when the condition changes. Moves command ownership to
70 * the button scheduler.
71 *
72 * @param command the command to start
73 * @return this trigger, so calls can be chained
74 */
76
77 /**
78 * Starts the given command whenever the condition changes from `false` to
79 * `true`.
80 *
81 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
82 * lifespan of the command.
83 *
84 * @param command the command to start
85 * @return this trigger, so calls can be chained
86 */
88
89 /**
90 * Starts the given command whenever the condition changes from `false` to
91 * `true`. Moves command ownership to the button scheduler.
92 *
93 * @param command The command to bind.
94 * @return The trigger, for chained calls.
95 */
97
98 /**
99 * Starts the given command whenever the condition changes from `true` to
100 * `false`.
101 *
102 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
103 * lifespan of the command.
104 *
105 * @param command the command to start
106 * @return this trigger, so calls can be chained
107 */
109
110 /**
111 * Starts the given command whenever the condition changes from `true` to
112 * `false`.
113 *
114 * @param command The command to bind.
115 * @return The trigger, for chained calls.
116 */
118
119 /**
120 * Starts the given command when the condition changes to `true` and cancels
121 * it when the condition changes to `false`.
122 *
123 * <p>Doesn't re-start the command if it ends while the condition is still
124 * `true`. If the command should restart, see RepeatCommand.
125 *
126 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
127 * lifespan of the command.
128 *
129 * @param command the command to start
130 * @return this trigger, so calls can be chained
131 */
133
134 /**
135 * Starts the given command when the condition changes to `true` and cancels
136 * it when the condition changes to `false`. Moves command ownership to the
137 * button scheduler.
138 *
139 * <p>Doesn't re-start the command if it ends while the condition is still
140 * `true`. If the command should restart, see RepeatCommand.
141 *
142 * @param command The command to bind.
143 * @return The trigger, for chained calls.
144 */
146
147 /**
148 * Starts the given command when the condition changes to `false` and cancels
149 * it when the condition changes to `true`.
150 *
151 * <p>Doesn't re-start the command if it ends while the condition is still
152 * `true`. If the command should restart, see RepeatCommand.
153 *
154 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
155 * lifespan of the command.
156 *
157 * @param command the command to start
158 * @return this trigger, so calls can be chained
159 */
161
162 /**
163 * Starts the given command when the condition changes to `false` and cancels
164 * it when the condition changes to `true`. Moves command ownership to the
165 * button scheduler.
166 *
167 * <p>Doesn't re-start the command if it ends while the condition is still
168 * `false`. If the command should restart, see RepeatCommand.
169 *
170 * @param command The command to bind.
171 * @return The trigger, for chained calls.
172 */
174
175 /**
176 * Toggles a command when the condition changes from `false` to `true`.
177 *
178 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
179 * lifespan of the command.
180 *
181 * @param command the command to toggle
182 * @return this trigger, so calls can be chained
183 */
185
186 /**
187 * Toggles a command when the condition changes from `false` to `true`.
188 *
189 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
190 * lifespan of the command.
191 *
192 * @param command the command to toggle
193 * @return this trigger, so calls can be chained
194 */
196
197 /**
198 * Toggles a command when the condition changes from `true` to the low
199 * state.
200 *
201 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
202 * lifespan of the command.
203 *
204 * @param command the command to toggle
205 * @return this trigger, so calls can be chained
206 */
208
209 /**
210 * Toggles a command when the condition changes from `true` to `false`.
211 *
212 * <p>Takes a raw pointer, and so is non-owning; users are responsible for the
213 * lifespan of the command.
214 *
215 * @param command the command to toggle
216 * @return this trigger, so calls can be chained
217 */
219
220 /**
221 * Composes two triggers with logical AND.
222 *
223 * @return A trigger which is active when both component triggers are active.
224 */
225 Trigger operator&&(std::function<bool()> rhs) {
226 return Trigger(m_loop, [condition = m_condition, rhs = std::move(rhs)] {
227 return condition() && rhs();
228 });
229 }
230
231 /**
232 * Composes two triggers with logical AND.
233 *
234 * @return A trigger which is active when both component triggers are active.
235 */
237 return Trigger(m_loop, [condition = m_condition, rhs] {
238 return condition() && rhs.m_condition();
239 });
240 }
241
242 /**
243 * Composes two triggers with logical OR.
244 *
245 * @return A trigger which is active when either component trigger is active.
246 */
247 Trigger operator||(std::function<bool()> rhs) {
248 return Trigger(m_loop, [condition = m_condition, rhs = std::move(rhs)] {
249 return condition() || rhs();
250 });
251 }
252
253 /**
254 * Composes two triggers with logical OR.
255 *
256 * @return A trigger which is active when either component trigger is active.
257 */
259 return Trigger(m_loop, [condition = m_condition, rhs] {
260 return condition() || rhs.m_condition();
261 });
262 }
263
264 /**
265 * Composes a trigger with logical NOT.
266 *
267 * @return A trigger which is active when the component trigger is inactive,
268 * and vice-versa.
269 */
271 return Trigger(m_loop, [condition = m_condition] { return !condition(); });
272 }
273
274 /**
275 * Creates a new debounced trigger from this trigger - it will become active
276 * when this trigger has been active for longer than the specified period.
277 *
278 * @param debounceTime The debounce period.
279 * @param type The debounce type.
280 * @return The debounced trigger.
281 */
282 Trigger Debounce(units::second_t debounceTime,
284 frc::Debouncer::DebounceType::kRising);
285
286 /**
287 * Returns the current state of this trigger.
288 * @return A bool representing the current state of the trigger.
289 */
290 bool Get() const;
291
292 private:
293 frc::EventLoop* m_loop;
294 std::function<bool()> m_condition;
295};
296} // 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: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: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:225
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:270
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:236
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:258
Trigger operator||(std::function< bool()> rhs)
Composes two triggers with logical OR.
Definition: Trigger.h:247
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: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:573
Definition: StartEndCommand.h:13
Implement std::hash so that hash_code can be used in STL containers.
Definition: array.h:89