Changes between Version 5 and Version 6 of ControlSystems/SoftwareTeam/Training/WPILib/CommandBasedProgramming


Ignore:
Timestamp:
Dec 17, 2019, 12:45:22 AM (6 years ago)
Author:
David Albert
Comment:

--

Legend:

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

    v5 v6  
    2020!ScreenStepsLive has a very good example of how a program might look if programmed for !TimedRobot and then the same program converted to !CommandRobot; you should review it [https://wpilib.screenstepslive.com/s/currentCS/m/cpp/l/241906-converting-a-simple-autonomous-program-to-a-command-based-autonomous-program here]
    2121
     22The following is an example of a very simple Command Based Robot that has a single subsystem consisting of a servo motor with two positions: Open, Closed and they are selected using the A and B buttons on a gamepad.  Replace the template files with:
     23
     24RobotMap.java:
     25{{{
     26package frc.robot;
     27public class RobotMap {
     28  public static int SERVO_PORT = 0;   // PWM port for servo motor
     29  public static int OPEN_BUTTON = 1;  // A button opens mechanism
     30  public static int CLOSE_BUTTON = 2; // B button closes mechanism
     31}
     32}}}
     33
     34Add ServoSubsystem.java under subsystems:
     35{{{
     36package frc.robot.subsystems;
     37
     38import edu.wpi.first.wpilibj.command.Subsystem;
     39import edu.wpi.first.wpilibj.Servo;
     40
     41public class ServoSubsystem extends Subsystem {
     42  private Servo servo;
     43
     44  private static ServoSubsystem instance = null;
     45
     46  private ServoSubsystem() {
     47      servo = new Servo(0);
     48  }
     49
     50  // Singleton design pattern
     51  public static ServoSubsystem getInstance() {
     52      if (instance == null) {
     53          instance = new ServoSubsystem();
     54      }
     55      return instance;
     56  }
     57
     58  public void setAngle(double degrees) {
     59    servo.setAngle(degrees);
     60  }
     61
     62  @Override
     63  public void initDefaultCommand() {
     64    // Set the default command for a subsystem here.
     65    // setDefaultCommand(new MySpecialCommand());
     66  }
     67}
     68}}}
     69
     70Add OpenCommand.java and CloseCommand.java under commands:
     71OpenCommand.java:
     72{{{
     73package frc.robot.commands;
     74
     75import edu.wpi.first.wpilibj.command.Command;
     76import frc.robot.Robot;
     77
     78public class OpenCommand extends Command {
     79  public OpenCommand() {
     80    requires(Robot.m_servo);
     81  }
     82
     83  @Override
     84  protected boolean isFinished() {
     85    System.out.println("Open Pressed.");
     86    Robot.m_servo.setAngle(30); // set servo to open position
     87    return true;
     88  }
     89}
     90}}}
     91
     92CloseCommand.java:
     93{{{
     94package frc.robot.commands;
     95
     96import edu.wpi.first.wpilibj.command.Command;
     97import frc.robot.Robot;
     98
     99public class CloseCommand extends Command {
     100  public CloseCommand() {
     101    requires(Robot.m_servo);
     102  }
     103
     104  @Override
     105  protected boolean isFinished() {
     106    System.out.println("Close Pressed.");
     107    Robot.m_servo.setAngle(120);
     108    return true;
     109  }
     110}
     111}}}
     112
     113OI.java:
     114{{{
     115package frc.robot;
     116
     117import edu.wpi.first.wpilibj.XboxController;
     118import edu.wpi.first.wpilibj.buttons.Button;
     119import edu.wpi.first.wpilibj.buttons.JoystickButton;
     120import frc.robot.RobotMap;
     121import frc.robot.commands.*;
     122
     123public class OI {
     124  XboxController xbox = new XboxController(0);
     125  Button openButton = new JoystickButton(xbox, RobotMap.OPEN_BUTTON);
     126  Button closeButton = new JoystickButton(xbox, RobotMap.CLOSE_BUTTON);
     127
     128  public OI() {
     129    openButton.whenPressed(new OpenCommand());
     130    closeButton.whenPressed(new CloseCommand());
     131  }
     132}
     133}}}
     134
     135Robot.java:
     136{{{
     137package frc.robot;
     138
     139import edu.wpi.first.wpilibj.TimedRobot;
     140import edu.wpi.first.wpilibj.command.Scheduler;
     141import frc.robot.subsystems.ServoSubsystem;
     142
     143public class Robot extends TimedRobot {
     144  public static ServoSubsystem m_servo = ServoSubsystem.getInstance();
     145  public static OI m_oi;
     146
     147  @Override
     148  public void robotInit() {
     149    m_oi = new OI();
     150  }
     151
     152  @Override
     153  public void disabledPeriodic() {
     154    Scheduler.getInstance().run();
     155  }
     156
     157  /**
     158   * This function is called periodically during autonomous.
     159   */
     160  @Override
     161  public void autonomousPeriodic() {
     162    Scheduler.getInstance().run();
     163  }
     164
     165  @Override
     166  public void teleopPeriodic() {
     167    Scheduler.getInstance().run();
     168  }
     169}
     170}}}
    22171
    23172=== How it works ===