How to pass in a gamepad's joystick Y axis value to a Command from the RobotContainer

I have this logitech gamepad and would like to be able to control the direction and speed of a motorcontroller via one of its joysticks.

I’m following the 2020 FRC programming examples that FRC made available on Github, which detail how to work with the new 2020 Command, RobotContainer, Subsystem, etc paradigm. I am able to press the controller’s L1 button and get my single motor controller (defined in my DriveTrain subsystem) to move.

  1. How can I retrieve the gamepad’s joystick values (x and y axes)? I’m able to retrieve button presses in the RobotContainer, but how do I access the values of the joysticks?:
private void configureButtonBindings() {
    //selecting the gamepad's L1 button
    final JoystickButton rightJoystickOnGamepad = new JoystickButton(m_joystick, 5);

    //when pressed, trigger command:
    rightJoystickOnGamepad.whenHeld(new TankDrive(m_drivetrain));
  1. How can I pass the values of the speed dynamically? I currently have these values hard-coded in my TankDrive command because the new FRC 2020 examples show the need to instantiate a new instance of the Command when using it (as shown above). My TankDrive class looks like this:
public class TankDrive extends CommandBase {

  private DriveTrain m_drivetrain;

   * Creates a new TankDrive.
  public TankDrive(DriveTrain driveTrain) {
    // Use addRequirements() here to declare subsystem dependencies.
    m_drivetrain = driveTrain;

  // Called when the command is initially scheduled.
  public void initialize() {

  // Called every time the scheduler runs while the command is scheduled.
  public void execute() {
    //How does one pass the joystick value here? There has to be a better way.;

  // Called once the command ends or is interrupted.
  public void end(boolean interrupted) {

  // Returns true when the command should end.
  public boolean isFinished() {
    return false;

Below is my subsystem:

public class DriveTrain extends SubsystemBase {

  //Motorcontroller attached to PWM port 0
  private static final Victor victorMotorController = new Victor(0);

  public void drive(double joystickValue) {
    //set the speed/direction of the motorcontroller
    System.out.println("joystick value is: " + joystickValue);

   * Creates a new DriveTrain.
  public DriveTrain() {

  public void periodic() {
    // This method will be called once per scheduler run

Any help would be greatly appreciated!

In our teams code we will make a static reference to the joystick in our Robot class and just say Robot.joystick but in your case you will need to get a reference to the m_joystick variable. You will fetch the rawAxisValue in the execute one the command.

Both mentioned solutions are not the recommended practice.

  1. Global references to variables (subsystems / joysticks) can be troublesome to debug. Passing in requirements from top down through dependency injection is a widely accepted best practice.
  2. Passing a Joystick into the command is not as bad as having everything have access to everything, but it’s still one step in that direction.

This example from the FRC Docs highlights how to use a DoubleSupplier to acheive what you’re trying to do. As for the implementation in RobotContainer, it would look like

m_drivetrain.setDefaultCommand(new TankDrive(m_drivetrain, () -> m_joystick.getRawAxis(1), () -> m_joystick.getRawAxis(2));

(That’s freehand without an editor, so don’t trust my syntax)

Also, your Victor in the DriveTrain subsystem should not be static. Just final and private. Your code will function absolutely fine with it, but again, best practice.

I use the DoubleSupplier and the SetDefaultCommand method

It gets a little trickier when you have buttons and joysticks that you would like to control your subsystem with

1 Like

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