Use joystick as button?

Hello,
I want to use a Joystick as a button by setting a threshold, at which point it will run a command. What’s they best way to accomplish this? I was going to write a custom trigger, but I can’t find a tutorial and I’m getting an error (described below). I guess I could just put an if statement in the command.

Trigger Code: TriggerL2Button.java

package frc.robot;
import edu.wpi.first.wpilibj.buttons.Trigger;

/** Add your docs here. */
public class TriggerL2Button extends Trigger {
  @Override
  public boolean get() {
    return RobotContainer.controller.getRawAxis(3) > 0.5;
  }
}

Relevant Code: RobotContainer.java

public static final TriggerL2Button intakeButton = new TriggerL2Button();
public static final TriggerR2Button flywheelButton = new TriggerR2Button();

Error-ing Code: RobotContainer.java

private void configureButtonBindings() {
    shootButton.whileHeld(spinFeedWheel);
    intakeButton.whileActive(spinIntakeWheel);
    flywheelButton.whileActive(spinFlywheel);
  }

Error:

The method whileActive(Command) in the type Trigger is not applicable for the arguments (SpinIntakeWheel)Java(67108979)

I don’t understand why this results in an error because SpinIntakeWheel is a command and TriggerL2Button extends Trigger. I generated the trigger using the old trigger template and added the old WPILib library, but it’s the same as what’s shown in the current docs.

Link to project: FRC-Code-2022/src/main/java/frc/robot at fixingQuickTestCode · frc6506/FRC-Code-2022 · GitHub

Update: I get a similar error if I create an instance of a Command without setting it to anything and pass it to whileActive.

At a glance, my guess is an issues with the new vs old command-based frameworks.

You’re using edu.wpi.first.wpilibj.buttons.Trigger, which takes in a edu.wpi.first.wpilibj.command.Command
https://first.wpi.edu/wpilib/allwpilib/docs/release/java/edu/wpi/first/wpilibj/buttons/Trigger.html

I think you want edu.wpi.first.wpilibj2.command.button.Trigger which takes in a edu.wpi.first.wpilibj2.command.Command
https://first.wpi.edu/wpilib/allwpilib/docs/release/java/edu/wpi/first/wpilibj2/command/button/Trigger.html

It looks like you shouldn’t need to change much (if any) code other than the import statement in your TriggerL2Button.java.

2 Likes

I just reached the same conclusion.

After you change the import, you will need to update lines 102-103 of RobotContainer from

intakeButton.whileActive(spinIntakeWheel);
flywheelButton.whileActive(spinFlywheel);

to

intakeButton.whileActiveContinuous(spinIntakeWheel);
flywheelButton.whileActiveContinuous(spinFlywheel);

EDIT: I just opened a PR with my changes: https://github.com/frc6506/FRC-Code-2022/pull/7

3 Likes

Thanks, that fixed it!

2 Likes

Thanks for catching the whileActiveContinuous bit too.

1 Like