| 2 | |
| 3 | Commands can be grouped together to run in sequence for example, when the X button is pressed, the following code will run the servo open command, allow time for it to finish, and then run the servo close command: |
| 4 | |
| 5 | Constants.java |
| 6 | {{{ |
| 7 | package frc.robot; |
| 8 | |
| 9 | public final class Constants { |
| 10 | public static int SERVO_PORT = 0; // PWM port for servo motor |
| 11 | public static int OPEN_BUTTON = 1; // A button opens mechanism |
| 12 | public static int CLOSE_BUTTON = 2; // B button closes mechanism |
| 13 | public static int OPEN_THEN_CLOSE_BUTTON = 3; // X button opens then closes |
| 14 | } |
| 15 | }}} |
| 16 | |
| 17 | ServoSubSystem.java: |
| 18 | {{{ |
| 19 | package frc.robot.subsystems; |
| 20 | |
| 21 | import edu.wpi.first.wpilibj2.command.SubsystemBase; |
| 22 | import frc.robot.Constants; |
| 23 | import edu.wpi.first.wpilibj.Servo; |
| 24 | |
| 25 | public class ServoSubsystem extends SubsystemBase { |
| 26 | private Servo servo; |
| 27 | private static ServoSubsystem instance = null; |
| 28 | |
| 29 | private ServoSubsystem() { |
| 30 | servo = new Servo(Constants.SERVO_PORT); |
| 31 | } |
| 32 | |
| 33 | // Singleton design pattern |
| 34 | public static ServoSubsystem getInstance() { |
| 35 | if (instance == null) { |
| 36 | instance = new ServoSubsystem(); |
| 37 | } |
| 38 | return instance; |
| 39 | } |
| 40 | |
| 41 | public void setAngle(double degrees) { |
| 42 | servo.setAngle(degrees); |
| 43 | } |
| 44 | } |
| 45 | }}} |
| 46 | |
| 47 | OpenCommand.java |
| 48 | {{{ |
| 49 | package frc.robot.commands; |
| 50 | |
| 51 | import edu.wpi.first.wpilibj2.command.CommandBase; |
| 52 | import edu.wpi.first.wpilibj2.command.SubsystemBase; |
| 53 | import frc.robot.subsystems.ServoSubsystem; |
| 54 | import edu.wpi.first.wpilibj.Timer; |
| 55 | |
| 56 | public class OpenCommand extends CommandBase { |
| 57 | private Timer delayTimer; |
| 58 | |
| 59 | public OpenCommand(SubsystemBase servoSubsystem) { |
| 60 | delayTimer = new Timer(); |
| 61 | addRequirements(servoSubsystem); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void initialize() { |
| 66 | ServoSubsystem.getInstance().setAngle(30); // set servo to open position |
| 67 | delayTimer.start(); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public boolean isFinished() { |
| 72 | return delayTimer.hasPeriodPassed(0.5); // allow 500ms for command to finish |
| 73 | } |
| 74 | } |
| 75 | }}} |
| 76 | |
| 77 | CloseCommand.java |
| 78 | {{{ |
| 79 | package frc.robot.commands; |
| 80 | |
| 81 | import edu.wpi.first.wpilibj2.command.CommandBase; |
| 82 | import edu.wpi.first.wpilibj2.command.SubsystemBase; |
| 83 | import frc.robot.subsystems.ServoSubsystem; |
| 84 | |
| 85 | public class CloseCommand extends CommandBase { |
| 86 | |
| 87 | public CloseCommand(SubsystemBase servoSubsystem) { |
| 88 | addRequirements(servoSubsystem); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public boolean isFinished() { |
| 93 | ServoSubsystem.getInstance().setAngle(120); // set servo to closed position |
| 94 | return true; // and then finish |
| 95 | } |
| 96 | } |
| 97 | }}} |
| 98 | |
| 99 | OpenThenCloseCommand.java: |
| 100 | {{{ |
| 101 | package frc.robot.commands; |
| 102 | |
| 103 | import edu.wpi.first.wpilibj2.command.SequentialCommandGroup; |
| 104 | import edu.wpi.first.wpilibj2.command.SubsystemBase; |
| 105 | |
| 106 | public class OpenThenCloseCommand extends SequentialCommandGroup { |
| 107 | |
| 108 | public OpenThenCloseCommand(SubsystemBase servoSubsystem) { |
| 109 | this.addCommands(new OpenCommand(servoSubsystem), new CloseCommand(servoSubsystem)); |
| 110 | } |
| 111 | } |
| 112 | }}} |
| 113 | |
| 114 | RobotContainer.java: |
| 115 | {{{ |
| 116 | package frc.robot; |
| 117 | |
| 118 | import edu.wpi.first.wpilibj.XboxController; |
| 119 | import edu.wpi.first.wpilibj2.command.button.Button; |
| 120 | import edu.wpi.first.wpilibj2.command.button.JoystickButton; |
| 121 | import edu.wpi.first.wpilibj2.command.Command; |
| 122 | import frc.robot.subsystems.ServoSubsystem; |
| 123 | import frc.robot.commands.OpenCommand; |
| 124 | import frc.robot.commands.CloseCommand; |
| 125 | import frc.robot.commands.OpenThenCloseCommand; |
| 126 | |
| 127 | public class RobotContainer { |
| 128 | private final ServoSubsystem m_servo = ServoSubsystem.getInstance(); |
| 129 | private final XboxController xbox; |
| 130 | private final Button openButton, closeButton, openThenCloseButton; |
| 131 | |
| 132 | public RobotContainer() { |
| 133 | xbox = new XboxController(0); |
| 134 | openButton = new JoystickButton(xbox, Constants.OPEN_BUTTON); |
| 135 | closeButton = new JoystickButton(xbox, Constants.CLOSE_BUTTON); |
| 136 | openThenCloseButton = new JoystickButton(xbox, Constants.OPEN_THEN_CLOSE_BUTTON); |
| 137 | |
| 138 | // Configure the button bindings |
| 139 | configureButtonBindings(); |
| 140 | } |
| 141 | |
| 142 | private void configureButtonBindings() { |
| 143 | openButton.whenPressed(new OpenCommand(m_servo)); |
| 144 | closeButton.whenPressed(new CloseCommand(m_servo)); |
| 145 | openThenCloseButton.whenPressed(new OpenThenCloseCommand(m_servo)); |
| 146 | } |
| 147 | |
| 148 | public Command getAutonomousCommand() { |
| 149 | return null; |
| 150 | } |
| 151 | } |
| 152 | }}} |