Command run from axis

How would I go about running a command when an xbox trigger is pressed? They are read as axes, not buttons, but I would like to run the motor at a set speed when the trigger is beyond a certain point.

Google “wpilib TriggerButton” and you’ll find MANY options

Feel free and use our implementation if you’d like.

Go to github and search, there are likely similar but better options than ours.

To answer your more fundamental question of how do I turn an analog axis into a digital button:

You need to understand the underlying WPILib functions that provide buttons, and extend that understanding to the actual trigger you’re trying to implement. The idea here in our implementation is something along the lines of:
Treat the axis as digital, either on or off. The default condition of ‘on’ is when the analog axis reads a certain threshold.

I tried using ‘’‘Trigger intakeTrigger = new Trigger(() → xbox.getRawAxis(2) > 0.65);’’’ to create a trigger and ‘’‘intakeTrigger.whenActive(new runIntake(m_intakeSubsystem));’’’ to run the command, but it is not outputting a signal to drive the motor. Am I missing something with this?

Also it works fine with a regular button, just not the axis input.

Thank you so much for this! Unfortunately, I am getting an error with this that states "Error at frc.robot.RobotContainer.configureButtonBindings(RobotContainer.java:111): Unhandled exception: java.lang.NullPointerException
The robot program quit unexpectedly. This is usually due to a code error.
The above stacktrace can help determine where the error occurred.
See https://wpilib.org/stacktrace for more information.
from: edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:388)

at frc.robot.RobotContainer.configureButtonBindings(RobotContainer.java:111)"
It deploys when I comment out this line: xlt.whileHeld(m_runIntake);

Is there a way to get around this issue? It seems to be acting strange about treated the axis button as a button.

Did you instantiate xlt before calling whileHeld()? The stacktrace suggests you didn’t.

This fixed it, thank you so much!

Happy to help

In case anyone from the robotpy (Python) user community lands here, we had a real headscratcher going to the commands2 AxisButton. Seems like get() was no longer called in v2, and the isPressed had to be set in the Button’s init() - at least, it didn’t work when we tried to redefine isPressed() the same way we did get() in the old version.

Working commands1 version:

from wpilib.command import Button
from wpilib import Joystick

class AxisButton(Button):

def __init__(self, joystick, axis):
    super().__init__()
    self.joystick = joystick
    self.axis = axis
    self.threshold = 0.05

def get(self):
    return self.joystick.getRawAxis(self.axis) > self.threshold

Working commands2 version:

from commands2.button import Button
from wpilib import XboxController

class AxisButton(Button):

def __init__(self, joystick:XboxController, axis):
    # In 2022 it seems we have to override the isPressed in init
    super().__init__(isPressed=lambda: joystick.getRawAxis(axis) > 0.05)

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.