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