RobotBuilder Created Code

The Layout of a RobotBuilder Generated Project

../../../../../_images/robotbuilder-created-code-java.png ../../../../../_images/robotbuilder-created-code-cpp.png

A RobotBuilder generated project consists of a package (in Java) or a folder (in C++) for Commands and another for Subsystems. Each command or subsystem object is stored under those containers. At the top level of the project you’ll find the robot main program (RobotContainer.java/C++).

For more information on the organization of a Command Based robot, see Structuring a Command-Based Robot Project

Autogenerated Code

// BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=AUTONOMOUS
m_chooser.setDefaultOption("Autonomous", new Autonomous());
// END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=AUTONOMOUS

SmartDashboard.putData("Auto Mode", m_chooser);

When the robot description is modified and code is re-exported RobotBuilder is designed to not modify any changes you made to the file, thus preserving your code. This makes RobotBuilder a full-lifecycle tool. To know what code is OK to be modified by RobotBuilder, it generates sections that will potentially have to be rewritten delimited with some special comments. These comments are shown in the example above. Don’t add any code within these comment blocks, it will be rewritten next time the project is exported from RobotBuilder.

If code inside one of these blocks must be modified, the comments can be removed, but this will prevent further updates from happening later. In the above example, if the //BEGIN and //END comments were removed, then later another required subsystem was added in RobotBuilder, it would not be generated on that next export.

Main Robot Program

 12 package frc.robot;
 13
 14 import edu.wpi.first.hal.FRCNetComm.tInstances;
 15 import edu.wpi.first.hal.FRCNetComm.tResourceType;
 16 import edu.wpi.first.hal.HAL;
 17 import edu.wpi.first.wpilibj.TimedRobot;
 18 import edu.wpi.first.wpilibj2.command.Command;
 19 import edu.wpi.first.wpilibj2.command.CommandScheduler;
 20
 21 /**
 22  * The VM is configured to automatically run this class, and to call the
 23  * functions corresponding to each mode, as described in the TimedRobot
 24  * documentation. If you change the name of this class or the package after
 25  * creating this project, you must also update the build.properties file in
 26  * the project.
 27  */
 28 public class Robot extends TimedRobot { // (1)
 29
 30     private Command m_autonomousCommand;
 31
 32     private RobotContainer m_robotContainer;
 33
 34     /**
 35      * This function is run when the robot is first started up and should be
 36      * used for any initialization code.
 37      */
 38     @Override
 39     public void robotInit() {
 40         // Instantiate our RobotContainer.  This will perform all our button bindings, and put our
 41         // autonomous chooser on the dashboard.
 42         m_robotContainer = new RobotContainer();
 43         HAL.report(tResourceType.kResourceType_Framework, tInstances.kFramework_RobotBuilder);
 44     }
 45
 46     /**
 47     * This function is called every robot packet, no matter the mode. Use this for items like
 48     * diagnostics that you want ran during disabled, autonomous, teleoperated and test.
 49     *
 50     * <p>This runs after the mode specific periodic functions, but before
 51     * LiveWindow and SmartDashboard integrated updating.
 52     */
 53     @Override
 54     public void robotPeriodic() {
 55         // Runs the Scheduler.  This is responsible for polling buttons, adding newly-scheduled
 56         // commands, running already-scheduled commands, removing finished or interrupted commands,
 57         // and running subsystem periodic() methods.  This must be called from the robot's periodic
 58         // block in order for anything in the Command-based framework to work.
 59         CommandScheduler.getInstance().run(); // (2)
 60     }
 61
 62
 63     /**
 64     * This function is called once each time the robot enters Disabled mode.
 65     */
 66     @Override
 67     public void disabledInit() {
 68     }
 69
 70     @Override
 71     public void disabledPeriodic() {
 72     }
 73
 74     /**
 75     * This autonomous runs the autonomous command selected by your {@link RobotContainer} class.
 76     */
 77     @Override
 78     public void autonomousInit() {
 79         m_autonomousCommand = m_robotContainer.getAutonomousCommand(); // (3)
 80
 81         // schedule the autonomous command (example)
 82         if (m_autonomousCommand != null) {
 83             m_autonomousCommand.schedule();
 84         }
 85     }
 86
 87     /**
 88     * This function is called periodically during autonomous.
 89     */
 90     @Override
 91     public void autonomousPeriodic() {
 92     }
 93
 94     @Override
 95     public void teleopInit() {
 96         // This makes sure that the autonomous stops running when
 97         // teleop starts running. If you want the autonomous to
 98         // continue until interrupted by another command, remove
 99         // this line or comment it out.
100         if (m_autonomousCommand != null) {
101             m_autonomousCommand.cancel();
102         }
103     }
104
105     /**
106      * This function is called periodically during operator control.
107      */
108     @Override
109     public void teleopPeriodic() {
110     }
111
112     @Override
113     public void testInit() {
114         // Cancels all running commands at the start of test mode.
115         CommandScheduler.getInstance().cancelAll();
116     }
117
118     /**
119     * This function is called periodically during test mode.
120     */
121     @Override
122     public void testPeriodic() {
123     }
124
125 }

