SimpleDrive
SimpleDrive is the command that controls the subsystem output. Commands can be called again and again which makes them perfect for autonomous routines.
1package frc.robot.commands.driveCommands;
2
3//WPI imports
4import edu.wpi.first.wpilibj2.command.CommandBase;
5
6//RobotContainer import
7import frc.robot.RobotContainer;
8
9//Subsystem imports
10import frc.robot.subsystems.DriveTrain;
11
12/**
13 * SimpleDrive class
14 * <p>
15 * This class drives a motor at 50% speed until the command is ended
16 */
17public class SimpleDrive extends CommandBase
18{
19 //Grab the subsystem instance from RobotContainer
20 private static final DriveTrain drive = RobotContainer.drive;
21
22 /**
23 * Constructor
24 */
25 public SimpleDrive()
26 {
27 addRequirements(drive); // Adds the subsystem to the command
28 }
29
30 /**
31 * Runs before execute
32 */
33 @Override
34 public void initialize()
35 {
36
37 }
38
39 /**
40 * Called continously until command is ended
41 */
42 @Override
43 public void execute()
44 {
45 drive.setMotorSpeed(0.5); // Set motor speed to 50%
46 }
47
48 /**
49 * Called when the command is told to end or is interrupted
50 */
51 @Override
52 public void end(boolean interrupted)
53 {
54 drive.setMotorSpeed(0.0); // Stop motor
55 }
56
57 /**
58 * Creates an isFinished condition if needed
59 */
60 @Override
61 public boolean isFinished()
62 {
63 return false;
64 }
65
66}
Lines
4-10are the imports required.Line
20grabs the instance of theDriveTrainsubsystem defined and instantiated inRobotContainer.Lines
25-28are the constructor.Line
27says that this command requires the subsystemdrivewhich is the handle for the subsystemDriveTrain.Lines
33-37is the initialize section of the command. In this case there is nothing to initialize so it is left blank.Lines
42-46is the execute section of the command. As long as the command is active anything in here will run every robot packet (20ms).Line
45is setting the motor speed to0.5which is equal to50%speed.Lines
51-55is the end section of the command. When the command is scheduled to end or is interrupted this method is called.Line
54sets the motor speed to0.0this will stop the motor. It is a good idea to always add a stop motor instruction here unless its not required.Lines
60-64is the isFinished section of the command. This method can be called to check if the command is finished or not. Useful if you wanted to put a stop condition based on sensor feedback here. For example using the sharp sensor to sence distance and it hits the threshold.