Making buttons do things when disabled via debounce + whenActive

Today I tried using this code:

    visionOn.debounce(Constants.INPUT_DEBOUNCE, DebounceType.kBoth).whenActive(new CommandBase() {
        @Override
        public void initialize() {
            _drive.getVisionState().setEnabled(true);
        }

        @Override
        public boolean runsWhenDisabled() {
            return true;
        }
    });
    visionOff.debounce(Constants.INPUT_DEBOUNCE, DebounceType.kBoth).whenActive(new CommandBase() {
      @Override
      public void initialize() {
          _drive.getVisionState().setEnabled(false);
      }

      @Override
      public boolean runsWhenDisabled() {
          return true;
      }
    });

I noticed that the command scheduler does nothing when the robot is disabled. Do we have to resort to doing stuff like this via getRawButton to make this work?

I want to be able to turn the vision on and off while the robot is disabled. Was this use case not thought of when designing the command scheduler stuff?

This should work. Are you calling CommandScheduler.getInstance().run() in robotPeriodic?

I take it back, it works as intended, I just should have extended RunCommand if I wanted it to end instantly.

I incorrectly assumed that the command wasn’t activating while disabled, but really, the “vision off” command was just running constantly.

@Oblarg can I suggest a decorator to allow a command to run while disabled so using stuff like RunCommand is intuitive when I want that to run while disabled?

It’s not a decorator, but there is this: Command (WPILib API 2022.4.1)

That’s for the old command framework. Is there something for the new one?

Whoops, you’re right, there isn’t an equivalent in the new framework. You have to override it yourself. There’s Java syntax to do that inline but it’s ugly.

Making this less verbose is on the radar and will be done by 2023.

2 Likes