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.
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.
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!
Sample Code
// 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.packagefrc.robot.commands;importfrc.robot.subsystems.XRPDrivetrain;importjava.util.Queue;importedu.wpi.first.wpilibj.XboxController;importedu.wpi.first.wpilibj2.command.Command;importedu.wpi.first.wpilibj2.command.button.CommandXboxController;/** An example command that uses an example subsystem. */publicclassMoveRobotCommandextendsCommand { @SuppressWarnings({"PMD.UnusedPrivateField","PMD.SingularField"})privatefinalXRPDrivetrain m_subsystem;privateDoubleSupplier leftSpeed;privateDoubleSupplier rightSpeed;privateCommandXboxController controller; /** * Creates a new ExampleCommand. * * @param subsystem The subsystem used by this command. */publicMoveRobotCommand(XRPDrivetrain subsystem,DoubleSupplier newLeftSpeed,DoubleSupplier newRightSpeed) { m_subsystem = subsystem; leftSpeed = newLeftSpeed; rightSpeed = newRightSpeed; controller =newCommandXboxController(0);// Use addRequirements() here to declare subsystem dependencies.addRequirements(subsystem); }// Called when the command is initially scheduled. @Overridepublicvoidinitialize() {}// Called every time the scheduler runs while the command is scheduled. @Overridepublicvoidexecute() {m_subsystem.moveMotors(leftSpeed.getAsDouble(),rightSpeed.getAsDouble()); }// Called once the command ends or is interrupted. @Overridepublicvoidend(boolean interrupted) {}// Returns true when the command should end. This command never ends until the end// of the match. @OverridepublicbooleanisFinished() {returnfalse; }}