Creating a "button" from a gamepad trigger

Last year, we could easily create a class to turn gamepad triggers into a true/false button using the following:

import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj2.command.button.Button;

/** Add your docs here. */
public class TriggerButton extends Button{

    private XboxController controller;
    private XboxController.Axis axis;


    public TriggerButton(XboxController controller, XboxController.Axis axis) {
        this.controller = controller;
        this.axis = axis;        
    }

    @Override
    public boolean get(){
        return (controller.getRawAxis(axis.value) >= 0.5);
    }
}

A bunch of this has been deprecated/changed this year and doesn’t really work. Any suggestions on the “correct” way to create a class with this functionality now?

1 Like

wpilib 2023 decided to deprecate the Button class in favor of Trigger class since it has a more concise method names.

Trigger and Button methods were renamed to be consistent and Button class deprecated.

  • Trigger’s bindings are changed to use True/False terminology, as it should be unambiguous. Each binding type has both True and False variants; for brevity, only the True variants are listed here:

So what that means for you is just to make your custom class inherit Trigger instead and Button and make sure you overload the constructor that takes in a BooleanSupplier and not the deprecated constructor.

So it should look something like this:

import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj2.command.button.Trigger;

public class TriggerButton extends Trigger {

  public TriggerButton(XboxController controller, XboxController.Axis axis) {
    super(() -> controller.getRawAxis(axis.value) >= 0.5);
  }

}

the constructor essentially takes care of the get() method

4 Likes

Even better than posted above, you can make this inline and skip all that boilerplate class stuff, not having to use new everywhere.

public static Trigger triggerButton (XboxController controller, XboxController.Axis axis) {
  return new Trigger(() -> controller.getRawAxis(axis.value) >= 0.5);
}

Much cleaner in my opinion.

6 Likes

Btw, methods for exactly this were added: CommandXboxController.rightTrigger() etc should do what the expected use of this is, and axisGreaterThan can be used for doing this on other axes.

3 Likes

Shoot! I completely missed that. Thanks!

Also, thanks to others for the coding suggestions. They are helpful for improving my grasp of Java.

2 Likes

Can you show us what the code would look like if using CommandXboxController.rightTrigger() . Would this mean you don’t have to use a separate class? Is this used in RobotContainer?

Yes. We also made a custom class to do this last year but now we don’t have to. This returns a trigger object that u can use to call a command when the trigger is pulled. U can also specify how much the trigger needs to be pull for it to activate. This is our last year’s robot updated to 2023 and this new method FRC2022/RobotContainer.java at main · Frc5572/FRC2022 · GitHub.

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