[wpilib] Add a default deadband to all gamepads (#8897)

The FTC DS had a deadband. Additionally, the FRC one had an implicit
deadband due to the only 8 bit resolution. We need a deadband by default
now with the high resolution gamepads.
This commit is contained in:
Thad House
2026-05-29 16:31:48 -07:00
committed by GitHub
parent 40fdb779d8
commit 635e971a02
5 changed files with 233 additions and 16 deletions

View File

@@ -4,39 +4,71 @@
#include "wpi/driverstation/Gamepad.hpp"
#include <algorithm>
#include <cmath>
#include "wpi/event/BooleanEvent.hpp"
#include "wpi/hal/UsageReporting.hpp"
#include "wpi/math/util/MathUtil.hpp"
#include "wpi/util/sendable/SendableBuilder.hpp"
using namespace wpi;
static double ClampDeadband(double deadband) {
if (std::isnan(deadband)) {
return 0.0;
}
return std::clamp(deadband, 0.0, std::nextafter(1.0, 0.0));
}
Gamepad::Gamepad(int port) : GenericHID(port) {
HAL_ReportUsage("HID", port, "Gamepad");
}
double Gamepad::GetLeftX() const {
return GetAxis(Axis::LEFT_X);
return wpi::math::ApplyDeadband(GetAxis(Axis::LEFT_X), m_leftXDeadband);
}
void Gamepad::SetLeftXDeadband(double deadband) {
m_leftXDeadband = ClampDeadband(deadband);
}
double Gamepad::GetLeftY() const {
return GetAxis(Axis::LEFT_Y);
return wpi::math::ApplyDeadband(GetAxis(Axis::LEFT_Y), m_leftYDeadband);
}
void Gamepad::SetLeftYDeadband(double deadband) {
m_leftYDeadband = ClampDeadband(deadband);
}
double Gamepad::GetRightX() const {
return GetAxis(Axis::RIGHT_X);
return wpi::math::ApplyDeadband(GetAxis(Axis::RIGHT_X), m_rightXDeadband);
}
void Gamepad::SetRightXDeadband(double deadband) {
m_rightXDeadband = ClampDeadband(deadband);
}
double Gamepad::GetRightY() const {
return GetAxis(Axis::RIGHT_Y);
return wpi::math::ApplyDeadband(GetAxis(Axis::RIGHT_Y), m_rightYDeadband);
}
void Gamepad::SetRightYDeadband(double deadband) {
m_rightYDeadband = ClampDeadband(deadband);
}
double Gamepad::GetLeftTriggerAxis() const {
return GetAxis(Axis::LEFT_TRIGGER);
return wpi::math::ApplyDeadband(GetAxis(Axis::LEFT_TRIGGER),
m_leftTriggerDeadband);
}
void Gamepad::SetLeftTriggerDeadband(double deadband) {
m_leftTriggerDeadband = ClampDeadband(deadband);
}
BooleanEvent Gamepad::LeftTrigger(double threshold, EventLoop* loop) const {
return BooleanEvent(loop, [this, threshold] {
return this->GetLeftTriggerAxis() > threshold;
return this->GetAxis(Axis::LEFT_TRIGGER) > threshold;
});
}
@@ -45,12 +77,17 @@ BooleanEvent Gamepad::LeftTrigger(EventLoop* loop) const {
}
double Gamepad::GetRightTriggerAxis() const {
return GetAxis(Axis::RIGHT_TRIGGER);
return wpi::math::ApplyDeadband(GetAxis(Axis::RIGHT_TRIGGER),
m_rightTriggerDeadband);
}
void Gamepad::SetRightTriggerDeadband(double deadband) {
m_rightTriggerDeadband = ClampDeadband(deadband);
}
BooleanEvent Gamepad::RightTrigger(double threshold, EventLoop* loop) const {
return BooleanEvent(loop, [this, threshold] {
return this->GetRightTriggerAxis() > threshold;
return this->GetAxis(Axis::RIGHT_TRIGGER) > threshold;
});
}

View File

@@ -116,39 +116,99 @@ class Gamepad : public GenericHID,
/**
* Get the X axis value of left side of the controller. Right is positive.
*
* A deadband of 0.1 is applied by default. Use SetLeftXDeadband() to change
* it.
*
* @return the axis value.
*/
double GetLeftX() const;
/**
* Set the deadband for the left X axis.
*
* The deadband is clamped to [0, 1).
*
* @param deadband The deadband to apply.
*/
void SetLeftXDeadband(double deadband);
/**
* Get the Y axis value of left side of the controller. Back is positive.
*
* A deadband of 0.1 is applied by default. Use SetLeftYDeadband() to change
* it.
*
* @return the axis value.
*/
double GetLeftY() const;
/**
* Set the deadband for the left Y axis.
*
* The deadband is clamped to [0, 1).
*
* @param deadband The deadband to apply.
*/
void SetLeftYDeadband(double deadband);
/**
* Get the X axis value of right side of the controller. Right is positive.
*
* A deadband of 0.1 is applied by default. Use SetRightXDeadband() to change
* it.
*
* @return the axis value.
*/
double GetRightX() const;
/**
* Set the deadband for the right X axis.
*
* The deadband is clamped to [0, 1).
*
* @param deadband The deadband to apply.
*/
void SetRightXDeadband(double deadband);
/**
* Get the Y axis value of right side of the controller. Back is positive.
*
* A deadband of 0.1 is applied by default. Use SetRightYDeadband() to change
* it.
*
* @return the axis value.
*/
double GetRightY() const;
/**
* Set the deadband for the right Y axis.
*
* The deadband is clamped to [0, 1).
*
* @param deadband The deadband to apply.
*/
void SetRightYDeadband(double deadband);
/**
* Get the left trigger axis value of the controller. Note that this axis
* is bound to the range of [0, 1] as opposed to the usual [-1, 1].
*
* A deadband of 0.01 is applied by default. Use SetLeftTriggerDeadband() to
* change it.
*
* @return the axis value.
*/
double GetLeftTriggerAxis() const;
/**
* Set the deadband for the left trigger axis.
*
* The deadband is clamped to [0, 1).
*
* @param deadband The deadband to apply.
*/
void SetLeftTriggerDeadband(double deadband);
/**
* Constructs an event instance around the axis value of the left trigger.
* The returned trigger will be true when the axis value is greater than
@@ -175,10 +235,22 @@ class Gamepad : public GenericHID,
* Get the right trigger axis value of the controller. Note that this axis
* is bound to the range of [0, 1] as opposed to the usual [-1, 1].
*
* A deadband of 0.01 is applied by default. Use SetRightTriggerDeadband() to
* change it.
*
* @return the axis value.
*/
double GetRightTriggerAxis() const;
/**
* Set the deadband for the right trigger axis.
*
* The deadband is clamped to [0, 1).
*
* @param deadband The deadband to apply.
*/
void SetRightTriggerDeadband(double deadband);
/**
* Constructs an event instance around the axis value of the right trigger.
* The returned trigger will be true when the axis value is greater than
@@ -1090,6 +1162,13 @@ class Gamepad : public GenericHID,
private:
double GetAxisForSendable(Axis axis) const;
bool GetButtonForSendable(Button button) const;
double m_leftXDeadband = 0.1;
double m_leftYDeadband = 0.1;
double m_rightXDeadband = 0.1;
double m_rightYDeadband = 0.1;
double m_leftTriggerDeadband = 0.01;
double m_rightTriggerDeadband = 0.01;
};
} // namespace wpi

View File

@@ -8,15 +8,21 @@ classes:
methods:
Gamepad:
GetLeftX:
SetLeftXDeadband:
GetLeftY:
SetLeftYDeadband:
GetRightX:
SetRightXDeadband:
GetRightY:
SetRightYDeadband:
GetLeftTriggerAxis:
SetLeftTriggerDeadband:
LeftTrigger:
overloads:
double, EventLoop* [const]:
EventLoop* [const]:
GetRightTriggerAxis:
SetRightTriggerDeadband:
RightTrigger:
overloads:
double, EventLoop* [const]: