private void configureButtonBindings() {
//Driver Buttons
zeroGyro.whenPressed(new InstantCommand(() → s_Swerve.zeroGyro()));
}
"The method whenPressed(Command) from the type Button is deprecated. "
what can i use instead of it?
.onTrue
I’m pretty sure if you hover over whenPressed in wpilib vscode it’ll tell you what to use. I don’t have a wpilib 23 install on this computer though so I can’t check.
Depending on what controller you are using, it may be best to switch your zeroGyro Button
object to a Trigger
Object (such as CommandXboxController
), since the Button
Class has been deprecated.
Using the CommandXboxController
, you can do the following:
driverController.back().onTrue(new InstantCommand(() → s_Swerve.zeroGyro()));
This can be even shortened to:
driverController.back().onTrue(new InstantCommand(s_Swerve::zeroGyro));
package frc.robot;
import edu.wpi.first.wpilibj.GenericHID;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import edu.wpi.first.wpilibj2.command.button.JoystickButton;
import frc.robot.autos.;
import frc.robot.commands.;
import frc.robot.subsystems.*;
/**
- This class is where the bulk of the robot should be declared. Since Command-based is a
- “declarative” paradigm, very little robot logic should actually be handled in the {@link Robot}
- periodic methods (other than the scheduler calls). Instead, the structure of the robot (including
- subsystems, commands, and button mappings) should be declared here.
/
public class RobotContainer {
/ Controllers */
private final Joystick driver = new Joystick(0);
/* Drive Controls */
private final int translationAxis = XboxController.Axis.kLeftY.value;
private final int strafeAxis = XboxController.Axis.kLeftX.value;
private final int rotationAxis = XboxController.Axis.kRightX.value;
/* Driver Buttons */
private final JoystickButton zeroGyro =
new JoystickButton(driver, XboxController.Button.kY.value);
private final JoystickButton robotCentric =
new JoystickButton(driver, XboxController.Button.kLeftBumper.value);
/* Subsystems */
private final Swerve s_Swerve = new Swerve();
/** The container for the robot. Contains subsystems, OI devices, and commands. */
public RobotContainer() {
s_Swerve.setDefaultCommand(
new TeleopSwerve(
s_Swerve,
() → -driver.getRawAxis(translationAxis),
() → -driver.getRawAxis(strafeAxis),
() → -driver.getRawAxis(rotationAxis),
() → robotCentric.getAsBoolean()
)
);
// Configure the button bindings
configureButtonBindings();
}
/**
- 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() {
//Driver Buttons
zeroGyro.whenPressed(new InstantCommand(() → s_Swerve.zeroGyro()));
}
/**
- 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 new exampleAuto(s_Swerve);
}
}
Can you please explain step by step where and how to use your answer?
First you should put your code between three ticks (the symbol to left of the 1
key in your keyboard) so it will be easier to read. If you include the language name after the first ``` the code will also be syntax-highlighted:
package frc.robot;
import edu.wpi.first.wpilibj.GenericHID;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import edu.wpi.first.wpilibj2.command.button.JoystickButton;
import frc.robot.autos.;
import frc.robot.commands.;
import frc.robot.subsystems.*;
/**
This class is where the bulk of the robot should be declared. Since Command-based is a
“declarative” paradigm, very little robot logic should actually be handled in the {@link Robot}
periodic methods (other than the scheduler calls). Instead, the structure of the robot (including
subsystems, commands, and button mappings) should be declared here.
*/
public class RobotContainer {
/* Controllers */
private final Joystick driver = new Joystick(0);
/* Drive Controls */
private final int translationAxis = XboxController.Axis.kLeftY.value;
private final int strafeAxis = XboxController.Axis.kLeftX.value;
private final int rotationAxis = XboxController.Axis.kRightX.value;
/* Driver Buttons */
private final JoystickButton zeroGyro =
new JoystickButton(driver, XboxController.Button.kY.value);
private final JoystickButton robotCentric =
new JoystickButton(driver, XboxController.Button.kLeftBumper.value);
/* Subsystems */
private final Swerve s_Swerve = new Swerve();
/** The container for the robot. Contains subsystems, OI devices, and commands. */
public RobotContainer() {
s_Swerve.setDefaultCommand(
new TeleopSwerve(
s_Swerve,
() → -driver.getRawAxis(translationAxis),
() → -driver.getRawAxis(strafeAxis),
() → -driver.getRawAxis(rotationAxis),
() → robotCentric.getAsBoolean()
)
);
// Configure the button bindings
configureButtonBindings();
}
/**
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() {
//Driver Buttons
zeroGyro.whenPressed(new InstantCommand(() → s_Swerve.zeroGyro()));
}
/**
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 new exampleAuto(s_Swerve);
}
}
The first problem I see is where you define driver
(which you should also give a more descriptive name because “driver” usually means something else in computers). You define it as Joystick
but then create Xbox buttons from it. So the first thing to fix is driver
to be an XboxController
.
You could also use the better version which is CommandXboxController
. This class provides factory methods returning a Trigger
for each button. There is a bit more explanation in the docs.
Now, once you have a Trigger
instead of a JoystickButton
, you can use onTrue()
as Christian suggested. The only change I offered is to refactor new InstantCommand(() -> s_Swerve.zeroGyro())
into new InstantCommand(s_Swerve::zeroGyro)
. Because zeroGyro()
takes no arguments, you can use here a method reference which is a bit nicer and shorter than a “regular” lambda expression.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.