Valkyrie 2026
Loading...
Searching...
No Matches
Gamepad.h
1#pragma once
2
3#include <frc/XboxController.h>
4#include <frc/filter/LinearFilter.h>
5#include <units/angle.h>
6
7#include "valkyrie/sensors/BaseSensor.h"
8
9namespace valor {
10
24class Gamepad : public frc::XboxController {
25 public:
37 class AnalogValue : public sensors::BaseSensor<double> {
38 public:
46 void SetGetter(std::function<double()> getter);
47
51 inline void Reset() override { filter.Reset(); }
52
61 inline bool Active() const { return Get() != 0; }
62
71 inline AnalogValue& SetDeadband(double deadband) {
72 this->deadband = deadband;
73 return *this;
74 }
75
86 inline AnalogValue& SetRange(double min, double max = 1) {
87 minValue = min;
88 maxValue = max;
89 return *this;
90 }
91
97 inline double GetDeadband() const { return deadband; }
98
104 inline double GetMaxValue() const { return maxValue; }
105
111 inline double GetMinValue() const { return minValue; }
112
123 inline AnalogValue& SetPolynomial(int poly) {
124 polynomial = poly;
125 return *this;
126 }
127
133 inline int GetPolynomial() const { return polynomial; }
134
140 inline void Calculate() override { filter.Calculate(Get()); }
141
147 inline double GetFiltered() const { return filter.LastValue(); }
148
154 inline AnalogValue& SetFilterSize(int size) {
155 filter = frc::LinearFilter<double>::MovingAverage(size);
156 return *this;
157 }
158
166 inline AnalogValue& SetFilterTime(units::second_t time) { return SetFilterSize(time / LOOP_PERIOD); }
167
168 private:
170 double deadband = 0;
171
173 double minValue = 0;
174
176 double maxValue = 1;
177
179 int polynomial = 1;
180
186 frc::LinearFilter<double> filter = frc::LinearFilter<double>::MovingAverage(1);
187 };
188
194 explicit Gamepad(int id);
195
202 inline bool DPadUp() const { return GetPOV() == 0; }
203
210 inline bool DPadDown() const { return GetPOV() == 180; }
211
218 inline bool DPadLeft() const { return GetPOV() == 270; }
219
226 inline bool DPadRight() const { return GetPOV() == 90; }
227
234 void SetRumble(bool turnOn, double intensity = .25);
235
241 constexpr AnalogValue& LeftStickX() { return leftStickX; }
242
248 constexpr const AnalogValue& LeftStickX() const { return leftStickX; }
249
255 constexpr AnalogValue& LeftStickY() { return leftStickY; }
256
262 constexpr const AnalogValue& LeftStickY() const { return leftStickY; }
263
269 constexpr AnalogValue& RightStickX() { return rightStickX; }
270
276 constexpr const AnalogValue& RightStickX() const { return rightStickX; }
277
283 constexpr AnalogValue& RightStickY() { return rightStickY; }
284
290 constexpr const AnalogValue& RightStickY() const { return rightStickY; }
291
297 constexpr AnalogValue& LeftTrigger() { return leftTrigger; }
298
304 constexpr const AnalogValue& LeftTrigger() const { return leftTrigger; }
305
311 constexpr AnalogValue& RightTrigger() { return rightTrigger; }
312
318 constexpr const AnalogValue& RightTrigger() const { return rightTrigger; }
319
320 private:
322 AnalogValue leftStickX;
323
325 AnalogValue leftStickY;
326
328 AnalogValue rightStickX;
329
331 AnalogValue rightStickY;
332
334 AnalogValue leftTrigger;
335
337 AnalogValue rightTrigger;
338};
339} // namespace valor
Represents a processed analog controller input.
Definition Gamepad.h:37
void Reset() override
Resets the internal filter state.
Definition Gamepad.h:51
bool Active() const
Checks whether the analog input is currently active.
Definition Gamepad.h:61
AnalogValue & SetFilterTime(units::second_t time)
Sets the moving-average filter duration.
Definition Gamepad.h:166
int GetPolynomial() const
Gets the configured polynomial exponent.
Definition Gamepad.h:133
AnalogValue & SetPolynomial(int poly)
Sets the polynomial response curve exponent.
Definition Gamepad.h:123
double GetFiltered() const
Gets the filtered analog value.
Definition Gamepad.h:147
void Calculate() override
Updates the internal filter with the current processed value.
Definition Gamepad.h:140
AnalogValue & SetFilterSize(int size)
Sets the moving-average filter window size.
Definition Gamepad.h:154
double GetMaxValue() const
Gets the configured maximum output value.
Definition Gamepad.h:104
void SetGetter(std::function< double()> getter)
Sets the function used to retrieve the raw input value.
double GetDeadband() const
Gets the configured deadband threshold.
Definition Gamepad.h:97
AnalogValue & SetRange(double min, double max=1)
Sets the output range of the processed analog input.
Definition Gamepad.h:86
AnalogValue & SetDeadband(double deadband)
Sets the deadband applied to the input.
Definition Gamepad.h:71
double GetMinValue() const
Gets the configured minimum output value.
Definition Gamepad.h:111
bool DPadUp() const
Is the gamepad's D-pad up button pressed?
Definition Gamepad.h:202
constexpr AnalogValue & LeftTrigger()
Gets the processed left trigger input.
Definition Gamepad.h:297
constexpr const AnalogValue & LeftTrigger() const
Gets the processed left trigger input.
Definition Gamepad.h:304
void SetRumble(bool turnOn, double intensity=.25)
Sets the rumble for the gamepad.
Gamepad(int id)
Construct a new Valor Gamepad object.
constexpr AnalogValue & LeftStickX()
Gets the processed left stick X-axis input.
Definition Gamepad.h:241
constexpr const AnalogValue & RightTrigger() const
Gets the processed right trigger input.
Definition Gamepad.h:318
bool DPadRight() const
Is the gamepad's D-pad right button pressed?
Definition Gamepad.h:226
constexpr AnalogValue & RightStickY()
Gets the processed right stick Y-axis input.
Definition Gamepad.h:283
constexpr const AnalogValue & LeftStickY() const
Gets the processed left stick Y-axis input.
Definition Gamepad.h:262
constexpr const AnalogValue & RightStickY() const
Gets the processed right stick Y-axis input.
Definition Gamepad.h:290
constexpr const AnalogValue & RightStickX() const
Gets the processed right stick X-axis input.
Definition Gamepad.h:276
constexpr const AnalogValue & LeftStickX() const
Gets the processed left stick X-axis input.
Definition Gamepad.h:248
constexpr AnalogValue & LeftStickY()
Gets the processed left stick Y-axis input.
Definition Gamepad.h:255
bool DPadDown() const
Is the gamepad's D-pad down button pressed?
Definition Gamepad.h:210
constexpr AnalogValue & RightTrigger()
Gets the processed right trigger input.
Definition Gamepad.h:311
constexpr AnalogValue & RightStickX()
Gets the processed right stick X-axis input.
Definition Gamepad.h:269
bool DPadLeft() const
Is the gamepad's D-pad left button pressed?
Definition Gamepad.h:218
Abstract class that all Valor sensors should implement.
Definition BaseSensor.h:52
double Get() const
Definition BaseSensor.h:91