| 22 | The 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 | |
| 24 | RobotMap.java: |
| 25 | {{{ |
| 26 | package frc.robot; |
| 27 | public 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 | |
| 34 | Add ServoSubsystem.java under subsystems: |
| 35 | {{{ |
| 36 | package frc.robot.subsystems; |
| 37 | |
| 38 | import edu.wpi.first.wpilibj.command.Subsystem; |
| 39 | import edu.wpi.first.wpilibj.Servo; |
| 40 | |
| 41 | public 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 | |
| 70 | Add OpenCommand.java and CloseCommand.java under commands: |
| 71 | OpenCommand.java: |
| 72 | {{{ |
| 73 | package frc.robot.commands; |
| 74 | |
| 75 | import edu.wpi.first.wpilibj.command.Command; |
| 76 | import frc.robot.Robot; |
| 77 | |
| 78 | public 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 | |
| 92 | CloseCommand.java: |
| 93 | {{{ |
| 94 | package frc.robot.commands; |
| 95 | |
| 96 | import edu.wpi.first.wpilibj.command.Command; |
| 97 | import frc.robot.Robot; |
| 98 | |
| 99 | public 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 | |
| 113 | OI.java: |
| 114 | {{{ |
| 115 | package frc.robot; |
| 116 | |
| 117 | import edu.wpi.first.wpilibj.XboxController; |
| 118 | import edu.wpi.first.wpilibj.buttons.Button; |
| 119 | import edu.wpi.first.wpilibj.buttons.JoystickButton; |
| 120 | import frc.robot.RobotMap; |
| 121 | import frc.robot.commands.*; |
| 122 | |
| 123 | public 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 | |
| 135 | Robot.java: |
| 136 | {{{ |
| 137 | package frc.robot; |
| 138 | |
| 139 | import edu.wpi.first.wpilibj.TimedRobot; |
| 140 | import edu.wpi.first.wpilibj.command.Scheduler; |
| 141 | import frc.robot.subsystems.ServoSubsystem; |
| 142 | |
| 143 | public 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 | }}} |