Challenge- Create & Bind the MoveRobot Command

For this page, you will create your first command! In this command, you'll need to do is make a class that:

chevron-rightSample Codehashtag
package frc.robot.commands;

import frc.robot.subsystems.RomiDrivetrain;
import edu.wpi.first.wpilibj2.command.Command;

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

  /**
   * Creates a new ExampleCommand.
   *
   * @param subsystem The subsystem used by this command.
   */
  public MoveRobotCommand(XRPDrivetrain subsystem, double lSpeed, double rSpeed) {
    m_subsystem = subsystem;
    leftSpeed = lSpeed;
    rightSpeed = rSpeed;
    // 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, rightSpeed);

  }

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

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


After this, we will now bind the command to a button on your controller. Let's make the robot move forward when we press the X button.

chevron-rightSample Codehashtag
int portNum = 0;
private CommandXboxController controller = new CommandXboxController(portNum);
// up in the header

        
private void configureBindings() {
    // making both speeds 1 makes it move forward.
    // making both speeds -1 makes it move backward.
    // making the speeds 1, -1 makes the robot rotate left. 
    // making the speeds -1, 1 makes the robot rotate right.
    controller.x().onTrue(new MoveRobotCommand(m_xrpDrivetrain, 1, 1));
    controller.x().onFalse(new MoveRobotCommand(m_xrpDrivetrain, 0, 0));
}

Now with all of this, let's try out your robot! Simulate your robot's code and run your first program!

Last updated