Moving your DifferentialDrive with Joysticks

Making a MoveRobotWithController Command

We will make a small tweak to the MoveRobot copy, but let's make a copy of it so we can use it later. Name the new copy, MoveRobotWithController. Make sure to fix the constructor, as well.

circle-info

If you are familiar with OOP, you can just make another constructor that takes two DoubleSupplier objects in place of doubles. We will explain this later.

We will also set this method as the default command for the XRPDrivetrain subsystem. However- there is a problem. Let's say we just put controller.getLeftY() and controller.getRightY() for the left and right speed parameters of our command. When we set the MoveRobotWithController command as the drivetrain's default command, the values won't change with the joystick.

These methods return a double, and if you were to move your joysticks, the values sent to your command won't be updated. In place of the two double parameters, use two DoubleSuppliers instead. DoubleSuppliers store doubles, and unlike regular doubles, if you update one's value, all instances of that DoubleSupplier will be updated too.

To create a DoubleSupplier, unlike creating most objects, you use a lambda rather than a constructor.

DoubleSupplier leftJoystickInput = () -> controller.getLeftY();
DoubleSupplier rightJoystickInput = () -> controller.getRightY();

To get the value stored in the DoubleSupplier object, simply use .getAsDouble() to get one. This means we just have to make a couple changes to configureBindings() and our command, and now we can control our robot with our joysticks!

chevron-rightSample Codehashtag
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot.commands;

import frc.robot.subsystems.XRPDrivetrain;

import java.util.Queue;

import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;

/** An example command that uses an example subsystem. */
public class MoveRobotCommand extends Command {
  @SuppressWarnings({"PMD.UnusedPrivateField", "PMD.SingularField"})
  private final XRPDrivetrain m_subsystem;
  private DoubleSupplier leftSpeed;
  private DoubleSupplier rightSpeed;

  private CommandXboxController controller;

  /**
   * Creates a new ExampleCommand.
   *
   * @param subsystem The subsystem used by this command.
   */
  public MoveRobotCommand(XRPDrivetrain subsystem, DoubleSupplier newLeftSpeed, DoubleSupplier newRightSpeed) {
    m_subsystem = subsystem;
    leftSpeed = newLeftSpeed;
    rightSpeed = newRightSpeed;

    controller = new CommandXboxController(0);

    // Use addRequirements() here to declare subsystem dependencies.
    addRequirements(subsystem);
  }

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

  // Called every time the scheduler runs while the command is scheduled.
  @Override
  public void execute() {
    m_subsystem.moveMotors(leftSpeed.getAsDouble(), rightSpeed.getAsDouble());

  }

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

  // Returns true when the command should end. This command never ends until the end
  // of the match.
  @Override
  public boolean isFinished() {
    return false;
  }
}

Last updated