# Robot¶

There are multiple sections of Robot.java, below discusses each one and the purpose

Title Block

 1 2 3 4 5 6 /*----------------------------------------------------------------------------*/ /* Copyright (c) 2019 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ /*----------------------------------------------------------------------------*/ 

Hint

The title block is important as it displays licenses, authors, last edited dates, etc…

Some nice things to have in any title block would be the author and date. This would provide the next person coming to see who wrote what and when they wrote it.

Imports

The import section holds all the imports of various libraries that are required for use in the current class.

  8 9 10 11 12 13 14 15 16 package frc.robot; import edu.wpi.first.wpilibj.TimedRobot; import edu.wpi.first.wpilibj.command.Command; import edu.wpi.first.wpilibj.command.Scheduler; import edu.wpi.first.wpilibj.smartdashboard.SendableChooser; import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; import frc.robot.commands.ExampleCommand; import frc.robot.subsystems.ExampleSubsystem; 

Class Declaration

In every Java program the class needs to be declared. Below is the class declaration for the Robot.java class.

 18 19 20 21 22 23 24 25 /** * 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.gradle file in the * project. */ public class Robot extends TimedRobot { 

Declaring Objects

This section declares objects that are required for use later in the class.

 26 27 28 29 30 public static ExampleSubsystem m_subsystem = new ExampleSubsystem(); public static OI m_oi; Command m_autonomousCommand; SendableChooser m_chooser = new SendableChooser<>(); 

Robot Initialization

This is where any code is to be run when the robot is first booting up.

 32 33 34 35 36 37 38 39 40 41 42 /** * This function is run when the robot is first started up and should be * used for any initialization code. */ @Override public void robotInit() { m_oi = new OI(); m_chooser.setDefaultOption("Default Auto", new ExampleCommand()); // chooser.addOption("My Auto", new MyAutoCommand()); SmartDashboard.putData("Auto mode", m_chooser); } 

Robot Periodic

Warning

Code here is run every robot packet and is not controlled by the Enable/Disable buttons.

Robot periodic is a good section to add code for diagnostics or anything that requires constant polling.

 44 45 46 47 48 49 50 51 52 53 54 /** * 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() { } 

Disabled Initialization

When ever the robot is put into a disabled state it enters here first.

 56 57 58 59 60 61 62 63 /** * This function is called once each time the robot enters Disabled mode. * You can use it to reset any subsystem information you want to clear when * the robot is disabled. */ @Override public void disabledInit() { } 

Disabled Periodic

Code that will run every robot packet when the robot is disabled.

 65 66 67 68 @Override public void disabledPeriodic() { Scheduler.getInstance().run(); } 

Autonomous Initialization

Code that is run at the start of an autonomous run.

 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 /** * This autonomous (along with the chooser code above) shows how to select * between different autonomous modes using the dashboard. The sendable * chooser code works with the Java SmartDashboard. If you prefer the * LabVIEW Dashboard, remove all of the chooser code and uncomment the * getString code to get the auto name from the text box below the Gyro * *

You can add additional auto modes by adding additional commands to the * chooser code above (like the commented example) or additional comparisons * to the switch structure below with additional strings & commands. */ @Override public void autonomousInit() { m_autonomousCommand = m_chooser.getSelected(); /* * String autoSelected = SmartDashboard.getString("Auto Selector", * "Default"); switch(autoSelected) { case "My Auto": autonomousCommand * = new MyAutoCommand(); break; case "Default Auto": default: * autonomousCommand = new ExampleCommand(); break; } */ // schedule the autonomous command (example) if (m_autonomousCommand != null) { m_autonomousCommand.start(); } } 

Autonomous Periodic

Code that is run every robot packet during the autonomous run.

Teleop Initialization

Code that is run at the start of a teleoperated run.

 106 107 108 109 110 111 112 113 114 115 @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(); } } 

Teleop Periodic

Code that is run every robot packet when in a periodic run.

 117 118 119 120 121 122 123 /** * This function is called periodically during operator control. */ @Override public void teleopPeriodic() { Scheduler.getInstance().run(); } 

Test Periodic

Code that is run every robot packet when in a test run.

 125 126 127 128 129 130 /** * This function is called periodically during test mode. */ @Override public void testPeriodic() { }