# RobotBuilder Created Code¶

## The Layout of a RobotBuilder Generated Project¶

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 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125  package frc.robot; import edu.wpi.first.hal.FRCNetComm.tInstances; import edu.wpi.first.hal.FRCNetComm.tResourceType; import edu.wpi.first.hal.HAL; import edu.wpi.first.wpilibj.TimedRobot; import edu.wpi.first.wpilibj2.command.Command; import edu.wpi.first.wpilibj2.command.CommandScheduler; /** * The VM is configured to automatically run this class, and to call the * functions corresponding to each mode, as described in the TimedRobot * documentation. If you change the name of this class or the package after * creating this project, you must also update the build.properties file in * the project. */ public class Robot extends TimedRobot { // (1) private Command m_autonomousCommand; private RobotContainer m_robotContainer; /** * This function is run when the robot is first started up and should be * used for any initialization code. */ @Override public void robotInit() { // Instantiate our RobotContainer. This will perform all our button bindings, and put our // autonomous chooser on the dashboard. m_robotContainer = new RobotContainer(); HAL.report(tResourceType.kResourceType_Framework, tInstances.kFramework_RobotBuilder); } /** * This function is called every robot packet, no matter the mode. Use this for items like * diagnostics that you want ran during disabled, autonomous, teleoperated and test. * *

This runs after the mode specific periodic functions, but before * LiveWindow and SmartDashboard integrated updating. */ @Override public void robotPeriodic() { // Runs the Scheduler. This is responsible for polling buttons, adding newly-scheduled // commands, running already-scheduled commands, removing finished or interrupted commands, // and running subsystem periodic() methods. This must be called from the robot's periodic // block in order for anything in the Command-based framework to work. CommandScheduler.getInstance().run(); // (2) } /** * This function is called once each time the robot enters Disabled mode. */ @Override public void disabledInit() { } @Override public void disabledPeriodic() { } /** * This autonomous runs the autonomous command selected by your {@link RobotContainer} class. */ @Override public void autonomousInit() { m_autonomousCommand = m_robotContainer.getAutonomousCommand(); // (3) // schedule the autonomous command (example) if (m_autonomousCommand != null) { m_autonomousCommand.schedule(); } } /** * This function is called periodically during autonomous. */ @Override public void autonomousPeriodic() { } @Override public void teleopInit() { // This makes sure that the autonomous stops running when // teleop starts running. If you want the autonomous to // continue until interrupted by another command, remove // this line or comment it out. if (m_autonomousCommand != null) { m_autonomousCommand.cancel(); } } /** * This function is called periodically during operator control. */ @Override public void teleopPeriodic() { } @Override public void testInit() { // Cancels all running commands at the start of test mode. CommandScheduler.getInstance().cancelAll(); } /** * This function is called periodically during test mode. */ @Override public void testPeriodic() { } } 

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¶

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

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.