Session 2.2 (Finished Example)
Drivetrain.java subsystem:
// 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.subsystems;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import edu.wpi.first.wpilibj.xrp.XRPMotor;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
public class Drivetrain extends SubsystemBase {
XRPMotor m_leftMotor = new XRPMotor(0);
XRPMotor m_rightMotor = new XRPMotor(1);
DifferentialDrive myDrive = new DifferentialDrive(m_leftMotor, m_rightMotor);
/** Creates a new Drivetrain. This is the constructor */
public Drivetrain() {
m_leftMotor.setInverted(true);
}
public void doArcadeDrive(double xSpeed, double zRotation){
myDrive.arcadeDrive(xSpeed, zRotation);
}
@Override
public void periodic() {
// This method will be called once per scheduler run
}
}
ArcadeDriveCommand(.java)
package frc.robot.commands;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.subsystems.Drivetrain;
/* You should consider using the more terse Command factories API instead https://docs.wpilib.org/en/stable/docs/software/commandbased/organizing-command-based.html#defining-commands */
public class ArcadeDriveCommand extends Command {
Drivetrain myDrivetrain;
XboxController myController;
/** Creates a new ArcadeDriveCommand. (This is the constructor) */
public ArcadeDriveCommand(Drivetrain theDrivetrain, XboxController theController) {
myDrivetrain = theDrivetrain;
myController = theController;
addRequirements(theDrivetrain);
}
// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
myDrivetrain.doArcadeDrive(myController.getLeftY(), myController.getLeftX());
}
// 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;
}
}
RobotContainer.java
// 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;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.Commands;
import edu.wpi.first.wpilibj2.command.PrintCommand;
import edu.wpi.first.wpilibj2.command.button.JoystickButton;
import frc.robot.commands.ArcadeDriveCommand;
import frc.robot.subsystems.Drivetrain;
/**
* 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 {
// The robot's subsystems and commands are defined here...
XboxController myController = new XboxController(0);
Drivetrain myDrivetrain = new Drivetrain();
/** The container for the robot. Contains subsystems, OI devices, and commands. */
public RobotContainer() {
// Configure the button bindings
configureButtonBindings();
}
/**Use this method to define your button->command mappings. */
private void configureButtonBindings() {
// Declare the button object:
JoystickButton button_A = new JoystickButton(myController, 1);
// Bind the button onTrue event to a print command:
button_A.onTrue(new PrintCommand("Button A was just pressed!"));
myDrivetrain.setDefaultCommand(new ArcadeDriveCommand(myDrivetrain, myController));
}
/**
* 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 Commands.print("No auto yet!");
}
}
Published @ June 15, 2026 7:07 pm