WPILibC++ 2024.3.2
PS4Controller.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 PS4 controllers connected to the Driver Station.
13 *
14 * This class handles PS4 input that comes from the Driver Station. Each time
15 * a value is requested the most recent value is returned. There is a single
16 * class instance for each controller and the mapping of ports to hardware
17 * buttons depends on the code in the Driver Station.
18 *
19 * Only first party controllers from Sony are guaranteed to have the correct
20 * mapping, and only through the official NI DS. Sim is not guaranteed to have
21 * the same mapping, as well as any 3rd party controllers.
22 */
23class PS4Controller : public GenericHID {
24 public:
25 /**
26 * Construct an instance of an PS4 controller.
27 *
28 * The controller index is the USB port on the Driver Station.
29 *
30 * @param port The port on the Driver Station that the controller is plugged
31 * into (0-5).
32 */
33 explicit PS4Controller(int port);
34
35 ~PS4Controller() override = default;
36
39
40 /**
41 * Get the X axis value of left side of the controller.
42 *
43 * @return the axis value.
44 */
45 double GetLeftX() const;
46
47 /**
48 * Get the X axis value of right side of the controller.
49 *
50 * @return the axis value.
51 */
52 double GetRightX() const;
53
54 /**
55 * Get the Y axis value of left side of the controller.
56 *
57 * @return the axis value.
58 */
59 double GetLeftY() const;
60
61 /**
62 * Get the Y axis value of right side of the controller.
63 *
64 * @return the axis value.
65 */
66 double GetRightY() const;
67
68 /**
69 * Get the L2 axis value of the controller. Note that this axis is bound to
70 * the range of [0, 1] as opposed to the usual [-1, 1].
71 *
72 * @return the axis value.
73 */
74 double GetL2Axis() const;
75
76 /**
77 * Get the R2 axis value of the controller. Note that this axis is bound to
78 * the range of [0, 1] as opposed to the usual [-1, 1].
79 *
80 * @return the axis value.
81 */
82 double GetR2Axis() const;
83
84 /**
85 * Read the value of the Square button on the controller.
86 *
87 * @return The state of the button.
88 */
89 bool GetSquareButton() const;
90
91 /**
92 * Whether the Square button was pressed since the last check.
93 *
94 * @return Whether the button was pressed since the last check.
95 */
97
98 /**
99 * Whether the Square button was released since the last check.
100 *
101 * @return Whether the button was released since the last check.
102 */
104
105 /**
106 * Constructs an event instance around the square button's digital signal.
107 *
108 * @param loop the event loop instance to attach the event to.
109 * @return an event instance representing the square button's digital signal
110 * attached to the given loop.
111 */
113
114 /**
115 * Read the value of the Cross button on the controller.
116 *
117 * @return The state of the button.
118 */
119 bool GetCrossButton() const;
120
121 /**
122 * Whether the Cross button was pressed since the last check.
123 *
124 * @return Whether the button was pressed since the last check.
125 */
127
128 /**
129 * Whether the Cross button was released since the last check.
130 *
131 * @return Whether the button was released since the last check.
132 */
134
135 /**
136 * Constructs an event instance around the cross button's digital signal.
137 *
138 * @param loop the event loop instance to attach the event to.
139 * @return an event instance representing the cross button's digital signal
140 * attached to the given loop.
141 */
143
144 /**
145 * Read the value of the Circle button on the controller.
146 *
147 * @return The state of the button.
148 */
149 bool GetCircleButton() const;
150
151 /**
152 * Whether the Circle button was pressed since the last check.
153 *
154 * @return Whether the button was pressed since the last check.
155 */
157
158 /**
159 * Whether the Circle button was released since the last check.
160 *
161 * @return Whether the button was released since the last check.
162 */
164
165 /**
166 * Constructs an event instance around the circle button's digital signal.
167 *
168 * @param loop the event loop instance to attach the event to.
169 * @return an event instance representing the circle button's digital signal
170 * attached to the given loop.
171 */
173
174 /**
175 * Read the value of the Triangle button on the controller.
176 *
177 * @return The state of the button.
178 */
179 bool GetTriangleButton() const;
180
181 /**
182 * Whether the Triangle button was pressed since the last check.
183 *
184 * @return Whether the button was pressed since the last check.
185 */
187
188 /**
189 * Whether the Triangle button was released since the last check.
190 *
191 * @return Whether the button was released since the last check.
192 */
194
195 /**
196 * Constructs an event instance around the triangle button's digital signal.
197 *
198 * @param loop the event loop instance to attach the event to.
199 * @return an event instance representing the triangle button's digital signal
200 * attached to the given loop.
201 */
203
204 /**
205 * Read the value of the L1 button on the controller.
206 *
207 * @return The state of the button.
208 */
209 bool GetL1Button() const;
210
211 /**
212 * Whether the L1 button was pressed since the last check.
213 *
214 * @return Whether the button was pressed since the last check.
215 */
217
218 /**
219 * Whether the L1 button was released since the last check.
220 *
221 * @return Whether the button was released since the last check.
222 */
224
225 /**
226 * Constructs an event instance around the L1 button's digital signal.
227 *
228 * @param loop the event loop instance to attach the event to.
229 * @return an event instance representing the L1 button's digital signal
230 * attached to the given loop.
231 */
233
234 /**
235 * Read the value of the R1 button on the controller.
236 *
237 * @return The state of the button.
238 */
239 bool GetR1Button() const;
240
241 /**
242 * Whether the R1 button was pressed since the last check.
243 *
244 * @return Whether the button was pressed since the last check.
245 */
247
248 /**
249 * Whether the R1 button was released since the last check.
250 *
251 * @return Whether the button was released since the last check.
252 */
254
255 /**
256 * Constructs an event instance around the R1 button's digital signal.
257 *
258 * @param loop the event loop instance to attach the event to.
259 * @return an event instance representing the R1 button's digital signal
260 * attached to the given loop.
261 */
263
264 /**
265 * Read the value of the L2 button on the controller.
266 *
267 * @return The state of the button.
268 */
269 bool GetL2Button() const;
270
271 /**
272 * Whether the L2 button was pressed since the last check.
273 *
274 * @return Whether the button was pressed since the last check.
275 */
277
278 /**
279 * Whether the L2 button was released since the last check.
280 *
281 * @return Whether the button was released since the last check.
282 */
284
285 /**
286 * Constructs an event instance around the L2 button's digital signal.
287 *
288 * @param loop the event loop instance to attach the event to.
289 * @return an event instance representing the L2 button's digital signal
290 * attached to the given loop.
291 */
293
294 /**
295 * Read the value of the R2 button on the controller.
296 *
297 * @return The state of the button.
298 */
299 bool GetR2Button() const;
300
301 /**
302 * Whether the R2 button was pressed since the last check.
303 *
304 * @return Whether the button was pressed since the last check.
305 */
307
308 /**
309 * Whether the R2 button was released since the last check.
310 *
311 * @return Whether the button was released since the last check.
312 */
314
315 /**
316 * Constructs an event instance around the R2 button's digital signal.
317 *
318 * @param loop the event loop instance to attach the event to.
319 * @return an event instance representing the R2 button's digital signal
320 * attached to the given loop.
321 */
323
324 /**
325 * Read the value of the Share button on the controller.
326 *
327 * @return The state of the button.
328 */
329 bool GetShareButton() const;
330
331 /**
332 * Whether the Share button was pressed since the last check.
333 *
334 * @return Whether the button was pressed since the last check.
335 */
337
338 /**
339 * Whether the Share button was released since the last check.
340 *
341 * @return Whether the button was released since the last check.
342 */
344
345 /**
346 * Constructs an event instance around the share button's digital signal.
347 *
348 * @param loop the event loop instance to attach the event to.
349 * @return an event instance representing the share button's digital signal
350 * attached to the given loop.
351 */
353
354 /**
355 * Read the value of the Options button on the controller.
356 *
357 * @return The state of the button.
358 */
359 bool GetOptionsButton() const;
360
361 /**
362 * Whether the Options button was pressed since the last check.
363 *
364 * @return Whether the button was pressed since the last check.
365 */
367
368 /**
369 * Whether the Options button was released since the last check.
370 *
371 * @return Whether the button was released since the last check.
372 */
374
375 /**
376 * Constructs an event instance around the options button's digital signal.
377 *
378 * @param loop the event loop instance to attach the event to.
379 * @return an event instance representing the options button's digital signal
380 * attached to the given loop.
381 */
383
384 /**
385 * Read the value of the L3 button (pressing the left analog stick) on the
386 * controller.
387 *
388 * @return The state of the button.
389 */
390 bool GetL3Button() const;
391
392 /**
393 * Whether the L3 (left stick) button was pressed since the last check.
394 *
395 * @return Whether the button was pressed since the last check.
396 */
398
399 /**
400 * Whether the L3 (left stick) button was released since the last check.
401 *
402 * @return Whether the button was released since the last check.
403 */
405
406 /**
407 * Constructs an event instance around the L3 button's digital signal.
408 *
409 * @param loop the event loop instance to attach the event to.
410 * @return an event instance representing the L3 button's digital signal
411 * attached to the given loop.
412 */
414
415 /**
416 * Read the value of the R3 button (pressing the right analog stick) on the
417 * controller.
418 *
419 * @return The state of the button.
420 */
421 bool GetR3Button() const;
422
423 /**
424 * Whether the R3 (right stick) button was pressed since the last check.
425 *
426 * @return Whether the button was pressed since the last check.
427 */
429
430 /**
431 * Whether the R3 (right stick) button was released since the last check.
432 *
433 * @return Whether the button was released since the last check.
434 */
436
437 /**
438 * Constructs an event instance around the R3 button's digital signal.
439 *
440 * @param loop the event loop instance to attach the event to.
441 * @return an event instance representing the R3 button's digital signal
442 * attached to the given loop.
443 */
445
446 /**
447 * Read the value of the PS button on the controller.
448 *
449 * @return The state of the button.
450 */
451 bool GetPSButton() const;
452
453 /**
454 * Whether the PS button was pressed since the last check.
455 *
456 * @return Whether the button was pressed since the last check.
457 */
459
460 /**
461 * Whether the PS button was released since the last check.
462 *
463 * @return Whether the button was released since the last check.
464 */
466
467 /**
468 * Constructs an event instance around the PS button's digital signal.
469 *
470 * @param loop the event loop instance to attach the event to.
471 * @return an event instance representing the PS button's digital signal
472 * attached to the given loop.
473 */
475
476 /**
477 * Read the value of the touchpad button on the controller.
478 *
479 * @return The state of the button.
480 */
481 bool GetTouchpad() const;
482
483 /**
484 * Whether the touchpad was pressed since the last check.
485 *
486 * @return Whether the touchpad was pressed since the last check.
487 */
489
490 /**
491 * Whether the touchpad was released since the last check.
492 *
493 * @return Whether the touchpad was released since the last check.
494 */
496
497 /**
498 * Constructs an event instance around the touchpad's digital signal.
499 *
500 * @param loop the event loop instance to attach the event to.
501 * @return an event instance representing the touchpad's digital signal
502 * attached to the given loop.
503 */
505
506 /**
507 * Represents a digital button on a PS4Controller.
508 */
509 struct Button {
510 /// Square button.
511 static constexpr int kSquare = 1;
512 /// X button.
513 static constexpr int kCross = 2;
514 /// Circle button.
515 static constexpr int kCircle = 3;
516 /// Triangle button.
517 static constexpr int kTriangle = 4;
518 /// Left Trigger 1 button.
519 static constexpr int kL1 = 5;
520 /// Right Trigger 1 button.
521 static constexpr int kR1 = 6;
522 /// Left Trigger 2 button.
523 static constexpr int kL2 = 7;
524 /// Right Trigger 2 button.
525 static constexpr int kR2 = 8;
526 /// Share button.
527 static constexpr int kShare = 9;
528 /// Option button.
529 static constexpr int kOptions = 10;
530 /// Left stick button.
531 static constexpr int kL3 = 11;
532 /// Right stick button.
533 static constexpr int kR3 = 12;
534 /// PlayStation button.
535 static constexpr int kPS = 13;
536 /// Touchpad click button.
537 static constexpr int kTouchpad = 14;
538 };
539
540 /**
541 * Represents an axis on a PS4Controller.
542 */
543 struct Axis {
544 /// Left X axis.
545 static constexpr int kLeftX = 0;
546 /// Left Y axis.
547 static constexpr int kLeftY = 1;
548 /// Right X axis.
549 static constexpr int kRightX = 2;
550 /// Right Y axis.
551 static constexpr int kRightY = 5;
552 /// Left Trigger 2.
553 static constexpr int kL2 = 3;
554 /// Right Trigger 2.
555 static constexpr int kR2 = 4;
556 };
557};
558
559} // 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 PS4 controllers connected to the Driver Station.
Definition: PS4Controller.h:23
bool GetR1ButtonPressed()
Whether the R1 button was pressed since the last check.
bool GetR2Button() const
Read the value of the R2 button on the controller.
BooleanEvent Options(EventLoop *loop) const
Constructs an event instance around the options button's digital signal.
BooleanEvent Square(EventLoop *loop) const
Constructs an event instance around the square button's digital signal.
bool GetSquareButton() const
Read the value of the Square button on the controller.
bool GetL1Button() const
Read the value of the L1 button on the controller.
bool GetR2ButtonReleased()
Whether the R2 button was released since the last check.
BooleanEvent Circle(EventLoop *loop) const
Constructs an event instance around the circle button's digital signal.
bool GetTouchpad() const
Read the value of the touchpad button on the controller.
BooleanEvent Triangle(EventLoop *loop) const
Constructs an event instance around the triangle button's digital signal.
bool GetSquareButtonReleased()
Whether the Square button was released since the last check.
double GetRightY() const
Get the Y axis value of right side of the controller.
bool GetShareButtonReleased()
Whether the Share button was released since the last check.
BooleanEvent Cross(EventLoop *loop) const
Constructs an event instance around the cross button's digital signal.
bool GetR3ButtonPressed()
Whether the R3 (right stick) button was pressed since the last check.
bool GetL3ButtonReleased()
Whether the L3 (left stick) button was released since the last check.
bool GetL3Button() const
Read the value of the L3 button (pressing the left analog stick) on the controller.
bool GetPSButtonPressed()
Whether the PS button was pressed since the last check.
BooleanEvent R3(EventLoop *loop) const
Constructs an event instance around the R3 button's digital signal.
bool GetPSButton() const
Read the value of the PS button on the controller.
BooleanEvent R1(EventLoop *loop) const
Constructs an event instance around the R1 button's digital signal.
double GetRightX() const
Get the X axis value of right side of the controller.
bool GetL3ButtonPressed()
Whether the L3 (left stick) button was pressed since the last check.
bool GetR1ButtonReleased()
Whether the R1 button was released since the last check.
bool GetOptionsButtonPressed()
Whether the Options button was pressed since the last check.
BooleanEvent R2(EventLoop *loop) const
Constructs an event instance around the R2 button's digital signal.
bool GetL1ButtonReleased()
Whether the L1 button was released since the last check.
double GetLeftY() const
Get the Y axis value of left side of the controller.
bool GetCrossButtonPressed()
Whether the Cross button was pressed since the last check.
bool GetShareButton() const
Read the value of the Share button on the controller.
BooleanEvent Share(EventLoop *loop) const
Constructs an event instance around the share button's digital signal.
bool GetPSButtonReleased()
Whether the PS button was released since the last check.
bool GetCrossButton() const
Read the value of the Cross button on the controller.
bool GetCircleButton() const
Read the value of the Circle button on the controller.
bool GetCrossButtonReleased()
Whether the Cross button was released since the last check.
double GetLeftX() const
Get the X axis value of left side of the controller.
bool GetR1Button() const
Read the value of the R1 button on the controller.
BooleanEvent Touchpad(EventLoop *loop) const
Constructs an event instance around the touchpad's digital signal.
PS4Controller(int port)
Construct an instance of an PS4 controller.
bool GetTriangleButton() const
Read the value of the Triangle button on the controller.
BooleanEvent L1(EventLoop *loop) const
Constructs an event instance around the L1 button's digital signal.
BooleanEvent PS(EventLoop *loop) const
Constructs an event instance around the PS button's digital signal.
bool GetTriangleButtonReleased()
Whether the Triangle button was released since the last check.
bool GetL1ButtonPressed()
Whether the L1 button was pressed since the last check.
bool GetSquareButtonPressed()
Whether the Square button was pressed since the last check.
bool GetCircleButtonPressed()
Whether the Circle button was pressed since the last check.
bool GetTouchpadReleased()
Whether the touchpad was released since the last check.
bool GetTriangleButtonPressed()
Whether the Triangle button was pressed since the last check.
bool GetR3Button() const
Read the value of the R3 button (pressing the right analog stick) on the controller.
bool GetOptionsButton() const
Read the value of the Options button on the controller.
double GetR2Axis() const
Get the R2 axis value of the controller.
bool GetL2Button() const
Read the value of the L2 button on the controller.
bool GetL2ButtonReleased()
Whether the L2 button was released since the last check.
bool GetOptionsButtonReleased()
Whether the Options button was released since the last check.
bool GetR3ButtonReleased()
Whether the R3 (right stick) button was released since the last check.
bool GetL2ButtonPressed()
Whether the L2 button was pressed since the last check.
bool GetShareButtonPressed()
Whether the Share button was pressed since the last check.
bool GetR2ButtonPressed()
Whether the R2 button was pressed since the last check.
PS4Controller(PS4Controller &&)=default
PS4Controller & operator=(PS4Controller &&)=default
BooleanEvent L3(EventLoop *loop) const
Constructs an event instance around the L3 button's digital signal.
bool GetTouchpadPressed()
Whether the touchpad was pressed since the last check.
~PS4Controller() override=default
bool GetCircleButtonReleased()
Whether the Circle button was released since the last check.
double GetL2Axis() const
Get the L2 axis value of the controller.
BooleanEvent L2(EventLoop *loop) const
Constructs an event instance around the L2 button's digital signal.
Definition: AprilTagPoseEstimator.h:15
Represents an axis on a PS4Controller.
Definition: PS4Controller.h:543
static constexpr int kRightY
Right Y axis.
Definition: PS4Controller.h:551
static constexpr int kR2
Right Trigger 2.
Definition: PS4Controller.h:555
static constexpr int kLeftX
Left X axis.
Definition: PS4Controller.h:545
static constexpr int kRightX
Right X axis.
Definition: PS4Controller.h:549
static constexpr int kL2
Left Trigger 2.
Definition: PS4Controller.h:553
static constexpr int kLeftY
Left Y axis.
Definition: PS4Controller.h:547
Represents a digital button on a PS4Controller.
Definition: PS4Controller.h:509
static constexpr int kTouchpad
Touchpad click button.
Definition: PS4Controller.h:537
static constexpr int kL3
Left stick button.
Definition: PS4Controller.h:531
static constexpr int kR1
Right Trigger 1 button.
Definition: PS4Controller.h:521
static constexpr int kTriangle
Triangle button.
Definition: PS4Controller.h:517
static constexpr int kSquare
Square button.
Definition: PS4Controller.h:511
static constexpr int kOptions
Option button.
Definition: PS4Controller.h:529
static constexpr int kShare
Share button.
Definition: PS4Controller.h:527
static constexpr int kR2
Right Trigger 2 button.
Definition: PS4Controller.h:525
static constexpr int kCircle
Circle button.
Definition: PS4Controller.h:515
static constexpr int kR3
Right stick button.
Definition: PS4Controller.h:533
static constexpr int kL2
Left Trigger 2 button.
Definition: PS4Controller.h:523
static constexpr int kCross
X button.
Definition: PS4Controller.h:513
static constexpr int kPS
PlayStation button.
Definition: PS4Controller.h:535
static constexpr int kL1
Left Trigger 1 button.
Definition: PS4Controller.h:519