Creating a robot program
The simplest way to create a robot program, is to start from one of the four supplied templates (Sample, Iterative, Timed, or Command). Sample is best used for very small sample programs or advanced programs that require total control over program flow. Iterative Robot is a template which provides better structure for robot programs while maintaining a minimal learning curve. Timed robot provides a similar structure to Iterative robot, but with more consistent timing. Command-Based robot is a template that provides a modular, extensible structure with a moderate learning curve.
The templates will get you the basis of a robot program, organizing a larger project can often be a complex task. RobotBuilder is recommended for creating and organizing Command-Based robot programs. You can learn more about RobotBuilder here. To create a command-based robot program that takes advantage of all the newer tools look at Creating a Robot Project in the Command Based Programming Chapter.
In this article we will create a new WPILib project in Visual Studio Code. In this example we will be making a TimedRobot, however the same methods apply to creating a project from any of the existing templates or examples.
Accessing The Command Palette
Clicking Ctrl+Shift+P will open the command palette. The command palette contains the WPILib commands for creating and interacting with projects.
Accessing The WPILib Commands
All WPILib commands start with "WPILib:", so in order to access the WPILib commands type "WPILib:" into the command palette search bar.
Creating A New WPILib Project
In order to create a new project select the "Create a new project" command. This will show a a form with a number of fields where you enter the information required to create your new project.
New project creator window
The steps to create the new project are outlined here:
- Select the kind of project you want to create. It can be an example project or one of the template projects provided by WPILib.
- Select the language that you are using for your project.
- In the case of a template - select the template type (Timed robot, Iterative robot, Command robot, etc.)
- Select the folder to place the project.
- If the "Create new folder" checkbox is checked, a new folder named with the project name is created in the supplied folder. If the checkbox is NOT checked, then the folder supplied is assumed to be empty (will give an error if not) and the project files will be placed into that directory.
- The project name is used in the project and also to optionally create the folder to place it if the checkbox from the previous step is checked.
- The team number for the project. This will be used for package names and to locate your robot when deploying code.
And last, click "Generate Project" and VS Code will create the project in the location specified.
Note: If there are any errors generating the project (such as trying to use a non-empty folder with the checkbox unchecked), they will pop up in the bottom right corner of the screen.
Opening The New Project
After successfully creating your project, Visual Studio Code will give you the option of opening the project as shown below. You can choose to do that now or later by typing Ctrl-O (Command+O on mac) and select the folder where you saved your project.
Once opened you will see the project hierarchy on the left. Double clicking on the file will open that file in the editor.
C++ Configurations (C++ Only)
For C++ projects, there is one more step to set up IntelliSense. Whenever you open a project, you should get a pop-up in the bottom right corner asking to refresh C++ configurations, click Yes to setup IntelliSense.
Building and Deploying Robot Code
To build the robot project, do one of:
- Open the Command Palette and select "Build Robot Code"
- Open the shortcut menu indicated by the ellipses in the top right corner of the VS Code window and select "Build Robot Code"
- Right-click on the build.gradle file in the project hierarchy and select "Build Robot Code"
Deploy robot code by selecting "Deploy Robot Code" from any of the three locations from the previous instructions. That will build (if necessary) and deploy the robot program to the roboRIO. If successful, your see a "Build Successful" message (2) and the RioLog will open with the console output from the robot program as it runs.