2023-Robot
Robot code for 2023 FRC season by Argos, FRC team #1756
Loading...
Searching...
No Matches
xbox_controller.h
Go to the documentation of this file.
1
4
5#pragma once
6
7#include <frc/GenericHID.h>
8#include <frc2/command/button/Trigger.h>
9
10#include <array>
11#include <vector>
12
14#include "vibration.h"
15
16namespace argos_lib {
17
18 class XboxController : public frc::GenericHID {
19 public:
24
25 enum class Button {
26 kA = 1,
27 kB = 2,
28 kX = 3,
29 kY = 4,
30 kBumperLeft = 5,
31 kBumperRight = 6,
32 kBack = 7,
33 kStart = 8,
34 kStickLeft = 9,
35 kStickRight = 10,
36 kLeftTrigger = 11,
37 kRightTrigger = 12,
38 kUp = 13,
39 kRight = 14,
40 kDown = 15,
41 kLeft = 16,
42 COUNT
43 };
44
48 struct UpdateStatus {
49 bool pressed = false;
50 bool released = false;
51 bool debouncePress = false;
52 bool debounceRelease = false;
53 bool rawActive = false;
54 bool debounceActive = false;
55 };
56
57 enum class Axis { kLeftX = 0, kLeftY = 1, kLeftTrigger = 2, kRightTrigger = 3, kRightX = 4, kRightY = 5, COUNT };
58
59 XboxController() = delete;
65 explicit XboxController(int port);
66
73 void SetButtonDebounce(Button targetButton, DebounceSettings newSettings);
74
81 void SwapSettings(XboxController& other);
82
89 [[nodiscard]] double GetX(JoystickHand hand) const;
96 [[nodiscard]] double GetY(JoystickHand hand) const;
103 [[nodiscard]] double GetTriggerAxis(JoystickHand hand) const;
104
111 [[nodiscard]] bool GetDebouncedButton(Button buttonIdx);
118 [[nodiscard]] bool GetDebouncedButtonPressed(Button buttonIdx);
125 [[nodiscard]] bool GetDebouncedButtonReleased(Button buttonIdx);
126
135 [[nodiscard]] bool GetDebouncedButton(std::vector<Button> buttonCombo);
144 [[nodiscard]] bool GetDebouncedButtonPressed(std::vector<Button> buttonCombo);
153 [[nodiscard]] bool GetDebouncedButtonReleased(std::vector<Button> buttonCombo);
154
161 [[nodiscard]] bool GetRawButton(Button buttonIdx);
168 [[nodiscard]] bool GetRawButtonPressed(Button buttonIdx);
175 [[nodiscard]] bool GetRawButtonReleased(Button buttonIdx);
176
185 [[nodiscard]] bool GetRawButton(std::vector<Button> buttonCombo);
193 [[nodiscard]] bool GetRawButtonPressed(std::vector<Button> buttonCombo);
201 [[nodiscard]] bool GetRawButtonReleased(std::vector<Button> buttonCombo);
202
209
215 void SetVibration(VibrationModel newVibrationModel);
216
220 void UpdateVibration();
221
230
237 [[nodiscard]] frc2::Trigger TriggerRaw(Button button);
238
245 [[nodiscard]] frc2::Trigger TriggerRaw(std::vector<Button> buttonCombo);
246
253 [[nodiscard]] frc2::Trigger TriggerRawAnyOf(std::vector<Button> buttonCombo);
254
261 [[nodiscard]] frc2::Trigger TriggerRawAllOf(std::vector<Button> buttonCombo);
262
269 [[nodiscard]] frc2::Trigger TriggerRawNoneOf(std::vector<Button> buttonCombo);
270
277 [[nodiscard]] frc2::Trigger TriggerRawOneOf(std::vector<Button> buttonCombo);
278
285 [[nodiscard]] frc2::Trigger TriggerDebounced(Button button);
286
293 [[nodiscard]] frc2::Trigger TriggerDebounced(std::vector<Button> buttonCombo);
294
301 [[nodiscard]] frc2::Trigger TriggerDebouncedAnyOf(std::vector<Button> buttonCombo);
302
309 [[nodiscard]] frc2::Trigger TriggerDebouncedAllOf(std::vector<Button> buttonCombo);
310
317 [[nodiscard]] frc2::Trigger TriggerDebouncedNoneOf(std::vector<Button> buttonCombo);
318
325 [[nodiscard]] frc2::Trigger TriggerDebouncedOneOf(std::vector<Button> buttonCombo);
326
327 private:
331 struct DPadButtons {
332 bool up = false;
333 bool right = false;
334 bool down = false;
335 bool left = false;
336 };
337
344
352 [[nodiscard]] frc2::Trigger TriggerAnyOf(std::vector<Button> buttonCombo,
353 std::function<bool(Button)> buttonGetterFunc);
354
362 [[nodiscard]] frc2::Trigger TriggerAllOf(std::vector<Button> buttonCombo,
363 std::function<bool(Button)> buttonGetterFunc);
364
372 [[nodiscard]] frc2::Trigger TriggerNoneOf(std::vector<Button> buttonCombo,
373 std::function<bool(Button)> buttonGetterFunc);
374
382 [[nodiscard]] frc2::Trigger TriggerOneOf(std::vector<Button> buttonCombo,
383 std::function<bool(Button)> buttonGetterFunc);
384
385 constexpr static double analogTriggerThresh = 0.5;
386
387 std::array<DebounceSettings, static_cast<int>(Button::COUNT)> m_buttonDebounceSettings;
388 std::array<bool, static_cast<int>(Button::COUNT)> m_buttonDebounceStatus;
389 std::array<bool, static_cast<int>(Button::COUNT)> m_rawButtonStatus;
390 std::array<std::chrono::time_point<std::chrono::steady_clock>, static_cast<int>(Button::COUNT)>
392
394 };
395
396} // namespace argos_lib
Definition xbox_controller.h:18
void SetVibration(VibrationModel newVibrationModel)
Sets a new vibration pattern and updates vibration output based on that new model.
Definition xbox_controller.cpp:135
frc2::Trigger TriggerRaw(Button button)
Generates a trigger that is true when button is true.
Definition xbox_controller.cpp:229
Button
Definition xbox_controller.h:25
frc2::Trigger TriggerRawNoneOf(std::vector< Button > buttonCombo)
Trigger when none of the selected buttons' raw value is true.
Definition xbox_controller.cpp:245
bool GetDebouncedButtonReleased(Button buttonIdx)
Detect if a button just transitioned from active to inactive after applying debounce.
Definition xbox_controller.cpp:51
frc2::Trigger TriggerOneOf(std::vector< Button > buttonCombo, std::function< bool(Button)> buttonGetterFunc)
Trigger when exactly one of the selected buttons is true according to the supplied buttonGetterFunc.
Definition xbox_controller.cpp:310
std::array< bool, static_cast< int >(Button::COUNT)> m_buttonDebounceStatus
Definition xbox_controller.h:388
void SwapSettings(XboxController &other)
Swap all configurations (debounce, etc) between this and other controller. Useful in conjunction with...
Definition xbox_controller.cpp:20
VibrationModel m_vibrationModel
Active vibration model.
Definition xbox_controller.h:393
void UpdateVibration()
Update vibration output based on current vibration model.
Definition xbox_controller.cpp:140
frc2::Trigger TriggerNoneOf(std::vector< Button > buttonCombo, std::function< bool(Button)> buttonGetterFunc)
Trigger when none of the selected buttons is true according to the supplied buttonGetterFunc.
Definition xbox_controller.cpp:299
frc2::Trigger TriggerRawAllOf(std::vector< Button > buttonCombo)
Trigger when all of the selected buttons' raw values are true.
Definition xbox_controller.cpp:241
frc2::Trigger TriggerAnyOf(std::vector< Button > buttonCombo, std::function< bool(Button)> buttonGetterFunc)
Trigger when any of the selected buttons is true according to the supplied buttonGetterFunc.
Definition xbox_controller.cpp:277
std::array< std::chrono::time_point< std::chrono::steady_clock >, static_cast< int >(Button::COUNT)> m_buttonDebounceTransitionTime
Time when new value was first seen.
Definition xbox_controller.h:391
frc2::Trigger TriggerDebouncedNoneOf(std::vector< Button > buttonCombo)
Trigger when none of the selected buttons' debounced value is true.
Definition xbox_controller.cpp:269
std::array< bool, static_cast< int >(Button::COUNT)> m_rawButtonStatus
Definition xbox_controller.h:389
JoystickHand
Replaces legacy joystick hand API for WPILib.
Definition xbox_controller.h:23
bool GetRawButtonPressed(Button buttonIdx)
Detect if a button just transitioned from inactive to active.
Definition xbox_controller.cpp:91
UpdateStatus UpdateButton(Button buttonIdx)
Determines the new status of a button. This is used by the other status retrieval functions.
Definition xbox_controller.cpp:146
frc2::Trigger TriggerRawAnyOf(std::vector< Button > buttonCombo)
Trigger when any of the selected buttons' raw value is true.
Definition xbox_controller.cpp:237
std::array< DebounceSettings, static_cast< int >(Button::COUNT)> m_buttonDebounceSettings
Definition xbox_controller.h:387
static constexpr double analogTriggerThresh
Percent trigger pressed to consider as a button press.
Definition xbox_controller.h:385
frc2::Trigger TriggerAllOf(std::vector< Button > buttonCombo, std::function< bool(Button)> buttonGetterFunc)
Trigger when all of the selected buttons is true according to the supplied buttonGetterFunc.
Definition xbox_controller.cpp:288
frc2::Trigger TriggerDebouncedAnyOf(std::vector< Button > buttonCombo)
Trigger when any of the selected buttons' debounced value is true.
Definition xbox_controller.cpp:261
double GetY(JoystickHand hand) const
Get Y joystick percent from specified joystick.
Definition xbox_controller.cpp:35
frc2::Trigger TriggerDebouncedOneOf(std::vector< Button > buttonCombo)
Trigger when exactly one of the selected buttons' debounced value is true.
Definition xbox_controller.cpp:273
VibrationModel GetVibration() const
Get the active vibration model.
Definition xbox_controller.cpp:131
bool GetRawButtonReleased(Button buttonIdx)
Detect if a button just transitioned from active to inactive.
Definition xbox_controller.cpp:95
frc2::Trigger TriggerDebounced(Button button)
Generates a trigger that is true when debounced button is true.
Definition xbox_controller.cpp:253
DPadButtons GetPOVButtons()
Convert POV angle to usable DPad button values.
Definition xbox_controller.cpp:344
bool GetRawButton(Button buttonIdx)
Get the status of button.
Definition xbox_controller.cpp:87
double GetTriggerAxis(JoystickHand hand) const
Get percent from specified controller trigger button.
Definition xbox_controller.cpp:39
bool GetDebouncedButton(Button buttonIdx)
Get the status of button after applying debounce.
Definition xbox_controller.cpp:43
void SetButtonDebounce(Button targetButton, DebounceSettings newSettings)
Configure debounce for a specified button.
Definition xbox_controller.cpp:16
frc2::Trigger TriggerRawOneOf(std::vector< Button > buttonCombo)
Trigger when exactly one of the selected buttons' raw value is true.
Definition xbox_controller.cpp:249
double GetX(JoystickHand hand) const
Get X joystick percent from specified joystick.
Definition xbox_controller.cpp:31
frc2::Trigger TriggerDebouncedAllOf(std::vector< Button > buttonCombo)
Trigger when all of the selected buttons' debounced values are true.
Definition xbox_controller.cpp:265
bool GetDebouncedButtonPressed(Button buttonIdx)
Detect if a button just transitioned from inactive to active after applying debounce.
Definition xbox_controller.cpp:47
Axis
Definition xbox_controller.h:57
Definition swap_controllers_command.h:12
std::function< VibrationStatus()> VibrationModel
Definition vibration.h:21
Definition debounce_settings.h:11
Parsed directional pad button states.
Definition xbox_controller.h:331
bool left
Left active (including adjacent diagonals)
Definition xbox_controller.h:335
bool down
Down active (including adjacent diagonals)
Definition xbox_controller.h:334
bool up
Up active (including adjacent diagonals)
Definition xbox_controller.h:332
bool right
Right active (including adjacent diagonals)
Definition xbox_controller.h:333
State of an individual button.
Definition xbox_controller.h:48
bool debounceActive
Button status after debounce applied.
Definition xbox_controller.h:54
bool pressed
Transitioned from inactive to active.
Definition xbox_controller.h:49
bool debouncePress
Transitioned from inactive to active after debounce applied.
Definition xbox_controller.h:51
bool debounceRelease
Transitioned from active to inactive after debounce applied.
Definition xbox_controller.h:52
bool rawActive
Raw button status.
Definition xbox_controller.h:53
bool released
Transitioned from active to inactive.
Definition xbox_controller.h:50