WPILibC++ 2023.4.3
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 double GetLeftX() const;
41
42 /**
43 * Get the X axis value of right side of the controller.
44 */
45 double GetRightX() const;
46
47 /**
48 * Get the Y axis value of left side of the controller.
49 */
50 double GetLeftY() const;
51
52 /**
53 * Get the Y axis value of right side of the controller.
54 */
55 double GetRightY() const;
56
57 /**
58 * Get the left trigger (LT) axis value of the controller. Note that this axis
59 * is bound to the range of [0, 1] as opposed to the usual [-1, 1].
60 */
61 double GetLeftTriggerAxis() const;
62
63 /**
64 * Get the right trigger (RT) axis value of the controller. Note that this
65 * axis is bound to the range of [0, 1] as opposed to the usual [-1, 1].
66 */
67 double GetRightTriggerAxis() const;
68
69 /**
70 * Read the value of the left bumper (LB) button on the controller.
71 */
72 bool GetLeftBumper() const;
73
74 /**
75 * Read the value of the right bumper (RB) button on the controller.
76 */
77 bool GetRightBumper() const;
78
79 /**
80 * Whether the left bumper (LB) was pressed since the last check.
81 */
83
84 /**
85 * Whether the right bumper (RB) was pressed since the last check.
86 */
88
89 /**
90 * Whether the left bumper (LB) was released since the last check.
91 */
93
94 /**
95 * Whether the right bumper (RB) was released since the last check.
96 */
98
99 /**
100 * Constructs an event instance around the left bumper's digital signal.
101 *
102 * @param loop the event loop instance to attach the event to.
103 * @return an event instance representing the left bumper's digital signal
104 * attached to the given loop.
105 */
107
108 /**
109 * Constructs an event instance around the right bumper's digital signal.
110 *
111 * @param loop the event loop instance to attach the event to.
112 * @return an event instance representing the right bumper's digital signal
113 * attached to the given loop.
114 */
116
117 /**
118 * Read the value of the left stick button (LSB) on the controller.
119 */
120 bool GetLeftStickButton() const;
121
122 /**
123 * Read the value of the right stick button (RSB) on the controller.
124 */
126
127 /**
128 * Whether the left stick button (LSB) was pressed since the last check.
129 */
131
132 /**
133 * Whether the right stick button (RSB) was pressed since the last check.
134 */
136
137 /**
138 * Whether the left stick button (LSB) was released since the last check.
139 */
141
142 /**
143 * Whether the right stick button (RSB) was released since the last check.
144 */
146
147 /**
148 * Constructs an event instance around the left stick's digital signal.
149 *
150 * @param loop the event loop instance to attach the event to.
151 * @return an event instance representing the left stick's digital signal
152 * attached to the given loop.
153 */
155
156 /**
157 * Constructs an event instance around the right stick's digital signal.
158 *
159 * @param loop the event loop instance to attach the event to.
160 * @return an event instance representing the right stick's digital signal
161 * attached to the given loop.
162 */
164
165 /**
166 * Read the value of the A button on the controller.
167 *
168 * @return The state of the button.
169 */
170 bool GetAButton() const;
171
172 /**
173 * Whether the A button was pressed since the last check.
174 *
175 * @return Whether the button was pressed since the last check.
176 */
178
179 /**
180 * Whether the A button was released since the last check.
181 *
182 * @return Whether the button was released since the last check.
183 */
185
186 /**
187 * Constructs an event instance around the A button's digital signal.
188 *
189 * @param loop the event loop instance to attach the event to.
190 * @return an event instance representing the A button's digital signal
191 * attached to the given loop.
192 */
194
195 /**
196 * Read the value of the B button on the controller.
197 *
198 * @return The state of the button.
199 */
200 bool GetBButton() const;
201
202 /**
203 * Whether the B button was pressed since the last check.
204 *
205 * @return Whether the button was pressed since the last check.
206 */
208
209 /**
210 * Whether the B button was released since the last check.
211 *
212 * @return Whether the button was released since the last check.
213 */
215
216 /**
217 * Constructs an event instance around the B button's digital signal.
218 *
219 * @param loop the event loop instance to attach the event to.
220 * @return an event instance representing the B button's digital signal
221 * attached to the given loop.
222 */
224
225 /**
226 * Read the value of the X button on the controller.
227 *
228 * @return The state of the button.
229 */
230 bool GetXButton() const;
231
232 /**
233 * Whether the X button was pressed since the last check.
234 *
235 * @return Whether the button was pressed since the last check.
236 */
238
239 /**
240 * Whether the X button was released since the last check.
241 *
242 * @return Whether the button was released since the last check.
243 */
245
246 /**
247 * Constructs an event instance around the X button's digital signal.
248 *
249 * @param loop the event loop instance to attach the event to.
250 * @return an event instance representing the X button's digital signal
251 * attached to the given loop.
252 */
254
255 /**
256 * Read the value of the Y button on the controller.
257 *
258 * @return The state of the button.
259 */
260 bool GetYButton() const;
261
262 /**
263 * Whether the Y button was pressed since the last check.
264 *
265 * @return Whether the button was pressed since the last check.
266 */
268
269 /**
270 * Whether the Y button was released since the last check.
271 *
272 * @return Whether the button was released since the last check.
273 */
275
276 /**
277 * Constructs an event instance around the Y button's digital signal.
278 *
279 * @param loop the event loop instance to attach the event to.
280 * @return an event instance representing the Y button's digital signal
281 * attached to the given loop.
282 */
284
285 /**
286 * Read the value of the back button on the controller.
287 *
288 * @return The state of the button.
289 */
290 bool GetBackButton() const;
291
292 /**
293 * Whether the back button was pressed since the last check.
294 *
295 * @return Whether the button was pressed since the last check.
296 */
298
299 /**
300 * Whether the back button was released since the last check.
301 *
302 * @return Whether the button was released since the last check.
303 */
305
306 /**
307 * Constructs an event instance around the back button's digital signal.
308 *
309 * @param loop the event loop instance to attach the event to.
310 * @return an event instance representing the back button's digital signal
311 * attached to the given loop.
312 */
314
315 /**
316 * Read the value of the start button on the controller.
317 *
318 * @return The state of the button.
319 */
320 bool GetStartButton() const;
321
322 /**
323 * Whether the start button was pressed since the last check.
324 *
325 * @return Whether the button was pressed since the last check.
326 */
328
329 /**
330 * Whether the start button was released since the last check.
331 *
332 * @return Whether the button was released since the last check.
333 */
335
336 /**
337 * Constructs an event instance around the start button's digital signal.
338 *
339 * @param loop the event loop instance to attach the event to.
340 * @return an event instance representing the start button's digital signal
341 * attached to the given loop.
342 */
344
345 /**
346 * Constructs an event instance around the axis value of the left trigger. The
347 * returned trigger will be true when the axis value is greater than {@code
348 * threshold}.
349 * @param threshold the minimum axis value for the returned event to be true.
350 * This value should be in the range [0, 1] where 0 is the unpressed state of
351 * the axis.
352 * @param loop the event loop instance to attach the event to.
353 * @return an event instance that is true when the left trigger's axis exceeds
354 * the provided threshold, attached to the given event loop
355 */
356 BooleanEvent LeftTrigger(double threshold, EventLoop* loop) const;
357
358 /**
359 * Constructs an event instance around the axis value of the left trigger.
360 * The returned trigger will be true when the axis value is greater than 0.5.
361 * @param loop the event loop instance to attach the event to.
362 * @return an event instance that is true when the left trigger's axis
363 * exceeds 0.5, attached to the given event loop
364 */
366
367 /**
368 * Constructs an event instance around the axis value of the right trigger.
369 * The returned trigger will be true when the axis value is greater than
370 * {@code threshold}.
371 * @param threshold the minimum axis value for the returned event to be true.
372 * This value should be in the range [0, 1] where 0 is the unpressed state of
373 * the axis.
374 * @param loop the event loop instance to attach the event to.
375 * @return an event instance that is true when the right trigger's axis
376 * exceeds the provided threshold, attached to the given event loop
377 */
378 BooleanEvent RightTrigger(double threshold, EventLoop* loop) const;
379
380 /**
381 * Constructs an event instance around the axis value of the right trigger.
382 * The returned trigger will be true when the axis value is greater than 0.5.
383 * @param loop the event loop instance to attach the event to.
384 * @return an event instance that is true when the right trigger's axis
385 * exceeds 0.5, attached to the given event loop
386 */
388
389 struct Button {
390 static constexpr int kLeftBumper = 5;
391 static constexpr int kRightBumper = 6;
392 static constexpr int kLeftStick = 9;
393 static constexpr int kRightStick = 10;
394 static constexpr int kA = 1;
395 static constexpr int kB = 2;
396 static constexpr int kX = 3;
397 static constexpr int kY = 4;
398 static constexpr int kBack = 7;
399 static constexpr int kStart = 8;
400 };
401
402 struct Axis {
403 static constexpr int kLeftX = 0;
404 static constexpr int kRightX = 4;
405 static constexpr int kLeftY = 1;
406 static constexpr int kRightY = 5;
407 static constexpr int kLeftTrigger = 2;
408 static constexpr int kRightTrigger = 3;
409 };
410};
411
412} // namespace frc
This class provides an easy way to link actions to inputs.
Definition: BooleanEvent.h:31
The loop polling BooleanEvent objects and executing the actions bound to them.
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: AprilTagFieldLayout.h:22
Definition: XboxController.h:402
static constexpr int kLeftTrigger
Definition: XboxController.h:407
static constexpr int kLeftY
Definition: XboxController.h:405
static constexpr int kLeftX
Definition: XboxController.h:403
static constexpr int kRightTrigger
Definition: XboxController.h:408
static constexpr int kRightY
Definition: XboxController.h:406
static constexpr int kRightX
Definition: XboxController.h:404
Definition: XboxController.h:389
static constexpr int kA
Definition: XboxController.h:394
static constexpr int kY
Definition: XboxController.h:397
static constexpr int kBack
Definition: XboxController.h:398
static constexpr int kB
Definition: XboxController.h:395
static constexpr int kLeftBumper
Definition: XboxController.h:390
static constexpr int kRightStick
Definition: XboxController.h:393
static constexpr int kRightBumper
Definition: XboxController.h:391
static constexpr int kLeftStick
Definition: XboxController.h:392
static constexpr int kX
Definition: XboxController.h:396
static constexpr int kStart
Definition: XboxController.h:399