Changes between Version 1 and Version 2 of ControlSystems/SoftwareTeam/Training/WPILib/CommandGroupProgramming


Ignore:
Timestamp:
Feb 27, 2020, 10:59:02 PM (5 years ago)
Author:
David Albert
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/Training/WPILib/CommandGroupProgramming

    v1 v2  
    11The last example showed two simple commands and how to connect them with the user interface, but the commands were simple: they set a servo position and immediately finished.  In practice, robot commands often need to run continuously until something happens; for example, a command to raise an arm might start a motor moving and continue until some time has passed or an encoder indicates that the arm has moved a certain distance or a limit switch is pressed.  WPILib commands make this easy.  Even better, you can string several such commands together in a !CommandGroup to effect complex behaviors.
     2
     3Commands 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
     5Constants.java
     6{{{
     7package frc.robot;
     8
     9public 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
     17ServoSubSystem.java:
     18{{{
     19package frc.robot.subsystems;
     20
     21import edu.wpi.first.wpilibj2.command.SubsystemBase;
     22import frc.robot.Constants;
     23import edu.wpi.first.wpilibj.Servo;
     24
     25public 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
     47OpenCommand.java
     48{{{
     49package frc.robot.commands;
     50
     51import edu.wpi.first.wpilibj2.command.CommandBase;
     52import edu.wpi.first.wpilibj2.command.SubsystemBase;
     53import frc.robot.subsystems.ServoSubsystem;
     54import edu.wpi.first.wpilibj.Timer;
     55
     56public 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
     77CloseCommand.java
     78{{{
     79package frc.robot.commands;
     80
     81import edu.wpi.first.wpilibj2.command.CommandBase;
     82import edu.wpi.first.wpilibj2.command.SubsystemBase;
     83import frc.robot.subsystems.ServoSubsystem;
     84
     85public 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
     99OpenThenCloseCommand.java:
     100{{{
     101package frc.robot.commands;
     102
     103import edu.wpi.first.wpilibj2.command.SequentialCommandGroup;
     104import edu.wpi.first.wpilibj2.command.SubsystemBase;
     105
     106public class OpenThenCloseCommand extends SequentialCommandGroup {
     107
     108    public OpenThenCloseCommand(SubsystemBase servoSubsystem) {
     109        this.addCommands(new OpenCommand(servoSubsystem), new CloseCommand(servoSubsystem));
     110    }
     111}
     112}}}
     113
     114RobotContainer.java:
     115{{{
     116package frc.robot;
     117
     118import edu.wpi.first.wpilibj.XboxController;
     119import edu.wpi.first.wpilibj2.command.button.Button;
     120import edu.wpi.first.wpilibj2.command.button.JoystickButton;
     121import edu.wpi.first.wpilibj2.command.Command;
     122import frc.robot.subsystems.ServoSubsystem;
     123import frc.robot.commands.OpenCommand;
     124import frc.robot.commands.CloseCommand;
     125import frc.robot.commands.OpenThenCloseCommand;
     126
     127public 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}}}