Button Configuration and Scheduling Commands

With OI.java gone this year, I’m having difficulty assigning commands to buttons. In RobotContainer, I created a Joystick object and button objects. But when I press the button, the command only runs once (right when the robot enables).

How do I make it so the buttons run throughout the enable period?

Side question: I can only run one command at a time by using CommandScheduler.getInstance().setDefault(subsystem, command);

How do I run multiple commands at once (based on joystick control)?

   public class RobotContainer {
  // The robot's subsystems and commands are defined here...
  private final ExampleSubsystem m_exampleSubsystem = new ExampleSubsystem();

  private final ExampleCommand m_autoCommand = new ExampleCommand(m_exampleSubsystem);
  
  public static DriveTrain drive;
  public static JoystickDrive joyDrive;

  //public static ToggleShift toggleShift = new ToggleShift(drive);
  public static Joystick joy = new Joystick(0);

  public static JoystickButton buttonA = new JoystickButton(joy, 1);

  

  /**
   * The container for the robot.  Contains subsystems, OI devices, and commands.
   */
  public RobotContainer() {
    // Configure the button bindings
    configureButtonBindings();

    drive = new DriveTrain();
    joyDrive = new JoystickDrive(drive);
  }


  /**
   * Use this method to define your button->command mappings.  Buttons can be created by
   * instantiating a {@link GenericHID} or one of its subclasses ({@link
   * edu.wpi.first.wpilibj.Joystick} or {@link XboxController}), and then passing it to a
   * {@link edu.wpi.first.wpilibj2.command.button.JoystickButton}.
   */
  private void configureButtonBindings() {
    buttonA.whenPressed(new ToggleShift(drive));
  }


  /**
   * Use this to pass the autonomous command to the main {@link Robot} class.
   *
   * @return the command to run in autonomous
   */
  public Command getAutonomousCommand() {
    // An ExampleCommand will run in autonomous
    return m_autoCommand;
  }

 
}

As written, the only thing that should happen, is whenever you press button 1 on joystick 0, buttonA, the ToggleShift Command should be scheduled.

If you are having issues with a default command not running, such as your JoystickDrive Command, (when its set as the default for your drive subsystem object, which it currently isn’t) then you should check that your ToggleShift actually finishes at some point. If it is never returning true in the isFinished() method, then that command never gives up control of your drive to allow the default command to take over.

If your ToggleShift only “runs” the “first” time you press the button, again its likely your command is never returning true for isFinished() as when you press the button again, it will only Schedule the command, which will do nothing if the command object is already running from a previous press.

If you are still having trouble after taking a look at what I said above, it would be very helpful for you to post your entire code, preferably using a Github repository.

Otherwise, everything that went in OI goes in RobotContainer, and works much the same way. Differences are that you now also setup default commands for subsystems in the RobotContainer instead of in the respective Subsystem.

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