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#include <valkyrie/sensors/BaseSensor.h>
7
8namespace valor {
9
23class Gamepad : public frc::XboxController {
24 public:
36 class AnalogValue : public sensors::BaseSensor<double> {
37 public:
45 void SetGetter(std::function<double()> getter);
46
50 inline void Reset() override { filter.Reset(); }
51
60 inline bool Active() const { return Get() != 0; }
61
70 inline AnalogValue& SetDeadband(double deadband) {
71 this->deadband = deadband;
72 return *this;
73 }
74
85 inline AnalogValue& SetRange(double min, double max = 1) {
86 minValue = min;
87 maxValue = max;
88 return *this;
89 }
90
96 inline double GetDeadband() const { return deadband; }
97
103 inline double GetMaxValue() const { return maxValue; }
104
110 inline double GetMinValue() const { return minValue; }
111
122 inline AnalogValue& SetPolynomial(int poly) {
123 polynomial = poly;
124 return *this;
125 }
126
132 inline int GetPolynomial() const { return polynomial; }
133
139 inline void Calculate() override { filter.Calculate(Get()); }
140
146 inline double GetFiltered() const { return filter.LastValue(); }
147
153 inline AnalogValue& SetFilterSize(int size) {
154 filter = frc::LinearFilter<double>::MovingAverage(size);
155 return *this;
156 }
157
165 inline AnalogValue& SetFilterTime(units::second_t time) { return SetFilterSize(time / LOOP_PERIOD); }
166
167 private:
169 double deadband = 0;
170
172 double minValue = 0;
173
175 double maxValue = 1;
176
178 int polynomial = 1;
179
185 frc::LinearFilter<double> filter = frc::LinearFilter<double>::MovingAverage(1);
186 };
187
193 explicit Gamepad(int id);
194
201 inline bool DPadUp() const { return GetPOV() == 0; }
202
209 inline bool DPadDown() const { return GetPOV() == 180; }
210
217 inline bool DPadLeft() const { return GetPOV() == 270; }
218
225 inline bool DPadRight() const { return GetPOV() == 90; }
226
232 constexpr AnalogValue& LeftStickX() { return leftStickX; }
233
239 constexpr const AnalogValue& LeftStickX() const { return leftStickX; }
240
246 constexpr AnalogValue& LeftStickY() { return leftStickY; }
247
253 constexpr const AnalogValue& LeftStickY() const { return leftStickY; }
254
260 constexpr AnalogValue& RightStickX() { return rightStickX; }
261
267 constexpr const AnalogValue& RightStickX() const { return rightStickX; }
268
274 constexpr AnalogValue& RightStickY() { return rightStickY; }
275
281 constexpr const AnalogValue& RightStickY() const { return rightStickY; }
282
288 constexpr AnalogValue& LeftTrigger() { return leftTrigger; }
289
295 constexpr const AnalogValue& LeftTrigger() const { return leftTrigger; }
296
302 constexpr AnalogValue& RightTrigger() { return rightTrigger; }
303
309 constexpr const AnalogValue& RightTrigger() const { return rightTrigger; }
310
311 private:
313 AnalogValue leftStickX;
314
316 AnalogValue leftStickY;
317
319 AnalogValue rightStickX;
320
322 AnalogValue rightStickY;
323
325 AnalogValue leftTrigger;
326
328 AnalogValue rightTrigger;
329};
330} // namespace valor
Represents a processed analog controller input.
Definition Gamepad.h:36
void Reset() override
Resets the internal filter state.
Definition Gamepad.h:50
bool Active() const
Checks whether the analog input is currently active.
Definition Gamepad.h:60
AnalogValue & SetFilterTime(units::second_t time)
Sets the moving-average filter duration.
Definition Gamepad.h:165
int GetPolynomial() const
Gets the configured polynomial exponent.
Definition Gamepad.h:132
AnalogValue & SetPolynomial(int poly)
Sets the polynomial response curve exponent.
Definition Gamepad.h:122
double GetFiltered() const
Gets the filtered analog value.
Definition Gamepad.h:146
void Calculate() override
Updates the internal filter with the current processed value.
Definition Gamepad.h:139
AnalogValue & SetFilterSize(int size)
Sets the moving-average filter window size.
Definition Gamepad.h:153
double GetMaxValue() const
Gets the configured maximum output value.
Definition Gamepad.h:103
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:96
AnalogValue & SetRange(double min, double max=1)
Sets the output range of the processed analog input.
Definition Gamepad.h:85
AnalogValue & SetDeadband(double deadband)
Sets the deadband applied to the input.
Definition Gamepad.h:70
double GetMinValue() const
Gets the configured minimum output value.
Definition Gamepad.h:110
bool DPadUp() const
Is the gamepad's D-pad up button pressed?
Definition Gamepad.h:201
constexpr AnalogValue & LeftTrigger()
Gets the processed left trigger input.
Definition Gamepad.h:288
constexpr const AnalogValue & LeftTrigger() const
Gets the processed left trigger input.
Definition Gamepad.h:295
Gamepad(int id)
Construct a new Valor Gamepad object.
constexpr AnalogValue & LeftStickX()
Gets the processed left stick X-axis input.
Definition Gamepad.h:232
constexpr const AnalogValue & RightTrigger() const
Gets the processed right trigger input.
Definition Gamepad.h:309
bool DPadRight() const
Is the gamepad's D-pad right button pressed?
Definition Gamepad.h:225
constexpr AnalogValue & RightStickY()
Gets the processed right stick Y-axis input.
Definition Gamepad.h:274
constexpr const AnalogValue & LeftStickY() const
Gets the processed left stick Y-axis input.
Definition Gamepad.h:253
constexpr const AnalogValue & RightStickY() const
Gets the processed right stick Y-axis input.
Definition Gamepad.h:281
constexpr const AnalogValue & RightStickX() const
Gets the processed right stick X-axis input.
Definition Gamepad.h:267
constexpr const AnalogValue & LeftStickX() const
Gets the processed left stick X-axis input.
Definition Gamepad.h:239
constexpr AnalogValue & LeftStickY()
Gets the processed left stick Y-axis input.
Definition Gamepad.h:246
bool DPadDown() const
Is the gamepad's D-pad down button pressed?
Definition Gamepad.h:209
constexpr AnalogValue & RightTrigger()
Gets the processed right trigger input.
Definition Gamepad.h:302
constexpr AnalogValue & RightStickX()
Gets the processed right stick X-axis input.
Definition Gamepad.h:260
bool DPadLeft() const
Is the gamepad's D-pad left button pressed?
Definition Gamepad.h:217
Abstract class that all Valor sensors should implement.
Definition BaseSensor.h:50
double Get() const
Definition BaseSensor.h:89