Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmd] Add deadband trigger methods to CommandGenericHID #7085

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class CommandGenericHID {
new HashMap<>();
private final Map<EventLoop, Map<Pair<Integer, Double>, Trigger>> m_axisGreaterThanCache =
new HashMap<>();
private final Map<EventLoop, Map<Pair<Integer, Double>, Trigger>> m_axisActive = new HashMap<>();
private final Map<EventLoop, Map<Integer, Trigger>> m_povCache = new HashMap<>();

/**
Expand Down Expand Up @@ -260,6 +261,37 @@ public Trigger axisGreaterThan(int axis, double threshold, EventLoop loop) {
Pair.of(axis, threshold), k -> new Trigger(loop, () -> getRawAxis(axis) > threshold));
}

/**
* Constructs a Trigger instance that is true when the axis value is active (non-zero) given a
* {@code deadband}, attached to the given loop.
*
* @param axis The axis to read, starting at 0
* @param deadband The value used to deadband the axis value. The trigger returns true if the
* deadbanded value is non-zero.
* @param loop the event loop instance to attach the trigger to.
* @return a Trigger instance that is true when the deadbanded axis value is active (non-zero).
narmstro2020 marked this conversation as resolved.
Show resolved Hide resolved
*/
public Trigger axisActive(int axis, double deadband, EventLoop loop) {
var cache = m_axisActive.computeIfAbsent(loop, k -> new HashMap<>());
return cache.computeIfAbsent(
Pair.of(axis, deadband),
k -> new Trigger(loop, () -> Math.abs(getRawAxis(axis)) > deadband));
}

/**
* Constructs a Trigger instance that is true when the axis value is active (non-zero) given a
* {@code deadband}, attached to {@link CommandScheduler#getDefaultButtonLoop() the default
* command scheduler button loop}.
*
* @param axis The axis to read, starting at 0
* @param deadband The value used to deadband the axis value. The trigger returns true if the
* deadbanded value is non-zero.
* @return a Trigger instance that is true when the deadbanded axis value is active (non-zero).
*/
public Trigger axisActive(int axis, double deadband) {
return axisActive(axis, deadband, CommandScheduler.getInstance().getDefaultButtonLoop());
}

/**
* Get the value of the axis.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ Trigger CommandGenericHID::AxisGreaterThan(int axis, double threshold,
});
}

Trigger CommandGenericHID::AxisActive(int axis, double deadband,
frc::EventLoop* loop) const {
return Trigger(loop, [this, axis, deadband]() {
return std::abs(m_hid.GetRawAxis(axis)) > deadband;
});
}

void CommandGenericHID::SetRumble(frc::GenericHID::RumbleType type,
double value) {
m_hid.SetRumble(type, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,22 @@ class CommandGenericHID {
frc::EventLoop* loop =
CommandScheduler::GetInstance().GetDefaultButtonLoop()) const;

/**
* Constructs a Trigger instance that is true when the axis value is active
* (non-zero) given a
* {@code deadband}, attached to the given loop.
*
* @param axis The axis to read, starting at 0
* @param deadband The value used to deadband the axis value. The trigger
* returns true if the deadbanded value is non-zero.
* @param loop the event loop instance to attach the trigger to.
* @return a Trigger instance that is true when the deadbanded axis value is
* active (non-zero).
*/
Trigger AxisActive(int axis, double deadband,
frc::EventLoop* loop = CommandScheduler::GetInstance()
.GetDefaultButtonLoop()) const;

/**
* Set the rumble output for the HID.
*
Expand Down
Loading