WPILibC++ 2024.1.1-beta-4
XboxController.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 "frc/GenericHID.h"
8
9namespace frc {
10
11/**
12 * Handle input from Xbox 360 or Xbox One controllers connected to the Driver
13 * Station.
14 *
15 * This class handles Xbox input that comes from the Driver Station. Each time a
16 * value is requested the most recent value is returned. There is a single class
17 * instance for each controller and the mapping of ports to hardware buttons
18 * depends on the code in the Driver Station.
19 */
20class XboxController : public GenericHID {
21 public:
22 /**
23 * Construct an instance of an Xbox controller.
24 *
25 * The controller index is the USB port on the Driver Station.
26 *
27 * @param port The port on the Driver Station that the controller is plugged
28 * into (0-5).
29 */
30 explicit XboxController(int port);
31
32 ~XboxController() override = default;
33
36
37 /**
38 * Get the X axis value of left side of the controller.
39 *
40 * @return the axis value
41 */
42 double GetLeftX() const;
43
44 /**
45 * Get the X axis value of right side of the controller.
46 *
47 * @return the axis value
48 */
49 double GetRightX() const;
50
51 /**
52 * Get the Y axis value of left side of the controller.
53 *
54 * @return the axis value
55 */
56 double GetLeftY() const;
57
58 /**
59 * Get the Y axis value of right side of the controller.
60 *
61 * @return the axis value
62 */
63 double GetRightY() const;
64
65 /**
66 * Get the left trigger (LT) axis value of the controller. Note that this axis
67 * is bound to the range of [0, 1] as opposed to the usual [-1, 1].
68 *
69 * @return the axis value
70 */
71 double GetLeftTriggerAxis() const;
72
73 /**
74 * Get the right trigger (RT) axis value of the controller. Note that this
75 * axis is bound to the range of [0, 1] as opposed to the usual [-1, 1].
76 *
77 * @return the axis value
78 */
79 double GetRightTriggerAxis() const;
80
81 /**
82 * Read the value of the left bumper (LB) button on the controller.
83 *
84 * @return the state of the button
85 */
86 bool GetLeftBumper() const;
87
88 /**
89 * Read the value of the right bumper (RB) button on the controller.
90 *
91 * @return the state of the button
92 */
93 bool GetRightBumper() const;
94
95 /**
96 * Whether the left bumper (LB) was pressed since the last check.
97 *
98 * @return Whether the button was pressed since the last check
99 */
101
102 /**
103 * Whether the right bumper (RB) was pressed since the last check.
104 *
105 * @return Whether the button was pressed since the last check
106 */
108
109 /**
110 * Whether the left bumper (LB) was released since the last check.
111 *
112 * @return Whether the button was released since the last check.
113 */
115
116 /**
117 * Whether the right bumper (RB) was released since the last check.
118 *
119 * @return Whether the button was released since the last check.
120 */
122
123 /**
124 * Constructs an event instance around the left bumper's digital signal.
125 *
126 * @param loop the event loop instance to attach the event to.
127 * @return an event instance representing the left bumper's digital signal
128 * attached to the given loop.
129 */
131
132 /**
133 * Constructs an event instance around the right bumper's digital signal.
134 *
135 * @param loop the event loop instance to attach the event to.
136 * @return an event instance representing the right bumper's digital signal
137 * attached to the given loop.
138 */
140
141 /**
142 * Read the value of the left stick button (LSB) on the controller.
143 *
144 * @return the state of the button
145 */
146 bool GetLeftStickButton() const;
147
148 /**
149 * Read the value of the right stick button (RSB) on the controller.
150 *
151 * @return the state of the button
152 */
154
155 /**
156 * Whether the left stick button (LSB) was pressed since the last check.
157 *
158 * @return Whether the button was pressed since the last check.
159 */
161
162 /**
163 * Whether the right stick button (RSB) was pressed since the last check.
164 *
165 * @return Whether the button was pressed since the last check
166 */
168
169 /**
170 * Whether the left stick button (LSB) was released since the last check.
171 *
172 * @return Whether the button was released since the last check.
173 */
175
176 /**
177 * Whether the right stick button (RSB) was released since the last check.
178 *
179 * @return Whether the button was released since the last check.
180 */
182
183 /**
184 * Constructs an event instance around the left stick's digital signal.
185 *
186 * @param loop the event loop instance to attach the event to.
187 * @return an event instance representing the left stick's digital signal
188 * attached to the given loop.
189 */
191
192 /**
193 * Constructs an event instance around the right stick's digital signal.
194 *
195 * @param loop the event loop instance to attach the event to.
196 * @return an event instance representing the right stick's digital signal
197 * attached to the given loop.
198 */
200
201 /**
202 * Read the value of the A button on the controller.
203 *
204 * @return The state of the button.
205 */
206 bool GetAButton() const;
207
208 /**
209 * Whether the A button was pressed since the last check.
210 *
211 * @return Whether the button was pressed since the last check.
212 */
214
215 /**
216 * Whether the A button was released since the last check.
217 *
218 * @return Whether the button was released since the last check.
219 */
221
222 /**
223 * Constructs an event instance around the A button's digital signal.
224 *
225 * @param loop the event loop instance to attach the event to.
226 * @return an event instance representing the A button's digital signal
227 * attached to the given loop.
228 */
230
231 /**
232 * Read the value of the B button on the controller.
233 *
234 * @return The state of the button.
235 */
236 bool GetBButton() const;
237
238 /**
239 * Whether the B button was pressed since the last check.
240 *
241 * @return Whether the button was pressed since the last check.
242 */
244
245 /**
246 * Whether the B button was released since the last check.
247 *
248 * @return Whether the button was released since the last check.
249 */
251
252 /**
253 * Constructs an event instance around the B button's digital signal.
254 *
255 * @param loop the event loop instance to attach the event to.
256 * @return an event instance representing the B button's digital signal
257 * attached to the given loop.
258 */
260
261 /**
262 * Read the value of the X button on the controller.
263 *
264 * @return The state of the button.
265 */
266 bool GetXButton() const;
267
268 /**
269 * Whether the X button was pressed since the last check.
270 *
271 * @return Whether the button was pressed since the last check.
272 */
274
275 /**
276 * Whether the X button was released since the last check.
277 *
278 * @return Whether the button was released since the last check.
279 */
281
282 /**
283 * Constructs an event instance around the X button's digital signal.
284 *
285 * @param loop the event loop instance to attach the event to.
286 * @return an event instance representing the X button's digital signal
287 * attached to the given loop.
288 */
290
291 /**
292 * Read the value of the Y button on the controller.
293 *
294 * @return The state of the button.
295 */
296 bool GetYButton() const;
297
298 /**
299 * Whether the Y button was pressed since the last check.
300 *
301 * @return Whether the button was pressed since the last check.
302 */
304
305 /**
306 * Whether the Y button was released since the last check.
307 *
308 * @return Whether the button was released since the last check.
309 */
311
312 /**
313 * Constructs an event instance around the Y button's digital signal.
314 *
315 * @param loop the event loop instance to attach the event to.
316 * @return an event instance representing the Y button's digital signal
317 * attached to the given loop.
318 */
320
321 /**
322 * Read the value of the back button on the controller.
323 *
324 * @return The state of the button.
325 */
326 bool GetBackButton() const;
327
328 /**
329 * Whether the back button was pressed since the last check.
330 *
331 * @return Whether the button was pressed since the last check.
332 */
334
335 /**
336 * Whether the back button was released since the last check.
337 *
338 * @return Whether the button was released since the last check.
339 */
341
342 /**
343 * Constructs an event instance around the back button's digital signal.
344 *
345 * @param loop the event loop instance to attach the event to.
346 * @return an event instance representing the back button's digital signal
347 * attached to the given loop.
348 */
350
351 /**
352 * Read the value of the start button on the controller.
353 *
354 * @return The state of the button.
355 */
356 bool GetStartButton() const;
357
358 /**
359 * Whether the start button was pressed since the last check.
360 *
361 * @return Whether the button was pressed since the last check.
362 */
364
365 /**
366 * Whether the start button was released since the last check.
367 *
368 * @return Whether the button was released since the last check.
369 */
371
372 /**
373 * Constructs an event instance around the start button's digital signal.
374 *
375 * @param loop the event loop instance to attach the event to.
376 * @return an event instance representing the start button's digital signal
377 * attached to the given loop.
378 */
380
381 /**
382 * Constructs an event instance around the axis value of the left trigger. The
383 * returned trigger will be true when the axis value is greater than {@code
384 * threshold}.
385 * @param threshold the minimum axis value for the returned event to be true.
386 * This value should be in the range [0, 1] where 0 is the unpressed state of
387 * the axis.
388 * @param loop the event loop instance to attach the event to.
389 * @return an event instance that is true when the left trigger's axis exceeds
390 * the provided threshold, attached to the given event loop
391 */
392 BooleanEvent LeftTrigger(double threshold, EventLoop* loop) const;
393
394 /**
395 * Constructs an event instance around the axis value of the left trigger.
396 * The returned trigger will be true when the axis value is greater than 0.5.
397 * @param loop the event loop instance to attach the event to.
398 * @return an event instance that is true when the left trigger's axis
399 * exceeds 0.5, attached to the given event loop
400 */
402
403 /**
404 * Constructs an event instance around the axis value of the right trigger.
405 * The returned trigger will be true when the axis value is greater than
406 * {@code threshold}.
407 * @param threshold the minimum axis value for the returned event to be true.
408 * This value should be in the range [0, 1] where 0 is the unpressed state of
409 * the axis.
410 * @param loop the event loop instance to attach the event to.
411 * @return an event instance that is true when the right trigger's axis
412 * exceeds the provided threshold, attached to the given event loop
413 */
414 BooleanEvent RightTrigger(double threshold, EventLoop* loop) const;
415
416 /**
417 * Constructs an event instance around the axis value of the right trigger.
418 * The returned trigger will be true when the axis value is greater than 0.5.
419 * @param loop the event loop instance to attach the event to.
420 * @return an event instance that is true when the right trigger's axis
421 * exceeds 0.5, attached to the given event loop
422 */
424
425 struct Button {
426 static constexpr int kLeftBumper = 5;
427 static constexpr int kRightBumper = 6;
428 static constexpr int kLeftStick = 9;
429 static constexpr int kRightStick = 10;
430 static constexpr int kA = 1;
431 static constexpr int kB = 2;
432 static constexpr int kX = 3;
433 static constexpr int kY = 4;
434 static constexpr int kBack = 7;
435 static constexpr int kStart = 8;
436 };
437
438 struct Axis {
439 static constexpr int kLeftX = 0;
440 static constexpr int kRightX = 4;
441 static constexpr int kLeftY = 1;
442 static constexpr int kRightY = 5;
443 static constexpr int kLeftTrigger = 2;
444 static constexpr int kRightTrigger = 3;
445 };
446};
447
448} // namespace frc
This class provides an easy way to link actions to inputs.
Definition: BooleanEvent.h:31
A declarative way to bind a set of actions to a loop and execute them when the loop is polled.
Definition: EventLoop.h:15
Handle input from standard HID devices connected to the Driver Station.
Definition: GenericHID.h:24
Handle input from Xbox 360 or Xbox One controllers connected to the Driver Station.
Definition: XboxController.h:20
bool GetBButtonReleased()
Whether the B button was released since the last check.
BooleanEvent A(EventLoop *loop) const
Constructs an event instance around the A button's digital signal.
bool GetBackButtonPressed()
Whether the back button was pressed since the last check.
bool GetStartButtonPressed()
Whether the start button was pressed since the last check.
bool GetBButtonPressed()
Whether the B button was pressed since the last check.
double GetLeftX() const
Get the X axis value of left side of the controller.
bool GetAButton() const
Read the value of the A button on the controller.
bool GetLeftBumper() const
Read the value of the left bumper (LB) button on the controller.
bool GetLeftStickButton() const
Read the value of the left stick button (LSB) on the controller.
BooleanEvent Back(EventLoop *loop) const
Constructs an event instance around the back button's digital signal.
BooleanEvent LeftStick(EventLoop *loop) const
Constructs an event instance around the left stick's digital signal.
bool GetRightBumperReleased()
Whether the right bumper (RB) was released since the last check.
bool GetAButtonReleased()
Whether the A button was released since the last check.
BooleanEvent Start(EventLoop *loop) const
Constructs an event instance around the start button's digital signal.
bool GetBButton() const
Read the value of the B button on the controller.
XboxController(XboxController &&)=default
double GetLeftY() const
Get the Y axis value of left side of the controller.
bool GetXButtonPressed()
Whether the X button was pressed since the last check.
XboxController & operator=(XboxController &&)=default
BooleanEvent B(EventLoop *loop) const
Constructs an event instance around the B button's digital signal.
double GetLeftTriggerAxis() const
Get the left trigger (LT) axis value of the controller.
bool GetYButtonReleased()
Whether the Y button was released since the last check.
BooleanEvent RightBumper(EventLoop *loop) const
Constructs an event instance around the right bumper's digital signal.
BooleanEvent RightTrigger(EventLoop *loop) const
Constructs an event instance around the axis value of the right trigger.
bool GetLeftBumperPressed()
Whether the left bumper (LB) was pressed since the last check.
bool GetXButtonReleased()
Whether the X button was released since the last check.
bool GetRightBumper() const
Read the value of the right bumper (RB) button on the controller.
bool GetBackButtonReleased()
Whether the back button was released since the last check.
XboxController(int port)
Construct an instance of an Xbox controller.
bool GetLeftBumperReleased()
Whether the left bumper (LB) was released since the last check.
BooleanEvent RightTrigger(double threshold, EventLoop *loop) const
Constructs an event instance around the axis value of the right trigger.
~XboxController() override=default
bool GetRightStickButtonPressed()
Whether the right stick button (RSB) was pressed since the last check.
bool GetYButton() const
Read the value of the Y button on the controller.
BooleanEvent LeftTrigger(EventLoop *loop) const
Constructs an event instance around the axis value of the left trigger.
double GetRightY() const
Get the Y axis value of right side of the controller.
bool GetAButtonPressed()
Whether the A button was pressed since the last check.
BooleanEvent X(EventLoop *loop) const
Constructs an event instance around the X button's digital signal.
BooleanEvent RightStick(EventLoop *loop) const
Constructs an event instance around the right stick's digital signal.
bool GetLeftStickButtonPressed()
Whether the left stick button (LSB) was pressed since the last check.
bool GetRightStickButtonReleased()
Whether the right stick button (RSB) was released since the last check.
bool GetRightBumperPressed()
Whether the right bumper (RB) was pressed since the last check.
bool GetXButton() const
Read the value of the X button on the controller.
bool GetRightStickButton() const
Read the value of the right stick button (RSB) on the controller.
bool GetStartButtonReleased()
Whether the start button was released since the last check.
bool GetLeftStickButtonReleased()
Whether the left stick button (LSB) was released since the last check.
double GetRightTriggerAxis() const
Get the right trigger (RT) axis value of the controller.
BooleanEvent LeftBumper(EventLoop *loop) const
Constructs an event instance around the left bumper's digital signal.
bool GetBackButton() const
Read the value of the back button on the controller.
bool GetStartButton() const
Read the value of the start button on the controller.
BooleanEvent LeftTrigger(double threshold, EventLoop *loop) const
Constructs an event instance around the axis value of the left trigger.
bool GetYButtonPressed()
Whether the Y button was pressed since the last check.
BooleanEvent Y(EventLoop *loop) const
Constructs an event instance around the Y button's digital signal.
double GetRightX() const
Get the X axis value of right side of the controller.
Definition: AprilTagPoseEstimator.h:15
Definition: XboxController.h:438
static constexpr int kLeftTrigger
Definition: XboxController.h:443
static constexpr int kLeftY
Definition: XboxController.h:441
static constexpr int kLeftX
Definition: XboxController.h:439
static constexpr int kRightTrigger
Definition: XboxController.h:444
static constexpr int kRightY
Definition: XboxController.h:442
static constexpr int kRightX
Definition: XboxController.h:440
Definition: XboxController.h:425
static constexpr int kA
Definition: XboxController.h:430
static constexpr int kY
Definition: XboxController.h:433
static constexpr int kBack
Definition: XboxController.h:434
static constexpr int kB
Definition: XboxController.h:431
static constexpr int kLeftBumper
Definition: XboxController.h:426
static constexpr int kRightStick
Definition: XboxController.h:429
static constexpr int kRightBumper
Definition: XboxController.h:427
static constexpr int kLeftStick
Definition: XboxController.h:428
static constexpr int kX
Definition: XboxController.h:432
static constexpr int kStart
Definition: XboxController.h:435