This is the main program generated by RobotBuilder. There are a number of parts to this program (highlighted sections):

  1. This class extends TimedRobot. TimedRobot will call your autonomousPeriodic() and teleopPeriodic() methods every 20ms.

  2. In the robotPeriodic method which is called every 20ms, make one scheduling pass.

  3. The autonomous command provided is scheduled at the start of autonomous in the autonomousInit() method and canceled at the end of the autonomous period in teleopInit().

RobotContainer

 12package frc.robot;
 13
 14import frc.robot.commands.*;
 15import frc.robot.subsystems.*;
 16import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
 17import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
 18
 19// BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=IMPORTS
 20import edu.wpi.first.wpilibj2.command.Command;
 21import edu.wpi.first.wpilibj2.command.InstantCommand;
 22import edu.wpi.first.wpilibj.Joystick;
 23import edu.wpi.first.wpilibj2.command.button.JoystickButton;
 24import frc.robot.subsystems.*;
 25
 26// END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=IMPORTS
 27
 28
 29/**
 30 * This class is where the bulk of the robot should be declared.  Since Command-based is a
 31 * "declarative" paradigm, very little robot logic should actually be handled in the {@link Robot}
 32 * periodic methods (other than the scheduler calls).  Instead, the structure of the robot
 33 * (including subsystems, commands, and button mappings) should be declared here.
 34 */
 35public class RobotContainer {
 36
 37  private static RobotContainer m_robotContainer = new RobotContainer();
 38
 39    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS
 40// The robot's subsystems
 41    private final Wrist m_wrist = new Wrist(); // (1)
 42    private final Elevator m_elevator = new Elevator();
 43    private final Claw m_claw = new Claw();
 44    private final Drivetrain m_drivetrain = new Drivetrain();
 45
 46// Joysticks
 47private final Joystick logitechController = new Joystick(0); // (3)
 48
 49    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=DECLARATIONS
 50
 51
 52  // A chooser for autonomous commands
 53  SendableChooser<Command> m_chooser = new SendableChooser<>();
 54
 55  /**
 56  * The container for the robot.  Contains subsystems, OI devices, and commands.
 57  */
 58  private RobotContainer() {
 59        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=SMARTDASHBOARD
 60    // Smartdashboard Subsystems
 61    SmartDashboard.putData(m_wrist);
 62    SmartDashboard.putData(m_elevator);
 63    SmartDashboard.putData(m_claw);
 64    SmartDashboard.putData(m_drivetrain);
 65
 66
 67    // SmartDashboard Buttons
 68    SmartDashboard.putData("Close Claw", new CloseClaw( m_claw )); // (6)
 69    SmartDashboard.putData("Open Claw", new OpenClaw( m_claw ));
 70    SmartDashboard.putData("Pickup", new Pickup());
 71    SmartDashboard.putData("Place", new Place());
 72    SmartDashboard.putData("Prepare To Pickup", new PrepareToPickup());
 73    SmartDashboard.putData("Set Elevator Setpoint: Bottom", new SetElevatorSetpoint(0, m_elevator));
 74    SmartDashboard.putData("Set Elevator Setpoint: Platform", new SetElevatorSetpoint(0.2, m_elevator));
 75    SmartDashboard.putData("Set Elevator Setpoint: Top", new SetElevatorSetpoint(0.3, m_elevator));
 76    SmartDashboard.putData("Set Wrist Setpoint: Horizontal", new SetWristSetpoint(0, m_wrist));
 77    SmartDashboard.putData("Set Wrist Setpoint: Raise Wrist", new SetWristSetpoint(-45, m_wrist));
 78    SmartDashboard.putData("Drive: Straight3Meters", new Drive(3, 0, m_drivetrain));
 79    SmartDashboard.putData("Drive: Place", new Drive(Drivetrain.PlaceDistance, Drivetrain.BackAwayDistance, m_drivetrain));
 80
 81        // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=SMARTDASHBOARD
 82    // Configure the button bindings
 83    configureButtonBindings();
 84
 85    // Configure default commands
 86        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=SUBSYSTEM_DEFAULT_COMMAND
 87    m_drivetrain.setDefaultCommand(new TankDrive( m_drivetrain ) ); // (5)
 88
 89
 90        // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=SUBSYSTEM_DEFAULT_COMMAND
 91
 92    // Configure autonomous sendable chooser
 93        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=AUTONOMOUS
 94
 95    m_chooser.setDefaultOption("Autonomous", new Autonomous()); // (2)
 96
 97        // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=AUTONOMOUS
 98
 99    SmartDashboard.putData("Auto Mode", m_chooser);
100  }
101
102  public static RobotContainer getInstance() {
103    return m_robotContainer;
104  }
105
106  /**
107   * Use this method to define your button->command mappings.  Buttons can be created by
108   * instantiating a {@link GenericHID} or one of its subclasses ({@link
109   * edu.wpi.first.wpilibj.Joystick} or {@link XboxController}), and then passing it to a
110   * {@link edu.wpi.first.wpilibj2.command.button.JoystickButton}.
111   */
112  private void configureButtonBindings() {
113        // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=BUTTONS
114// Create some buttons
115final JoystickButton dpadUp = new JoystickButton(logitechController, 5); // (4)
116dpadUp.whenPressed(new SetElevatorSetpoint(0.3, m_elevator) ,true);
117    SmartDashboard.putData("Dpad Up",new SetElevatorSetpoint(0.3, m_elevator) );
118
119final JoystickButton dpadDown = new JoystickButton(logitechController, 7);
120dpadDown.whenPressed(new SetElevatorSetpoint(0, m_elevator) ,true);
121    SmartDashboard.putData("Dpad Down",new SetElevatorSetpoint(0, m_elevator) );
122
123final JoystickButton dpadRight = new JoystickButton(logitechController, 6);
124dpadRight.whenPressed(new CloseClaw( m_claw ) ,true);
125    SmartDashboard.putData("Dpad Right",new CloseClaw( m_claw ) );
126
127final JoystickButton dpadLeft = new JoystickButton(logitechController, 8);
128dpadLeft.whenPressed(new OpenClaw( m_claw ) ,true);
129    SmartDashboard.putData("Dpad Left",new OpenClaw( m_claw ) );
130
131final JoystickButton l2 = new JoystickButton(logitechController, 9);
132l2.whenPressed(new PrepareToPickup() ,true);
133    SmartDashboard.putData("L2",new PrepareToPickup() );
134
135final JoystickButton r2 = new JoystickButton(logitechController, 10);
136r2.whenPressed(new Pickup() ,true);
137    SmartDashboard.putData("R2",new Pickup() );
138
139final JoystickButton l1 = new JoystickButton(logitechController, 11);
140l1.whenPressed(new Place() ,true);
141    SmartDashboard.putData("L1",new Place() );
142
143final JoystickButton r1 = new JoystickButton(logitechController, 12);
144r1.whenPressed(new Autonomous() ,true);
145    SmartDashboard.putData("R1",new Autonomous() );
146
147
148
149        // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=BUTTONS
150  }
151
152    // BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=FUNCTIONS
153public Joystick getLogitechController() {
154        return logitechController;
155    }
156
157
158    // END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=FUNCTIONS
159
160  /**
161   * Use this to pass the autonomous command to the main {@link Robot} class.
162   *
163   * @return the command to run in autonomous
164  */
165  public Command getAutonomousCommand() {
166    // The selected command will be run in autonomous
167    return m_chooser.getSelected();
168  }
169
170
171}

This is the RobotContainer generated by RobotBuilder which is where the subsystems and operator interface are defined. There are a number of parts to this program (highlighted sections):

  1. Each of the subsystems is declared here. They can be passed as parameters to any commands that require them.

  2. If there is an autonomous command provided in RobotBuilder robot properties, it is added to the Sendable Chooser to be selected on the dashboard.

  3. The code for all the operator interface components is generated here.

  4. In addition the code to link the OI buttons to commands that should run is also generated here.

  5. Commands to be run on a subystem when no other commands are running are defined here.

  6. Commands to be run via a dashboard are defined here.