WPILibC++ 2024.3.2
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 *
20 * Only first party controllers from Microsoft are guaranteed to have the
21 * correct mapping, and only through the official NI DS. Sim is not guaranteed
22 * to have the same mapping, as well as any 3rd party controllers.
23 */
24class XboxController : public GenericHID {
25 public:
26 /**
27 * Construct an instance of an Xbox controller.
28 *
29 * The controller index is the USB port on the Driver Station.
30 *
31 * @param port The port on the Driver Station that the controller is plugged
32 * into (0-5).
33 */
34 explicit XboxController(int port);
35
36 ~XboxController() override = default;
37
40
41 /**
42 * Get the X axis value of left side of the controller.
43 *
44 * @return the axis value
45 */
46 double GetLeftX() const;
47
48 /**
49 * Get the X axis value of right side of the controller.
50 *
51 * @return the axis value
52 */
53 double GetRightX() const;
54
55 /**
56 * Get the Y axis value of left side of the controller.
57 *
58 * @return the axis value
59 */
60 double GetLeftY() const;
61
62 /**
63 * Get the Y axis value of right side of the controller.
64 *
65 * @return the axis value
66 */
67 double GetRightY() const;
68
69 /**
70 * Get the left trigger (LT) axis value of the controller. Note that this axis
71 * is bound to the range of [0, 1] as opposed to the usual [-1, 1].
72 *
73 * @return the axis value
74 */
75 double GetLeftTriggerAxis() const;
76
77 /**
78 * Get the right trigger (RT) axis value of the controller. Note that this
79 * axis is bound to the range of [0, 1] as opposed to the usual [-1, 1].
80 *
81 * @return the axis value
82 */
83 double GetRightTriggerAxis() const;
84
85 /**
86 * Read the value of the left bumper (LB) button on the controller.
87 *
88 * @return the state of the button
89 */
90 bool GetLeftBumper() const;
91
92 /**
93 * Read the value of the right bumper (RB) button on the controller.
94 *
95 * @return the state of the button
96 */
97 bool GetRightBumper() const;
98
99 /**
100 * Whether the left bumper (LB) was pressed since the last check.
101 *
102 * @return Whether the button was pressed since the last check
103 */
105
106 /**
107 * Whether the right bumper (RB) was pressed since the last check.
108 *
109 * @return Whether the button was pressed since the last check
110 */
112
113 /**
114 * Whether the left bumper (LB) was released since the last check.
115 *
116 * @return Whether the button was released since the last check.
117 */
119
120 /**
121 * Whether the right bumper (RB) was released since the last check.
122 *
123 * @return Whether the button was released since the last check.
124 */
126
127 /**
128 * Constructs an event instance around the left bumper's digital signal.
129 *
130 * @param loop the event loop instance to attach the event to.
131 * @return an event instance representing the left bumper's digital signal
132 * attached to the given loop.
133 */
135
136 /**
137 * Constructs an event instance around the right bumper's digital signal.
138 *
139 * @param loop the event loop instance to attach the event to.
140 * @return an event instance representing the right bumper's digital signal
141 * attached to the given loop.
142 */
144
145 /**
146 * Read the value of the left stick button (LSB) on the controller.
147 *
148 * @return the state of the button
149 */
150 bool GetLeftStickButton() const;
151
152 /**
153 * Read the value of the right stick button (RSB) on the controller.
154 *
155 * @return the state of the button
156 */
158
159 /**
160 * Whether the left stick button (LSB) was pressed since the last check.
161 *
162 * @return Whether the button was pressed since the last check.
163 */
165
166 /**
167 * Whether the right stick button (RSB) was pressed since the last check.
168 *
169 * @return Whether the button was pressed since the last check
170 */
172
173 /**
174 * Whether the left stick button (LSB) was released since the last check.
175 *
176 * @return Whether the button was released since the last check.
177 */
179
180 /**
181 * Whether the right stick button (RSB) was released since the last check.
182 *
183 * @return Whether the button was released since the last check.
184 */
186
187 /**
188 * Constructs an event instance around the left stick's digital signal.
189 *
190 * @param loop the event loop instance to attach the event to.
191 * @return an event instance representing the left stick's digital signal
192 * attached to the given loop.
193 */
195
196 /**
197 * Constructs an event instance around the right stick's digital signal.
198 *
199 * @param loop the event loop instance to attach the event to.
200 * @return an event instance representing the right stick's digital signal
201 * attached to the given loop.
202 */
204
205 /**
206 * Read the value of the A button on the controller.
207 *
208 * @return The state of the button.
209 */
210 bool GetAButton() const;
211
212 /**
213 * Whether the A button was pressed since the last check.
214 *
215 * @return Whether the button was pressed since the last check.
216 */
218
219 /**
220 * Whether the A button was released since the last check.
221 *
222 * @return Whether the button was released since the last check.
223 */
225
226 /**
227 * Constructs an event instance around the A button's digital signal.
228 *
229 * @param loop the event loop instance to attach the event to.
230 * @return an event instance representing the A button's digital signal
231 * attached to the given loop.
232 */
234
235 /**
236 * Read the value of the B button on the controller.
237 *
238 * @return The state of the button.
239 */
240 bool GetBButton() const;
241
242 /**
243 * Whether the B button was pressed since the last check.
244 *
245 * @return Whether the button was pressed since the last check.
246 */
248
249 /**
250 * Whether the B button was released since the last check.
251 *
252 * @return Whether the button was released since the last check.
253 */
255
256 /**
257 * Constructs an event instance around the B button's digital signal.
258 *
259 * @param loop the event loop instance to attach the event to.
260 * @return an event instance representing the B button's digital signal
261 * attached to the given loop.
262 */
264
265 /**
266 * Read the value of the X button on the controller.
267 *
268 * @return The state of the button.
269 */
270 bool GetXButton() const;
271
272 /**
273 * Whether the X button was pressed since the last check.
274 *
275 * @return Whether the button was pressed since the last check.
276 */
278
279 /**
280 * Whether the X button was released since the last check.
281 *
282 * @return Whether the button was released since the last check.
283 */
285
286 /**
287 * Constructs an event instance around the X button's digital signal.
288 *
289 * @param loop the event loop instance to attach the event to.
290 * @return an event instance representing the X button's digital signal
291 * attached to the given loop.
292 */
294
295 /**
296 * Read the value of the Y button on the controller.
297 *
298 * @return The state of the button.
299 */
300 bool GetYButton() const;
301
302 /**
303 * Whether the Y button was pressed since the last check.
304 *
305 * @return Whether the button was pressed since the last check.
306 */
308
309 /**
310 * Whether the Y button was released since the last check.
311 *
312 * @return Whether the button was released since the last check.
313 */
315
316 /**
317 * Constructs an event instance around the Y button's digital signal.
318 *
319 * @param loop the event loop instance to attach the event to.
320 * @return an event instance representing the Y button's digital signal
321 * attached to the given loop.
322 */
324
325 /**
326 * Read the value of the back button on the controller.
327 *
328 * @return The state of the button.
329 */
330 bool GetBackButton() const;
331
332 /**
333 * Whether the back button was pressed since the last check.
334 *
335 * @return Whether the button was pressed since the last check.
336 */
338
339 /**
340 * Whether the back button was released since the last check.
341 *
342 * @return Whether the button was released since the last check.
343 */
345
346 /**
347 * Constructs an event instance around the back button's digital signal.
348 *
349 * @param loop the event loop instance to attach the event to.
350 * @return an event instance representing the back button's digital signal
351 * attached to the given loop.
352 */
354
355 /**
356 * Read the value of the start button on the controller.
357 *
358 * @return The state of the button.
359 */
360 bool GetStartButton() const;
361
362 /**
363 * Whether the start button was pressed since the last check.
364 *
365 * @return Whether the button was pressed since the last check.
366 */
368
369 /**
370 * Whether the start button was released since the last check.
371 *
372 * @return Whether the button was released since the last check.
373 */
375
376 /**
377 * Constructs an event instance around the start button's digital signal.
378 *
379 * @param loop the event loop instance to attach the event to.
380 * @return an event instance representing the start button's digital signal
381 * attached to the given loop.
382 */
384
385 /**
386 * Constructs an event instance around the axis value of the left trigger. The
387 * returned trigger will be true when the axis value is greater than {@code
388 * threshold}.
389 * @param threshold the minimum axis value for the returned event to be true.
390 * This value should be in the range [0, 1] where 0 is the unpressed state of
391 * the axis.
392 * @param loop the event loop instance to attach the event to.
393 * @return an event instance that is true when the left trigger's axis exceeds
394 * the provided threshold, attached to the given event loop
395 */
396 BooleanEvent LeftTrigger(double threshold, EventLoop* loop) const;
397
398 /**
399 * Constructs an event instance around the axis value of the left trigger.
400 * The returned trigger will be true when the axis value is greater than 0.5.
401 * @param loop the event loop instance to attach the event to.
402 * @return an event instance that is true when the left trigger's axis
403 * exceeds 0.5, attached to the given event loop
404 */
406
407 /**
408 * Constructs an event instance around the axis value of the right trigger.
409 * The returned trigger will be true when the axis value is greater than
410 * {@code threshold}.
411 * @param threshold the minimum axis value for the returned event to be true.
412 * This value should be in the range [0, 1] where 0 is the unpressed state of
413 * the axis.
414 * @param loop the event loop instance to attach the event to.
415 * @return an event instance that is true when the right trigger's axis
416 * exceeds the provided threshold, attached to the given event loop
417 */
418 BooleanEvent RightTrigger(double threshold, EventLoop* loop) const;
419
420 /**
421 * Constructs an event instance around the axis value of the right trigger.
422 * The returned trigger will be true when the axis value is greater than 0.5.
423 * @param loop the event loop instance to attach the event to.
424 * @return an event instance that is true when the right trigger's axis
425 * exceeds 0.5, attached to the given event loop
426 */
428
429 /** Represents a digital button on an XboxController. */
430 struct Button {
431 /// Left bumper.
432 static constexpr int kLeftBumper = 5;
433 /// Right bumper.
434 static constexpr int kRightBumper = 6;
435 /// Left stick.
436 static constexpr int kLeftStick = 9;
437 /// Right stick.
438 static constexpr int kRightStick = 10;
439 /// A.
440 static constexpr int kA = 1;
441 /// B.
442 static constexpr int kB = 2;
443 /// X.
444 static constexpr int kX = 3;
445 /// Y.
446 static constexpr int kY = 4;
447 /// Back.
448 static constexpr int kBack = 7;
449 /// Start.
450 static constexpr int kStart = 8;
451 };
452
453 /** Represents an axis on an XboxController. */
454 struct Axis {
455 /// Left X.
456 static constexpr int kLeftX = 0;
457 /// Right X.
458 static constexpr int kRightX = 4;
459 /// Left Y.
460 static constexpr int kLeftY = 1;
461 /// Right Y.
462 static constexpr int kRightY = 5;
463 /// Left trigger.
464 static constexpr int kLeftTrigger = 2;
465 /// Right trigger.
466 static constexpr int kRightTrigger = 3;
467 };
468};
469
470} // 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:24
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
Represents an axis on an XboxController.
Definition: XboxController.h:454
static constexpr int kLeftTrigger
Left trigger.
Definition: XboxController.h:464
static constexpr int kLeftY
Left Y.
Definition: XboxController.h:460
static constexpr int kLeftX
Left X.
Definition: XboxController.h:456
static constexpr int kRightTrigger
Right trigger.
Definition: XboxController.h:466
static constexpr int kRightY
Right Y.
Definition: XboxController.h:462
static constexpr int kRightX
Right X.
Definition: XboxController.h:458
Represents a digital button on an XboxController.
Definition: XboxController.h:430
static constexpr int kA
A.
Definition: XboxController.h:440
static constexpr int kY
Y.
Definition: XboxController.h:446
static constexpr int kBack
Back.
Definition: XboxController.h:448
static constexpr int kB
B.
Definition: XboxController.h:442
static constexpr int kLeftBumper
Left bumper.
Definition: XboxController.h:432
static constexpr int kRightStick
Right stick.
Definition: XboxController.h:438
static constexpr int kRightBumper
Right bumper.
Definition: XboxController.h:434
static constexpr int kLeftStick
Left stick.
Definition: XboxController.h:436
static constexpr int kX
X.
Definition: XboxController.h:444
static constexpr int kStart
Start.
Definition: XboxController.h:450