Creating the project

Since the project requires a lot of setup and support files, the easiest way to create a project is to copy another project as a template. The project we are going to copy is the TimedRobot project. It would be nice if there were a nice VS Code wizard to create the project, but in lieu of that, I have created a small Java program called CreateRobotProject which we can use to make a workable copy of the TimedRobot project and then we can load that new project into VS Code.

To run this program, open the Windows File Explorer, find the VSCode folder inside RobotTools2021 / VSCode , and then double click on the CreateRobotProject.jar file. This should display the following dialog box.

In the Project Name field, enter the name of your new project (e.g. JohnsRobot) and leave the Template field set to TimedRobot. Don’t put spaces or other punctuation in your project name; you’ll regret it later. Then click the Create button. If all goes well, it should create a project with the name you gave.

Once the project has been created, go back to Visual Studio and choose the File / Open Folder command:

 And select the folder that you just created (e.g. JohnsRobot):

Click the Select Folder button and it should load your project:

Close the “Welcome Window” in the right pane and expand the src tree in the left pane to see a listing of the source files for your program. Click on Robot.java in this pane to display this source file in the right pane:

Your program consists of a Robot class which inherits from the built in TimedRobot class. Let’s take a look and try and understand the structure of this class:

The first thing we see is the declaration of, m_robotContainer. This will contain an instance of the RobotContainer class which will hold many of the resources that we will need for our program. Note that we are not initializing this variable here, instead we wait and do that when the robotInit function is called:

As the comment suggests, this function will be called once the robot in initialized and is where we should do any initialization that is required. Here is where we create the instance of RobotContainer.

The next function we see is robotPeriodic:

As the comment explains, this function will be called periodically as long as your program is running. It is principally used to run the Command Scheduler, but would be the place to put any code that you needed to run regardless of the mode or whether the robot is enabled or disabled.

Next we see a set of eight functions which are used to control the four modes of the robot, DisabledAutonomous, Teleop, and Test.

The comments in the code are pretty self explanatory. Each of the modes has a function (e.g. autonomousInit) which is called when that mode starts. Then, as long as the robot stays in that particular mode, that mode’s ‘periodic’ function (e.g. autonomousPeriodic) will be repeatedly called.

Before we move on, I would like to bring your attention the the numerous calls to Logger.Log(…). This is a debugging function which allows you to add logging to your program. This can be most useful when trying to figure out why your program is not behaving the way you expect. The Log function takes 3 parameters. The first is a tag which identifies this particular log and is usually the class name. The second is a priority which determines when the log will be displayed, and the third is whatever you wish to log. I strongly suggest that you make use of this feature when writing your code. It can make finding an elusive problem much easier later, especially when the problem only manifests itself when running the robot in competition.

Next: Running the program